Skip to content

Commit 4e93889

Browse files
committed
Update DisplayPad to v1.0.3, add async support, and image handling
Upgraded project version to 1.0.3. Refactored examples and event loop to use asyncio instead of time for improved async handling. Added the Python Imaging Library (PIL) dependency to facilitate setting key images.
1 parent 0e6135c commit 4e93889

File tree

8 files changed

+81
-39
lines changed

8 files changed

+81
-39
lines changed

README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,43 @@ This library allows you to customize your own Mountain DisplayPad by assigning e
44
# Example
55

66
```python
7-
import time
7+
import asyncio
88

99
from displaypad import DisplayPad
1010

1111

12-
def main():
12+
async def main():
13+
# Create a new DisplayPad instance
1314
pad = DisplayPad.DisplayPad()
1415

16+
# Define event handlers
1517
@pad.on('down')
1618
def on_key_down(key_index):
1719
print(f"Key {key_index} has been pressed.")
1820

21+
# Define event handlers
1922
@pad.on('up')
2023
def on_key_down(key_index):
2124
print(f"Key {key_index} has been released.")
2225

26+
# Define event handlers
2327
@pad.on('error')
2428
def on_error(error):
2529
print(f"Error: {error}")
2630

31+
# Clear all keys
2732
pad.clear_all_keys()
2833

34+
# Set the first three keys to red, green and blue
2935
pad.set_key_color(0, 255, 0, 0)
3036
pad.set_key_color(1, 0, 255, 0)
3137
pad.set_key_color(2, 0, 0, 255)
3238

33-
image = bytearray(pad.ICON_SIZE * pad.ICON_SIZE * 3)
34-
for i in range(pad.ICON_SIZE * pad.ICON_SIZE):
35-
image[i * 3] = 0xff
36-
image[i * 3 + 1] = 0x00
37-
image[i * 3 + 2] = 0x00
39+
# Keep the script running
3840
while True:
39-
time.sleep(1)
40-
41+
await asyncio.sleep(1)
4142

4243
if __name__ == "__main__":
43-
main()
44-
44+
# Run the main function
45+
asyncio.run(main())
4546
```

displaypad/DisplayPad.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import threading
22

33
import hid
4+
from PIL import Image
45
from pyee import EventEmitter
56

67

@@ -153,20 +154,20 @@ def _handle_key_press(self, key_index, key_pressed):
153154
def _process_device_event(self, data):
154155
if data[0] == 0x01:
155156
# Row 1
156-
self._handle_key_press(1, (data[42] & 0x02) != 0)
157-
self._handle_key_press(2, (data[42] & 0x04) != 0)
158-
self._handle_key_press(3, (data[42] & 0x08) != 0)
159-
self._handle_key_press(4, (data[42] & 0x10) != 0)
160-
self._handle_key_press(5, (data[42] & 0x20) != 0)
161-
self._handle_key_press(6, (data[42] & 0x40) != 0)
157+
self._handle_key_press(0, (data[42] & 0x02) != 0)
158+
self._handle_key_press(1, (data[42] & 0x04) != 0)
159+
self._handle_key_press(2, (data[42] & 0x08) != 0)
160+
self._handle_key_press(3, (data[42] & 0x10) != 0)
161+
self._handle_key_press(4, (data[42] & 0x20) != 0)
162+
self._handle_key_press(5, (data[42] & 0x40) != 0)
162163

163164
# Row 2
164-
self._handle_key_press(7, (data[42] & 0x80) != 0)
165-
self._handle_key_press(8, (data[47] & 0x01) != 0)
166-
self._handle_key_press(9, (data[47] & 0x02) != 0)
167-
self._handle_key_press(10, (data[47] & 0x04) != 0)
168-
self._handle_key_press(11, (data[47] & 0x08) != 0)
169-
self._handle_key_press(12, (data[47] & 0x10) != 0)
165+
self._handle_key_press(6, (data[42] & 0x80) != 0)
166+
self._handle_key_press(7, (data[47] & 0x01) != 0)
167+
self._handle_key_press(8, (data[47] & 0x02) != 0)
168+
self._handle_key_press(9, (data[47] & 0x04) != 0)
169+
self._handle_key_press(10, (data[47] & 0x08) != 0)
170+
self._handle_key_press(11, (data[47] & 0x10) != 0)
170171

