aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2020-03-06 12:49:45 +0000
committerGitHub <noreply@github.com>2020-03-06 12:49:45 +0000
commit918a85d342aa608deac1650ddd0692dd1717c5e3 (patch)
tree6de4d888cd8b6dba52ebf2fd82125e79762ad1a6
parent116c0e44a1a4999c54019e48337c0e6b92a710f8 (diff)
downloadqmk_firmware-918a85d342aa608deac1650ddd0692dd1717c5e3.tar.gz
qmk_firmware-918a85d342aa608deac1650ddd0692dd1717c5e3.zip
Refactor more backlight to a common location (#8292)
* Refactor more backlight to a common location * BACKLIGHT_PIN not defined for custom backlight * align function names
-rw-r--r--quantum/backlight/backlight.c58
-rw-r--r--quantum/backlight/backlight.h8
-rw-r--r--quantum/backlight/backlight_arm.c7
-rw-r--r--quantum/backlight/backlight_avr.c63
-rw-r--r--quantum/backlight/backlight_soft.c54
5 files changed, 78 insertions, 112 deletions
diff --git a/quantum/backlight/backlight.c b/quantum/backlight/backlight.c
index 4aa74667d..4a0eac64c 100644
--- a/quantum/backlight/backlight.c
+++ b/quantum/backlight/backlight.c
@@ -15,14 +15,62 @@ You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "quantum.h"
18#include "backlight.h" 19#include "backlight.h"
19#include "eeconfig.h" 20#include "eeconfig.h"
20#include "debug.h" 21#include "debug.h"
21 22
22backlight_config_t backlight_config; 23backlight_config_t backlight_config;
23 24
25#ifdef BACKLIGHT_BREATHING
24// TODO: migrate to backlight_config_t 26// TODO: migrate to backlight_config_t
25static uint8_t breathing_period = BREATHING_PERIOD; 27static uint8_t breathing_period = BREATHING_PERIOD;
28#endif
29
30#ifndef BACKLIGHT_CUSTOM_DRIVER
31# if defined(BACKLIGHT_PINS)
32static const pin_t backlight_pins[] = BACKLIGHT_PINS;
33# ifndef BACKLIGHT_LED_COUNT
34# define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t))
35# endif
36
37# define FOR_EACH_LED(x) \
38 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
39 pin_t backlight_pin = backlight_pins[i]; \
40 { x } \
41 }
42# else
43// we support only one backlight pin
44static const pin_t backlight_pin = BACKLIGHT_PIN;
45# define FOR_EACH_LED(x) x
46# endif
47
48static inline void backlight_on(pin_t backlight_pin) {
49# if BACKLIGHT_ON_STATE == 0
50 writePinLow(backlight_pin);
51# else
52 writePinHigh(backlight_pin);
53# endif
54}
55
56static inline void backlight_off(pin_t backlight_pin) {
57# if BACKLIGHT_ON_STATE == 0
58 writePinHigh(backlight_pin);
59# else
60 writePinLow(backlight_pin);
61# endif
62}
63
64void backlight_pins_init(void) {
65 // Setup backlight pin as output and output to off state.
66 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);)
67}
68
69void backlight_pins_on(void) { FOR_EACH_LED(backlight_on(backlight_pin);) }
70
71void backlight_pins_off(void) { FOR_EACH_LED(backlight_off(backlight_pin);) }
72
73#endif
26 74
27/** \brief Backlight initialization 75/** \brief Backlight initialization
28 * 76 *
@@ -205,7 +253,6 @@ void backlight_disable_breathing(void) {
205 * FIXME: needs doc 253 * FIXME: needs doc
206 */ 254 */
207bool is_backlight_breathing(void) { return backlight_config.breathing; } 255bool is_backlight_breathing(void) { return backlight_config.breathing; }
208#endif
209 256
210// following are marked as weak purely for backwards compatibility 257// following are marked as weak purely for backwards compatibility
211__attribute__((weak)) void breathing_period_set(uint8_t value) { breathing_period = value ? value : 1; } 258__attribute__((weak)) void breathing_period_set(uint8_t value) { breathing_period = value ? value : 1; }
@@ -218,6 +265,15 @@ __attribute__((weak)) void breathing_period_inc(void) { breathing_period_set(bre
218 265
219__attribute__((weak)) void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); } 266__attribute__((weak)) void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }
220 267
268__attribute__((weak)) void breathing_toggle(void) {
269 if (is_breathing())
270 breathing_disable();
271 else
272 breathing_enable();
273}
274
275#endif
276
221// defaults for backlight api 277// defaults for backlight api
222__attribute__((weak)) void backlight_init_ports(void) {} 278__attribute__((weak)) void backlight_init_ports(void) {}
223 279
diff --git a/quantum/backlight/backlight.h b/quantum/backlight/backlight.h
index 08acf942f..07a4880e9 100644
--- a/quantum/backlight/backlight.h
+++ b/quantum/backlight/backlight.h
@@ -26,6 +26,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
26# error "Maximum value of BACKLIGHT_LEVELS is 31" 26# error "Maximum value of BACKLIGHT_LEVELS is 31"
27#endif 27#endif
28 28
29#ifndef BACKLIGHT_ON_STATE
30# define BACKLIGHT_ON_STATE 1
31#endif
32
29#ifndef BREATHING_PERIOD 33#ifndef BREATHING_PERIOD
30# define BREATHING_PERIOD 6 34# define BREATHING_PERIOD 6
31#endif 35#endif
@@ -40,6 +44,10 @@ typedef union {
40 }; 44 };
41} backlight_config_t; 45} backlight_config_t;
42 46
47void backlight_pins_init(void);
48void backlight_pins_on(void);
49void backlight_pins_off(void);
50
43void backlight_init(void); 51void backlight_init(void);
44void backlight_toggle(void); 52void backlight_toggle(void);
45void backlight_enable(void); 53void backlight_enable(void);
diff --git a/quantum/backlight/backlight_arm.c b/quantum/backlight/backlight_arm.c
index 397a1ac1a..a6d38a1a0 100644
--- a/quantum/backlight/backlight_arm.c
+++ b/quantum/backlight/backlight_arm.c
@@ -158,13 +158,6 @@ void breathing_self_disable(void) {
158 breathing_halt = BREATHING_HALT_ON; 158 breathing_halt = BREATHING_HALT_ON;
159} 159}
160 160
161void breathing_toggle(void) {
162 if (is_breathing())
163 breathing_disable();
164 else
165 breathing_enable();
166}
167
168/* To generate breathing curve in python: 161/* To generate breathing curve in python:
169 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] 162 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
170 */ 163 */
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index ce6611fb5..40291d382 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -164,49 +164,7 @@ error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk")
164error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk") 164error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk")
165#endif 165#endif
166 166
167#ifndef BACKLIGHT_ON_STATE 167#ifndef BACKLIGHT_PWM_TIMER // pwm through software
168# define BACKLIGHT_ON_STATE 1
169#endif
170
171void backlight_on(pin_t backlight_pin) {
172#if BACKLIGHT_ON_STATE == 1
173 writePinHigh(backlight_pin);
174#else
175 writePinLow(backlight_pin);
176#endif
177}
178
179void backlight_off(pin_t backlight_pin) {
180#if BACKLIGHT_ON_STATE == 1
181 writePinLow(backlight_pin);
182#else
183 writePinHigh(backlight_pin);
184#endif
185}
186
187#ifdef BACKLIGHT_PWM_TIMER // pwm through software
188
189// we support multiple backlight pins
190# ifndef BACKLIGHT_LED_COUNT
191# define BACKLIGHT_LED_COUNT 1
192# endif
193
194# if BACKLIGHT_LED_COUNT == 1
195# define BACKLIGHT_PIN_INIT \
196 { BACKLIGHT_PIN }
197# else
198# define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
199# endif
200
201# define FOR_EACH_LED(x) \
202 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
203 pin_t backlight_pin = backlight_pins[i]; \
204 { x } \
205 }
206
207static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
208
209#else // full hardware PWM
210 168
211static inline void enable_pwm(void) { 169static inline void enable_pwm(void) {
212# if BACKLIGHT_ON_STATE == 1 170# if BACKLIGHT_ON_STATE == 1
@@ -224,10 +182,6 @@ static inline void disable_pwm(void) {
224# endif 182# endif
225} 183}
226 184
227// we support only one backlight pin
228static const pin_t backlight_pin = BACKLIGHT_PIN;
229# define FOR_EACH_LED(x) x
230
231#endif 185#endif
232 186
233#ifdef BACKLIGHT_PWM_TIMER 187#ifdef BACKLIGHT_PWM_TIMER
@@ -246,7 +200,7 @@ static const pin_t backlight_pin = BACKLIGHT_PIN;
246// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz. 200// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
247 201
248// Triggered when the counter reaches the OCRx value 202// Triggered when the counter reaches the OCRx value
249ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) } 203ISR(TIMERx_COMPA_vect) { backlight_pins_off(); }
250 204
251// Triggered when the counter reaches the TOP value 205// Triggered when the counter reaches the TOP value
252// this one triggers at F_CPU/65536 =~ 244 Hz 206// this one triggers at F_CPU/65536 =~ 244 Hz
@@ -265,7 +219,7 @@ ISR(TIMERx_OVF_vect) {
265 // takes many computation cycles). 219 // takes many computation cycles).
266 // so better not turn them on while the counter TOP is very low. 220 // so better not turn them on while the counter TOP is very low.
267 if (OCRxx > 256) { 221 if (OCRxx > 256) {
268 FOR_EACH_LED(backlight_on(backlight_pin);) 222 backlight_pins_on();
269 } 223 }
270} 224}
271 225
@@ -305,7 +259,7 @@ void backlight_set(uint8_t level) {
305 // Turn off PWM control on backlight pin 259 // Turn off PWM control on backlight pin
306 disable_pwm(); 260 disable_pwm();
307#endif 261#endif
308 FOR_EACH_LED(backlight_off(backlight_pin);) 262 backlight_pins_off();
309 } else { 263 } else {
310#ifdef BACKLIGHT_PWM_TIMER 264#ifdef BACKLIGHT_PWM_TIMER
311 if (!OCRxx) { 265 if (!OCRxx) {
@@ -397,13 +351,6 @@ void breathing_self_disable(void) {
397 breathing_halt = BREATHING_HALT_ON; 351 breathing_halt = BREATHING_HALT_ON;
398} 352}
399 353
400void breathing_toggle(void) {
401 if (is_breathing())
402 breathing_disable();
403 else
404 breathing_enable();
405}
406
407/* To generate breathing curve in python: 354/* To generate breathing curve in python:
408 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] 355 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
409 */ 356 */
@@ -438,7 +385,7 @@ ISR(TIMERx_OVF_vect)
438 385
439void backlight_init_ports(void) { 386void backlight_init_ports(void) {
440 // Setup backlight pin as output and output to on state. 387 // Setup backlight pin as output and output to on state.
441 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);) 388 backlight_pins_init();
442 389
443 // I could write a wall of text here to explain... but TL;DW 390 // I could write a wall of text here to explain... but TL;DW
444 // Go read the ATmega32u4 datasheet. 391 // Go read the ATmega32u4 datasheet.
diff --git a/quantum/backlight/backlight_soft.c b/quantum/backlight/backlight_soft.c
index 096b41d91..8552384a4 100644
--- a/quantum/backlight/backlight_soft.c
+++ b/quantum/backlight/backlight_soft.c
@@ -9,47 +9,7 @@
9# error "Backlight breathing is not available for software PWM. Please disable." 9# error "Backlight breathing is not available for software PWM. Please disable."
10#endif 10#endif
11 11
12#ifndef BACKLIGHT_ON_STATE 12static uint16_t s_duty_pattern = 0;
13# define BACKLIGHT_ON_STATE 1
14#endif
15
16#ifdef BACKLIGHT_PINS
17# define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
18#else
19# define BACKLIGHT_PIN_INIT \
20 { BACKLIGHT_PIN }
21#endif
22
23static uint16_t s_duty_pattern = 0;
24static const pin_t backlight_pins[] = BACKLIGHT_PIN_INIT;
25#define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t))
26
27#define FOR_EACH_LED(x) \
28 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
29 pin_t backlight_pin = backlight_pins[i]; \
30 { x } \
31 }
32
33void backlight_on(pin_t backlight_pin) {
34#if BACKLIGHT_ON_STATE == 0
35 writePinLow(backlight_pin);
36#else
37 writePinHigh(backlight_pin);
38#endif
39}
40
41void backlight_off(pin_t backlight_pin) {
42#if BACKLIGHT_ON_STATE == 0
43 writePinHigh(backlight_pin);
44#else
45 writePinLow(backlight_pin);
46#endif
47}
48
49void backlight_init_ports(void) {
50 // Setup backlight pin as output and output to off state.
51 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);)
52}
53 13
54// clang-format off 14// clang-format off
55 15
@@ -58,7 +18,7 @@ void backlight_init_ports(void) {
58 * We scale the current backlight level to an index within this array. This allows 18 * We scale the current backlight level to an index within this array. This allows
59 * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern 19 * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern
60 */ 20 */
61static uint16_t backlight_duty_table[] = { 21static const uint16_t backlight_duty_table[] = {
62 0b0000000000000000, 22 0b0000000000000000,
63 0b1000000000000000, 23 0b1000000000000000,
64 0b1000000010000000, 24 0b1000000010000000,
@@ -75,15 +35,17 @@ static uint16_t backlight_duty_table[] = {
75 35
76static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } 36static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; }
77 37
38void backlight_init_ports(void) { backlight_pins_init(); }
39
40void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; }
41
78void backlight_task(void) { 42void backlight_task(void) {
79 static uint8_t backlight_tick = 0; 43 static uint8_t backlight_tick = 0;
80 44
81 if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { 45 if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) {
82 FOR_EACH_LED(backlight_on(backlight_pin);) 46 backlight_pins_on();
83 } else { 47 } else {
84 FOR_EACH_LED(backlight_off(backlight_pin);) 48 backlight_pins_off();
85 } 49 }
86 backlight_tick = (backlight_tick + 1) % 16; 50 backlight_tick = (backlight_tick + 1) % 16;
87} 51}
88
89void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; }