Skip to content

Commit

Permalink
Adding fade profile brightness control
Browse files Browse the repository at this point in the history
- Adding fade_control capability
  fade_control(<profile>, <command>, <arg>)
  profile: 0..3
  Commands:
  0: Reset fade profile (all settings)
  1: Reset all fade profiles (all settings)
  2: Set fade profile brightness
  3: Increment fade profile brightness
  4: Decrement fade profile brightness
  5: Set fade profile brightness to default
- Requires KLL v0.5.7.8
- Default brightness can be set using KLL_LED_FadeBrightness array in
KLL
  • Loading branch information
haata committed Feb 22, 2019
1 parent f3af41c commit ae7bf0b
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Keyboards/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
kll = "==0.5.7.6"
kll = "==0.5.7.8"

[dev-packages]

Expand Down
2 changes: 1 addition & 1 deletion Lib/CMake/kll.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ endif ()
message ( STATUS "Checking for kll" )

### XXX XXX XXX - Remember to update Pipfile as well when you change the version! ###
set ( KLL_MIN_VERSION "0.5.7.6" )
set ( KLL_MIN_VERSION "0.5.7.8" )

# 1) Check for environment variable
if ( NOT DEFINED KLL_EXECUTABLE )
Expand Down
26 changes: 24 additions & 2 deletions Macro/PixelMap/capabilities.kll
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# PixelMap
Name = PixelMapCapabilities;
Version = 0.6;
Version = 0.7;
Author = "HaaTa (Jacob Alexander) 2015-2019";
KLL = 0.5;

# Modified Date
Date = 2019-02-01;
Date = 2019-02-18;

## Capabilities ##
# XXX (HaaTa): Don't use animation capability, it is deprecated
Expand Down Expand Up @@ -86,6 +86,28 @@ animation_control => Pixel_AnimationControl_capability( func : 1 );
fade_set => Pixel_FadeSet_capability( profile : 1, config : 1, period : 1 );
fade_layer_highlight => Pixel_FadeLayerHighlight_capability( layer : 2 );

# Fade Control
# Fade brightness is applied before the fade computation is applied
# Profile: 0..3
# Command:
# 0) Resets profile to defaults (arg ignored)
# 1) Resets all fade profiles to defaults (profile ignored, arg ignored)
# 2) Sets profile brightness
# 3) Increment profile brightness
# 4) Decrement profile brightness
# 5) Set profile brightness to default
# Resets all fade profiles to default settings
fade_control => Pixel_FadeControl_capability( profile : 1, command : 1, arg : 1 );

# Fade Brightness (per profile)
# From 0 to 255, this is an adjustment on the default brightness using the color PWM
# One brightness setting per fade group
KLL_LED_FadeBrightness => KLL_LED_FadeBrightness_define;
KLL_LED_FadeBrightness[0] = 255;
KLL_LED_FadeBrightness[1] = 255;
KLL_LED_FadeBrightness[2] = 255;
KLL_LED_FadeBrightness[3] = 255;

# Fade Period
# .start and .end must be between 0 and 15
# .start == (1 << start) - 1
Expand Down
152 changes: 148 additions & 4 deletions Macro/PixelMap/pixel.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ typedef enum PixelTest {
PixelTest_XY_Roll = 33,
} PixelTest;

typedef enum PixelFadeControl {
PixelFadeControl_Reset = 0, // Resets fade profile to defaults (arg ignored)
PixelFadeControl_Reset_All = 1, // Resets all fade profiles to defaults (profile, arg ignored)
PixelFadeControl_Brightness_Set = 2, // Sets fade profile to a given brightness
PixelFadeControl_Brightness_Increment = 3, // Increment brightness by given amount
PixelFadeControl_Brightness_Decrement = 4, // Decrement brightness by given amount
PixelFadeControl_Brightness_Default = 5, // Set profile brightness to default
PixelFadeControl_LAST,
} PixelFadeControl;



