Skip to content

adding CH9328 CPython demo #2834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions CH9328_CPython_Demo/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import sys
import serial
import keyboard

port = '/dev/ttyUSB0' # Replace with your actual serial port

# Define a mapping for special characters when shift is pressed
SHIFTED_KEYS = {
'1': '!', '2': '@', '3': '#', '4': '$', '5': '%',
'6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
'`': '~', '-': '_', '=': '+', '[': '{', ']': '}',
'\\': '|', ';': ':', "'": '"', ',': '<', '.': '>',
'/': '?'
}

def send_key(serial_port, key):
"""
Send a key press to the CH9328 via UART.

Parameters:
serial_port (serial.Serial): The serial port connection.
key (str): The key to send.
"""
serial_port.write(key.encode('ascii'))
serial_port.flush()

def send_empty_report(serial_port):
"""
Send an empty HID report to reset the state of the device.

Parameters:
serial_port (serial.Serial): The serial port connection.
"""
try:
empty_report = bytearray([0] * 8)
serial_port.write(empty_report)
serial_port.flush()
except serial.SerialException as e:
print(f"Failed to send empty report: {e}")

def main():
# Configure the serial connection
baudrate = 9600 # Default baud rate for CH9328 in Mode 1
timeout = 1

with serial.Serial(port, baudrate, timeout=timeout) as ser:

print("Listening for keyboard inputs. Press 'ESC' to exit.")

def on_key_event(event):
if event.event_type == 'down':
key = event.name
if len(key) == 1: # Only process single character keys
if keyboard.is_pressed('shift'): # Check if shift is pressed
key = SHIFTED_KEYS.get(key, key.upper())
send_key(ser, key)
elif key == 'space':
send_key(ser, ' ')
elif key == 'enter':
send_key(ser, '\n')
send_empty_report(ser)

# Hook the keyboard event
keyboard.hook(on_key_event)

# Wait for ESC to exit
keyboard.wait('esc')

if __name__ == "__main__":
main()
sys.exit()