Skip to content

Commit 18c62f2

Browse files
authored
Merge pull request #1626 from kattni/neokey-snap
Adding NeoKey Snap code.
2 parents df156df + 4c539c0 commit 18c62f2

4 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
NeoKey 4x2 Ortho Snap-Apart simple key press NeoPixel demo.
3+
"""
4+
import board
5+
import keypad
6+
import neopixel
7+
8+
COLUMNS = 2
9+
ROWS = 4
10+
11+
pixels = neopixel.NeoPixel(board.D5, 30, brightness=0.3)
12+
13+
keys = keypad.KeyMatrix(
14+
row_pins=(board.D4, board.A3, board.A2, board.A1),
15+
column_pins=(board.D13, board.D12),
16+
columns_to_anodes=False,
17+
)
18+
19+
20+
def key_to_pixel_map(key_number):
21+
row = key_number // COLUMNS
22+
column = (key_number % COLUMNS)
23+
if row % 2 == 1:
24+
column = COLUMNS - column - 1
25+
return row * COLUMNS + column
26+
27+
28+
pixels.fill((0, 0, 0)) # Begin with pixels off.
29+
while True:
30+
key_event = keys.events.get()
31+
if key_event:
32+
print(key_event)
33+
if key_event.pressed:
34+
pixels[key_to_pixel_map(key_event.key_number)] = (255, 0, 0)
35+
else:
36+
pixels.fill((0, 0, 0))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
NeoKey 5x6 Ortho Snap-Apart simple key press NeoPixel demo.
3+
"""
4+
import board
5+
import keypad
6+
import neopixel
7+
8+
COLUMNS = 6
9+
ROWS = 5
10+
11+
pixels = neopixel.NeoPixel(board.D5, 30, brightness=0.3)
12+
13+
keys = keypad.KeyMatrix(
14+
row_pins=(board.D4, board.A3, board.A2, board.A1, board.A0),
15+
column_pins=(board.D13, board.D12, board.D11, board.D10, board.D9, board.D6),
16+
columns_to_anodes=False,
17+
)
18+
19+
20+
def key_to_pixel_map(key_number):
21+
row = key_number // COLUMNS
22+
column = (key_number % COLUMNS)
23+
if row % 2 == 1:
24+
column = COLUMNS - column - 1
25+
return row * COLUMNS + column
26+
27+
28+
pixels.fill((0, 0, 0)) # Begin with pixels off.
29+
while True:
30+
key_event = keys.events.get()
31+
if key_event:
32+
print(key_event)
33+
if key_event.pressed:
34+
pixels[key_to_pixel_map(key_event.key_number)] = (255, 0, 0)
35+
else:
36+
pixels.fill((0, 0, 0))

NeoKey_5x6_Ortho_Snap-Apart/Ortho5x6Demo/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include "Adafruit_Keypad.h"
3+
4+
#define ROWS 5 // rows
5+
#define COLS 6 // columns
6+
7+
#define NEOPIXEL_PIN 7
8+
#define NUM_PIXELS (ROWS * COLS)
9+
10+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
11+
12+
13+
//define the symbols on the buttons of the keypads
14+
char keys[ROWS][COLS] = {
15+
{'1','2','3','4','5','6'},
16+
{'7','8','9','A','B','C'},
17+
{'D','E','F','G','H','I'},
18+
{'J','K','L','M','N','O'},
19+
{'P','Q','R','S','T','U'}
20+
};
21+
22+
uint8_t rowPins[ROWS] = {6, 29, 28, 27, 26}; //connect to the row pinouts of the keypad
23+
uint8_t colPins[COLS] = {13, 12, 11, 10, 9, 8}; //connect to the column pinouts of the keypad
24+
25+
//initialize an instance of class NewKeypad
26+
Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
27+
28+
bool lit[ROWS*COLS] = {0};
29+
30+
void setup() {
31+
Serial.begin(115200);
32+
//while (!Serial);
33+
Serial.println("Ortho 5x6 keypad demo");
34+
strip.begin();
35+
strip.setBrightness(40);
36+
strip.show(); // Initialize all pixels to 'off'
37+
38+
customKeypad.begin();
39+
for (int i=0; i<ROWS*COLS; i++) {
40+
lit[i] = false;
41+
}
42+
}
43+
44+
uint8_t j=0; // color ticker
45+
46+
void loop() {
47+
//Serial.println("Test NeoPixels");
48+
customKeypad.tick();
49+
50+
while(customKeypad.available()){
51+
keypadEvent e = customKeypad.read();
52+
Serial.print((char)e.bit.KEY);
53+
if (e.bit.EVENT == KEY_JUST_PRESSED) {
54+
Serial.println(" pressed");
55+
uint8_t row = e.bit.ROW;
56+
uint8_t col = e.bit.COL;
57+
Serial.print("Row: "); Serial.print(row);
58+
Serial.print(" col: "); Serial.print(col);
59+
Serial.print(" -> ");
60+
uint16_t keynum;
61+
if (row % 2 == 0) { // even row
62+
keynum = row * COLS + col;
63+
} else { // odd row the neopixels go BACKWARDS!
64+
keynum = row * COLS + (5 - col);
65+
}
66+
Serial.println(keynum);
67+
lit[keynum] = !lit[keynum]; // invert neopixel status
68+
}
69+
else if(e.bit.EVENT == KEY_JUST_RELEASED) {
70+
Serial.println(" released");
71+
}
72+
}
73+
74+
for(int i=0; i< strip.numPixels(); i++) {
75+
if (lit[i]) {
76+
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
77+
} else {
78+
strip.setPixelColor(i, 0);
79+
}
80+
}
81+
strip.show();
82+
j++;
83+
84+
delay(10);
85+
}
86+
87+
88+
// Input a value 0 to 255 to get a color value.
89+
// The colours are a transition r - g - b - back to r.
90+
uint32_t Wheel(byte WheelPos) {
91+
if(WheelPos < 85) {
92+
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
93+
} else if(WheelPos < 170) {
94+
WheelPos -= 85;
95+
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
96+
} else {
97+
WheelPos -= 170;
98+
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
99+
}
100+
}

0 commit comments

Comments
 (0)