Browse Source

Relate brightness of RGB LED to backlight

old_master
Kai Ryu 9 years ago
parent
commit
420b175269
3 changed files with 139 additions and 33 deletions
  1. 19
    1
      keyboard/staryu/backlight.c
  2. 114
    29
      keyboard/staryu/rgb.c
  3. 6
    3
      keyboard/staryu/rgb.h

+ 19
- 1
keyboard/staryu/backlight.c View File

#include "backlight.h" #include "backlight.h"
#include "softpwm_led.h" #include "softpwm_led.h"
#include "action.h" #include "action.h"
#include "rgb.h"


#ifdef BACKLIGHT_ENABLE #ifdef BACKLIGHT_ENABLE


}; };


extern backlight_config_t backlight_config; extern backlight_config_t backlight_config;
uint8_t backlight_brightness;


void backlight_set(uint8_t level) void backlight_set(uint8_t level)
{ {
case 3: case 3:
fading_led_disable_all(); fading_led_disable_all();
breathing_led_disable_all(); breathing_led_disable_all();
softpwm_led_set_all(pgm_read_byte(&backlight_table[level]));
backlight_brightness = pgm_read_byte(&backlight_table[level]);
softpwm_led_set_all(backlight_brightness);
rgb_set_brightness(backlight_brightness);
break; break;
case 4: case 4:
case 5: case 5:
default: default:
fading_led_disable_all(); fading_led_disable_all();
breathing_led_disable_all(); breathing_led_disable_all();
backlight_brightness = 0;
softpwm_led_set_all(backlight_brightness);
break; break;
} }
} }
} }
} }


#ifdef CUSTOM_LED_ENABLE
void fading_led_custom(uint8_t *value)
{
rgb_set_brightness(*value);
}

void breathing_led_custom(uint8_t *value)
{
rgb_set_brightness(*value);
}
#endif

#endif #endif

+ 114
- 29
keyboard/staryu/rgb.c View File



#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include "softpwm_led.h"
#include "backlight.h"
#include "rgb.h" #include "rgb.h"
#include "light_ws2812.h" #include "light_ws2812.h"


static const uint8_t rgb_table[RGB_LEVELS][3] PROGMEM = {
{ 0, 0, 0 },
{ 255, 0, 0 },
{ 255, 127, 0 },
{ 255, 255, 0 },
{ 0, 255, 0 },
{ 0, 255, 255 },
{ 0, 0, 255 },
{ 143, 0, 255 },
{ 255, 255, 255 }
};

volatile static uint8_t rgb_fading_enable = 0;
static rgb_config_t rgb_config; static rgb_config_t rgb_config;
static uint16_t rgb_hue = 0;
static uint8_t rgb_saturation = 255;
static uint8_t rgb_brightness = 16;

extern backlight_config_t backlight_config;
extern uint8_t backlight_brightness;


void rgb_write_config(void);
void rgb_read_config(void);
void rgb_set(uint8_t level);
static void rgb_write_config(void);
static void rgb_read_config(void);
static void rgb_set_level(uint8_t level);
static void hue_to_rgb(uint16_t hue, struct cRGB *rgb);
static void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb);


void rgb_init(void) void rgb_init(void)
{ {
rgb_write_config(); rgb_write_config();
} }
if (rgb_config.enable) { if (rgb_config.enable) {
rgb_set(rgb_config.level);
rgb_set_level(rgb_config.level);
} }
} }


void rgb_on(void) void rgb_on(void)
{ {
rgb_config.enable = 1; rgb_config.enable = 1;
rgb_set(rgb_config.level);
rgb_set_level(rgb_config.level);
rgb_write_config(); rgb_write_config();
} }


void rgb_off(void) void rgb_off(void)
{ {
rgb_config.enable = 0; rgb_config.enable = 0;
rgb_set(RGB_OFF);
rgb_set_level(RGB_OFF);
rgb_write_config(); rgb_write_config();
} }


rgb_config.enable = (rgb_config.level != 0); rgb_config.enable = (rgb_config.level != 0);
rgb_write_config(); rgb_write_config();
} }
rgb_set(rgb_config.level);
rgb_set_level(rgb_config.level);
} }


void rgb_increase(void) void rgb_increase(void)
{ {
if(rgb_config.level < RGB_LEVELS - 1) {
if(rgb_config.level < RGB_LEVELS) {
rgb_config.level++; rgb_config.level++;
rgb_config.enable = 1; rgb_config.enable = 1;
rgb_write_config(); rgb_write_config();
} }
rgb_set(rgb_config.level);
rgb_set_level(rgb_config.level);
} }


