aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/backlight/backlight_soft.c
diff options
context:
space:
mode:
authorWilliam Chang <william@factual.com>2019-11-20 22:17:07 -0800
committerWilliam Chang <william@factual.com>2019-11-20 22:17:07 -0800
commite7f4d56592b3975c38af329e77b4efd9108495e8 (patch)
tree0a416bccbf70bfdbdb9ffcdb3bf136b47378c014 /quantum/backlight/backlight_soft.c
parent71493b2f9bbd5f3d18373c518fa14ccafcbf48fc (diff)
parent8416a94ad27b3ff058576f09f35f0704a8b39ff3 (diff)
downloadfirmware-e7f4d56592b3975c38af329e77b4efd9108495e8.tar.gz
firmware-e7f4d56592b3975c38af329e77b4efd9108495e8.tar.bz2
firmware-e7f4d56592b3975c38af329e77b4efd9108495e8.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'quantum/backlight/backlight_soft.c')
-rw-r--r--quantum/backlight/backlight_soft.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/quantum/backlight/backlight_soft.c b/quantum/backlight/backlight_soft.c
new file mode 100644
index 000000000..a6aba7782
--- /dev/null
+++ b/quantum/backlight/backlight_soft.c
@@ -0,0 +1,66 @@
+#include "quantum.h"
+#include "backlight.h"
+
+#if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS)
+# error "Backlight pin/pins not defined. Please configure."
+#endif
+
+#ifdef BACKLIGHT_BREATHING
+# error "Backlight breathing is not available for software PWM. Please disable."
+#endif
+
+#ifndef BACKLIGHT_ON_STATE
+# define BACKLIGHT_ON_STATE 0
+#endif
+
+#ifdef BACKLIGHT_PINS
+# define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
+#else
+# define BACKLIGHT_PIN_INIT \
+ { BACKLIGHT_PIN }
+#endif
+
+static const pin_t backlight_pins[] = BACKLIGHT_PIN_INIT;
+#define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t))
+
+#define FOR_EACH_LED(x) \
+ for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
+ pin_t backlight_pin = backlight_pins[i]; \
+ { x } \
+ }
+
+void backlight_on(pin_t backlight_pin) {
+#if BACKLIGHT_ON_STATE == 0
+ writePinLow(backlight_pin);
+#else
+ writePinHigh(backlight_pin);
+#endif
+}
+
+void backlight_off(pin_t backlight_pin) {
+#if BACKLIGHT_ON_STATE == 0
+ writePinHigh(backlight_pin);
+#else
+ writePinLow(backlight_pin);
+#endif
+}
+
+void backlight_init_ports(void) {
+ // Setup backlight pin as output and output to on state.
+ FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
+}
+
+void backlight_task(void) {
+ static uint8_t backlight_tick = 0;
+
+ if ((0xFFFF >> (get_backlight_level() * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
+ FOR_EACH_LED(backlight_on(backlight_pin);)
+ } else {
+ FOR_EACH_LED(backlight_off(backlight_pin);)
+ }
+ backlight_tick = (backlight_tick + 1) % 16;
+}
+
+void backlight_set(uint8_t level) {
+ // noop as backlight_task uses get_backlight_level()
+}