// ----- Variables -----
Expand All @@ -94,6 +104,7 @@ typedef struct {
typedef struct {
PixelConfigElem animations[Pixel_AnimationStackSize];
PixelPeriodConfig fade_periods[4][4];
uint8_t fade_brightness[4];
} PixelConfig;

static PixelConfig settings;
Expand Down Expand Up @@ -596,6 +607,100 @@ void Pixel_FadeLayerHighlight_capability( TriggerMacro *trigger, uint8_t state,
}
}

void Pixel_FadeControl_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );

switch ( cstate )
{
case CapabilityState_Initial:
// Only activate on press event
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_FadeControl_capability(test)");
return;
default:
return;
}

// Get arguments
uint8_t profile = args[0];
uint8_t command = args[1];
uint8_t arg = args[2];

// Make sure profile is valid
if ( profile >= sizeof(Pixel_pixel_fade_profile_entries) )
{
return;
}

// Process command
uint8_t tmp;
switch ( command )
{
case PixelFadeControl_Reset:
for ( uint8_t config = 0; config < 4; config++ )
{
Pixel_pixel_fade_profile_entries[profile].conf[config] = \
Pixel_LED_FadePeriods[Pixel_LED_FadePeriod_Defaults[profile][config]];
}
Pixel_pixel_fade_profile_entries[profile].pos = 0;
Pixel_pixel_fade_profile_entries[profile].period_conf = PixelPeriodIndex_Off_to_On;
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[profile];
break;

case PixelFadeControl_Reset_All:
// Setup fade defaults
for ( uint8_t pr = 0; pr < 4; pr++ )
{
for ( uint8_t config = 0; config < 4; config++ )
{
Pixel_pixel_fade_profile_entries[pr].conf[config] = \
Pixel_LED_FadePeriods[Pixel_LED_FadePeriod_Defaults[pr][config]];
}
Pixel_pixel_fade_profile_entries[pr].pos = 0;
Pixel_pixel_fade_profile_entries[pr].period_conf = PixelPeriodIndex_Off_to_On;
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[pr];
}
break;

case PixelFadeControl_Brightness_Set:
// Set brightness
Pixel_pixel_fade_profile_entries[profile].brightness = arg;
break;

case PixelFadeControl_Brightness_Increment:
// Increment with no rollover
tmp = Pixel_pixel_fade_profile_entries[profile].brightness;
if ( tmp + arg < tmp )
{
Pixel_pixel_fade_profile_entries[profile].brightness = 0xFF;
break;
}
Pixel_pixel_fade_profile_entries[profile].brightness += arg;
break;

case PixelFadeControl_Brightness_Decrement:
// Decrement with no rollover
tmp = Pixel_pixel_fade_profile_entries[profile].brightness;
if ( tmp - arg > tmp )
{
Pixel_pixel_fade_profile_entries[profile].brightness = 0x00;
break;
}
Pixel_pixel_fade_profile_entries[profile].brightness -= arg;
break;

case PixelFadeControl_Brightness_Default:
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[profile];
break;

default:
return;
}
}

void Pixel_LEDTest_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
Expand Down Expand Up @@ -2071,7 +2176,25 @@ void Pixel_SecondaryProcessing_setup()
// Reset state
Pixel_pixel_fade_profile_entries[pf].pos = 0;
Pixel_pixel_fade_profile_entries[pf].period_conf = PixelPeriodIndex_Off_to_On;
Pixel_pixel_fade_profile_entries[pf].brightness = settings.fade_brightness[pf];
}
}

// Given a starting value and profile, calculate the resulting brightness
// The returned value is always equal to or less than val
static inline uint32_t Pixel_ApplyFadeBrightness( uint8_t brightness, uint32_t val )
{
// No need to calculate if brightness is max or 0
if ( brightness == 255 )
{
return val;
}
if ( brightness == 0 )
{
return 0;
}
uint32_t result = (val * brightness) >> 8; // val * brightness / 255
return result;
}

