summaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2019-11-02 21:20:03 +0000
committerFlorian Didron <fdidron@users.noreply.github.com>2020-01-09 08:57:11 +0900
commit2c0173f0ff0177db415c9dab6fe4eeaefe4b0ab6 (patch)
treeb5a7f545edcc93c88623784f3df8c5e1db6a8a71 /quantum
parentbac9fc60fe5a969c87bf288a2896b1941458eebf (diff)
Initial migration of software PWM backlight (#6709)
* Initial migration of software PWM backlight * First pass at backlight driver docs * Correct driver name in docs * Run backlight_task when using BACKLIGHT_PINS * Resolve backlight docs TODOs
Diffstat (limited to 'quantum')
-rw-r--r--quantum/backlight/backlight_avr.c10
-rw-r--r--quantum/backlight/backlight_soft.c66
-rw-r--r--quantum/quantum.c2
-rw-r--r--quantum/quantum.h3
4 files changed, 75 insertions, 6 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index 445698f47c..648a37adf3 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -159,7 +159,7 @@
# define BACKLIGHT_ON_STATE 0
# endif
-void backlight_on(uint8_t backlight_pin) {
+void backlight_on(pin_t backlight_pin) {
# if BACKLIGHT_ON_STATE == 0
writePinLow(backlight_pin);
# else
@@ -167,7 +167,7 @@ void backlight_on(uint8_t backlight_pin) {
# endif
}
-void backlight_off(uint8_t backlight_pin) {
+void backlight_off(pin_t backlight_pin) {
# if BACKLIGHT_ON_STATE == 0
writePinHigh(backlight_pin);
# else
@@ -191,16 +191,16 @@ void backlight_off(uint8_t backlight_pin) {
# define FOR_EACH_LED(x) \
for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
- uint8_t backlight_pin = backlight_pins[i]; \
+ pin_t backlight_pin = backlight_pins[i]; \
{ x } \
}
-static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
+static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
# else // full hardware PWM
// we support only one backlight pin
-static const uint8_t backlight_pin = BACKLIGHT_PIN;
+static const pin_t backlight_pin = BACKLIGHT_PIN;
# define FOR_EACH_LED(x) x
# endif
diff --git a/quantum/backlight/backlight_soft.c b/quantum/backlight/backlight_soft.c
new file mode 100644
index 0000000000..a6aba7782c
--- /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()
+}
diff --git a/quantum/quantum.c b/quantum/quantum.c
index 575bf88bbb..248707057d 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -992,7 +992,7 @@ void matrix_scan_quantum() {
#if defined(BACKLIGHT_ENABLE)
# if defined(LED_MATRIX_ENABLE)
led_matrix_task();
-# elif defined(BACKLIGHT_PIN)
+# elif defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS)
backlight_task();
# endif
#endif
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 759f7712c2..01abe1c0a1 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -256,6 +256,9 @@ void tap_code16(uint16_t code);
#ifdef BACKLIGHT_ENABLE
void backlight_init_ports(void);
void backlight_task(void);
+void backlight_task_internal(void);
+void backlight_on(pin_t backlight_pin);
+void backlight_off(pin_t backlight_pin);
# ifdef BACKLIGHT_BREATHING
void breathing_task(void);