Backlighting for JJ40 and underglow initialisation code (#2260)
[jackhill/qmk/firmware.git] / keyboards / jj40 / backlight.c
1 /**
2 * Backlighting code for PS2AVRGB boards (ATMEGA32A)
3 * Kenneth A. (github.com/krusli | krusli.me)
4 */
5
6 #include "backlight.h"
7 #include "quantum.h"
8
9 #include <avr/pgmspace.h>
10 #include <avr/interrupt.h>
11
12 #include "backlight_custom.h"
13 #include "breathing_custom.h"
14
15 // DEBUG
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 // Port D: digital pins of the AVR chipset
20 #define NUMLOCK_PORT (1 << 1) // 1st pin of Port D (digital)
21 #define CAPSLOCK_PORT (1 << 2) // 2nd pin
22 #define BACKLIGHT_PORT (1 << 4) // 4th pin
23 #define SCROLLLOCK_PORT (1 << 6) // 6th pin
24
25 #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64
26 #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default
27
28 #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask
29
30 #define PWM_MAX 0xFF
31 #define TIMER_TOP 255 // 8 bit PWM
32
33 extern backlight_config_t backlight_config;
34
35 /**
36 * References
37 * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation
38 * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b
39 * Timers: http://www.avrbeginners.net/architecture/timers/timers.html
40 * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/
41 * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware
42 */
43
44 // @Override
45 // turn LEDs on and off depending on USB caps/num/scroll lock states.
46 void led_set_user(uint8_t usb_led) {
47 if (usb_led & (1 << USB_LED_NUM_LOCK)) {
48 // turn on
49 DDRD |= NUMLOCK_PORT;
50 PORTD |= NUMLOCK_PORT;
51 } else {
52 // turn off
53 DDRD &= ~NUMLOCK_PORT;
54 PORTD &= ~NUMLOCK_PORT;
55 }
56
57 if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
58 DDRD |= CAPSLOCK_PORT;
59 PORTD |= CAPSLOCK_PORT;
60 } else {
61 DDRD &= ~CAPSLOCK_PORT;
62 PORTD &= ~CAPSLOCK_PORT;
63 }
64
65 if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
66 DDRD |= SCROLLLOCK_PORT;
67 PORTD |= SCROLLLOCK_PORT;
68 } else {
69 DDRD &= ~SCROLLLOCK_PORT;
70 PORTD &= ~SCROLLLOCK_PORT;
71 }
72 }
73
74 #ifdef BACKLIGHT_ENABLE
75
76 // sets up Timer 1 for 8-bit PWM
77 void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE
78 // default 8 bit mode
79 TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH
80 TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW
81
82 // clear output compare value A
83 // outb(OCR1AH, 0);
84 // outb(OCR1AL, 0);
85
86 // clear output comparator registers for B
87 OCR1BH = 0; // outb(OCR1BH, 0);
88 OCR1BL = 0; // outb(OCR1BL, 0);
89 }
90
91 bool is_init = false;
92 void timer1Init(void) {
93 // timer1SetPrescaler(TIMER1PRESCALE)
94 // set to DIV/64
95 (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
96
97 // reset TCNT1
98 TCNT1H = 0; // outb(TCNT1H, 0);
99 TCNT1L = 0; // outb(TCNT1L, 0);
100
101 // TOIE1: Timer Overflow Interrupt Enable (Timer 1);
102 TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1);
103
104 is_init = true;
105 }
106
107 void timer1UnInit(void) {
108 // set prescaler back to NONE
109 (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP
110
111 // disable timer overflow interrupt
112 TIMSK &= ~_BV(TOIE1); // overflow bit?
113
114 setPWM(0);
115
116 is_init = false;
117 }
118
119
120 // handle TCNT1 overflow
121 //! Interrupt handler for tcnt1 overflow interrupt
122 ISR(TIMER1_OVF_vect, ISR_NOBLOCK)
123 {
124 // sei();
125 // handle breathing here
126 #ifdef BACKLIGHT_BREATHING
127 if (is_breathing()) {
128 custom_breathing_handler();
129 }
130 #endif
131
132 // TODO call user defined function
133 }
134
135 // enable timer 1 PWM
136 // timer1PWMBOn()
137 void timer1PWMBEnable(void) {
138 // turn on channel B (OC1B) PWM output
139 // set OC1B as non-inverted PWM
140 TCCR1A |= _BV(COM1B1);
141 TCCR1A &= ~_BV(COM1B0);
142 }
143
144 // disable timer 1 PWM
145 // timer1PWMBOff()
146 void timer1PWMBDisable(void) {
147 TCCR1A &= ~_BV(COM1B1);
148 TCCR1A &= ~_BV(COM1B0);
149 }
150
151 void enableBacklight(void) {
152 DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
153 PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
154 }
155
156 void disableBacklight(void) {
157 // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
158 PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
159 }
160
161 void startPWM(void) {
162 timer1Init();
163 timer1PWMBEnable();
164 enableBacklight();
165 }
166
167 void stopPWM(void) {
168 timer1UnInit();
169 disableBacklight();
170 timer1PWMBDisable();
171 }
172
173 void b_led_init_ports(void) {
174 /* turn backlight on/off depending on user preference */
175 #if BACKLIGHT_ON_STATE == 0
176 // DDRx register: sets the direction of Port D
177 // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
178 PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
179 #else
180 DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
181 PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
182 #endif
183
184 timer1PWMSetup();
185 startPWM();
186
187 #ifdef BACKLIGHT_BREATHING
188 breathing_enable();
189 #endif
190 }
191
192 void b_led_set(uint8_t level) {
193 if (level > BACKLIGHT_LEVELS) {
194 level = BACKLIGHT_LEVELS;
195 }
196
197 setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS));
198 }
199
200 // called every matrix scan
201 void b_led_task(void) {
202 // do nothing for now
203 }
204
205 void setPWM(uint16_t xValue) {
206 if (xValue > TIMER_TOP) {
207 xValue = TIMER_TOP;
208 }
209 OCR1B = xValue; // timer1PWMBSet(xValue);
210 }
211
212 #endif // BACKLIGHT_ENABLE