use callback example

This commit is contained in:
Philipp Kramer 2024-10-01 14:45:39 +02:00
parent c8f0c34c77
commit 161acf624c

View file

@ -5,6 +5,31 @@
Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object
uint32_t Wheel(byte WheelPos);
NeoKey1x4Callback blink(keyEvent evt);
//define a callback for key presses
NeoKey1x4Callback blink(keyEvent evt) {
uint8_t key = evt.bit.NUM;
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
Serial.print("Key press ");
Serial.println(key);
neokey.pixels.setPixelColor(key, Wheel(map(key, 0, neokey.pixels.numPixels(), 0, 255)));
} else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
Serial.print("Key release ");
Serial.println(key);
neokey.pixels.setPixelColor(key, 0);
}
// Turn on/off the neopixels!
neokey.pixels.show();
return 0;
}
void setup() {
Serial.begin(115200);
while (! Serial) delay(10);
@ -27,42 +52,16 @@ void setup() {
neokey.pixels.show();
delay(50);
}
// set callbacks
for(int i=0; i<NEOKEY_1X4_KEYS; i++){
neokey.registerCallback(i, blink);
}
}
void loop() {
uint8_t buttons = neokey.read();
// Check each button, if pressed, light the matching neopixel
if (buttons & (1<<0)) {
Serial.println("Button A");
neokey.pixels.setPixelColor(0, 0xFF0000); // red
} else {
neokey.pixels.setPixelColor(0, 0);
}
if (buttons & (1<<1)) {
Serial.println("Button B");
neokey.pixels.setPixelColor(1, 0xFFFF00); // yellow
} else {
neokey.pixels.setPixelColor(1, 0);
}
if (buttons & (1<<2)) {
Serial.println("Button C");
neokey.pixels.setPixelColor(2, 0x00FF00); // green
} else {
neokey.pixels.setPixelColor(2, 0);
}
if (buttons & (1<<3)) {
Serial.println("Button D");
neokey.pixels.setPixelColor(3, 0x00FFFF); // blue
} else {
neokey.pixels.setPixelColor(3, 0);
}
neokey.pixels.show();
// we handle all key events with the callbacks
neokey.read();
delay(10); // don't print too fast
}