void rgb_step(void) void rgb_step(void)
{ {
rgb_config.level++; rgb_config.level++;
if(rgb_config.level >= RGB_LEVELS)
if(rgb_config.level > RGB_LEVELS)
{ {
rgb_config.level = 0; rgb_config.level = 0;
} }
rgb_config.enable = (rgb_config.level != 0); rgb_config.enable = (rgb_config.level != 0);
rgb_set(rgb_config.level);
rgb_set_level(rgb_config.level);
}

void rgb_set_level(uint8_t level)
{
if (level <= RGB_WHITE) {
rgb_fading_enable = 0;
if (level == RGB_OFF) {
rgb_brightness = 0;
}
else {
if (level == RGB_WHITE) {
rgb_saturation = 0;
}
else {
rgb_hue = (level - 1) * 128;
rgb_saturation = 255;
}
if (backlight_config.enable) {
if (backlight_config.level >= 1 && backlight_config.level <= 3) {
rgb_brightness = backlight_brightness;
}
}
else {
rgb_brightness = 16;
}
}
rgb_refresh();
}
else {
rgb_saturation = 255;
rgb_fading_enable = 3 - (level - RGB_FADE_SLOW);
}
}

void rgb_set_brightness(uint8_t brightness)
{
rgb_brightness = brightness;
rgb_refresh();
} }


void rgb_set(uint8_t level)
void rgb_refresh(void)
{ {
struct cRGB rgb[1];
rgb[0].r = pgm_read_byte(&rgb_table[level][0]);
rgb[0].g = pgm_read_byte(&rgb_table[level][1]);
rgb[0].b = pgm_read_byte(&rgb_table[level][2]);
ws2812_setleds(rgb, 1);
struct cRGB rgb_color[1];
hsb_to_rgb(rgb_hue, rgb_saturation, rgb_brightness, rgb_color);
ws2812_setleds(rgb_color, 1);
}

void hue_to_rgb(uint16_t hue, struct cRGB *rgb)
{
uint8_t hi = hue / 60;
uint16_t f = (hue % 60) * 425 / 100;
uint8_t q = 255 - f;
switch (hi) {
case 0: rgb->r = 255; rgb->g = f; rgb->b = 0; break;
case 1: rgb->r = q; rgb->g = 255; rgb->b = 0; break;
case 2: rgb->r = 0; rgb->g = 255; rgb->b = f; break;
case 3: rgb->r = 0; rgb->g = q; rgb->b = 255; break;
case 4: rgb->r = f; rgb->g = 0; rgb->b = 255; break;
case 5: rgb->r = 255; rgb->g = 0; rgb->b = q; break;
}
}

/*
* original code: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/
*/
void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb)
{
uint8_t temp[5];
uint8_t n = (hue >> 8) % 3;
uint8_t x = ((((hue & 255) * saturation) >> 8) * brightness) >> 8;
uint8_t s = ((256 - saturation) * brightness) >> 8;
temp[0] = temp[3] = s;
temp[1] = temp[4] = x + s;
temp[2] = brightness - x;
rgb->r = temp[n + 2];
rgb->g = temp[n + 1];
rgb->b = temp[n];
}

#ifdef CUSTOM_LED_ENABLE
void softpwm_led_custom(void)
{
static uint8_t step = 0;
static uint16_t hue = 0;
if (rgb_fading_enable) {
if (++step > rgb_fading_enable) {
step = 0;
rgb_hue = hue;
rgb_refresh();
if (++hue >= 768) {
hue = 0;
}
}
}
} }
#endif

+ 6
- 3
keyboard/staryu/rgb.h View File

enum { enum {
RGB_OFF = 0, RGB_OFF = 0,
RGB_RED, RGB_RED,
RGB_ORANGE,
RGB_YELLOW, RGB_YELLOW,
RGB_GREEN, RGB_GREEN,
RGB_CYAN, RGB_CYAN,
RGB_BLUE, RGB_BLUE,
RGB_VIOLET,
RGB_MAGENTA,
RGB_WHITE, RGB_WHITE,
RGB_LEVELS
RGB_FADE_SLOW,
RGB_FADE_MID,
RGB_FADE_FAST,
RGB_LEVELS = RGB_FADE_FAST
}; };


#define EECONFIG_RGB (uint8_t *)7 #define EECONFIG_RGB (uint8_t *)7
void rgb_decrease(void); void rgb_decrease(void);
void rgb_increase(void); void rgb_increase(void);
void rgb_step(void); void rgb_step(void);
void rgb_set_brightness(uint8_t brightness);


#endif #endif