Skip to content

Commit

Permalink
Update DisplayPad to v1.0.3, add async support, and image handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Erik05Master committed Oct 19, 2024
1 parent 0e6135c commit 4e93889
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 39 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@ This library allows you to customize your own Mountain DisplayPad by assigning e
# Example

```python
import time
import asyncio

from displaypad import DisplayPad


def main():
async def main():
# Create a new DisplayPad instance
pad = DisplayPad.DisplayPad()

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

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

# Define event handlers
@pad.on('error')
def on_error(error):
print(f"Error: {error}")

# Clear all keys
pad.clear_all_keys()

# Set the first three keys to red, green and blue
pad.set_key_color(0, 255, 0, 0)
pad.set_key_color(1, 0, 255, 0)
pad.set_key_color(2, 0, 0, 255)

image = bytearray(pad.ICON_SIZE * pad.ICON_SIZE * 3)
for i in range(pad.ICON_SIZE * pad.ICON_SIZE):
image[i * 3] = 0xff
image[i * 3 + 1] = 0x00
image[i * 3 + 2] = 0x00
# Keep the script running
while True:
time.sleep(1)

await asyncio.sleep(1)

if __name__ == "__main__":
main()

# Run the main function
asyncio.run(main())
```
34 changes: 18 additions & 16 deletions displaypad/DisplayPad.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import threading

import hid
from PIL import Image
from pyee import EventEmitter


Expand Down Expand Up @@ -153,20 +154,20 @@ def _handle_key_press(self, key_index, key_pressed):
def _process_device_event(self, data):
if data[0] == 0x01:
# Row 1
self._handle_key_press(1, (data[42] & 0x02) != 0)
self._handle_key_press(2, (data[42] & 0x04) != 0)
self._handle_key_press(3, (data[42] & 0x08) != 0)
self._handle_key_press(4, (data[42] & 0x10) != 0)
self._handle_key_press(5, (data[42] & 0x20) != 0)
self._handle_key_press(6, (data[42] & 0x40) != 0)
self._handle_key_press(0, (data[42] & 0x02) != 0)
self._handle_key_press(1, (data[42] & 0x04) != 0)
self._handle_key_press(2, (data[42] & 0x08) != 0)
self._handle_key_press(3, (data[42] & 0x10) != 0)
self._handle_key_press(4, (data[42] & 0x20) != 0)
self._handle_key_press(5, (data[42] & 0x40) != 0)

# Row 2
self._handle_key_press(7, (data[42] & 0x80) != 0)
self._handle_key_press(8, (data[47] & 0x01) != 0)
self._handle_key_press(9, (data[47] & 0x02) != 0)
self._handle_key_press(10, (data[47] & 0x04) != 0)
self._handle_key_press(11, (data[47] & 0x08) != 0)
self._handle_key_press(12, (data[47] & 0x10) != 0)
self._handle_key_press(6, (data[42] & 0x80) != 0)
self._handle_key_press(7, (data[47] & 0x01) != 0)
self._handle_key_press(8, (data[47] & 0x02) != 0)
self._handle_key_press(9, (data[47] & 0x04) != 0)
self._handle_key_press(10, (data[47] & 0x08) != 0)
self._handle_key_press(11, (data[47] & 0x10) != 0)

elif data[0] == 0x11:
self.initializing = False
Expand Down Expand Up @@ -219,7 +220,8 @@ def _initiate_pixel_transfer(self, key_index):
data[5] = key_index
self.device.write(data)

def get_image_buffer(self, img):
img = img.resize((self.ICON_SIZE, self.ICON_SIZE))
img = img.convert("RGB")
return bytearray(img.tobytes())
def get_image_buffer(self, image_path):
with Image.open(image_path) as img:
img = img.resize((self.ICON_SIZE, self.ICON_SIZE))
img = img.convert("RGB")
return bytearray(img.tobytes())
22 changes: 12 additions & 10 deletions tests/example.py → examples/basic_pad.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import time
import asyncio

from displaypad import DisplayPad


def main():
async def main():
# Create a new DisplayPad instance
pad = DisplayPad.DisplayPad()

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

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

# Define event handlers
@pad.on('error')
def on_error(error):
print(f"Error: {error}")

# Clear all keys
pad.clear_all_keys()

# Set the first three keys to red, green and blue
pad.set_key_color(0, 255, 0, 0)
pad.set_key_color(1, 0, 255, 0)
pad.set_key_color(2, 0, 0, 255)

image = bytearray(pad.ICON_SIZE * pad.ICON_SIZE * 3)
for i in range(pad.ICON_SIZE * pad.ICON_SIZE):
image[i * 3] = 0xff
image[i * 3 + 1] = 0x00
image[i * 3 + 2] = 0x00
# Keep the script running
while True:
time.sleep(1)

await asyncio.sleep(1)

if __name__ == "__main__":
main()
# Run the main function
asyncio.run(main())
Binary file added examples/icons/sl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/pad_with_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio

from displaypad import DisplayPad


async def main():
# Create a new DisplayPad instance
pad = DisplayPad.DisplayPad()

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

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

# Define event handlers
@pad.on('error')
def on_error(error):
print(f"Error: {error}")

# Clear all keys
pad.clear_all_keys()

# Set the fourth key to an image
pad.set_key_image(0, pad.get_image_buffer('icons/sl.png'))

# Keep the script running
while True:
await asyncio.sleep(1)

if __name__ == "__main__":
# Run the main function
asyncio.run(main())
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "DisplayPad"
version = "1.0.2"
version = "1.0.3"
authors = [
{ name="Sytxlabs", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='DisplayPad',
version='1.0.2',
version='1.0.3',
author='Sytxlabs',
author_email='[email protected]',
url='https://sytxlabs.eu',
Expand Down
Empty file removed tests/.gitignore
Empty file.

0 comments on commit 4e93889

Please sign in to comment.