-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
170 lines (142 loc) · 5.13 KB
/
code.py
File metadata and controls
170 lines (142 loc) · 5.13 KB
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
import time
import board
import busio
import digitalio
import displayio
import terminalio
import fourwire
import bitbangio
import gc
from adafruit_display_text import label
from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.roundrect import RoundRect
from adafruit_ili9341 import ILI9341
from scent_engine import ScentEngine
# --- MEMORY MANAGEMENT ---
gc.collect()
print(f"Start Memory: {gc.mem_free()}")
# --- 1. HARDWARE SETUP ---
displayio.release_displays() # Frigör den fördefinierade board.DISPLAY så vi kan ställa in rotation/bgr
# Display (Hardware SPI)
tft_spi = busio.SPI(clock=board.LCD_SCK, MOSI=board.LCD_MOSI, MISO=board.LCD_MISO)
tft_bus = fourwire.FourWire(
tft_spi, command=board.LCD_DC, chip_select=board.LCD_CS, reset=None
)
display = ILI9341(tft_bus, width=320, height=240, rotation=90, bgr=True)
# Backlight
backlight = digitalio.DigitalInOut(board.LCD_BCKL)
backlight.direction = digitalio.Direction.OUTPUT
backlight.value = True
# Touch (Bitbang SPI)
touch_cs = digitalio.DigitalInOut(board.TOUCH_CS)
touch_cs.direction = digitalio.Direction.OUTPUT
touch_cs.value = True
touch_spi = bitbangio.SPI(clock=board.TOUCH_SCK, MOSI=board.TOUCH_MOSI, MISO=board.TOUCH_MISO)
# --- 2. TOUCH DRIVER ---
class XPT2046_Sunton:
def __init__(self, spi, cs_pin):
self.spi = spi
self.cs = cs_pin
def get_touch(self):
if not self.spi.try_lock(): return None
try:
self.spi.configure(baudrate=1000000, polarity=0, phase=0)
self.cs.value = False
# Read X (Command 0xD0)
self.spi.write(b'\xD0')
buf = bytearray(2)
self.spi.readinto(buf)
x_raw = ((buf[0] << 8) | buf[1]) >> 3
# Read Y (Command 0x90)
self.spi.write(b'\x90')
self.spi.readinto(buf)
y_raw = ((buf[0] << 8) | buf[1]) >> 3
self.cs.value = True
finally:
self.spi.unlock()
if x_raw < 100 or y_raw < 100: return None
x = int(self.map_range(y_raw, 3900, 200, 0, 320))
y = int(self.map_range(x_raw, 3800, 250, 0, 240))
return (max(0, min(x, 320)), max(0, min(y, 240)))
def map_range(self, x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
touch = XPT2046_Sunton(touch_spi, touch_cs)
# --- 3. GRAPHICS SETUP ---
main_group = displayio.Group()
display.root_group = main_group
# A. Background
try:
# We must keep the file handle open for OnDiskBitmap
bg_file = open("/bg.bmp", "rb")
bg_bitmap = displayio.OnDiskBitmap(bg_file)
bg_grid = displayio.TileGrid(bg_bitmap, pixel_shader=bg_bitmap.pixel_shader)
main_group.append(bg_grid)
except Exception as e:
print(f"Error loading bg.bmp: {e}")
# Fallback black screen
fallback = displayio.Bitmap(320, 240, 1)
bg_grid = displayio.TileGrid(fallback, pixel_shader=displayio.Palette(1))
main_group.append(bg_grid)
# B. The Cursor (Hollow Circle)
cursor = Circle(0, 0, 30, fill=None, outline=0xFFFF00, stroke=5)
main_group.append(cursor)
# C. Action Highlights (Hollow Rounded Rects)
switch_rect = RoundRect(10, 170, 145, 60, r=10, fill=None, outline=0xFFFF00, stroke=4)
switch_rect.hidden = True
main_group.append(switch_rect)
activate_rect = RoundRect(165, 170, 145, 60, r=10, fill=None, outline=0xFF0000, stroke=4)
activate_rect.hidden = True
main_group.append(activate_rect)
# D. Status Text
# Bottom center, slightly overlaying the button gap
status_lbl = label.Label(terminalio.FONT, text="Ready", color=0xAAAAAA)
status_lbl.anchor_point = (0.5, 1.0)
status_lbl.anchored_position = (160, 238)
main_group.append(status_lbl)
# --- 4. LOGIC ---
engine = ScentEngine()
selected_pos = 1
def move_cursor(pos_id):
"""Updates circle position based on ID 1-8"""
idx = pos_id - 1
row = idx // 4
col = idx % 4
cursor.x = (col * 80) + 10
cursor.y = (row * 80) + 10
move_cursor(1)
gc.collect()
print(f"UI Ready. Mem: {gc.mem_free()}")
while True:
try:
p = touch.get_touch()
except:
p = None
if p:
x, y = p
row_idx = y // 80
if row_idx < 2:
col = x // 80
if col > 3: col = 3
new_id = (row_idx * 4) + col + 1
if new_id != selected_pos:
selected_pos = new_id
move_cursor(selected_pos)
elif row_idx == 2:
if x < 160:
switch_rect.hidden = False
status_lbl.text = f"Switching {selected_pos}..."
display.refresh()
engine.move(selected_pos, activate=False)
time.sleep(0.1)
switch_rect.hidden = True
status_lbl.text = "Ready"
else:
activate_rect.hidden = False
status_lbl.text = f"ACTIVATING {selected_pos}!"
display.refresh()
engine.move(selected_pos, activate=True)
time.sleep(0.2)
activate_rect.hidden = True
status_lbl.text = "Ready"
time.sleep(0.15)
time.sleep(0.01)