Skip to content

Commit 897d94f

Browse files
committed
add Clue versions
1 parent 62cb79e commit 897d94f

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Display stuff
2+
import board
3+
import displayio
4+
import adafruit_imageload
5+
from adafruit_bitmap_font import bitmap_font
6+
from adafruit_display_text import label
7+
# BLE stuff
8+
from adafruit_ble import BLERadio
9+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10+
from adafruit_ble.services.nordic import UARTService
11+
from adafruit_bluefruit_connect.packet import Packet
12+
from adafruit_bluefruit_connect.color_packet import ColorPacket
13+
14+
#---| User Config |--------------------------------------------------
15+
BLE_NAME = "Candy Heart"
16+
MESSAGE_DELIMITER = ","
17+
MESSAGE_COLOR = 0xFF0000
18+
#---| User Config |--------------------------------------------------
19+
20+
# Setup BLE radio and service
21+
ble = BLERadio()
22+
uart = UARTService()
23+
advertisement = ProvideServicesAdvertisement(uart)
24+
ble._adapter.name = BLE_NAME #pylint: disable=protected-access
25+
26+
# Create the display
27+
display = board.DISPLAY
28+
29+
# Load the candy heart BMP
30+
bitmap, palette = adafruit_imageload.load("/heart_bw.bmp",
31+
bitmap=displayio.Bitmap,
32+
palette=displayio.Palette)
33+
34+
heart = displayio.TileGrid(bitmap, pixel_shader=palette)
35+
36+
# Set up message text
37+
LINE1_MAX = 9
38+
LINE2_MAX = 5
39+
font = bitmap_font.load_font("/Multicolore_36.bdf")
40+
line1 = label.Label(font, text="?"*LINE1_MAX)
41+
line2 = label.Label(font, text="?"*LINE2_MAX)
42+
line1.anchor_point = (0.5, 0) # middle top
43+
line2.anchor_point = (0.5, 1.0) # middle bottom
44+
line1.anchored_position = (120, 85)
45+
line2.anchored_position = (120, 175)
46+
line1.color = line2.color = MESSAGE_COLOR
47+
48+
# Set up group and add to display
49+
group = displayio.Group()
50+
group.append(heart)
51+
group.append(line1)
52+
group.append(line2)
53+
display.show(group)
54+
55+
def update_heart(message, heart_color):
56+
# turn off auto refresh while we change some things
57+
display.auto_refresh = False
58+
# set message text
59+
text1, _, text2 = message.partition(MESSAGE_DELIMITER)
60+
line1.text = text1[:LINE1_MAX] if len(text1) > LINE1_MAX else text1
61+
line2.text = text1[:LINE2_MAX] if len(text2) > LINE2_MAX else text2
62+
# update location for new text bounds
63+
line1.anchored_position = (120, 85)
64+
line2.anchored_position = (120, 175)
65+
# set heart color
66+
palette[1] = heart_color
67+
# OK, now turn auto refresh back on to display
68+
display.auto_refresh = True
69+
70+
# Initial update
71+
text = "TEXT,ME"
72+
color = 0x00FFFF
73+
update_heart(text, color)
74+
75+
# Loop forever
76+
while True:
77+
# advertise and wait for connection
78+
print("WAITING...")
79+
ble.start_advertising(advertisement)
80+
while not ble.connected:
81+
pass
82+
83+
# connected
84+
print("CONNECTED")
85+
ble.stop_advertising()
86+
87+
# receive and handle BLE traffic
88+
while ble.connected:
89+
if uart.in_waiting:
90+
raw_bytes = uart.read(uart.in_waiting)
91+
if raw_bytes[0] == ord('!'):
92+
# BLE Connect Control Packet
93+
packet = Packet.from_bytes(raw_bytes)
94+
if isinstance(packet, ColorPacket):
95+
print("color = ", color)
96+
color = packet.color
97+
else:
98+
# Just plain text
99+
text = raw_bytes.decode("utf-8").strip()
100+
print("text = ", text)
101+
update_heart(text, color)
102+
103+
# disconnected
104+
print("DISCONNECTED")

TFT_Gizmo_Candy_Hearts/clue_hearts.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import time
2+
from random import choice
3+
import board
4+
import displayio
5+
import adafruit_imageload
6+
from adafruit_bitmap_font import bitmap_font
7+
from adafruit_display_text import label
8+
from adafruit_clue import clue
9+
10+
#---| User Config |--------------------------------------------------
11+
HEART_MESSAGES = (
12+
("I LUV", "YOU"),
13+
("SAY", "YES"),
14+
("HUG", "ME"),
15+
("BE", "MINE"),
16+
("TEXT","ME"),
17+
("OMG","LOL"),
18+
("PEACE",""),
19+
)
20+
HEART_COLORS = (
21+
0xEAFF50, # yellow
22+
0xFFAD50, # orange
23+
0x9D50FF, # purple
24+
0x13B0FE, # blue
25+
0xABFF96, # green
26+
0xFF96FF, # pink
27+
)
28+
MESSAGE_COLORS = (
29+
0xFF0000, # red
30+
)
31+
#---| User Config |--------------------------------------------------
32+
33+
# Create the display
34+
display = board.DISPLAY
35+
36+
# Load the candy heart BMP
37+
bitmap, palette = adafruit_imageload.load("/heart_bw.bmp",
38+
bitmap=displayio.Bitmap,
39+
palette=displayio.Palette)
40+
41+
heart = displayio.TileGrid(bitmap, pixel_shader=palette)
42+
43+
# Set up message text
44+
font = bitmap_font.load_font("/Multicolore_36.bdf")
45+
line1 = label.Label(font, text="?"*9)
46+
line2 = label.Label(font, text="?"*5)
47+
line1.anchor_point = (0.5, 0) # middle top
48+
line2.anchor_point = (0.5, 1.0) # middle bottom
49+
50+
# Set up group and add to display
51+
group = displayio.Group()
52+
group.append(heart)
53+
group.append(line1)
54+
group.append(line2)
55+
display.show(group)
56+
57+
while True:
58+
# turn off auto refresh while we change some things
59+
display.auto_refresh = False
60+
# pick a random message
61+
line1.text, line2.text = choice(HEART_MESSAGES)
62+
# update location for new text bounds
63+
line1.anchored_position = (120, 85)
64+
line2.anchored_position = (120, 175)
65+
# pick a random text color
66+
line1.color = line2.color = choice(MESSAGE_COLORS)
67+
# pick a ranomd heart color
68+
palette[1] = choice(HEART_COLORS)
69+
# OK, now turn auto refresh back on to display
70+
display.auto_refresh = True
71+
# wait for button press
72+
while not clue.button_a and not clue.button_b:
73+
pass
74+
# just a little debounce
75+
time.sleep(0.25)

0 commit comments

Comments
 (0)