Simple test

Ensure your device works with this simple test.

examples/ch9328_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5"""Simple demo to type "Hello World!" and then delete it"""
 6
 7import time
 8
 9import board
10
11from adafruit_ch9328.ch9328 import Adafruit_CH9328
12from adafruit_ch9328.ch9328_keymap import Keymap
13
14# Initialize UART for the CH9328
15# check for Raspberry Pi
16if "CE0" and "CE1" in dir(board):
17    import serial
18
19    uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=3000)
20# otherwise use busio
21else:
22    import busio
23
24    uart = busio.UART(board.TX, board.RX, baudrate=9600)
25ch9328 = Adafruit_CH9328(uart)
26
27# Wait for 2 seconds
28time.sleep(2)
29# Sending "Hello World!" as an ASCII character string
30ch9328.send_string("Hello World!")
31# Wait for 2 seconds
32time.sleep(2)
33
34# Send the backspace key 12 times to erase the string
35keys = [Keymap.BACKSPACE, 0, 0, 0, 0, 0]  # Keycode for backspace in US mapping
36no_keys_pressed = [0, 0, 0, 0, 0, 0]
37for _ in range(12):
38    ch9328.send_key_press(keys, 0)  # Press
39    ch9328.send_key_press(no_keys_pressed, 0)  # Release the key