Skip to content

Commit

Permalink
Merge pull request #54 from zv0n/master
Browse files Browse the repository at this point in the history
Add commands for manual color control
  • Loading branch information
Codetector1374 authored May 23, 2022
2 parents c2c40a4 + fd6f647 commit a087858
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
51 changes: 45 additions & 6 deletions source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "miniFastLED.h"
#include "profiles.h"
#include "protocol.h"
#include "settings.h"
#include <string.h>

/*
Expand Down Expand Up @@ -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],
Expand All @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions source/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions source/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions source/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
3 changes: 3 additions & 0 deletions source/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a087858

Please sign in to comment.