Skip to content

Instantly share code, notes, and snippets.

@dglaude
Forked from antonizoon/code.py
Created February 1, 2021 21:31
Show Gist options
  • Save dglaude/01fc40a560a1ee397488d21b831d47f8 to your computer and use it in GitHub Desktop.
Save dglaude/01fc40a560a1ee397488d21b831d47f8 to your computer and use it in GitHub Desktop.
bb10keyboard_hid
# based on the example here, needs the same libraries https://github.com/arturo182/keyboard_featherwing_sw/tree/master/circuitpython
# also drop in the adafruit_hid library. basically just added keyboard send ability when it displays to the screen
# as per the README: https://github.com/adafruit/Adafruit_CircuitPython_HID
#
# HID Keyboard functionality could be improved. Currently it sends the current line to the UART Output terminal as a log
# but it might be better to just remove the UART functionality as it adds lag.
from bbq10keyboard import BBQ10Keyboard, STATE_PRESS, STATE_RELEASE, STATE_LONG_PRESS
from adafruit_display_text.label import Label
from adafruit_display_shapes.rect import Rect
import adafruit_ili9341
import terminalio
import displayio
import neopixel
import board
import busio
import time
import gc
# HID keyboard libraries
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# The keyboard object!
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
hid_kbd = Keyboard(usb_hid.devices)
hid_kbd_layout = KeyboardLayoutUS(hid_kbd) # We're in the US :)
#
# BBQ10 Keyboard UART Demo
#
# ui dimensions
header = 32
margin = 8
border = 1
# Release any resources currently in use for the displays
displayio.release_displays()
spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
uart = busio.UART(board.TX, board.RX, baudrate=115200, receiver_buffer_size=128)
i2c = board.I2C()
kbd = BBQ10Keyboard(i2c)
splash = displayio.Group(max_size=25)
display.show(splash)
# background
def bg_stripe(x, color):
width = display.width // 8
color_bitmap = displayio.Bitmap(width, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = color
bg_sprite = displayio.TileGrid(color_bitmap, x=x*width, y=0, pixel_shader=color_palette)
splash.append(bg_sprite)
bg_stripe(0, 0x000000)
bg_stripe(1, 0x784F17)
bg_stripe(2, 0xFF0018)
bg_stripe(3, 0xFFA52C)
bg_stripe(4, 0xFFFF41)
bg_stripe(5, 0x008018)
bg_stripe(6, 0x0000F9)
bg_stripe(7, 0x86007D)
# output rect
output_rect = Rect(margin, margin, display.width-margin*2, display.height-margin*2-header-margin, fill=0xFFFFFF, outline=0x666666)
splash.append(output_rect)
# output header
header_rect = Rect(margin + border, margin+border, display.width-(margin+border)*2, header, fill=0xCCCCCC)
splash.append(header_rect)
header_text = Label(terminalio.FONT, text="UART Demo", x=margin*2+border, y=int(margin+border+header/2), color=0x000000)
splash.append(header_text)
# output text
p = displayio.Palette(2)
p.make_transparent(0)
p[1] = 0x000000
w, h = terminalio.FONT.get_bounding_box()
tilegrid = displayio.TileGrid(terminalio.FONT.bitmap, pixel_shader=p, x=margin*2+border, y=int(margin+border+header+margin/2), width=48, height=10, tile_width=w, tile_height=h)
term = terminalio.Terminal(tilegrid, terminalio.FONT)
splash.append(tilegrid)
# input textarea
input_rect = Rect(margin, display.height-margin-header, display.width-margin*2, header, fill=0xFFFFFF, outline=0x666666)
splash.append(input_rect)
# input text
input_text = Label(terminalio.FONT, text='', x=margin*2+border, y=int(display.height-margin-border-header*0.7), color=0x000000, max_glyphs=50)
splash.append(input_text)
# carret
carret = Rect(input_text.x + input_text.bounding_box[2] + 1, int(display.height-margin-header/2-header/4), 1, header//2, fill=0x000000)
splash.append(carret)
carret_blink_time = time.monotonic()
carret_blink_state = True
while True:
# Carret blink animation
if time.monotonic() - carret_blink_time >= 0.5:
if carret_blink_state:
splash.remove(carret)
else:
splash.append(carret)
carret_blink_state = not carret_blink_state
carret_blink_time = time.monotonic()
# Get UART incoming data and print to terminal on screen
if uart.in_waiting > 0:
uart_in = uart.read(uart.in_waiting)
print(uart_in)
term.write(uart_in)
# Process keyboard
if kbd.key_count > 0:
k = kbd.key
if k[0] == STATE_RELEASE:
# Actually type the key
# find the correct keycode for this press from the layout
keycodes = hid_kbd_layout.keycodes(k[1])
# send the keycode tuple as function arguments using *
hid_kbd.send(*keycodes)
try:
# display key to the screen
if k[1] == '\x08': # Backspace
if len(input_text.text) > 0:
input_text.text = input_text.text[:-1]
elif k[1] == '\n': # Enter, send over UART
text = input_text.text + '\n'
uart.write(text.encode('utf-8'))
term.write(text.encode('utf-8'))
input_text.text = ''
else: # Anything else, we add to the text field
input_text.text += k[1]
carret.x = input_text.x + input_text.bounding_box[2] + 1
except RuntimeError as e: # File "adafruit_display_text/label.py", line 375, in text Text length exceeds max_glyphs
# clear the textbox. future versions would put it into the buffer if we delete UART data
text = input_text.text + '\n'
term.write(text.encode('utf-8'))
input_text.text = ''
pass
@dglaude
Copy link
Author

dglaude commented Feb 1, 2021

Need to try this!
Does it support the extra character in BB mode (I never had a BlackBerry, but I know it exist)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment