aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/backlight/backlight_software.c
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2020-03-07 12:09:49 +0000
committerGitHub <noreply@github.com>2020-03-07 12:09:49 +0000
commit3a303bd2aec57fd2d4d0f6d3e5583d746367d6e1 (patch)
tree8ab57281f90be800b6e3556e2705ffbcabf1494c /quantum/backlight/backlight_software.c
parentd7ba0ad684a18c07952963427ca89a8e8e7e1903 (diff)
downloadfirmware-3a303bd2aec57fd2d4d0f6d3e5583d746367d6e1.tar.gz
firmware-3a303bd2aec57fd2d4d0f6d3e5583d746367d6e1.tar.bz2
firmware-3a303bd2aec57fd2d4d0f6d3e5583d746367d6e1.zip
Backlight - Carve out a better location for private driver functionality (#8329)
* rename backlight_soft to match rules.mk * rename backlight_soft to match rules.mk - update common_features * Carve out a better location for private driver backlight functionality
Diffstat (limited to 'quantum/backlight/backlight_software.c')
-rw-r--r--quantum/backlight/backlight_software.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/quantum/backlight/backlight_software.c b/quantum/backlight/backlight_software.c
new file mode 100644
index 000000000..709304f55
--- /dev/null
+++ b/quantum/backlight/backlight_software.c
@@ -0,0 +1,48 @@
+#include "quantum.h"
+#include "backlight.h"
+#include "backlight_driver_common.h"
+
+#ifdef BACKLIGHT_BREATHING
+# error "Backlight breathing is not available for software PWM. Please disable."
+#endif
+
+static uint16_t s_duty_pattern = 0;
+
+// clang-format off
+
+/** \brief PWM duty patterns
+ *
+ * We scale the current backlight level to an index within this array. This allows
+ * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern
+ */
+static const uint16_t backlight_duty_table[] = {
+ 0b0000000000000000,
+ 0b1000000000000000,
+ 0b1000000010000000,
+ 0b1000001000010000,
+ 0b1000100010001000,
+ 0b1001001001001000,
+ 0b1010101010101010,
+ 0b1110111011101110,
+ 0b1111111111111111,
+};
+#define backlight_duty_table_size (sizeof(backlight_duty_table) / sizeof(backlight_duty_table[0]))
+
+// clang-format on
+
+static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; }
+
+void backlight_init_ports(void) { backlight_pins_init(); }
+
+void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; }
+
+void backlight_task(void) {
+ static uint8_t backlight_tick = 0;
+
+ if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) {
+ backlight_pins_on();
+ } else {
+ backlight_pins_off();
+ }
+ backlight_tick = (backlight_tick + 1) % 16;
+}