Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e952fc

Browse files
boot_fri3d2024.py: add support for buttons and joysticks
1 parent093da7b commit2e952fc

File tree

1 file changed

+100
-12
lines changed

1 file changed

+100
-12
lines changed

‎internal_filesystem/boot_fri3d2024.py‎

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Hardware initialization for Fri3d Camp 2024 Badge
2+
current_hardware="fri3d-2024"
23

34
frommachineimportPin,SPI
45
importst7789
@@ -14,24 +15,16 @@
1415

1516
# Pin configuration
1617
SPI_BUS=2
17-
#SPI_FREQ = 40000000
18-
#SPI_BUS = 1
19-
SPI_FREQ=20000000
18+
SPI_FREQ=40000000
19+
#SPI_FREQ = 20000000 # also works but I guess higher is better
2020
LCD_SCLK=7
2121
LCD_MOSI=6
2222
LCD_MISO=8
2323
LCD_DC=4
2424
LCD_CS=5
25-
#LCD_BL = 1
25+
#LCD_BL = 1 # backlight can't be controlled on this hardware
2626
LCD_RST=48
2727

28-
#I2C_BUS = 0
29-
#I2C_FREQ = 100000
30-
#TP_SDA = 48
31-
#TP_SCL = 47
32-
#TP_ADDR = 0x15
33-
#TP_REGBITS = 8
34-
3528
TFT_HOR_RES=296
3629
TFT_VER_RES=240
3730

@@ -48,7 +41,6 @@
4841
cs=LCD_CS
4942
)
5043

51-
#rs=LCD_RST
5244
# lv.color_format_get_size(lv.COLOR_FORMAT.RGB565) = 2 bytes per pixel * 320 * 240 px = 153600 bytes
5345
# The default was /10 so 15360 bytes.
5446
# /2 = 76800 shows something on display and then hangs the board
@@ -88,4 +80,100 @@
8880
display.set_rotation(lv.DISPLAY_ROTATION._270)# must be done after initializing display and creating the touch drivers, to ensure proper handling
8981
display.set_params(0x36,bytearray([0x28]))
9082

83+
# Button and joystick handling code:
84+
frommachineimportADC,Pin
85+
86+
btn_x=Pin(38,Pin.IN,Pin.PULL_UP)# X
87+
btn_y=Pin(41,Pin.IN,Pin.PULL_UP)# Y
88+
btn_a=Pin(39,Pin.IN,Pin.PULL_UP)# A
89+
btn_b=Pin(40,Pin.IN,Pin.PULL_UP)# B
90+
btn_start=Pin(0,Pin.IN,Pin.PULL_UP)# START
91+
btn_menu=Pin(45,Pin.IN,Pin.PULL_UP)# START
92+
93+
ADC_KEY_MAP= [
94+
{'key':'UP','unit':1,'channel':2,'min':3072,'max':4096},
95+
{'key':'DOWN','unit':1,'channel':2,'min':0,'max':1024},
96+
{'key':'RIGHT','unit':1,'channel':0,'min':3072,'max':4096},
97+
{'key':'LEFT','unit':1,'channel':0,'min':0,'max':1024},
98+
]
99+
100+
# Initialize ADC for the two channels
101+
adc_up_down=ADC(Pin(3))# ADC1_CHANNEL_2 (GPIO 33)
102+
adc_up_down.atten(ADC.ATTN_11DB)# 0-3.3V range
103+
adc_left_right=ADC(Pin(1))# ADC1_CHANNEL_0 (GPIO 36)
104+
adc_left_right.atten(ADC.ATTN_11DB)# 0-3.3V range
105+
106+
defread_joystick():
107+
# Read ADC values
108+
val_up_down=adc_up_down.read()
109+
val_left_right=adc_left_right.read()
110+
111+
# Check each key's range
112+
formappinginADC_KEY_MAP:
113+
adc_val=val_up_downifmapping['channel']==2elseval_left_right
114+
ifmapping['min']<=adc_val<=mapping['max']:
115+
returnmapping['key']
116+
returnNone# No key triggered
117+
118+
119+
# Read callback
120+
# Warning: This gets called several times per second, and if it outputs continuous debugging on the serial line,
121+
# that will break tools like mpremote from working properly to upload new files over the serial line, thus needing a reflash.
122+
defkeypad_read_cb(indev,data):
123+
data.continue_reading=False# No more data to read
124+
# Check GPIOs and set key/state (only one key at a time)
125+
ifbtn_x.value()==0:
126+
data.key=lv.KEY.ESC
127+
data.state=lv.INDEV_STATE.PRESSED
128+
mpos.ui.back_screen()
129+
elifbtn_y.value()==0:
130+
data.key=lv.KEY.ESC
131+
data.state=lv.INDEV_STATE.PRESSED
132+
mpos.ui.back_screen()
133+
elifbtn_a.value()==0:
134+
#print("A pressed")
135+
data.key=lv.KEY.ENTER
136+
data.state=lv.INDEV_STATE.PRESSED
137+
elifbtn_b.value()==0:
138+
#print("B pressed")
139+
data.key=lv.KEY.ENTER
140+
data.state=lv.INDEV_STATE.PRESSED
141+
elifbtn_menu.value()==0:
142+
data.key=lv.KEY.HOME
143+
data.state=lv.INDEV_STATE.PRESSED
144+
elifbtn_start.value()==0:
145+
data.key=lv.KEY.END
146+
data.state=lv.INDEV_STATE.PRESSED
147+
else:
148+
data.state=lv.INDEV_STATE.RELEASED
149+
ifdata.state==lv.INDEV_STATE.RELEASED:
150+
joystick=read_joystick()
151+
ifjoystick=="LEFT":
152+
data.key=lv.KEY.PREV
153+
data.state=lv.INDEV_STATE.PRESSED
154+
elifjoystick=="RIGHT":
155+
data.key=lv.KEY.NEXT
156+
data.state=lv.INDEV_STATE.PRESSED
157+
elifjoystick=="UP":
158+
data.key=lv.KEY.UP
159+
data.state=lv.INDEV_STATE.PRESSED
160+
elifjoystick=="DOWN":
161+
data.key=lv.KEY.DOWN
162+
data.state=lv.INDEV_STATE.PRESSED
163+
else:
164+
data.state=lv.INDEV_STATE.RELEASED
165+
166+
167+
group=lv.group_create()
168+
group.set_default()
169+
170+
# Create and set up the input device
171+
indev=lv.indev_create()
172+
indev.set_type(lv.INDEV_TYPE.KEYPAD)
173+
indev.set_read_cb(keypad_read_cb)
174+
indev.set_group(group)
175+
disp=lv.display_get_default()# NOQA
176+
indev.set_display(disp)# different from display
177+
indev.enable(True)# NOQA
178+
91179
print("boot.py finished")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp