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