Skip to content

Commit f3d992c

Browse files
authored
Merge branch 'master' into master
2 parents 7d8ea3e + 6d5badc commit f3d992c

File tree

6 files changed

+402
-1
lines changed

6 files changed

+402
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Demonstration of a Bluefruit BLE Central/client. Connects to the first BLE UART peripheral it finds.
3+
Sends Bluefruit ColorPackets, read from three potentiometers, to the peripheral.
4+
"""
5+
6+
import time
7+
8+
import board
9+
from analogio import AnalogIn
10+
11+
#from adafruit_bluefruit_connect.packet import Packet
12+
# Only the packet classes that are imported will be known to Packet.
13+
from adafruit_bluefruit_connect.color_packet import ColorPacket
14+
15+
from adafruit_ble.scanner import Scanner
16+
from adafruit_ble.uart_client import UARTClient
17+
18+
def scale(value):
19+
"""Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
20+
return int(value / 65535 * 255)
21+
22+
scanner = Scanner()
23+
uart_client = UARTClient()
24+
25+
a3 = AnalogIn(board.A4)
26+
a4 = AnalogIn(board.A5)
27+
a5 = AnalogIn(board.A6)
28+
29+
while True:
30+
uart_addresses = []
31+
# Keep trying to find a UART peripheral
32+
while not uart_addresses:
33+
uart_addresses = uart_client.scan(scanner)
34+
uart_client.connect(uart_addresses[0], 5)
35+
36+
while uart_client.connected:
37+
r = scale(a3.value)
38+
g = scale(a4.value)
39+
b = scale(a5.value)
40+
41+
color = (r, g, b)
42+
print(color)
43+
color_packet = ColorPacket(color)
44+
try:
45+
uart_client.write(color_packet.to_bytes())
46+
except OSError:
47+
pass
48+
time.sleep(0.3)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Used along with cpb_remote_color_client.py. Receives Bluefruit LE ColorPackets from a central,
3+
and updates NeoPixels to show the history of the received packets.
4+
"""
5+
6+
import board
7+
import neopixel
8+
9+
from adafruit_ble.uart_server import UARTServer
10+
from adafruit_bluefruit_connect.packet import Packet
11+
# Only the packet classes that are imported will be known to Packet.
12+
from adafruit_bluefruit_connect.color_packet import ColorPacket
13+
14+
uart_server = UARTServer()
15+
16+
NUM_PIXELS = 10
17+
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
18+
next_pixel = 0
19+
20+
def mod(i):
21+
"""Wrap i to modulus NUM_PIXELS."""
22+
return i % NUM_PIXELS
23+
24+
while True:
25+
# Advertise when not connected.
26+
uart_server.start_advertising()
27+
while not uart_server.connected:
28+
pass
29+
30+
while uart_server.connected:
31+
packet = Packet.from_stream(uart_server)
32+
if isinstance(packet, ColorPacket):
33+
print(packet.color)
34+
np[next_pixel] = packet.color
35+
np[mod(next_pixel + 1)] = (0, 0, 0)
36+
next_pixel = (next_pixel + 1) % NUM_PIXELS
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
"""
2+
GFX Helper for PyPortal AWS IoT Plant Monitor
3+
"""
4+
import board
5+
import displayio
6+
import terminalio
7+
from adafruit_display_text.label import Label
8+
9+
# the current working directory (where this file is)
10+
cwd = ("/"+__file__).rsplit('/', 1)[0]
11+
12+
# GFX Font
13+
font = terminalio.FONT
14+
15+
class AWS_GFX(displayio.Group):
16+
def __init__(self, is_celsius=False):
17+
"""Creates an AWS_GFX object for displaying plant
18+
and AWS IoT status.
19+
:param bool is_celsius: Temperature displayed in Celsius.
20+
"""
21+
# root displayio group
22+
root_group = displayio.Group(max_size=23)
23+
self.display = board.DISPLAY
24+
self.display.show(root_group)
25+
super().__init__(max_size=15)
26+
27+
# temperature display option
28+
self._is_celsius = is_celsius
29+
30+
# create background icon group
31+
self._icon_group = displayio.Group(max_size=3)
32+
self.append(self._icon_group)
33+
self.display.show(self._icon_group)
34+
# create text object group
35+
self._text_group = displayio.Group(max_size=40)
36+
self.append(self._text_group)
37+
38+
print("Displaying splash screen")
39+
self._icon_sprite = None
40+
self._icon_file = None
41+
self._cwd = cwd
42+
self.set_icon(self._cwd+"/images/aws_splash.bmp")
43+
44+
print('Setting up labels...')
45+
header_group = displayio.Group(scale=3)
46+
header_label = Label(font, text=" AWS IoT\n Planter")
47+
header_label.x = (self.display.width // 2) // 22
48+
header_label.y = 15
49+
header_group.append(header_label)
50+
self._text_group.append(header_group)
51+
52+
# Temperature Display
53+
temp_group = displayio.Group(scale=2, max_size=400)
54+
temp_label = Label(font, text="Temperature: ")
55+
temp_label.x = (self.display.width//2) // 11
56+
temp_label.y = 55
57+
temp_group.append(temp_label)
58+
59+
self.temp_data_label = Label(font, text="75 F")
60+
self.temp_data_label.x = (self.display.width//3)
61+
self.temp_data_label.y = 55
62+
temp_group.append(self.temp_data_label)
63+
self._text_group.append(temp_group)
64+
65+
# Water Level
66+
water_group = displayio.Group(scale=2, max_size=2)
67+
self.water_level = Label(font, text="Water Level: ")
68+
self.water_level.x = (self.display.width//2) // 11
69+
self.water_level.y = 75
70+
water_group.append(self.water_level)
71+
72+
self.water_lvl_label = Label(font, text="350")
73+
self.water_lvl_label.x = (self.display.width//3)
74+
self.water_lvl_label.y = 75
75+
temp_group.append(self.water_lvl_label)
76+
self._text_group.append(water_group)
77+
78+
# AWS Status
79+
status_group = displayio.Group()
80+
self.aws_status_label = Label(font, text="Connecting to AWS IoT...")
81+
self.aws_status_label.x = int(self.display.width//3.5)
82+
self.aws_status_label.y = 200
83+
status_group.append(self.aws_status_label)
84+
self._text_group.append(status_group)
85+
86+
self.display.show(self._text_group)
87+
88+
def show_aws_status(self, status_text):
89+
"""Displays the system status on the PyPortal
90+
:param str status_text: Description of current AWS IoT status.
91+
"""
92+
self.aws_status_label.text = status_text
93+
94+
def show_water_level(self, water_data):
95+
"""Displays the water level from the Stemma Soil Sensor.
96+
:param int water_data: water value
97+
"""
98+
self.water_lvl_label.text = str(water_data)
99+
100+
def show_temp(self, temp_data):
101+
"""Displays the temperature from the Stemma Soil Sensor.
102+
:param float temp_data: Temperature value.
103+
"""
104+
if not self._is_celsius:
105+
temp_data = (temp_data * 9 / 5) + 32 - 15
106+
print('Temperature: %0.0f°F'%temp_data)
107+
if temp_data >= 212:
108+
self.temp_data_label.color = 0xFD2EE
109+
elif temp_data <= 32:
110+
self.temp_data_label.color = 0xFF0000
111+
self.temp_data_label = '%0.0f°F'%temp_data
112+
temp_data = '%0.0f'%temp_data
113+
return int(temp_data)
114+
else:
115+
print('Temperature: %0.0f°C'%temp_data)
116+
if temp_data <= 0:
117+
self.temp_data_label.color = 0xFD2EE
118+
elif temp_data >= 100:
119+
self.temp_data_label.color = 0xFF0000
120+
self.temp_data_label.text = '%0.0f°C'%temp_data
121+
temp_data = '%0.0f'%temp_data
122+
return int(temp_data)
123+
124+
def set_icon(self, filename):
125+
"""Sets the background image to a bitmap file.
126+
127+
:param filename: The filename of the chosen icon
128+
"""
129+
print("Set icon to ", filename)
130+
if self._icon_group:
131+
self._icon_group.pop()
132+
133+
if not filename:
134+
return # we're done, no icon desired
135+
if self._icon_file:
136+
self._icon_file.close()
137+
self._icon_file = open(filename, "rb")
138+
icon = displayio.OnDiskBitmap(self._icon_file)
139+
try:
140+
self._icon_sprite = displayio.TileGrid(icon,
141+
pixel_shader=displayio.ColorConverter())
142+
except TypeError:
143+
self._icon_sprite = displayio.TileGrid(icon,
144+
pixel_shader=displayio.ColorConverter(),
145+
position=(0,0))
146+
147+
self._icon_group.append(self._icon_sprite)
148+
self.display.refresh_soon()
149+
self.display.wait_for_frame()
225 KB
Binary file not shown.

0 commit comments

Comments
 (0)