diff --git a/src/adafruit_circuitplayground/express.py b/src/adafruit_circuitplayground/express.py index 58773b93e..9c97857ea 100644 --- a/src/adafruit_circuitplayground/express.py +++ b/src/adafruit_circuitplayground/express.py @@ -1,5 +1,7 @@ import json import sys +import os +import simpleaudio as sa from .pixel import Pixel from . import utils @@ -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): @@ -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() diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 461f81250..8018950f3 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -2,5 +2,9 @@ import json def show(state): - print(json.dumps(state) + '\0', end='') - sys.stdout.flush() \ No newline at end of file + print(json.dumps(state) + '\0', end='', flush=True) + +def remove_leading_slashes(string): + string = string.lstrip('\\/') + + return string \ No newline at end of file diff --git a/src/setup.py b/src/setup.py index 1185365ee..189916df3 100644 --- a/src/setup.py +++ b/src/setup.py @@ -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): @@ -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()