void Pixel_SecondaryProcessing()
Expand Down Expand Up @@ -2132,15 +2255,17 @@ void Pixel_SecondaryProcessing()
// If start and end are set to 0, ignore
if ( period->end == 0 && period->start == 0 )
{
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = Pixel_ApplyFadeBrightness(profile->brightness, val);
if (gamma_enabled) {
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = gamma_table[val];
((uint16_t*)buf->data)[chan - buf->offset] = (uint8_t)val;
}
((uint16_t*)buf->data)[chan - buf->offset] = (uint8_t)val;
break;
}

val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = Pixel_ApplyFadeBrightness(profile->brightness, val);
if (gamma_enabled) {
val = gamma_table[val];
}
Expand All @@ -2150,11 +2275,12 @@ void Pixel_SecondaryProcessing()
break;
// On hold time
case PixelPeriodIndex_On:
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = Pixel_ApplyFadeBrightness(profile->brightness, val);
if (gamma_enabled) {
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = gamma_table[val];
((uint16_t*)buf->data)[chan - buf->offset] = (uint8_t)val;
}
((uint16_t*)buf->data)[chan - buf->offset] = (uint8_t)val;
break;
// Off hold time
case PixelPeriodIndex_Off:
Expand All @@ -2165,6 +2291,7 @@ void Pixel_SecondaryProcessing()
if ( prev->start == 0 && prev->end == 0 )
{
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = Pixel_ApplyFadeBrightness(profile->brightness, val);
if (gamma_enabled) {
val = gamma_table[val];
}
Expand All @@ -2178,6 +2305,7 @@ void Pixel_SecondaryProcessing()
if ( prev->start != 0 )
{
val = (uint8_t)((uint16_t*)buf->data)[chan - buf->offset];
val = Pixel_ApplyFadeBrightness(profile->brightness, val);
if (gamma_enabled) {
val = gamma_table[val];
}
Expand Down Expand Up @@ -2738,6 +2866,11 @@ inline void Pixel_setup()
Pixel_LED_FadePeriods[Pixel_LED_FadePeriod_Defaults[profile][config]];
#endif
}
#if Storage_Enable_define == 1
defaults.fade_brightness[profile] = Pixel_LED_FadeBrightness[profile];
#else
settings.fade_brightness[profile] = Pixel_LED_FadeBrightness[profile];
#endif
}

// Register storage module
Expand Down Expand Up @@ -3327,6 +3460,7 @@ void Pixel_saveConfig() {
const PixelPeriodConfig period_config = Pixel_pixel_fade_profile_entries[profile].conf[config];
settings.fade_periods[profile][config] = period_config;
}
settings.fade_brightness[profile] = Pixel_pixel_fade_profile_entries[profile].brightness;
}
}

Expand Down Expand Up @@ -3367,5 +3501,15 @@ void Pixel_printConfig() {
print(NL);
}
}

// Profile brightness
print(NL " \033[35mProfile Brightnesses\033[0m" NL);
for (uint8_t profile=0; profile<4; profile++)
{
printInt8(profile);
print(" ");
printInt8(settings.fade_brightness[profile]);
print(NL);
}
}
#endif
2 changes: 2 additions & 0 deletions Macro/PixelMap/pixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ typedef struct PixelFadeProfile {
PixelPeriodConfig conf[4];
uint32_t pos; // Current position with the current PixelPeriodConfig
PixelPeriodIndex period_conf; // Which PixelPeriodConfig is being processed
uint8_t brightness;
} PixelFadeProfile;

typedef struct PixelLEDGroupEntry {
Expand All @@ -258,6 +259,7 @@ extern const AnimationStackElement Pixel_AnimationSettings[];
extern const PixelLEDGroupEntry Pixel_LED_DefaultFadeGroups[];
extern const PixelPeriodConfig Pixel_LED_FadePeriods[];
extern const uint8_t Pixel_LED_FadePeriod_Defaults[4][4];
extern const uint8_t Pixel_LED_FadeBrightness[4];

extern PixelBuf Pixel_Buffers[];
extern PixelBuf LED_Buffers[];
Expand Down

0 comments on commit ae7bf0b

Please sign in to comment.