Skip to content

Commit 9c8fc90

Browse files
committed
Adding NeoKey Trinkey examples.
1 parent 37e7c7e commit 9c8fc90

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

NeoKey_Trinkey/Arduino_HID_Cap_Touch_Example/.neokeytrinkey_m0.only

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include "Adafruit_FreeTouch.h"
3+
#include "HID-Project.h" // https://github.com/NicoHood/HID
4+
5+
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
6+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
7+
8+
// Create the touch pad
9+
Adafruit_FreeTouch qt = Adafruit_FreeTouch(PIN_TOUCH, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
10+
11+
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
12+
13+
bool last_switch = true;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
//while (!Serial);
18+
strip.begin();
19+
strip.setBrightness(neo_brightness);
20+
strip.show(); // Initialize all pixels to 'off'
21+
22+
if (! qt.begin())
23+
Serial.println("Failed to begin qt");
24+
25+
pinMode(PIN_SWITCH, INPUT_PULLDOWN);
26+
27+
// Sends a clean report to the host. This is important on any Arduino type.
28+
Consumer.begin();
29+
}
30+
31+
uint8_t j=0;
32+
void loop() {
33+
// measure the captouches
34+
uint16_t touch = qt.measure();
35+
// don't print touch data constantly, only every 10 runs
36+
if (j % 10 == 0) {
37+
Serial.print("Touch: "); Serial.println(touch);
38+
}
39+
40+
// If the pad is touched, turn on neopix!
41+
if (touch > 500) {
42+
Serial.println("Touched!");
43+
strip.setBrightness(neo_brightness);
44+
} else {
45+
strip.setBrightness(0);
46+
}
47+
48+
// check mechswitch
49+
bool curr_switch = digitalRead(PIN_SWITCH);
50+
if (curr_switch != last_switch) {
51+
if (curr_switch) {
52+
Serial.println("Pressed");
53+
Consumer.write(MEDIA_PLAY_PAUSE);
54+
} else {
55+
Serial.println("Released");
56+
}
57+
last_switch = curr_switch;
58+
}
59+
60+
61+
// cycles of all colors on wheel, only visible if cap it touched
62+
strip.setPixelColor(0, Wheel(j++));
63+
strip.show();
64+
65+
delay(10);
66+
}
67+
68+
// Input a value 0 to 255 to get a color value.
69+
// The colours are a transition r - g - b - back to r.
70+
uint32_t Wheel(byte WheelPos) {
71+
if(WheelPos < 85) {
72+
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
73+
} else if(WheelPos < 170) {
74+
WheelPos -= 85;
75+
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
76+
} else {
77+
WheelPos -= 170;
78+
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
79+
}
80+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""NeoKey Trinkey Capacitive Touch and HID Keyboard example"""
2+
import time
3+
import board
4+
import neopixel
5+
import usb_hid
6+
from adafruit_hid.keyboard import Keyboard
7+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
8+
from adafruit_hid.keycode import Keycode # pylint: disable=unused-import
9+
from digitalio import DigitalInOut, Pull
10+
import touchio
11+
12+
print("NeoKey Trinkey HID")
13+
14+
# create the pixel and turn it off
15+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
16+
pixel.fill(0x0)
17+
18+
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
19+
keyboard = Keyboard(usb_hid.devices)
20+
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
21+
22+
# create the switch, add a pullup, start it with not being pressed
23+
button = DigitalInOut(board.SWITCH)
24+
button.switch_to_input(pull=Pull.DOWN)
25+
button_state = False
26+
27+
# create the captouch element and start it with not touched
28+
touch = touchio.TouchIn(board.TOUCH)
29+
touch_state = False
30+
31+
# print a string on keypress
32+
key_output = "Hello World!\n"
33+
34+
# one character on keypress
35+
# key_output = Keycode.A
36+
37+
# multiple simultaneous keypresses
38+
# key_output = (Keycode.SHIFT, Keycode.A) # capital A
39+
# key_output = (Keycode.CONTROL, Keycode.ALT, Keycode.DELETE) # three finger salute!
40+
41+
# complex commands! we make a list of dictionary entries for each command
42+
# each line has 'keys' which is either a single key, a list of keys, or a string
43+
# then the 'delay' is in seconds, since we often need to give the computer a minute
44+
# before doing something!
45+
46+
# this will open up a notepad in windows, and ducky the user
47+
"""
48+
key_output = (
49+
{'keys': Keycode.GUI, 'delay': 0.1},
50+
{'keys': "notepad\n", 'delay': 1}, # give it a moment to launch!
51+
{'keys': "YOU HAVE BEEN DUCKIED!", 'delay': 0.1},
52+
{'keys': (Keycode.ALT, Keycode.O), 'delay': 0.1}, # open format menu
53+
{'keys': Keycode.F, 'delay': 0.1}, # open font submenu
54+
{'keys': "\t\t100\n", 'delay': 0.1}, # tab over to font size, enter 100
55+
)
56+
"""
57+
58+
59+
# our helper function will press the keys themselves
60+
def make_keystrokes(keys, delay):
61+
if isinstance(keys, str): # If it's a string...
62+
keyboard_layout.write(keys) # ...Print the string
63+
elif isinstance(keys, int): # If its a single key
64+
keyboard.press(keys) # "Press"...
65+
keyboard.release_all() # ..."Release"!
66+
elif isinstance(keys, (list, tuple)): # If its multiple keys
67+
keyboard.press(*keys) # "Press"...
68+
keyboard.release_all() # ..."Release"!
69+
time.sleep(delay)
70+
71+
72+
while True:
73+
if button.value and not button_state:
74+
pixel.fill((255, 0, 255))
75+
print("Button pressed.")
76+
button_state = True
77+
78+
if not button.value and button_state:
79+
pixel.fill(0x0)
80+
print("Button released.")
81+
if isinstance(key_output, (list, tuple)) and isinstance(key_output[0], dict):
82+
for k in key_output:
83+
make_keystrokes(k['keys'], k['delay'])
84+
else:
85+
make_keystrokes(key_output, delay=0)
86+
button_state = False
87+
88+
if touch.value and not touch_state:
89+
print("Touched!")
90+
pixel.fill((0, 255, 0))
91+
touch_state = True
92+
if not touch.value and touch_state:
93+
print("Untouched!")
94+
pixel.fill(0x0)
95+
touch_state = False

0 commit comments

Comments
 (0)