Skip to content

Commit

Permalink
Finish infrastructure for backlight
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed Jan 18, 2024
1 parent 97a3119 commit abacfbe
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ generated/
.gdb_history
test_main
__pycache__/
simulator/common/
3 changes: 3 additions & 0 deletions common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ const uint8_t ROW_PINS[] =

const uint16_t CHECKSUM_PERIOD = 65500;

const uint32_t DEFAULT_BLINK_PERIOD = 500;
const uint32_t DEFAULT_FADE_TIME = 1000;

}
26 changes: 24 additions & 2 deletions core/backlight/backlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,34 @@
namespace core::backlight
{

Backlight::Backlight(Device& device) : device{device}
Backlight::Backlight(Device& device, schemes::Scheme** schemes, int num_schemes)
: device{device}, num_schemes{num_schemes}, schemes{schemes}
{
for (uint8_t i = 0; i < common::constants::TOTAL_NUM_LEDS; ++i)
{
ledStates[i].description = &common::constants::LED_PROPERTIES[i];
led_states[i].description = &common::constants::LED_PROPERTIES[i];
}
schemes[current_scheme_index]->reset();
}

void Backlight::increment_scheme()
{
current_scheme_index = (current_scheme_index + 1) % num_schemes;
schemes[current_scheme_index]->reset();
}

void Backlight::update(const core::keyboard::KeyboardScanResult& scan_result)
{
auto* scheme = schemes[current_scheme_index];
scheme->update(scan_result, led_states);
for (uint8_t i = 0; i < common::constants::TOTAL_NUM_LEDS; ++i)
{
LEDState& state = led_states[i];
auto c = state.update(device);
device.set_led(i, c.get_r_byte(), c.get_g_byte(), c.get_b_byte());

}
device.update_leds();
}

}
35 changes: 16 additions & 19 deletions core/backlight/backlight.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
#pragma once

#include <stdint.h>
#include "../../device.h"
#include "../../common/key_properties.h"
#include "../../common/constants.h"
#include "color.h"
#include "ledstate.h"
#include "../../common/constants.h"
#include "../util/timer.h"
#include "schemes/scheme.h"


namespace core::backlight
{

struct LEDState
{
Color color;

bool blinking = false;
util::Timer blink_timer;

bool fading = false;
util::Timer fade_timer;

const common::LEDDescription* description;
};

class Backlight
{
public:
Backlight(Device& device);
void update();
Backlight(Device& device, schemes::Scheme* schemes[], int num_schemes);
void update(const core::keyboard::KeyboardScanResult& scan_result);
void increment_scheme();

private:
Color update_fade(LEDState& state);
Color update_blink(LEDState& state);

Device& device;
LEDState ledStates[common::constants::TOTAL_NUM_LEDS];
LEDState led_states[common::constants::TOTAL_NUM_LEDS];

int current_scheme_index = 0;

const int num_schemes;
schemes::Scheme** schemes;
};

}
42 changes: 39 additions & 3 deletions core/backlight/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,45 @@

#include <stdint.h>

namespace core::backlight
{

struct Color
{
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
float r = 0;
float g = 0;
float b = 0;

uint8_t get_r_byte() const
{
return static_cast<uint8_t>(r * 255.0f);
}

uint8_t get_g_byte() const
{
return static_cast<uint8_t>(g * 255.0f);
}

uint8_t get_b_byte() const
{
return static_cast<uint8_t>(b * 255.0f);
}

Color operator*(const float f) const
{
return Color{
static_cast<float>(r * f),
static_cast<float>(g * f),
static_cast<float>(b * f)};
}

Color operator+(const Color& other) const
{
return Color{
static_cast<float>(r + other.r),
static_cast<float>(g + other.g),
static_cast<float>(b + other.b)};
}
};

}
64 changes: 64 additions & 0 deletions core/backlight/ledstate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "ledstate.h"

namespace core::backlight
{


void LEDState::start_blink(const Device& device, const uint32_t period)
{
blinking = true;
blink_timer.duration = period;
blink_state = true;
blink_timer.start(device);
}

void LEDState::start_fade(const Device& device, const Color& fade_start_color, const uint32_t fade_time)
{
fading = true;
fade_timer.duration = fade_time;
fade_timer.start(device);
this->fade_start_color = fade_start_color;
}

bool LEDState::is_blinking() const
{
return blinking;
}

bool LEDState::get_blink_state() const
{
return blink_state;
}

bool LEDState::is_fading() const
{
return fading;
}

Color LEDState::update(const Device& device)
{
if (blinking)
{
blink_timer.update(device);
if (blink_timer.is_finished())
{
blink_state = !blink_state;
blink_timer.start(device);
}
return blink_state ? color : Color{0, 0, 0};
}
if (fading)
{
fade_timer.update(device);
if (fade_timer.is_finished())
{
fading = false;
return color;
}
const auto progress = fade_timer.progress();
return (fade_start_color * (1.0f - progress)) + (color * progress);
}
return color;
}

}
63 changes: 63 additions & 0 deletions core/backlight/ledstate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include "../../device.h"
#include "color.h"
#include "../../common/constants.h"
#include "../util/timer.h"

namespace core::backlight
{

struct LEDState
{
/**
* The current color of the LED, may be overridden by a blink or fade sequence.
*/
Color color;

/**
* Whether the current LED is blinking.
*/
bool is_blinking() const;

/**
* The state of the blinking, true if illuminated and false if off.
*/
bool get_blink_state() const;

/**
* Whether the current LED is fading.
*/
bool is_fading() const;

/**
* Starts a blink sequence on the LED.
*/
void start_blink(const Device& device, const uint32_t period = common::constants::DEFAULT_BLINK_PERIOD);

/**
* Starts a fade sequence on the LED. Will fade from the fade_start_color
* to the current color.
*/
void start_fade(const Device& device, const Color& fade_start_color, const uint32_t fade_time = common::constants::DEFAULT_FADE_TIME);

const common::LEDDescription* description;

friend class Backlight;

private:

/**
* Updates the LED state, returns the current color of the LED.
*/
Color update(const Device& device);

Color fade_start_color;
bool blinking = false;
bool fading = false;
bool blink_state = false;
util::Timer blink_timer;
util::Timer fade_timer;
};

}
26 changes: 26 additions & 0 deletions core/backlight/schemes/scheme.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "../../../device.h"
#include "../../keyboard/keyscan.h"
#include "../ledstate.h"

namespace core::backlight::schemes
{

/**
* Base class for backlight schemes.
*/
class Scheme
{
public:
Scheme(Device& device) : device(device) { }

virtual void reset() = 0;

virtual void update(const core::keyboard::KeyboardScanResult& scan_result,
core::backlight::LEDState led_states[common::constants::TOTAL_NUM_LEDS]) = 0;

protected:
Device& device;
};

}
Loading

0 comments on commit abacfbe

Please sign in to comment.