2021-01-19 18:40:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
|
|
|
#include "pico_rgb_keypad.hpp"
|
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
/*
|
|
|
|
Test buttons/lights by lighting them up in a sequence of colors.
|
|
|
|
|
|
|
|
Press each button in turn, it will light up green.
|
|
|
|
Once all buttons are lit, they will turn off.
|
|
|
|
Press them again to light them in the next color.
|
|
|
|
*/
|
|
|
|
|
2021-01-19 18:40:51 +00:00
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
PicoRGBKeypad pico_keypad;
|
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
constexpr uint8_t num_colors = 6;
|
2021-01-19 18:40:51 +00:00
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
// Simple struct to pair r/g/b together as a color
|
|
|
|
struct color {uint8_t r, g, b;};
|
|
|
|
|
|
|
|
color colors[num_colors] = {
|
|
|
|
{0x00, 0x20, 0x00}, // Green
|
|
|
|
{0x20, 0x20, 0x00}, // Yellow
|
|
|
|
{0x20, 0x00, 0x00}, // Red
|
|
|
|
{0x20, 0x00, 0x20}, // Pink
|
|
|
|
{0x00, 0x00, 0x20}, // Blue
|
|
|
|
{0x00, 0x20, 0x20} // Teal
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
2021-01-19 18:40:51 +00:00
|
|
|
pico_keypad.init();
|
|
|
|
pico_keypad.set_brightness(1.0f);
|
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
uint16_t lit_buttons = 0;
|
|
|
|
uint8_t color_index = 0;
|
|
|
|
color current_color = colors[color_index];
|
2021-01-19 18:40:51 +00:00
|
|
|
|
|
|
|
while(true) {
|
2021-05-24 16:48:15 +01:00
|
|
|
// Read button states from i2c expander
|
|
|
|
// for any pressed buttons set the corresponding bit in "lit_buttons"
|
|
|
|
lit_buttons |= pico_keypad.get_button_states();
|
|
|
|
|
2021-09-08 18:34:35 +01:00
|
|
|
// You could use a bitwise XOR (^) to make the buttons toggle their respective "lit" bits on and off:
|
2021-05-24 16:48:15 +01:00
|
|
|
// lit_buttons ^= pico_keypad.get_button_states();
|
2021-01-19 18:40:51 +00:00
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
// Iterate through the lights
|
|
|
|
for(auto i = 0u; i < PicoRGBKeypad::NUM_PADS; i++) {
|
|
|
|
if(lit_buttons & (0b1 << i)) {
|
|
|
|
pico_keypad.illuminate(i, current_color.r, current_color.g, current_color.b);
|
2021-01-19 18:40:51 +00:00
|
|
|
}else{
|
2021-05-24 16:48:15 +01:00
|
|
|
// Kinda dim white-ish
|
2021-01-19 18:40:51 +00:00
|
|
|
pico_keypad.illuminate(i, 0x05, 0x05, 0x05);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:48:15 +01:00
|
|
|
// Display the LED changes
|
2021-01-19 18:40:51 +00:00
|
|
|
pico_keypad.update();
|
|
|
|
|
|
|
|
sleep_ms(100);
|
2021-05-24 16:48:15 +01:00
|
|
|
|
|
|
|
if (lit_buttons == 0xffff) {
|
|
|
|
sleep_ms(500); // Wait a little so the last button can be seen
|
|
|
|
lit_buttons = 0; // Reset lit buttons
|
|
|
|
color_index += 1; // Proceed to the next color
|
|
|
|
if(color_index == num_colors) color_index = 0; // Wrap at last color
|
|
|
|
current_color = colors[color_index]; // Update the current color
|
|
|
|
}
|
2021-01-19 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|