Our next step is to pull in the HID library and send keys instead of beeping. The GBoard app accepts '.' (period) and '-' (minus sign), for dots and dashes, respectively, so that's what we'll send.
The changes are that we import the Keyboard
and Keycode
modules from the HID library, and initialize a keyboard object that is then used to send keys to the connected device.
For setting up the keyboard we need to add:
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import usb_hid
kbd = Keyboard(usb_hid.devices)
To actually send keys, we replace the dot and dash tone generation with kbd.send(Keycode.PERIOD)
and kbd.send(Keycode.MINUS)
The code is below, copy it to code.py
on your Circuit Playground Express.
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries # # SPDX-License-Identifier: MIT """ Circuit Playground Express GBoard: capacitive touch generating keycodes Adafruit invests time and resources providing this open source code. Please support Adafruit and open source hardware by purchasing products from Adafruit! Written by Dave Astels for Adafruit Industries Copyright (c) 2018 Adafruit Industries Licensed under the MIT license. All text above must be included in any redistribution. """ import usb_hid from adafruit_circuitplayground.express import cpx from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode DOT_DURATION = 0.25 DASH_DURATION = 0.5 kbd = Keyboard(usb_hid.devices) # You can adjust this to get the level of sensitivity you want. cpx.adjust_touch_threshold(100) while True: if cpx.touch_A4: kbd.send(Keycode.PERIOD) while cpx.touch_A4: pass elif cpx.touch_A3: kbd.send(Keycode.MINUS) while cpx.touch_A3: pass
Open up an app that accepts text. It can be a word processor like Google Docs or just be a text input box like the SMS/phone message app or a Twitter message input box.
Now, when you touch the A4 pad a dot will be sent, and when you touch the A3 pad a dash will be. The GBoard keyboard replacement will convert those dots and dashes into conventional characters and send them to the app.
You can refer to the table below for International and American Morse Code (via Wikipedia).
Text editor powered by tinymce.