171172
elif data[0] == 0x11:
172173
self.initializing = False
@@ -219,7 +220,8 @@ def _initiate_pixel_transfer(self, key_index):
219220
data[5] = key_index
220221
self.device.write(data)
221222

222-
def get_image_buffer(self, img):
223-
img = img.resize((self.ICON_SIZE, self.ICON_SIZE))
224-
img = img.convert("RGB")
225-
return bytearray(img.tobytes())
223+
def get_image_buffer(self, image_path):
224+
with Image.open(image_path) as img:
225+
img = img.resize((self.ICON_SIZE, self.ICON_SIZE))
226+
img = img.convert("RGB")
227+
return bytearray(img.tobytes())
+12-10
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import time
1+
import asyncio
22

33
from displaypad import DisplayPad
44

55

6-
def main():
6+
async def main():
7+
# Create a new DisplayPad instance
78
pad = DisplayPad.DisplayPad()
89

10+
# Define event handlers
911
@pad.on('down')
1012
def on_key_down(key_index):
1113
print(f"Key {key_index} has been pressed.")
1214

15+
# Define event handlers
1316
@pad.on('up')
1417
def on_key_down(key_index):
1518
print(f"Key {key_index} has been released.")
1619

20+
# Define event handlers
1721
@pad.on('error')
1822
def on_error(error):
1923
print(f"Error: {error}")
2024

25+
# Clear all keys
2126
pad.clear_all_keys()
2227

28+
# Set the first three keys to red, green and blue
2329
pad.set_key_color(0, 255, 0, 0)
2430
pad.set_key_color(1, 0, 255, 0)
2531
pad.set_key_color(2, 0, 0, 255)
2632

27-
image = bytearray(pad.ICON_SIZE * pad.ICON_SIZE * 3)
28-
for i in range(pad.ICON_SIZE * pad.ICON_SIZE):
29-
image[i * 3] = 0xff
30-
image[i * 3 + 1] = 0x00
31-
image[i * 3 + 2] = 0x00
33+
# Keep the script running
3234
while True:
33-
time.sleep(1)
34-
35+
await asyncio.sleep(1)
3536

3637
if __name__ == "__main__":
37-
main()
38+
# Run the main function
39+
asyncio.run(main())

examples/icons/sl.png

13.3 KB
Loading

examples/pad_with_image.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import asyncio
2+
3+
from displaypad import DisplayPad
4+
5+
6+
async def main():
7+
# Create a new DisplayPad instance
8+
pad = DisplayPad.DisplayPad()
9+
10+
# Define event handlers
11+
@pad.on('down')
12+
def on_key_down(key_index):
13+
print(f"Key {key_index} has been pressed.")
14+
15+
# Define event handlers
16+
@pad.on('up')
17+
def on_key_down(key_index):
18+
print(f"Key {key_index} has been released.")
19+
20+
# Define event handlers
21+
@pad.on('error')
22+
def on_error(error):
23+
print(f"Error: {error}")
24+
25+
# Clear all keys
26+
pad.clear_all_keys()
27+
28+
# Set the fourth key to an image
29+
pad.set_key_image(0, pad.get_image_buffer('icons/sl.png'))
30+
31+
# Keep the script running
32+
while True:
33+
await asyncio.sleep(1)
34+
35+
if __name__ == "__main__":
36+
# Run the main function
37+
asyncio.run(main())

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "DisplayPad"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
authors = [
55
{ name="Sytxlabs", email="info@sytxlabs.eu" },
66
]

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='DisplayPad',
5-
version='1.0.2',
5+
version='1.0.3',
66
author='Sytxlabs',
77
author_email='info@sytxlabs.eu',
88
url='https://sytxlabs.eu',

tests/.gitignore

Whitespace-only changes.

0 commit comments

Comments
 (0)