Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
Provide functionality for playing a .wav file (#20)
Browse files Browse the repository at this point in the history
PBI: 27951
Task: 28656

* Refactor slash stripping method

* Refactor remove_leading_slashes()

* Add checking to see if file name ends with .wav

* Change displayed error to our error and provide meaningful error message
  • Loading branch information
jonathanwangg authored Jun 22, 2019
1 parent 0045e17 commit 334e768
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import sys
import os
import simpleaudio as sa
from .pixel import Pixel
from . import utils

Expand Down Expand Up @@ -28,6 +30,8 @@ def __init__(self):
}

self.pixels = Pixel(self.__state)
self.__speaker_enabled = False
self.__abs_path_to_code_file = ''

@property
def button_a(self):
Expand All @@ -49,4 +53,23 @@ def red_led(self, value):
def __show(self):
utils.show(self.__state)

def play_file(self, file_name):
file_name = utils.remove_leading_slashes(file_name)
abs_path_parent_dir = os.path.abspath(os.path.join(self.__abs_path_to_code_file, os.pardir))
abs_path_wav_file = os.path.normpath(os.path.join(abs_path_parent_dir, file_name))

if sys.implementation.version[0] >= 3:
if file_name.endswith(".wav"):
wave_obj = sa.WaveObject.from_wave_file(abs_path_wav_file)
try:
play_obj = wave_obj.play()
except:
# TODO TASK: 29054 Verfication of a "valid" .wav file
raise EnvironmentError("Your .wav file is not suitable for the Circuit Playground Express.")
play_obj.wait_done()
else:
raise TypeError(file_name + " is not a path to a .wav file.")
else:
raise NotImplementedError("Please use Python 3 or higher.")

cpx = Express()
8 changes: 6 additions & 2 deletions src/adafruit_circuitplayground/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
import json

def show(state):
print(json.dumps(state) + '\0', end='')
sys.stdout.flush()
print(json.dumps(state) + '\0', end='', flush=True)

def remove_leading_slashes(string):
string = string.lstrip('\\/')

return string
4 changes: 2 additions & 2 deletions src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
import threading
import copy
from adafruit_circuitplayground.express import cpx
from pathlib import Path

read_val = ""


class UserInput(threading.Thread):

def __init__(self):
Expand Down Expand Up @@ -39,9 +39,9 @@ def run(self):
threads.append(user_input)
user_input.start()


# User code thread
def execute_user_code(abs_path_to_code_file):
cpx._Express__abs_path_to_code_file = abs_path_to_code_file
# Execute the user's code.py file
with open(abs_path_to_code_file) as file:
user_code = file.read()
Expand Down

0 comments on commit 334e768

Please sign in to comment.