Skip to content

Commit

Permalink
Implement reactive lighting effects (OpenAnnePro#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdarkhan authored and bwisn committed Sep 12, 2021
1 parent fb5829f commit b63980e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions keyboards/annepro2/annepro2.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void matrix_scan_kb() {
*/
bool OVERRIDE process_record_kb(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
if (AP2_LED_ENABLED && AP2_LED_DYNAMIC_PROFILE) {
annepro2LedForwardKeypress(record->event.key.row, record->event.key.col);
}

switch (keycode) {
case KC_AP2_BT1:
annepro2_ble_broadcast(0);
Expand Down
26 changes: 26 additions & 0 deletions keyboards/annepro2/qmk_ap2_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
#include "annepro2.h"
#include "qmk_ap2_led.h"

bool AP2_LED_ENABLED = false;
bool AP2_LED_DYNAMIC_PROFILE = false;

void annepro2LedDisable(void)
{
sdPut(&SD0, CMD_LED_OFF);
AP2_LED_ENABLED = false;
}

void annepro2LedEnable(void)
{
sdPut(&SD0, CMD_LED_ON);
AP2_LED_ENABLED = true;
}

void annepro2LedUpdate(uint8_t row, uint8_t col)
Expand All @@ -31,6 +36,8 @@ void annepro2LedSetProfile(uint8_t prof)
{
sdPut(&SD0, CMD_LED_SET_PROFILE);
sdPut(&SD0, prof);
uint8_t buf = sdGet(&SD0);
AP2_LED_DYNAMIC_PROFILE = buf;
}

uint8_t annepro2LedGetProfile()
Expand All @@ -52,6 +59,8 @@ uint8_t annepro2LedGetNumProfiles()
void annepro2LedNextProfile()
{
sdPut(&SD0, CMD_LED_NEXT_PROFILE);
uint8_t buf = sdGet(&SD0);
AP2_LED_DYNAMIC_PROFILE = buf;
}

void annepro2LedNextIntensity()
Expand Down Expand Up @@ -94,3 +103,20 @@ void annepro2LedResetForegroundColor()
annepro2LedSetProfile(currentProfile);
}

/*
* If enabled, this data is sent to LED MCU on every keypress.
* In order to improve performance, both row and column values
* are packed into a single byte.
* Row range is [0, 4] and requires only 3 bits.
* Column range is [0, 13] and requires 4 bits.
*
* In order to differentiate this command from regular commands,
* the leftmost bit is set to 1 (0b10000000).
* Following it are 3 bits of row and 4 bits of col.
* 1 + 3 + 4 = 8 bits - only a single byte is sent for every keypress.
*/
void annepro2LedForwardKeypress(uint8_t row, uint8_t col)
{
uint8_t msg = 0b10000000 | (row << 4) | col;
sdPut(&SD0, msg);
}
6 changes: 6 additions & 0 deletions keyboards/annepro2/qmk_ap2_led.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define CMD_LED_NEXT_INTENSITY 0xC
#define CMD_LED_NEXT_ANIMATION_SPEED 0xD
#define CMD_LED_SET_FOREGROUND_COLOR 0xE
#define CMD_LED_KEYPRESS 0xF

void annepro2LedDisable(void);
void annepro2LedEnable(void);
Expand All @@ -30,3 +31,8 @@ void annepro2LedNextIntensity(void);
void annepro2LedNextAnimationSpeed(void);
void annepro2LedSetForegroundColor(uint8_t red, uint8_t green, uint8_t blue);
void annepro2LedResetForegroundColor(void);
void annepro2LedForwardKeypress(uint8_t row, uint8_t col);


extern bool AP2_LED_ENABLED;
extern bool AP2_LED_DYNAMIC_PROFILE;

0 comments on commit b63980e

Please sign in to comment.