-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
111 lines (90 loc) · 2.94 KB
/
code.py
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
import time
import board
import digitalio
import usb_hid
import rotaryio
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
import usb_cdc
import busio
import displayio
import adafruit_displayio_ssd1306
import terminalio
from adafruit_display_text import label
#Initalise USB Serial & clear buffer
uart = usb_cdc.data
time.sleep(0.5)
uart.reset_input_buffer()
# Clear any display data holding up pins
displayio.release_displays()
# Init I2C
i2c = busio.I2C(board.GP1, board.GP0)
# Lock the I2C device before we try to scan
while not i2c.try_lock():
pass
# Print the addresses found once
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
# Unlock I2C now that we're done scanning.
i2c.unlock()
# Init display
display_bus = displayio.I2CDisplay(i2c, device_address = 0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)
# Draw a label
text = "Hello World"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=2, y=5)
display.show(text_area)
# Media Buttons
btnPrev = digitalio.DigitalInOut(board.GP18)
btnPrev.direction = digitalio.Direction.INPUT
btnPrev.pull = digitalio.Pull.UP
btnPlay = digitalio.DigitalInOut(board.GP17)
btnPlay.direction = digitalio.Direction.INPUT
btnPlay.pull = digitalio.Pull.UP
btnNext = digitalio.DigitalInOut(board.GP16)
btnNext.direction = digitalio.Direction.INPUT
btnNext.pull = digitalio.Pull.UP
# builtin LED
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT
# USB Device
consumer = ConsumerControl(usb_hid.devices)
delay = 0.2
def blink_led(duration):
led.value = True
time.sleep(duration)
led.value = False
time.sleep(duration)
while True:
# poll encoder position
# position = enc.position
# if position != lastPosition:
# led.value = True
# if lastPosition < position:
# consumer.send(ConsumerControlCode.VOLUME_INCREMENT)
# else:
# consumer.send(ConsumerControlCode.VOLUME_DECREMENT)
# lastPosition = position
# led.value = False
# poll encoder button
# if encSw.value == 0:
# consumer.send(ConsumerControlCode.MUTE)
# led.value = True
# time.sleep(delay)
# led.value = False
if btnPrev.value == 0:
consumer.send(ConsumerControlCode.SCAN_PREVIOUS_TRACK)
blink_led(delay)
if btnPlay.value == 0:
consumer.send(ConsumerControlCode.PLAY_PAUSE)
blink_led(delay)
if btnNext.value == 0:
consumer.send(ConsumerControlCode.SCAN_NEXT_TRACK)
blink_led(delay)
if uart.in_waiting:
data = uart.read(10)
time.sleep(0.2)
text = data.decode('utf-8').strip()
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=2, y=5)
display.show(text_area)
uart.reset_input_buffer()
time.sleep(0.1)