/ Make_It_Talk / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # CircuitPython Speaking Thermometer Example 6 # Coded for Circuit Playground Express but it may be 7 # modified for any CircuitPython board with changes to 8 # button, thermister and audio board definitions. 9 # Anne Barela for Adafruit Industries, MIT License 10 11 import time 12 import board 13 import adafruit_thermistor 14 import audioio 15 import audiocore 16 from digitalio import DigitalInOut, Direction, Pull 17 18 # Enables the speaker for audio output 19 spkrenable = DigitalInOut(board.SPEAKER_ENABLE) 20 spkrenable.direction = Direction.OUTPUT 21 spkrenable.value = True 22 23 D1 = board.BUTTON_A 24 D2 = board.BUTTON_B 25 26 # Button A setup (Celsius) 27 button_a = DigitalInOut(D1) 28 button_a.direction = Direction.INPUT 29 button_a.pull = Pull.DOWN 30 # Button B setup (Fahrenheit) 31 button_b = DigitalInOut(D2) 32 button_b.direction = Direction.INPUT 33 button_b.pull = Pull.DOWN 34 35 # Set up reading the Circuit Playground Express thermistor 36 thermistor = adafruit_thermistor.Thermistor( 37 board.TEMPERATURE, 10000, 10000, 25, 3950) 38 39 # Audio playback object and helper to play a full file 40 aout = audioio.AudioOut(board.A0) 41 42 # Play a wave file 43 def play_file(wavfile): 44 wavfile = "/numbers/" + wavfile 45 print("Playing", wavfile) 46 with open(wavfile, "rb") as f: 47 wav = audiocore.WaveFile(f) 48 aout.play(wav) 49 while aout.playing: 50 pass 51 52 # Function should take an integer -299 to 299 and say it 53 # Assumes wav files are available for the numbers 54 def read_temp(temp): 55 play_file("The temperature is.wav") 56 if temp < 0: 57 play_file("negative.wav") 58 temp = - temp 59 if temp >= 200: 60 play_file("200.wav") 61 temp = temp - 200 62 elif temp >= 100: 63 play_file("100.wav") 64 temp = temp - 100 65 if (temp >= 0 and temp < 20) or temp % 10 == 0: 66 play_file(str(temp) + ".wav") 67 else: 68 play_file(str(temp // 10) + "0.wav") 69 temp = temp - ((temp // 10) * 10 ) 70 play_file(str(temp) + ".wav") 71 play_file("degrees.wav") 72 73 while True: 74 if button_a.value: 75 read_temp(int(thermistor.temperature)) 76 play_file("celsius.wav") 77 if button_b.value: 78 read_temp(int(thermistor.temperature * 9 / 5 + 32)) 79 play_file("fahrenheit.wav") 80 time.sleep(0.01)