]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ymd75/backlight.c
[Keyboard] fixed pins for numpad_5x4 layout (#6311)
[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)  // D4
24 //#define SCROLLLOCK_PORT (1 << 6)  // D6
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 __attribute__ ((weak))
48 void led_set_user(uint8_t usb_led) {
49     /*
50     if (usb_led & (1 << USB_LED_NUM_LOCK)) {
51       // turn on
52       DDRD  |= NUMLOCK_PORT;
53       PORTD |= NUMLOCK_PORT;
54     } else {
55       // turn off
56       DDRD  &= ~NUMLOCK_PORT;
57       PORTD &= ~NUMLOCK_PORT;
58     }
59      */
60     if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
61       DDRD  |= CAPSLOCK_PORT;
62       PORTD |= CAPSLOCK_PORT;
63     } else {
64       DDRD  &= ~CAPSLOCK_PORT;
65       PORTD &= ~CAPSLOCK_PORT;
66     }
67     /*
68     if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
69       DDRD  |= SCROLLLOCK_PORT;
70       PORTD |= SCROLLLOCK_PORT;
71     } else {
72       DDRD  &= ~SCROLLLOCK_PORT;
73       PORTD &= ~SCROLLLOCK_PORT;
74     }
75      */
76 }
77
78 #ifdef BACKLIGHT_ENABLE
79
80 // sets up Timer 1 for 8-bit PWM
81 void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE
82   // default 8 bit mode
83   TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH
84   TCCR1A |= (1 << 0);  // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW
85
86   // clear output compare value A
87   // outb(OCR1AH, 0);
88   // outb(OCR1AL, 0);
89
90   // clear output comparator registers for B
91         OCR1BH = 0; // outb(OCR1BH, 0);
92         OCR1BL = 0; // outb(OCR1BL, 0);
93 }
94
95 bool is_init = false;
96 void timer1Init(void) {
97   // timer1SetPrescaler(TIMER1PRESCALE)
98   // set to DIV/64
99   (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
100
101   // reset TCNT1
102   TCNT1H = 0;  // outb(TCNT1H, 0);
103         TCNT1L = 0;  // outb(TCNT1L, 0);
104
105   // TOIE1: Timer Overflow Interrupt Enable (Timer 1);
106         TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1);
107
108   is_init = true;
109 }
110
111 void timer1UnInit(void) {
112   // set prescaler back to NONE
113   (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00;  // TIMERRTC_CLK_STOP
114
115   // disable timer overflow interrupt
116   TIMSK &= ~_BV(TOIE1); // overflow bit?
117
118   setPWM(0);
119
120   is_init = false;
121 }
122
123
124 // handle TCNT1 overflow
125 //! Interrupt handler for tcnt1 overflow interrupt
126 ISR(TIMER1_OVF_vect, ISR_NOBLOCK)
127 {
128         // sei();
129   // handle breathing here
130   #ifdef BACKLIGHT_BREATHING
131   if (is_breathing()) {
132     custom_breathing_handler();
133   }
134   #endif
135
136   // TODO call user defined function
137 }
138
139 // enable timer 1 PWM
140 // timer1PWMBOn()
141 void timer1PWMBEnable(void) {
142   // turn on channel B (OC1B) PWM output
143   // set OC1B as non-inverted PWM
144   TCCR1A |= _BV(COM1B1);
145   TCCR1A &= ~_BV(COM1B0);
146 }
147
148 // disable timer 1 PWM
149 // timer1PWMBOff()
150 void timer1PWMBDisable(void) {
151   TCCR1A &= ~_BV(COM1B1);
152   TCCR1A &= ~_BV(COM1B0);
153 }
154
155 void enableBacklight(void) {
156   DDRD  |= BACKLIGHT_PORT;  // set digital pin 4 as output
157   PORTD |= BACKLIGHT_PORT;  // set digital pin 4 to high
158 }
159
160 void disableBacklight(void) {
161   // DDRD  &= ~BACKLIGHT_PORT;  // set digital pin 4 as input
162   PORTD &= ~BACKLIGHT_PORT;  // set digital pin 4 to low
163 }
164
165 void startPWM(void) {
166   timer1Init();
167   timer1PWMBEnable();
168   enableBacklight();
169 }
170
171 void stopPWM(void) {
172   timer1UnInit();
173   disableBacklight();
174   timer1PWMBDisable();
175 }
176
177 void b_led_init_ports(void) {
178   /* turn backlight on/off depending on user preference */
179   #if BACKLIGHT_ON_STATE == 0
180     // DDRx register: sets the direction of Port D
181     // DDRD  &= ~BACKLIGHT_PORT;  // set digital pin 4 as input
182     PORTD &= ~BACKLIGHT_PORT;  // set digital pin 4 to low
183   #else
184     DDRD  |= BACKLIGHT_PORT;  // set digital pin 4 as output
185     PORTD |= BACKLIGHT_PORT;  // set digital pin 4 to high
186   #endif
187
188   timer1PWMSetup();
189   startPWM();
190
191   #ifdef BACKLIGHT_BREATHING
192   breathing_enable();
193   #endif
194 }
195
196 void b_led_set(uint8_t level) {
197   if (level > BACKLIGHT_LEVELS) {
198     level = BACKLIGHT_LEVELS;
199   }
200
201   setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS));
202 }
203
204 // called every matrix scan
205 void b_led_task(void) {
206   // do nothing for now
207 }
208
209 void setPWM(uint16_t xValue) {
210   if (xValue > TIMER_TOP) {
211     xValue = TIMER_TOP;
212   }
213   OCR1B = xValue; // timer1PWMBSet(xValue);
214 }
215
216 #endif  // BACKLIGHT_ENABLE