forked from adafruit/Adafruit_Learning_System_Guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
172 lines (136 loc) · 4.32 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# SPDX-FileCopyrightText: 2020 Andy Doro for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# AUTOCHEER DEVICE
# code by Andy Doro
#
# plays an MP3 at a specific time.
#
# uses native CircuitPython mp3 playback
#
# REQUIREMENTS:
# should use M4 (or higher)
# use CircuitPython 6.0.0+
#
#
# HARDWARE:
# Feather M4 Express https://www.adafruit.com/product/3857
# Adalogger https://www.adafruit.com/product/2922
#
#
# TO DO
# ---
# - daylight saving time
# - use built-in NeoPixel as indicator
#
import os
import time
import board
import audiomp3
import audioio
import digitalio
# For hardware I2C (M0 boards) use this line:
import busio as io
# Or for software I2C (ESP8266) use this line instead:
# import bitbangio as io
# import adafruit_ds3231
from adafruit_pcf8523.pcf8523 import PCF8523
# SD card
import sdcardio
import storage
# NeoPixel
# import neopixel
# Use any pin that is not taken by SPI
# For Adalogger FeatherWing: https://learn.adafruit.com/adafruit-adalogger-featherwing/pinouts
# The SDCS pin is the chip select line:
# On ESP8266, the SD CS pin is on GPIO 15
# On ESP32 it's GPIO 33
# On WICED it's GPIO PB5
# On the nRF52832 it's GPIO 11
# On Atmel M0, M4, 328p or 32u4 it's on GPIO 10
# On Teensy 3.x it's on GPIO 10
SD_CS = board.D10 # for M4
# Connect to the card and mount the filesystem.
spi = io.SPI(board.SCK, board.MOSI, board.MISO)
sdcard = sdcardio.SDCard(spi, SD_CS)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
# Use the filesystem as normal! Our files are under /sd
# This helper function will print the contents of the SD
def print_directory(path, tabs=0):
for file in os.listdir(path):
stats = os.stat(path + "/" + file)
filesize = stats[6]
isdir = stats[0] & 0x4000
if filesize < 1000:
sizestr = str(filesize) + " by"
elif filesize < 1000000:
sizestr = "%0.1f KB" % (filesize / 1000)
else:
sizestr = "%0.1f MB" % (filesize / 1000000)
prettyprintname = ""
for _ in range(tabs):
prettyprintname += " "
prettyprintname += file
if isdir:
prettyprintname += "/"
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
# recursively print directory contents
if isdir:
print_directory(path + "/" + file, tabs + 1)
print("Files on filesystem:")
print("====================")
print_directory("/sd")
data = open("/sd/cheer.mp3", "rb")
mp3 = audiomp3.MP3Decoder(data)
# a = audioio.AudioOut(board.A0) # mono
a = audioio.AudioOut(board.A0, right_channel=board.A1) # stereo sound through A0 & A1
i2c = io.I2C(board.SCL, board.SDA) # Change to the appropriate I2C clock & data
# pins here!
# Create the RTC instance:
# rtc = adafruit_ds3231.DS3231(i2c)
rtc = PCF8523(i2c)
# Lookup table for names of days (nicer printing).
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
# selected time
# 24 hour time
playhour = 19
playmin = 0
# pylint: disable-msg=bad-whitespace
# pylint: disable-msg=using-constant-test
# no DST adjustment yet!
if False: # change to True if you want to set the time!
# year, mon, date, hour, min, sec, wday, yday, isdst
t = time.struct_time((2020, 5, 13, 15, 15, 15, 0, -1, -1))
# you must set year, mon, date, hour, min, sec and weekday
# yearday is not supported, isdst can be set but we don't do anything with it at this time
print("Setting time to:", t) # uncomment for debugging
rtc.datetime = t
print()
# pylint: enable-msg=using-constant-test
# pylint: enable-msg=bad-whitespace
# setup NeoPixel
# pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
# Main loop:
while True:
t = rtc.datetime
# print(t) # uncomment for debugging
print(
"The date is {} {}/{}/{}".format(
days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year
)
)
print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
if t.tm_hour == playhour and t.tm_min == playmin:
print("it is time!")
# turn NeoPixel green
# pixel[0] = (0, 255, 0)
# play the file
print("playing")
a.play(mp3)
while a.playing:
pass
print("stopped")
# turn NeoPixel off
# pixel[0] = (0, 0, 0)
time.sleep(1) # wait a second