Skip to content

Commit 8719a60

Browse files
authored
Merge pull request #2524 from kattni/neokey-breakout-code
Add NeoKey breakouts guide code
2 parents cfdb0e3 + 1081357 commit 8719a60

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

NeoKey_MX_and_CHOC_Breakouts/Arduino/NeoKey_NeoPixel_Rainbow_and_HID/.qt_py_rp2040.test.only

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
7+
#define NEOPIXEL_PIN A3
8+
#define NUM_PIXELS 2
9+
10+
#define SWITCHA_PIN A1
11+
#define SWITCHB_PIN A2
12+
13+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
//while (!Serial);
18+
strip.begin();
19+
strip.setBrightness(25);
20+
strip.show(); // Initialize all pixels to 'off'
21+
pinMode(SWITCHA_PIN, INPUT_PULLUP);
22+
pinMode(SWITCHB_PIN, INPUT_PULLUP);
23+
}
24+
25+
uint8_t j=0;
26+
void loop() {
27+
for(int i=0; i< strip.numPixels(); i++) {
28+
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
29+
}
30+
31+
// turn off LED if not pressed!
32+
if (!digitalRead(SWITCHA_PIN)) {
33+
Serial.println("A");
34+
strip.setPixelColor(0, 0);
35+
}
36+
if (!digitalRead(SWITCHB_PIN)) {
37+
Serial.println("B");
38+
strip.setPixelColor(1, 0);
39+
}
40+
41+
strip.show();
42+
43+
j++; // go to next color
44+
delay(10);
45+
}
46+
47+
// Slightly different, this makes the rainbow equally distributed throughout
48+
void rainbowCycle(uint8_t wait) {
49+
50+
}
51+
52+
// Input a value 0 to 255 to get a color value.
53+
// The colours are a transition r - g - b - back to r.
54+
uint32_t Wheel(byte WheelPos) {
55+
if(WheelPos < 85) {
56+
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
57+
} else if(WheelPos < 170) {
58+
WheelPos -= 85;
59+
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
60+
} else {
61+
WheelPos -= 170;
62+
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
63+
}
64+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""NeoKey Breakout HID Demo"""
6+
7+
import time
8+
import board
9+
import keypad
10+
import usb_hid
11+
import neopixel
12+
from adafruit_hid.keyboard import Keyboard
13+
from adafruit_hid.keycode import Keycode
14+
15+
# --- CONFIGURATION ---
16+
# Keycode to send on key press.
17+
SEND_ON_PRESS = (
18+
Keycode.B,
19+
Keycode.THREE,
20+
)
21+
22+
# NeoPixel colors for each key when pressed.
23+
COLORS = (
24+
(255, 255, 0), # Yellow
25+
(0, 255, 255), # Cyan
26+
)
27+
28+
# NeoPixel brightness. Must be a float or integer between 0.0 and 1.0, where 0 is off and
29+
# 1 is maximum brightness. Defaults to 0.3.
30+
BRIGHTNESS = 0.3
31+
32+
# NeoPixel and key switch pins. Update to match your wiring setup.
33+
PIXEL_PIN = board.A3
34+
KEY_PINS = (
35+
board.A1,
36+
board.A2,
37+
)
38+
39+
# --- SETUP AND CODE ---
40+
# Number of NeoPixels. This will always match the number of breakouts and
41+
# therefore the number of key pins listed.
42+
NUM_PIXELS = len(KEY_PINS)
43+
44+
# Create keyboard object.
45+
time.sleep(1) # Delay to avoid a race condition on some systems.
46+
keyboard = Keyboard(usb_hid.devices)
47+
48+
# Create NeoPixel object.
49+
pixels = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, brightness=BRIGHTNESS)
50+
51+
# Create keypad object.
52+
keys = keypad.Keys(KEY_PINS, value_when_pressed=False, pull=True)
53+
54+
while True:
55+
# Begin getting key events.
56+
event = keys.events.get()
57+
58+
# If there is a key press event, run this block.
59+
if event and event.pressed:
60+
# Save the number of the key pressed to `key_number` to use in multiple places.
61+
key_number = event.key_number
62+
# Light up the corresponding NeoPixel to the appropriate color in the `COLORS` tuple.
63+
pixels[key_number] = COLORS[key_number]
64+
# Send the appropriate `Keycode` as identified in the `SEND_ON_PRESS` tuple.
65+
keyboard.press(SEND_ON_PRESS[key_number])
66+
67+
# If there is a key released event, run this block.
68+
if event and event.released:
69+
# Turn off the LEDs.
70+
pixels.fill((0, 0, 0))
71+
# Report that the key switch has been released.
72+
keyboard.release_all()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2023 Dan Halbert for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
"""NeoKey Breakout NeoPixel Rainbow Cycle Demo"""
7+
8+
import time
9+
import board
10+
import keypad
11+
import neopixel
12+
from rainbowio import colorwheel
13+
14+
# --- CONFIGURATION ---
15+
# NeoPixel brightness. Must be a float or integer between 0.0 and 1.0, where 0 is off and
16+
# 1 is maximum brightness. Defaults to 0.3.
17+
BRIGHTNESS = 0.3
18+
19+
# NeoPixel and key switch pins. Update to match your wiring setup. If adding more
20+
# NeoKey breakouts, add the pins to `KEY_PINS` in the same format as the two shown.
21+
PIXEL_PIN = board.A3
22+
KEY_PINS = (
23+
board.A1,
24+
board.A2,
25+
)
26+
27+
# --- SETUP AND CODE ---
28+
# Number of NeoPixels. This will always match the number of breakouts and
29+
# therefore the number of key pins listed.
30+
NUM_PIXELS = len(KEY_PINS)
31+
32+
# Create NeoPixel object.
33+
pixels = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, brightness=BRIGHTNESS)
34+
35+
# Create keypad object.
36+
keys = keypad.Keys(KEY_PINS, value_when_pressed=False, pull=True)
37+
38+
# Create two lists.
39+
# The `pressed` list is used to track the key press state.
40+
pressed = [False] * NUM_PIXELS
41+
# The `color_value` list is used to track the current color value for a specific NeoPixel.
42+
color_value = [0] * NUM_PIXELS
43+
44+
while True:
45+
# Begin getting key events.
46+
event = keys.events.get()
47+
if event:
48+
# Remember the last state of the key when pressed.
49+
pressed[event.key_number] = event.pressed
50+
51+
# Advance the rainbow for the key that is currently pressed.
52+
for index in range(NUM_PIXELS):
53+
if pressed[index]:
54+
# Increase the color value by 1.
55+
color_value[index] += 1
56+
# Set the pixel color to the current color value.
57+
pixels[index] = colorwheel(color_value[index])
58+
59+
time.sleep(0.01)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""NeoKey Breakout Random NeoPixel Color Demo"""
6+
7+
import random
8+
import board
9+
import keypad
10+
import neopixel
11+
from rainbowio import colorwheel
12+
13+
# --- CONFIGURATION ---
14+
# NeoPixel brightness. Must be a float or integer between 0.0 and 1.0, where 0 is off and
15+
# 1 is maximum brightness. Defaults to 0.3.
16+
BRIGHTNESS = 0.3
17+
18+
# NeoPixel and key switch pins. Update to match your wiring setup.
19+
PIXEL_PIN = board.A3
20+
KEY_PINS = (
21+
board.A1,
22+
board.A2,
23+
)
24+
25+
# --- SET UP AND CODE ---
26+
# Number of NeoPixels. This will always match the number of breakouts and
27+
# therefore the number of key pins listed.
28+
NUM_PIXELS = len(KEY_PINS)
29+
30+
# Create NeoPixel object.
31+
pixels = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, brightness=BRIGHTNESS)
32+
33+
# Create keypad object.
34+
keys = keypad.Keys(KEY_PINS, value_when_pressed=False, pull=True)
35+
36+
while True:
37+
# Begin getting key events.
38+
event = keys.events.get()
39+
# If there is a key press event, run this block.
40+
if event and event.pressed:
41+
# Print the key number of the pressed key.
42+
print(f"Key {event.key_number} pressed!")
43+
# Light up the corresponding NeoPixel to a random color.
44+
pixels[event.key_number] = colorwheel(random.randint(0, 255))

0 commit comments

Comments
 (0)