Adding NeoKey Trinkey examples.

This commit is contained in:
Kattni Rembor 2021-05-25 15:31:34 -04:00
parent 37e7c7e19b
commit 9c8fc907ee
3 changed files with 175 additions and 0 deletions

View file

@ -0,0 +1,80 @@
#include <Adafruit_NeoPixel.h>
#include "Adafruit_FreeTouch.h"
#include "HID-Project.h" // https://github.com/NicoHood/HID
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
// Create the touch pad
Adafruit_FreeTouch qt = Adafruit_FreeTouch(PIN_TOUCH, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
bool last_switch = true;
void setup() {
Serial.begin(9600);
//while (!Serial);
strip.begin();
strip.setBrightness(neo_brightness);
strip.show(); // Initialize all pixels to 'off'
if (! qt.begin())
Serial.println("Failed to begin qt");
pinMode(PIN_SWITCH, INPUT_PULLDOWN);
// Sends a clean report to the host. This is important on any Arduino type.
Consumer.begin();
}
uint8_t j=0;
void loop() {
// measure the captouches
uint16_t touch = qt.measure();
// don't print touch data constantly, only every 10 runs
if (j % 10 == 0) {
Serial.print("Touch: "); Serial.println(touch);
}
// If the pad is touched, turn on neopix!
if (touch > 500) {
Serial.println("Touched!");
strip.setBrightness(neo_brightness);
} else {
strip.setBrightness(0);
}
// check mechswitch
bool curr_switch = digitalRead(PIN_SWITCH);
if (curr_switch != last_switch) {
if (curr_switch) {
Serial.println("Pressed");
Consumer.write(MEDIA_PLAY_PAUSE);
} else {
Serial.println("Released");
}
last_switch = curr_switch;
}
// cycles of all colors on wheel, only visible if cap it touched
strip.setPixelColor(0, Wheel(j++));
strip.show();
delay(10);
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

View file

@ -0,0 +1,95 @@
"""NeoKey Trinkey Capacitive Touch and HID Keyboard example"""
import time
import board
import neopixel
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode # pylint: disable=unused-import
from digitalio import DigitalInOut, Pull
import touchio
print("NeoKey Trinkey HID")
# create the pixel and turn it off
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
pixel.fill(0x0)
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
# create the switch, add a pullup, start it with not being pressed
button = DigitalInOut(board.SWITCH)
button.switch_to_input(pull=Pull.DOWN)
button_state = False
# create the captouch element and start it with not touched
touch = touchio.TouchIn(board.TOUCH)
touch_state = False
# print a string on keypress
key_output = "Hello World!\n"
# one character on keypress
# key_output = Keycode.A
# multiple simultaneous keypresses
# key_output = (Keycode.SHIFT, Keycode.A) # capital A
# key_output = (Keycode.CONTROL, Keycode.ALT, Keycode.DELETE) # three finger salute!
# complex commands! we make a list of dictionary entries for each command
# each line has 'keys' which is either a single key, a list of keys, or a string
# then the 'delay' is in seconds, since we often need to give the computer a minute
# before doing something!
# this will open up a notepad in windows, and ducky the user
"""
key_output = (
{'keys': Keycode.GUI, 'delay': 0.1},
{'keys': "notepad\n", 'delay': 1}, # give it a moment to launch!
{'keys': "YOU HAVE BEEN DUCKIED!", 'delay': 0.1},
{'keys': (Keycode.ALT, Keycode.O), 'delay': 0.1}, # open format menu
{'keys': Keycode.F, 'delay': 0.1}, # open font submenu
{'keys': "\t\t100\n", 'delay': 0.1}, # tab over to font size, enter 100
)
"""
# our helper function will press the keys themselves
def make_keystrokes(keys, delay):
if isinstance(keys, str): # If it's a string...
keyboard_layout.write(keys) # ...Print the string
elif isinstance(keys, int): # If its a single key
keyboard.press(keys) # "Press"...
keyboard.release_all() # ..."Release"!
elif isinstance(keys, (list, tuple)): # If its multiple keys
keyboard.press(*keys) # "Press"...
keyboard.release_all() # ..."Release"!
time.sleep(delay)
while True:
if button.value and not button_state:
pixel.fill((255, 0, 255))
print("Button pressed.")
button_state = True
if not button.value and button_state:
pixel.fill(0x0)
print("Button released.")
if isinstance(key_output, (list, tuple)) and isinstance(key_output[0], dict):
for k in key_output:
make_keystrokes(k['keys'], k['delay'])
else:
make_keystrokes(key_output, delay=0)
button_state = False
if touch.value and not touch_state:
print("Touched!")
pixel.fill((0, 255, 0))
touch_state = True
if not touch.value and touch_state:
print("Untouched!")
pixel.fill(0x0)
touch_state = False