Merge commit '0ae7e1b'

* commit '0ae7e1b':
  First pass at bundling the LED code into its own class
  First pass of making led_control into a class
  remove function definitions that moved elsewhere
  data type modernization
  Trivial first library wrapper for the storage functions
pull/18/head
Jesse Vincent 9 years ago
commit 3d7fa7613b

@ -30,6 +30,7 @@ void setup();
#include "debouncing.h" #include "debouncing.h"
#include "KeyboardioSX1509.h" #include "KeyboardioSX1509.h"
#include "mouse_movement.h" #include "mouse_movement.h"
#include "storage.h"
//extern uint8_t usbMaxPower; //extern uint8_t usbMaxPower;
char x; char x;
@ -38,15 +39,8 @@ char y;
uint8_t matrixState[ROWS][COLS] = {0}; uint8_t matrixState[ROWS][COLS] = {0};
static const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST }; static const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST };
byte primary_keymap = 0; uint8_t primary_keymap = 0;
byte temporary_keymap = 0; uint8_t temporary_keymap = 0;
// EEPROM related
void save_primary_keymap(byte keymap);
byte load_primary_keymap();

@ -6,6 +6,10 @@
#include "KeyboardioFirmware.h" #include "KeyboardioFirmware.h"
#include "HID-Project.h" #include "HID-Project.h"
KeyboardStorage Storage;
LEDControl BlinkyLights;
void set_keymap(Key keymapEntry, byte matrixStateEntry) { void set_keymap(Key keymapEntry, byte matrixStateEntry) {
if (keymapEntry.flags & SWITCH_TO_KEYMAP) { if (keymapEntry.flags & SWITCH_TO_KEYMAP) {
// this logic sucks. there is a better way TODO this // this logic sucks. there is a better way TODO this
@ -27,7 +31,7 @@ void set_keymap(Key keymapEntry, byte matrixStateEntry) {
// switch keymap and stay there // switch keymap and stay there
if (key_toggled_on(matrixStateEntry)) { if (key_toggled_on(matrixStateEntry)) {
temporary_keymap = primary_keymap = keymapEntry.rawKey; temporary_keymap = primary_keymap = keymapEntry.rawKey;
save_primary_keymap(primary_keymap); Storage.save_primary_keymap(primary_keymap);
} }
} }
} }
@ -68,13 +72,14 @@ void setup() {
wdt_disable(); wdt_disable();
Serial.begin(115200); Serial.begin(115200);
//usbMaxPower = 100; //usbMaxPower = 100;
Keyboard.begin(); Keyboard.begin();
Mouse.begin(); Mouse.begin();
implementation_setup_leds(); implementation_setup_leds();
led_bootup(); BlinkyLights.led_bootup();
implementation_pins_setup(); implementation_pins_setup();
temporary_keymap = primary_keymap = load_primary_keymap(); temporary_keymap = primary_keymap = Storage.load_primary_keymap();
} }
@ -83,7 +88,7 @@ void loop() {
TS("about to scan the matrix") TS("about to scan the matrix")
scan_matrix(); scan_matrix();
TS("updating LEDs"); TS("updating LEDs");
update_leds(temporary_keymap == NUMPAD_KEYMAP); BlinkyLights.update_leds(temporary_keymap == NUMPAD_KEYMAP);
} }
@ -103,11 +108,10 @@ void handle_synthetic_key_press(byte switchState, Key mappedKey) {
if (key_toggled_on (switchState)) { if (key_toggled_on (switchState)) {
ConsumerControl.press(mappedKey.rawKey); ConsumerControl.press(mappedKey.rawKey);
} }
} } else if (mappedKey.flags & IS_INTERNAL) {
else if (mappedKey.flags & IS_INTERNAL) {
if (key_toggled_on (switchState)) { if (key_toggled_on (switchState)) {
if (mappedKey.rawKey == LED_TOGGLE) { if (mappedKey.rawKey == LED_TOGGLE) {
next_led_mode(); BlinkyLights.next_led_mode();
} }
} }
} else if (mappedKey.flags & IS_SYSCTL) { } else if (mappedKey.flags & IS_SYSCTL) {

@ -1,49 +1,26 @@
#include "led_control.h" #include "led_control.h"
static uint8_t led_mode; uint8_t LEDControl::pos= 0;
static uint8_t last_led_mode; uint8_t LEDControl::rainbow_hue=0;
static uint8_t stored_led_mode; uint8_t LEDControl::rainbow_steps = 1;
static uint8_t pos = 0; uint8_t LEDControl::rainbow_wave_steps=1;
long LEDControl::rainbow_current_ticks =0;
static cRGB led_off = { .r = 0, .g = 0, .b = 0 }; uint8_t LEDControl::breathe_brightness=0;
static cRGB led_steady = { .r = 0, .g = 255, .b = 0}; uint8_t LEDControl::breathe_fadeAmount=1;
static cRGB led_blue = { .r = 0, .g = 0, .b = 255 }; uint8_t LEDControl::chase_pixels= 1;
static cRGB led_dark_blue = { .r = 0, .g = 0, .b = 127 }; uint8_t LEDControl::current_chase_counter = 0;
static cRGB led_bright_red = { .r = 255, .g = 0, .b = 0};
static cRGB led_breathe; void LEDControl::set_key_color(byte row, byte col, cRGB color) {
static cRGB rainbow;
static uint8_t rainbow_hue = 0; //stores 0 to 614
static uint8_t rainbow_steps = 1; //number of hues we skip in a 360 range per update
static uint8_t rainbow_wave_steps =1; //number of hues we skip in a 360 range per update
static byte rainbow_saturation = 255;
static byte rainbow_value = 190;
static long rainbow_wave_ticks = 1; //delays between update
static long rainbow_ticks = 5; //delays between update
static long rainbow_current_ticks =0;
static uint8_t breathe_brightness = 0; // how bright the LED is
static uint8_t breathe_fadeAmount = 1; // how many pouint8_ts to fade the LED by
static uint8_t chase_pixels = 1;
static uint8_t chase_threshold = 6;
static uint8_t current_chase_counter = 0;
// End RGB stuff
void set_key_color(byte row, byte col, cRGB color) {
implementation_led_set_crgb_at(row, col, color); implementation_led_set_crgb_at(row, col, color);
} }
cRGB get_key_color(byte row, byte col) { cRGB LEDControl::get_key_color(byte row, byte col) {
return implementation_get_key_color(row, col); return implementation_get_key_color(row, col);
} }
void initialize_led_mode(uint8_t mode) { void LEDControl::initialize_led_mode(uint8_t mode) {
set_all_leds_to(led_off); set_all_leds_to(led_off);
if (mode == LED_MODE_OFF) { if (mode == LED_MODE_OFF) {
// set_all_leds_to(led_off); // set_all_leds_to(led_off);
@ -58,27 +35,27 @@ void initialize_led_mode(uint8_t mode) {
} }
} }
void set_all_leds_to(cRGB color) { void LEDControl::set_all_leds_to(cRGB color) {
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
implementation_led_set_crgb_at(i, color); implementation_led_set_crgb_at(i, color);
} }
} }
void next_led_mode() { void LEDControl::next_led_mode() {
if (led_mode++ >= LED_MODES) { if (led_mode++ >= LED_MODES) {
led_mode = 0; led_mode = 0;
} }
} }
void set_led_mode(uint8_t mode) { void LEDControl::set_led_mode(uint8_t mode) {
led_mode = mode; led_mode = mode;
} }
void update_leds(uint8_t numlock_enabled) { void LEDControl::update_leds(uint8_t numlock_enabled) {
if (numlock_enabled) { if (numlock_enabled) {
if (led_mode != LED_SPECIAL_MODE_NUMLOCK) { if (led_mode != LED_SPECIAL_MODE_NUMLOCK) {
stored_led_mode = led_mode; stored_led_mode = led_mode;
@ -117,7 +94,7 @@ void update_leds(uint8_t numlock_enabled) {
void led_effect_numlock_update() { void LEDControl::led_effect_numlock_update() {
for (uint8_t i = 0; i < 44; i++) { for (uint8_t i = 0; i < 44; i++) {
implementation_led_set_crgb_at(i, led_off); implementation_led_set_crgb_at(i, led_off);
} }
@ -129,11 +106,11 @@ void led_effect_numlock_update() {
implementation_led_sync(); implementation_led_sync();
} }
void led_effect_steady_update() { void LEDControl::led_effect_steady_update() {
implementation_led_sync(); implementation_led_sync();
} }
void led_compute_breath() { void LEDControl::led_compute_breath() {
// algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ // algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0; breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
// change the brightness for next time through the loop: // change the brightness for next time through the loop:
@ -148,13 +125,13 @@ void led_compute_breath() {
SetHSV(led_breathe,200, 255, breathe_brightness); SetHSV(led_breathe,200, 255, breathe_brightness);
} }
void led_effect_breathe_update() { void LEDControl::led_effect_breathe_update() {
led_compute_breath(); led_compute_breath();
set_all_leds_to(led_breathe); set_all_leds_to(led_breathe);
implementation_led_sync(); implementation_led_sync();
} }
void led_effect_chase_update() { void LEDControl::led_effect_chase_update() {
if (current_chase_counter++ < chase_threshold) { if (current_chase_counter++ < chase_threshold) {
return; return;
} }
@ -171,7 +148,7 @@ void led_effect_chase_update() {
implementation_led_sync(); implementation_led_sync();
} }
void led_effect_rainbow_update() { void LEDControl::led_effect_rainbow_update() {
if (rainbow_current_ticks++ < rainbow_ticks) { if (rainbow_current_ticks++ < rainbow_ticks) {
return; return;
} else { } else {
@ -186,7 +163,7 @@ void led_effect_rainbow_update() {
implementation_led_sync(); implementation_led_sync();
} }
void led_effect_rainbow_wave_update() { void LEDControl::led_effect_rainbow_wave_update() {
if (rainbow_current_ticks++ < rainbow_wave_ticks) { if (rainbow_current_ticks++ < rainbow_wave_ticks) {
return; return;
} else { } else {
@ -208,7 +185,7 @@ void led_effect_rainbow_wave_update() {
implementation_led_sync(); implementation_led_sync();
} }
void led_bootup() { void LEDControl::led_bootup() {
set_all_leds_to(led_off); set_all_leds_to(led_off);
led_type_letter(LED_K); led_type_letter(LED_K);
@ -230,7 +207,7 @@ void led_bootup() {
} }
void led_type_letter(uint8_t letter) { void LEDControl::led_type_letter(uint8_t letter) {
implementation_led_set_crgb_at(letter,led_bright_red); implementation_led_set_crgb_at(letter,led_bright_red);
implementation_led_sync(); implementation_led_sync();
delay(250); delay(250);
@ -240,11 +217,12 @@ void led_type_letter(uint8_t letter) {
} }
/* SetHSV from light_ws2812. Their source was:
void SetHSV(cRGB crgb, int hue, byte sat, byte val) { * getRGB() function based on <http://www.codeproject.com/miscctrl/CPicker.asp>
* dim_curve idea by Jims
* */
void LEDControl::SetHSV(cRGB crgb, int hue, byte sat, byte val) {
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB /* convert hue, saturation and brightness ( HSB/HSV ) to RGB
The dim_curve is used only on brightness/value and on saturation (inverted).
This looks the most natural.
*/ */
int base; int base;

@ -15,21 +15,56 @@
#define LED_SPECIAL_MODE_NUMLOCK 100 #define LED_SPECIAL_MODE_NUMLOCK 100
void update_leds(uint8_t numlock_enabled);
void set_all_leds_to(cRGB color);
void set_led_mode(uint8_t mode); class LEDControl {
public:
void next_led_mode(); void next_led_mode();
void led_bootup();
void update_leds(uint8_t numlock_enabled);
void led_type_letter(uint8_t letter);
void set_led_mode(uint8_t mode);
private:
uint8_t led_mode;
uint8_t last_led_mode;
uint8_t stored_led_mode;
static uint8_t pos;
cRGB led_off = { .r = 0, .g = 0, .b = 0 };
cRGB led_steady = { .r = 0, .g = 255, .b = 0};
cRGB led_blue = { .r = 0, .g = 0, .b = 255 };
cRGB led_dark_blue = { .r = 0, .g = 0, .b = 127 };
cRGB led_bright_red = { .r = 255, .g = 0, .b = 0};
cRGB led_breathe;
cRGB rainbow;
static uint8_t rainbow_hue; //stores 0 to 614
static uint8_t rainbow_steps; //number of hues we skip in a 360 range per update
static uint8_t rainbow_wave_steps; //number of hues we skip in a 360 range per update
static const byte rainbow_saturation = 255;
static const byte rainbow_value = 190;
static const long rainbow_wave_ticks = 1; //delays between update
static const long rainbow_ticks = 5; //delays between update
static long rainbow_current_ticks;
static uint8_t breathe_brightness; // how bright the LED is
static uint8_t breathe_fadeAmount; // how many points to fade the LED by
static uint8_t chase_pixels;
static uint8_t current_chase_counter;
static const uint8_t chase_threshold = 6;
// End RGB stuff
void set_key_color(uint8_t row, uint8_t col, cRGB color); void set_key_color(uint8_t row, uint8_t col, cRGB color);
cRGB get_key_color(uint8_t row, uint8_t col); cRGB get_key_color(uint8_t row, uint8_t col);
void led_compute_breath(); void led_compute_breath();
void led_effect_breathe_init(); void led_effect_breathe_init();
void led_effect_rainbow_init(); void led_effect_rainbow_init();
void led_effect_chase_init(); void led_effect_chase_init();
void led_effect_steady_init(); void led_effect_steady_init();
void led_Effect_heatmap_init(); void led_effect_heatmap_init();
void led_effect_breathe_update(); void led_effect_breathe_update();
void led_effect_rainbow_update(); void led_effect_rainbow_update();
@ -38,7 +73,9 @@ void led_effect_chase_update();
void led_effect_steady_update(); void led_effect_steady_update();
void led_effect_heatmap_update(); void led_effect_heatmap_update();
void led_effect_numlock_update(); void led_effect_numlock_update();
void led_bootup(); void set_all_leds_to(cRGB color);
void led_type_letter(uint8_t letter);
void SetHSV(cRGB crgb, int hue, byte sat, byte val); void SetHSV(cRGB crgb, int hue, byte sat, byte val);
void initialize_led_mode(uint8_t mode);
};

@ -1,11 +1,13 @@
#include "storage.h" #include "storage.h"
void save_primary_keymap(byte keymap) {
void KeyboardStorage::save_primary_keymap(uint8_t keymap) {
EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap); EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap);
} }
byte load_primary_keymap() { uint8_t KeyboardStorage::load_primary_keymap() {
byte keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION); uint8_t keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION);
if (keymap >= KEYMAPS ) { if (keymap >= KEYMAPS ) {
return 0; // undefined positions get saved as 255 return 0; // undefined positions get saved as 255
} }

@ -2,5 +2,8 @@
#include <EEPROM.h> #include <EEPROM.h>
#include "KeyboardConfig.h" #include "KeyboardConfig.h"
void save_primary_keymap(byte keymap); class KeyboardStorage {
byte load_primary_keymap(); public:
uint8_t load_primary_keymap();
void save_primary_keymap(uint8_t keymap);
};

Loading…
Cancel
Save