]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/cannonkeys/stm32f072/led.c
[Keymap] jotix ortho_4x12 layout match new keycaps (#6209)
[qmk_firmware.git] / keyboards / cannonkeys / stm32f072 / led.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "hal.h"
19 #include "led_custom.h"
20 #include "keyboard.h"
21 #include "printf.h"
22
23 static void breathing_callback(PWMDriver *pwmp);
24
25 static PWMConfig pwmCFG = {
26   0xFFFF,                              /* PWM clock frequency  */
27   256,                              /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
28   NULL,                               /* No Callback */
29   {
30       {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */
31       {PWM_OUTPUT_DISABLED, NULL},
32       {PWM_OUTPUT_DISABLED, NULL},
33       {PWM_OUTPUT_DISABLED, NULL}
34   },
35   0,                                  /* HW dependent part.*/
36   0
37 };
38
39 static PWMConfig pwmCFG_breathing = {
40   0xFFFF,                              /* 10kHz PWM clock frequency  */
41   256,                              /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
42   breathing_callback,                               /* Breathing Callback */
43   {
44       {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */
45       {PWM_OUTPUT_DISABLED, NULL},
46       {PWM_OUTPUT_DISABLED, NULL},
47       {PWM_OUTPUT_DISABLED, NULL}
48   },
49   0,                                  /* HW dependent part.*/
50   0
51 };
52
53 // See http://jared.geek.nz/2013/feb/linear-led-pwm
54 static uint16_t cie_lightness(uint16_t v) {
55   if (v <= 5243) // if below 8% of max
56     return v / 9; // same as dividing by 900%
57   else {
58     uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
59     // to get a useful result with integer division, we shift left in the expression above
60     // and revert what we've done again after squaring.
61     y = y * y * y >> 8;
62     if (y > 0xFFFFUL) // prevent overflow
63       return 0xFFFFU;
64     else
65       return (uint16_t) y;
66   }
67 }
68
69
70 uint8_t get_backlight_level(void){
71   return kb_backlight_config.level;
72 }
73
74 void backlight_init_ports(void) {
75   printf("backlight_init_ports()\n");
76   palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(1));
77   pwmStart(&PWMD3, &pwmCFG);
78   // pwmEnableChannel(&PWMD3, 0, PWM_FRACTION_TO_WIDTH(&PWMD3, 0xFFFF,cie_lightness(0xFFFF)));
79   if(kb_backlight_config.enable){
80     backlight_set(kb_backlight_config.level);
81     if(kb_backlight_config.breathing){
82       breathing_enable();
83     }
84   } else {
85     backlight_set(0);
86   }
87
88 }
89
90 void backlight_set(uint8_t level) {
91     printf("backlight_set(%d)\n", level);
92     uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / BACKLIGHT_LEVELS));
93     printf("duty: (%d)\n", duty);
94     if (level == 0) {
95         // Turn backlight off
96         pwmDisableChannel(&PWMD3, 0);
97     } else {
98       // Turn backlight on
99       if(!is_breathing()){
100         pwmEnableChannel(&PWMD3, 0, PWM_FRACTION_TO_WIDTH(&PWMD3,0xFFFF,duty));
101       }
102     }
103 }
104
105
106 uint8_t backlight_tick = 0;
107
108 void backlight_task(void) {
109 }
110
111 #define BREATHING_NO_HALT  0
112 #define BREATHING_HALT_OFF 1
113 #define BREATHING_HALT_ON  2
114 #define BREATHING_STEPS 128
115
116 static uint8_t breathing_period = BREATHING_PERIOD;
117 static uint8_t breathing_halt = BREATHING_NO_HALT;
118 static uint16_t breathing_counter = 0;
119
120 bool is_breathing(void) {
121     return PWMD3.config == &pwmCFG_breathing;
122 }
123
124 #define breathing_min() do {breathing_counter = 0;} while (0)
125 #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0)
126
127
128 void breathing_interrupt_enable(void){
129     pwmStop(&PWMD3);
130     pwmStart(&PWMD3, &pwmCFG_breathing);
131     chSysLockFromISR();
132     pwmEnablePeriodicNotification(&PWMD3);
133     pwmEnableChannelI(
134       &PWMD3,
135       0,
136       PWM_FRACTION_TO_WIDTH(
137         &PWMD3,
138         0xFFFF,
139         0xFFFF
140       )
141     );
142     chSysUnlockFromISR();
143 }
144
145 void breathing_interrupt_disable(void){
146     pwmStop(&PWMD3);
147     pwmStart(&PWMD3, &pwmCFG);
148 }
149
150 void breathing_enable(void)
151 {
152   breathing_counter = 0;
153   breathing_halt = BREATHING_NO_HALT;
154   breathing_interrupt_enable();
155 }
156
157 void breathing_pulse(void)
158 {
159     if (get_backlight_level() == 0)
160       breathing_min();
161     else
162       breathing_max();
163     breathing_halt = BREATHING_HALT_ON;
164     breathing_interrupt_enable();
165 }
166
167 void breathing_disable(void)
168 {
169     printf("breathing_disable()\n");
170     breathing_interrupt_disable();
171     // Restore backlight level
172     backlight_set(get_backlight_level());
173 }
174
175 void breathing_self_disable(void)
176 {
177   if (get_backlight_level() == 0)
178     breathing_halt = BREATHING_HALT_OFF;
179   else
180     breathing_halt = BREATHING_HALT_ON;
181 }
182
183 void breathing_toggle(void) {
184   if (is_breathing()){
185     printf("disable breathing\n");
186     breathing_disable();
187   } else {
188     printf("enable breathing\n");
189     breathing_enable();
190   }
191 }
192
193 void breathing_period_set(uint8_t value)
194 {
195   if (!value)
196     value = 1;
197   breathing_period = value;
198 }
199
200 void breathing_period_default(void) {
201   breathing_period_set(BREATHING_PERIOD);
202 }
203
204 void breathing_period_inc(void)
205 {
206   breathing_period_set(breathing_period+1);
207 }
208
209 void breathing_period_dec(void)
210 {
211   breathing_period_set(breathing_period-1);
212 }
213
214 /* To generate breathing curve in python:
215  * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
216  */
217 static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
218
219 // Use this before the cie_lightness function.
220 static inline uint16_t scale_backlight(uint16_t v) {
221   return v / BACKLIGHT_LEVELS * get_backlight_level();
222 }
223
224 static void breathing_callback(PWMDriver *pwmp)
225 {
226   (void)pwmp;
227   uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS;
228   // resetting after one period to prevent ugly reset at overflow.
229   breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
230   uint8_t index = breathing_counter / interval % BREATHING_STEPS;
231
232   if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
233       ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
234   {
235       breathing_interrupt_disable();
236   }
237
238   uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
239
240   chSysLockFromISR();
241   pwmEnableChannelI(
242     &PWMD3,
243     0,
244     PWM_FRACTION_TO_WIDTH(
245       &PWMD3,
246       0xFFFF,
247       duty
248     )
249   );
250   chSysUnlockFromISR();
251 }