Skip to content

Commit 17b43c3

Browse files
Merge branch 'master' into master
2 parents 8b8c4e6 + 8797452 commit 17b43c3

File tree

7 files changed

+295
-21
lines changed

7 files changed

+295
-21
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import pulseio
4+
5+
led = pulseio.PWMOut(board.D5, frequency=5000, duty_cycle=0)
6+
7+
while True:
8+
for i in range(100):
9+
# PWM LED up and down
10+
if i < 50:
11+
led.duty_cycle = int(i * 2 * 65535 / 100) # Up
12+
else:
13+
led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down
14+
time.sleep(0.01)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
import board
3+
import pulseio
4+
from adafruit_motor import servo
5+
6+
# create a PWMOut object on Pin D5.
7+
pwm = pulseio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50)
8+
9+
# Create a servo object.
10+
servo = servo.Servo(pwm)
11+
12+
while True:
13+
for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time.
14+
servo.angle = angle
15+
time.sleep(0.05)
16+
for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
17+
servo.angle = angle
18+
time.sleep(0.05)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
import board
3+
import pulseio
4+
5+
# Initialize PWM output for the servo (on pin D5):
6+
servo = pulseio.PWMOut(board.D5, frequency=50)
7+
8+
9+
# Create a function to simplify setting PWM duty cycle for the servo:
10+
def servo_duty_cycle(pulse_ms, frequency=50):
11+
period_ms = 1.0 / frequency * 1000.0
12+
duty_cycle = int(pulse_ms / (period_ms / 65535.0))
13+
return duty_cycle
14+
15+
16+
# Main loop will run forever moving between 1.0 and 2.0 mS long pulses:
17+
while True:
18+
servo.duty_cycle = servo_duty_cycle(1.0)
19+
time.sleep(1.0)
20+
servo.duty_cycle = servo_duty_cycle(2.0)
21+
time.sleep(1.0)
23.1 KB
Binary file not shown.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
This example queries the Open Weather Maps site API to find out the current
3+
weather for your location... and display it on a eInk Bonnet!
4+
"""
5+
6+
import time
7+
import urllib.request
8+
import urllib.parse
9+
import digitalio
10+
import busio
11+
import board
12+
from adafruit_epd.epd import Adafruit_EPD
13+
from adafruit_epd.ssd1675 import Adafruit_SSD1675
14+
from weather_graphics import Weather_Graphics
15+
16+
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
17+
ecs = digitalio.DigitalInOut(board.CE0)
18+
dc = digitalio.DigitalInOut(board.D22)
19+
rst = digitalio.DigitalInOut(board.D27)
20+
busy = digitalio.DigitalInOut(board.D17)
21+
22+
# You'll need to get a token from openweathermap.org, looks like:
23+
# 'b6907d289e10d714a6e88b30761fae22'
24+
OPEN_WEATHER_TOKEN = ""
25+
LOCATION = "Manhattan, US"
26+
DATA_SOURCE_URL = "http://api.openweathermap.org/data/2.5/weather"
27+
28+
if len(OPEN_WEATHER_TOKEN) == 0:
29+
raise RuntimeError(
30+
"You need to set your token first. If you don't already have one, you can register for a free account at https://home.openweathermap.org/users/sign_up"
31+
)
32+
33+
# Set up where we'll be fetching data from
34+
params = {"q": LOCATION, "appid": OPEN_WEATHER_TOKEN}
35+
data_source = DATA_SOURCE_URL + "?" + urllib.parse.urlencode(params)
36+
37+
# Initialize the Display
38+
display = Adafruit_SSD1675(
39+
122, 250, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=None, rst_pin=rst, busy_pin=busy,
40+
)
41+
42+
display.rotation = 3
43+
44+
gfx = Weather_Graphics(display, am_pm=True, celsius=False)
45+
weather_refresh = None
46+
47+
while True:
48+
# only query the weather every 10 minutes (and on first run)
49+
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
50+
response = urllib.request.urlopen(data_source)
51+
if response.getcode() == 200:
52+
value = response.read()
53+
print("Response is", value)
54+
gfx.display_weather(value)
55+
weather_refresh = time.monotonic()
56+
else:
57+
print("Unable to retrieve data at {}".format(url))
58+
59+
gfx.update_time()
60+
time.sleep(300) # wait 5 minutes before updating anything again
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
from datetime import datetime
2+
import json
3+
from PIL import Image, ImageDraw, ImageFont
4+
from adafruit_epd.epd import Adafruit_EPD
5+
6+
small_font = ImageFont.truetype(
7+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16
8+
)
9+
medium_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
10+
large_font = ImageFont.truetype(
11+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 24
12+
)
13+
icon_font = ImageFont.truetype("./meteocons.ttf", 48)
14+
15+
# Map the OpenWeatherMap icon code to the appropriate font character
16+
# See http://www.alessioatzeni.com/meteocons/ for icons
17+
ICON_MAP = {
18+
"01d": "B",
19+
"01n": "C",
20+
"02d": "H",
21+
"02n": "I",
22+
"03d": "N",
23+
"03n": "N",
24+
"04d": "Y",
25+
"04n": "Y",
26+
"09d": "Q",
27+
"09n": "Q",
28+
"10d": "R",
29+
"10n": "R",
30+
"11d": "Z",
31+
"11n": "Z",
32+
"13d": "W",
33+
"13n": "W",
34+
"50d": "J",
35+
"50n": "K",
36+
}
37+
38+
# RGB Colors
39+
WHITE = (255, 255, 255)
40+
BLACK = (0, 0, 0)
41+
42+
43+
class Weather_Graphics:
44+
def __init__(self, display, *, am_pm=True, celsius=True):
45+
self.am_pm = am_pm
46+
self.celsius = celsius
47+
48+
self.small_font = small_font
49+
self.medium_font = medium_font
50+
self.large_font = large_font
51+
52+
self.display = display
53+
54+
self._weather_icon = None
55+
self._city_name = None
56+
self._main_text = None
57+
self._temperature = None
58+
self._description = None
59+
self._time_text = None
60+
61+
def display_weather(self, weather):
62+
weather = json.loads(weather)
63+
64+
# set the icon/background
65+
self._weather_icon = ICON_MAP[weather["weather"][0]["icon"]]
66+
67+
city_name = weather["name"] + ", " + weather["sys"]["country"]
68+
print(city_name)
69+
self._city_name = city_name
70+
71+
main = weather["weather"][0]["main"]
72+
print(main)
73+
self._main_text = main
74+
75+
temperature = weather["main"]["temp"] - 273.15 # its...in kelvin
76+
print(temperature)
77+
if self.celsius:
78+
self._temperature = "%d °C" % temperature
79+
else:
80+
self._temperature = "%d °F" % ((temperature * 9 / 5) + 32)
81+
82+
description = weather["weather"][0]["description"]
83+
description = description[0].upper() + description[1:]
84+
print(description)
85+
self._description = description
86+
# "thunderstorm with heavy drizzle"
87+
88+
self.update_time()
89+
90+
def update_time(self):
91+
now = datetime.now()
92+
self._time_text = now.strftime("%I:%M %p").lstrip("0").replace(" 0", " ")
93+
self.update_display()
94+
95+
def update_display(self):
96+
self.display.fill(Adafruit_EPD.WHITE)
97+
image = Image.new("RGB", (self.display.width, self.display.height), color=WHITE)
98+
draw = ImageDraw.Draw(image)
99+
100+
# Draw the Icon
101+
(font_width, font_height) = icon_font.getsize(self._weather_icon)
102+
draw.text(
103+
(
104+
self.display.width // 2 - font_width // 2,
105+
self.display.height // 2 - font_height // 2 - 5,
106+
),
107+
self._weather_icon,
108+
font=icon_font,
109+
fill=BLACK,
110+
)
111+
112+
# Draw the city
113+
draw.text(
114+
(5, 5), self._city_name, font=self.medium_font, fill=BLACK,
115+
)
116+
117+
# Draw the time
118+
(font_width, font_height) = medium_font.getsize(self._time_text)
119+
draw.text(
120+
(5, font_height * 2 - 5),
121+
self._time_text,
122+
font=self.medium_font,
123+
fill=BLACK,
124+
)
125+
126+
# Draw the main text
127+
(font_width, font_height) = large_font.getsize(self._main_text)
128+
draw.text(
129+
(5, self.display.height - font_height * 2),
130+
self._main_text,
131+
font=self.large_font,
132+
fill=BLACK,
133+
)
134+
135+
# Draw the description
136+
(font_width, font_height) = small_font.getsize(self._description)
137+
draw.text(
138+
(5, self.display.height - font_height - 5),
139+
self._description,
140+
font=self.small_font,
141+
fill=BLACK,
142+
)
143+
144+
# Draw the temperature
145+
(font_width, font_height) = large_font.getsize(self._temperature)
146+
draw.text(
147+
(
148+
self.display.width - font_width - 5,
149+
self.display.height - font_height * 2,
150+
),
151+
self._temperature,
152+
font=self.large_font,
153+
fill=BLACK,
154+
)
155+
156+
self.display.image(image)
157+
self.display.display()

Glowing_Bottle_Castle/code.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
mpr121 = adafruit_mpr121.MPR121(i2c)
6868

6969
# Demo MODE LED Animations ------------------------------------------------------
70-
rainbow = Rainbow(pixels, speed=0.1, period=10, name="rainbow", step=1)
70+
rainbow = Rainbow(pixels, speed=0, period=10, name="rainbow", step=1)
7171
rainbow_chase = RainbowChase(pixels, speed=0, size=5, spacing=10)
7272
chase = Chase(pixels, speed=0.1, color=RED, size=1, spacing=6)
7373
rainbow_comet = RainbowComet(pixels, speed=0.01, tail_length=60, bounce=True)
@@ -102,21 +102,22 @@ def go_dark():
102102
audio = AudioOut(board.SPEAKER)
103103

104104
tracks = (
105-
WaveFile(open("sounds/F1.wav", "rb")), # 0
106-
WaveFile(open("sounds/G1.wav", "rb")), # 1
107-
WaveFile(open("sounds/A1.wav", "rb")), # 2
108-
WaveFile(open("sounds/Bb1.wav", "rb")), # 3
109-
WaveFile(open("sounds/C1.wav", "rb")), # 4
110-
WaveFile(open("sounds/D2.wav", "rb")), # 5
111-
WaveFile(open("sounds/E2.wav", "rb")), # 6
112-
WaveFile(open("sounds/F2.wav", "rb")), # 7
113-
WaveFile(open("sounds/G2.wav", "rb")), # 8
114-
WaveFile(open("sounds/A2.wav", "rb")), # 9
115-
WaveFile(open("sounds/Bb2.wav", "rb")), # 10
116-
WaveFile(open("sounds/C2.wav", "rb")), # 11
117-
WaveFile(open("sounds/D3.wav", "rb")), # 12
118-
WaveFile(open("sounds/E3.wav", "rb")), # 13
119-
WaveFile(open("sounds/F3.wav", "rb")), # 13
105+
WaveFile(open("sounds/F2.wav", "rb")), # 0
106+
WaveFile(open("sounds/G2.wav", "rb")), # 1
107+
WaveFile(open("sounds/A2.wav", "rb")), # 2
108+
WaveFile(open("sounds/Bb2.wav", "rb")), # 3
109+
WaveFile(open("sounds/C2.wav", "rb")), # 4
110+
WaveFile(open("sounds/D3.wav", "rb")), # 5
111+
WaveFile(open("sounds/E3.wav", "rb")), # 6
112+
WaveFile(open("sounds/F3.wav", "rb")), # 7
113+
WaveFile(open("sounds/F1.wav", "rb")), # 7
114+
WaveFile(open("sounds/G1.wav", "rb")), # 8
115+
WaveFile(open("sounds/A1.wav", "rb")), # 9
116+
WaveFile(open("sounds/Bb1.wav", "rb")), # 10
117+
WaveFile(open("sounds/C1.wav", "rb")), # 11
118+
WaveFile(open("sounds/D2.wav", "rb")), # 12
119+
WaveFile(open("sounds/E2.wav", "rb")), # 13
120+
WaveFile(open("sounds/F2.wav", "rb")), # 13
120121
)
121122

122123
# Add or change song track names here. They will play in the order listed.
@@ -146,7 +147,7 @@ def play_bottle(bottle_id, is_octave):
146147
light_up(bottle_id)
147148
if is_octave:
148149
audio.play(tracks[bottle_id + 7]) # Start playing sound
149-
light_up(9)
150+
light_up(8)
150151
else:
151152
audio.play(tracks[bottle_id]) # Start playing sound
152153
pixels.show()
@@ -157,6 +158,7 @@ def check_buttons(touched):
157158
''' check to see if buttons have been pressed'''
158159
global MODE, LAST_BUTTON
159160
octave = touched[11]
161+
off = touched[9]
160162
if octave:
161163
light_up(8)
162164
for pad in range(1, 9):
@@ -165,7 +167,7 @@ def check_buttons(touched):
165167
if pad != LAST_BUTTON and touched[pad]:
166168
LAST_BUTTON = pad
167169
play_bottle(pad - 1, octave)
168-
if touched[9]:
170+
if off:
169171
MODE = 9
170172
go_dark()
171173
if touched[10]:
@@ -180,10 +182,12 @@ def check_buttons(touched):
180182
while True:
181183
# Idle mode: Play a Rainbow animation when nothing's being touched
182184
if MODE == 0:
183-
pixels.brightness = 1 #rainbow mode is much brighter than the other modes, so adjust here
185+
pixels.brightness = 0.3 #rainbow mode is much brighter than the other modes, so adjust here
184186
rainbow.animate()
185187
for button in buttons:
186188
button.update()
189+
check_buttons(mpr121.touched_pins)
190+
time.sleep(0.1)
187191
for i in range(12):
188192
if buttons[i].fell:
189193
MODE = 1
@@ -210,8 +214,8 @@ def check_buttons(touched):
210214
else:
211215
MODE = 0 # Return to idle mode
212216
if MODE == 9: # MODE 9 is "off" mode, listening for a new button press to wake up.
213-
for button in buttons:
214-
button.update()
217+
# for button in buttons:
218+
# button.update()
215219
for i in range(12):
216220
if buttons[i].fell:
217221
MODE = 1

0 commit comments

Comments
 (0)