diff --git a/source/commands.c b/source/commands.c index 83ff03a..665039c 100644 --- a/source/commands.c +++ b/source/commands.c @@ -4,6 +4,7 @@ #include "miniFastLED.h" #include "profiles.h" #include "protocol.h" +#include "settings.h" #include /* @@ -88,7 +89,7 @@ static inline void setProfile(uint8_t profile) { */ /* Override one key with a given color */ -static inline void setMaskKey(const message_t *msg) { +static inline void setLedKey(led_t *ledArray, const message_t *msg) { uint8_t row = msg->payload[0]; uint8_t col = msg->payload[1]; led_t color = {.p.blue = msg->payload[2], @@ -97,11 +98,11 @@ static inline void setMaskKey(const message_t *msg) { .p.alpha = msg->payload[5]}; naiveDimLed(&color); if (row < NUM_ROW && col <= NUM_COLUMN) - setKeyColor(&ledMask[ROWCOL2IDX(row, col)], color.rgb); + setKeyColor(&ledArray[ROWCOL2IDX(row, col)], color.rgb); } /* Override all keys with given color */ -static inline void setMaskRow(const message_t *msg) { +static inline void setLedRow(led_t *ledArray, const message_t *msg) { uint8_t row = msg->payload[0]; if (row > NUM_ROW) return; @@ -116,19 +117,19 @@ static inline void setMaskRow(const message_t *msg) { color.p.alpha = *(payloadPtr++); naiveDimLed(&color); - ledMask[ROWCOL2IDX(row, col)] = color; + setKeyColor(&ledArray[ROWCOL2IDX(row, col)], color.rgb); } } /* Override all keys with given color */ -static inline void setMaskMono(const message_t *msg) { +static inline void setLedMono(led_t *ledArray, const message_t *msg) { led_t color = {.p.red = msg->payload[2], .p.green = msg->payload[1], .p.blue = msg->payload[0], .p.alpha = msg->payload[3]}; naiveDimLed(&color); - setAllKeysColor(ledMask, color.rgb); + setAllKeysColor(ledArray, color.rgb); } /* Thread status */ @@ -186,6 +187,30 @@ static inline void blinkKey(const message_t *msg) { "blinker", NORMALPRIO, blinkerFun, NULL); } +static inline void setManual(const message_t *msg) { + manualControl = msg->payload[0]; +} + +static inline void setMaskKey(const message_t *msg) { setLedKey(ledMask, msg); } + +static inline void setMaskRow(const message_t *msg) { setLedRow(ledMask, msg); } + +static inline void setMaskMono(const message_t *msg) { + setLedMono(ledMask, msg); +} + +static inline void setColorKey(const message_t *msg) { + setLedKey(ledColors, msg); +} + +static inline void setColorRow(const message_t *msg) { + setLedRow(ledColors, msg); +} + +static inline void setColorMono(const message_t *msg) { + setLedMono(ledColors, msg); +} + /* * Execute action based on a message. This runs in a separate thread than LED * refreshing algorithm. Keep it simple, fast, mark something in a variable @@ -247,6 +272,20 @@ void commandCallback(const message_t *msg) { blinkKey(msg); break; + /* Handle manual color control */ + case CMD_LED_SET_MANUAL: + setManual(msg); + break; + case CMD_LED_COLOR_SET_KEY: + setColorKey(msg); + break; + case CMD_LED_COLOR_SET_ROW: + setColorRow(msg); + break; + case CMD_LED_COLOR_SET_MONO: + setColorMono(msg); + break; + default: proto.errors++; break; diff --git a/source/matrix.c b/source/matrix.c index 392d7eb..98eeac1 100644 --- a/source/matrix.c +++ b/source/matrix.c @@ -182,7 +182,7 @@ void mainCallback(GPTDriver *_driver) { } /* Update profile if required before starting new cycle */ - if (needToCallbackProfile) { + if (!manualControl && needToCallbackProfile) { needToCallbackProfile = false; profiles[currentProfile].callback(ledColors); } @@ -191,7 +191,7 @@ void mainCallback(GPTDriver *_driver) { * pwmCounterLimit=80 + 80kHz timer this refreshes at 80kHz/80/14 = 71Hz and * should be a sensible maximum speed for a fluent smooth animation. */ - if (animationSkipTicks > 0 && currentColumn == 13) { + if (!manualControl && animationSkipTicks > 0 && currentColumn == 13) { animationTicks++; if (animationTicks >= animationSkipTicks) { animationTicks = 0; diff --git a/source/protocol.h b/source/protocol.h index 0c12c79..b35a743 100644 --- a/source/protocol.h +++ b/source/protocol.h @@ -43,6 +43,12 @@ enum { CMD_LED_KEY_UP = 0x23, /* TODO */ CMD_LED_IAP = 0x24, + /* Manual color control */ + CMD_LED_SET_MANUAL = 0x30, + CMD_LED_COLOR_SET_KEY = 0x31, + CMD_LED_COLOR_SET_ROW = 0x32, + CMD_LED_COLOR_SET_MONO = 0x33, + /* LED -> Main */ /* Payload with data to send over HID */ CMD_LED_DEBUG = 0x40, diff --git a/source/settings.c b/source/settings.c index 14ea8d9..679a1be 100644 --- a/source/settings.c +++ b/source/settings.c @@ -27,6 +27,7 @@ profile profiles[] = { uint8_t currentProfile = 0; const uint8_t amountOfProfiles = sizeof(profiles) / sizeof(profile); volatile uint8_t currentSpeed = 0; +uint8_t manualControl = 0; uint8_t ledIntensity = 0; led_t color_correction = (led_t){.rgb = 0x80FF99}; led_t color_temperature = (led_t){.rgb = 0xFFFFFF}; diff --git a/source/settings.h b/source/settings.h index 4920535..054d4d1 100644 --- a/source/settings.h +++ b/source/settings.h @@ -38,6 +38,9 @@ extern uint8_t currentProfile; extern const uint8_t amountOfProfiles; extern volatile uint8_t currentSpeed; +/* Whether ledColors should be updated in mainCallback in matrix.c */ +extern uint8_t manualControl; + /* 0 - 7: Zero corresponds to the full intensity. */ extern uint8_t ledIntensity;