Skip to content

Commit

Permalink
Add rgb key off/on and brightness level set.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bits committed Mar 5, 2022
1 parent cf89f90 commit f35f9fa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ static const smk_keycode_type smk_default_matrix_keymaps[LAYERS][LAYOUT_ROWS][LA
/*< /==========='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`.====='`==========/`======/`. / >*/
{ _____ , Win , Mac , BClra , XXXXX , XXXXX , XXXXX , XXXXX , p4 , p5 , P6 , plus , enter , Home }, \
/*< /============='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`==.==='`========.=======/`======/`. / >*/
{ _____ , XXXXX , XXXXX , RGBON , RGBOFF, XXXXX , XXXXX , XXXXX , p1 , P2 , P3 , XXXXX , _____ , End }, \
{ _____ , XXXXX , XXXXX , RGBON , RGBOFF, XXXXX , XXXXX , XXXXX , p1 , P2 , P3 , XXXXX , RGBBU , End }, \
/*< /=========.======='`.====='`==.==='`======'`======'`======'`======'`======'`====.='`====.='`====.='`====.=======/`======/`======/`. / >*/
{ _____ , _____ , _____ , _____ , p0 , MO(2) , dot , RGBMD, _____ , RGBMU } \
{ _____ , _____ , _____ , _____ , p0 , MO(2) , dot , RGBMD, RGBBD , RGBMU } \
/*< `---------'`--------'`--------'`------------------------------------------------'`------'`------'`------'`------'`------'`------'`. / >*/
/*< V V V V Layer 2 V V V V V V V / >*/
/*< `----------'`--------'`--------'`------------------------------------------------'`------'`------'`------'`------'`------'`------' >*/
Expand Down
4 changes: 4 additions & 0 deletions firmware/keyboard/sipeed_keyboard_68/include/smk_spirgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ typedef struct RGB_COLOR_DESC {
} RGB_COLOR_DESC;
extern RGB_COLOR_DESC rgb_color_descriptor[10];

void RGB_Set_Global_Enable(uint8_t enable);
void RGB_Set_Brightness_level_up();
void RGB_Set_Brightness_level_down();
void RGB_Set_Brightness_level(uint8_t level);
DRGB rgb_alpha(DRGB source, uint8_t alpha);
void rgb_loop_task(void *pvParameters);

Expand Down
9 changes: 5 additions & 4 deletions firmware/keyboard/sipeed_keyboard_68/keyboard/smk_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "smk_endpoints.h"
#include "events/rgb_switch_mode_event.h"
#include "smk_ble.h"
#include "smk_spirgb.h"

smk_keyboard_map_type * smk_keymap_init(const smk_keyboard_hardware_type * const hardware, QueueHandle_t queue_in, QueueHandle_t queue_out)
{
Expand Down Expand Up @@ -259,16 +260,16 @@ void smk_keymap_handle_keycode(smk_keyboard_map_type *map, const smk_event_type
MAP_DEBUG("[SMK][KeyMap] KeyFunc RGB mode down\r\n");
break;
case KF_RGB_OFF:
// TODO: Implement RGB off
RGB_Set_Global_Enable(0);
break;
case KF_RGB_ON:
// TODO: Implement RGB on
RGB_Set_Global_Enable(1);
break;
case KF_RGB_BRIGHTNESS_UP:
// TODO: Implement RGB brightness up
RGB_Set_Brightness_level_up();
break;
case KF_RGB_BRIGHTNESS_DOWN:
// TODO: Implement RGB brightness down
RGB_Set_Brightness_level_down();
break;
case KF_USB:
endpoint = SMK_ENDPOINT_USB;
Expand Down
54 changes: 48 additions & 6 deletions firmware/keyboard/sipeed_keyboard_68/smk_spirgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include <math.h>

#include "smk_hid_protocol.h"
#include "easyflash.h"


extern const int CAPS_KEY_LED;
extern const float LED_GAMMA;
uint8_t shared_kb_led=0;
uint8_t rgb_enable=1;

static uint8_t gammalist[256];

Expand All @@ -27,6 +29,14 @@ static void init_gammalist() {
}
}

#define ENV_RGB_GLOBAL_ENABLE "RGB_GLOBAL_ENABLE"
#define ENV_RGB_GLOBAL_BRIGHTNESS_LEVEL "RGB_BR_LEVEL"

void RGB_Set_Global_Enable(uint8_t enable) {
rgb_enable = enable;
ef_set_env_blob(ENV_RGB_GLOBAL_ENABLE, &enable, sizeof(enable));
}

#if(SMK_RGB_USE_DMA)
uint32_t RGB_DMA_Buffer[RGB_LENGTH*3];

Expand Down Expand Up @@ -93,6 +103,24 @@ void RGB_Transmit(struct device *spi, DRGB * rgbbuffer) {

/* static */ int rgb_mode = 0;
/* static */ uint8_t rgb_global_brightness = 120;
uint8_t rgb_global_brightness_level = 2;
uint8_t rgb_brightness_level_list[5] = {60, 90, 120, 150, 180};

void RGB_Set_Brightness_level(uint8_t level) {
rgb_global_brightness_level = level;
ef_set_env_blob(ENV_RGB_GLOBAL_BRIGHTNESS_LEVEL, &level, sizeof(level));
rgb_global_brightness = rgb_brightness_level_list[level];
}

void RGB_Set_Brightness_level_up() {
rgb_global_brightness_level = (rgb_global_brightness_level+1) > 4 ? 4 : (rgb_global_brightness_level+1);
RGB_Set_Brightness_level(rgb_global_brightness_level);
}

void RGB_Set_Brightness_level_down() {
rgb_global_brightness_level = (rgb_global_brightness_level-1) < 0 ? 0 : (rgb_global_brightness_level-1);
RGB_Set_Brightness_level(rgb_global_brightness_level);
}

DRGB RGB_Buffer[RGB_LENGTH];

Expand Down Expand Up @@ -155,6 +183,9 @@ void rgb_loop_task(void *pvParameters)
uint32_t i;

init_gammalist();
ef_get_env_blob(ENV_RGB_GLOBAL_ENABLE, &rgb_enable, sizeof(rgb_enable), NULL);
ef_get_env_blob(ENV_RGB_GLOBAL_BRIGHTNESS_LEVEL, &rgb_global_brightness_level, sizeof(rgb_global_brightness_level), NULL);
RGB_Set_Brightness_level(rgb_global_brightness_level);

spi_register(SPI0_INDEX, "spi");
spi = device_find("spi");
Expand All @@ -176,6 +207,19 @@ void rgb_loop_task(void *pvParameters)

for (;;) {
vTaskDelay(10);
if (!rgb_enable) {
for (i = 0; i < RGB_LENGTH; i++) {
RGB_Buffer[i].word = 0;
}
#if(SMK_RGB_USE_DMA)
RGB_DMA_Transmit(spi, dma_ch3, RGB_Buffer);
#else
vTaskEnterCritical();
RGB_Transmit(spi, RGB_Buffer);
vTaskExitCritical();
#endif
continue;
}

if (rgb_mode < RGB_MODE_COUNT) {

Expand All @@ -187,12 +231,10 @@ void rgb_loop_task(void *pvParameters)
int ceffid = ceff->eff_id;
int ceffoffset = ceff->time_offset;
rgb_effect_descriptor[ceffid].eff_func(ceff, timestamp - ceffoffset);

if (rgb_global_brightness != 255) {
for (i = 0; i < RGB_LENGTH; i++) {
RGB_Buffer[i] = rgb_alpha(RGB_Buffer[i], rgb_global_brightness);
}
}

for (i = 0; i < RGB_LENGTH; i++) {
RGB_Buffer[i] = rgb_alpha(RGB_Buffer[i], rgb_global_brightness);
}
}

timestamp ++;
Expand Down

0 comments on commit f35f9fa

Please sign in to comment.