From ace84d07737a8b030103a5f489af73f516f81ef0 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Thu, 27 Apr 2023 20:43:24 -0400 Subject: [PATCH] [+] Keyboard lights --- firmware/src/config.h | 2 ++ firmware/src/keyboard_lights.cpp | 46 ++++++++++++++++++++++++++++++++ firmware/src/main.cpp | 1 + 3 files changed, 49 insertions(+) create mode 100644 firmware/src/keyboard_lights.cpp diff --git a/firmware/src/config.h b/firmware/src/config.h index 8b86de1..79b7f92 100644 --- a/firmware/src/config.h +++ b/firmware/src/config.h @@ -24,6 +24,8 @@ const int MUX_SEL_OUT[NUM_MUX_SEL] = {14, 13, 12, 11}; const int LK_PIN = 2; const int LK_LIGHTS_PER_METER = 60; const float LK_NUM_METERS = 2; +const int LK_KEY_SPACING_MM = 7; +const int LK_KEY_LEN_MM = 15; const int LK_NUM_LIGHTS = (int) round(LK_LIGHTS_PER_METER * LK_NUM_METERS); // ======================================== diff --git a/firmware/src/keyboard_lights.cpp b/firmware/src/keyboard_lights.cpp new file mode 100644 index 0000000..d00b355 --- /dev/null +++ b/firmware/src/keyboard_lights.cpp @@ -0,0 +1,46 @@ +// +// Created by Hykilpikonna on 4/21/23. +// + +#include "config.h" +#include "Adafruit_NeoPixel.h" + +class KeyboardLights +{ +private: + Adafruit_NeoPixel led_key; + +public: + KeyboardLights() : led_key(4, LK_PIN, NEO_GRB + NEO_KHZ800) {} + + void begin() + { + led_key.begin(); + } + + /** + * Convert a key index to a light index + * + * The light strip's spacing doesn't match the keyboard's spacing, so we need to convert. + * Light strip info are defined by LK_LIGHTS_PER_METER and LK_NUM_LIGHTS. + * Key spacing info are defined by LK_KEY_SPACING_MM and LK_KEY_LEN_MM. + * + * @param key Key index from 0 to 60 + * @return Light index + */ + static int keyToLight(int key) + { + return (int) (key * LK_KEY_SPACING_MM / LK_KEY_LEN_MM * LK_LIGHTS_PER_METER); + } + + void hit(int key) + { + // 1. Calculate the starting index + int start = keyToLight(key); + + Serial.printf("Key %d -> Light %d\n", key, start); + + // 2. Set the color of the lights + led_key.setPixelColor(start, Adafruit_NeoPixel::ColorHSV(0, 255, 255)); + } +}; \ No newline at end of file diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index d4bad64..4435e65 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -4,6 +4,7 @@ #include "main.h" #include "utils.h" #include "panel.cpp" +#include "keyboard_lights.cpp" u32 lasts[NUM_NOTES]; // variable to store the value coming from the sensor u64 last_hit_times[NUM_NOTES];