Skip to content

adding a helper class for keyboard featherwing #64

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 6 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
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
82 changes: 82 additions & 0 deletions adafruit_featherwing/keyboard_featherwing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# The MIT License (MIT)
#
# Copyright (c) 2020 Foamyguy for Adafruit Industries LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_featherwing.keyboard_featherwing`
====================================================

Helper for using the `Keyboard Featherwing`
<https://www.tindie.com/products/arturo182/keyboard-featherwing-qwerty-keyboard-26-lcd/>`_.

* Author(s): Foamyguy

Requires:
* adafruit_ili9341
* adafruit_stmpe610
"""

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

import board
import digitalio
import displayio
import adafruit_ili9341
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
import sdcardio
import storage
from bbq10keyboard import BBQ10Keyboard
import neopixel


# pylint: disable-msg=too-few-public-methods
class KeyboardFeatherwing:
"""Class representing a `Keyboard Featherwing`
<https://www.tindie.com/products/arturo182/keyboard-featherwing-qwerty-keyboard-26-lcd/>`_.

"""

def __init__(self, spi=None, cs=None, dc=None, i2c=None):
displayio.release_displays()
if spi is None:
spi = board.SPI()
if cs is None:
cs = board.D9
if dc is None:
dc = board.D10
if i2c is None:
i2c = board.I2C()

ts_cs = digitalio.DigitalInOut(board.D6)
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)

display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
self.display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
self.neopixel = neopixel.NeoPixel(board.D11, 1)
self.keyboard = BBQ10Keyboard(i2c)
sd_cs = board.D5
self._sdcard = None
try:
self._sdcard = sdcardio.SDCard(spi, sd_cs)
vfs = storage.VfsFat(self._sdcard)
storage.mount(vfs, "/sd")
except OSError as error:
print("No SD card found:", error)
47 changes: 47 additions & 0 deletions examples/featherwing_keyboard_featherwing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
This example will display a CircuitPython console and
print the coordinates of touchscreen presses, as well
as keyboard events from the BBQ10 keyboard.
It will also try to write and then read a file on the
SD Card.
It will also set the color of the neopixel on the featherwing.

"""
from bbq10keyboard import STATE_RELEASE, STATE_LONG_PRESS
from adafruit_featherwing import keyboard_featherwing

kbd_featherwing = keyboard_featherwing.KeyboardFeatherwing()

# set the neopixel on the kbd featherwing
kbd_featherwing.neopixel.brightness = 0.1
kbd_featherwing.neopixel[0] = 0x002244

try:
f = open("/sd/tft_featherwing.txt", "w")
f.write("Blinka\nBlackberry Q10 Keyboard")
f.close()

f = open("/sd/tft_featherwing.txt", "r")
print(f.read())
f.close()
except OSError as error:
print("Unable to write to SD Card.")

while True:
# print touch info
if not kbd_featherwing.touchscreen.buffer_empty:
print(kbd_featherwing.touchscreen.read_data())

# print keyboard events
key_count = kbd_featherwing.keyboard.key_count
if key_count > 0:
key = kbd_featherwing.keyboard.key
STATE = "pressed"
if key[0] == STATE_LONG_PRESS:
STATE = "held down"
elif key[0] == STATE_RELEASE:
STATE = "released"
print(
"key: '%s' (dec %d, hex %02x) %s"
% (key[1], ord(key[1]), ord(key[1]), STATE)
)