aboutsummaryrefslogtreecommitdiff
path: root/keyboards/handwired/co60/rev6/led.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/handwired/co60/rev6/led.c')
-rw-r--r--keyboards/handwired/co60/rev6/led.c240
1 files changed, 0 insertions, 240 deletions
diff --git a/keyboards/handwired/co60/rev6/led.c b/keyboards/handwired/co60/rev6/led.c
deleted file mode 100644
index fe28ce2e8..000000000
--- a/keyboards/handwired/co60/rev6/led.c
+++ /dev/null
@@ -1,240 +0,0 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "hal.h"
19#include "led_custom.h"
20#include "rev6.h"
21#include "printf.h"
22
23static void breathing_callback(PWMDriver *pwmp);
24
25static 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_DISABLED, NULL},
31 {PWM_OUTPUT_DISABLED, NULL},
32 {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */
33 {PWM_OUTPUT_DISABLED, NULL}
34 },
35 0, /* HW dependent part.*/
36 0
37};
38
39static 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_DISABLED, NULL},
45 {PWM_OUTPUT_DISABLED, NULL},
46 {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */
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
54static 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
69void backlight_init_ports(void) {
70 palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(2));
71 pwmStart(&PWMD4, &pwmCFG);
72 if (kb_backlight_config.enable) {
73 if (kb_backlight_config.breathing) {
74 breathing_enable();
75 } else {
76 backlight_set(kb_backlight_config.level);
77 }
78 } else {
79 backlight_set(0);
80 }
81}
82
83void backlight_set(uint8_t level) {
84 uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS));
85 if (level == 0) {
86 // Turn backlight off
87 // Disable channel 3 on PWM4
88 pwmDisableChannel(&PWMD4, 2);
89 } else {
90 // Turn backlight on
91 if (!is_breathing()) {
92 // Enable channel 3 on PWM4
93 pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4, 0xFFFF, duty));
94 }
95 }
96}
97
98uint8_t backlight_tick = 0;
99
100void backlight_task(void) {
101}
102
103#define BREATHING_NO_HALT 0
104#define BREATHING_HALT_OFF 1
105#define BREATHING_HALT_ON 2
106#define BREATHING_STEPS 128
107
108static uint8_t breathing_period = BREATHING_PERIOD;
109static uint8_t breathing_halt = BREATHING_NO_HALT;
110static uint16_t breathing_counter = 0;
111
112bool is_breathing(void) {
113 return PWMD4.config == &pwmCFG_breathing;
114}
115
116#define breathing_min() do {breathing_counter = 0;} while (0)
117#define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0)
118
119
120void breathing_interrupt_enable(void){
121 pwmStop(&PWMD4);
122 pwmStart(&PWMD4, &pwmCFG_breathing);
123 chSysLockFromISR();
124 pwmEnablePeriodicNotification(&PWMD4);
125 pwmEnableChannelI(
126 &PWMD4,
127 2,
128 PWM_FRACTION_TO_WIDTH(
129 &PWMD4,
130 0xFFFF,
131 0xFFFF
132 )
133 );
134 chSysUnlockFromISR();
135}
136
137void breathing_interrupt_disable(void){
138 pwmStop(&PWMD4);
139 pwmStart(&PWMD4, &pwmCFG);
140}
141
142void breathing_enable(void)
143{
144 breathing_counter = 0;
145 breathing_halt = BREATHING_NO_HALT;
146 breathing_interrupt_enable();
147}
148
149void breathing_pulse(void)
150{
151 if (kb_backlight_config.level == 0)
152 breathing_min();
153 else
154 breathing_max();
155 breathing_halt = BREATHING_HALT_ON;
156 breathing_interrupt_enable();
157}
158
159void breathing_disable(void)
160{
161 breathing_interrupt_disable();
162 // Restore backlight level
163 backlight_set(kb_backlight_config.level);
164}
165
166void breathing_self_disable(void)
167{
168 if (kb_backlight_config.level == 0)
169 breathing_halt = BREATHING_HALT_OFF;
170 else
171 breathing_halt = BREATHING_HALT_ON;
172}
173
174void breathing_toggle(void) {
175 if (is_breathing()){
176 breathing_disable();
177 } else {
178 breathing_enable();
179 }
180}
181
182void breathing_period_set(uint8_t value)
183{
184 if (!value)
185 value = 1;
186 breathing_period = value;
187}
188
189void breathing_period_default(void) {
190 breathing_period_set(BREATHING_PERIOD);
191}
192
193void breathing_period_inc(void)
194{
195 breathing_period_set(breathing_period+1);
196}
197
198void breathing_period_dec(void)
199{
200 breathing_period_set(breathing_period-1);
201}
202
203/* To generate breathing curve in python:
204 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
205 */
206static 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};
207
208// Use this before the cie_lightness function.
209static inline uint16_t scale_backlight(uint16_t v) {
210 return v / BACKLIGHT_LEVELS * kb_backlight_config.level;
211}
212
213static void breathing_callback(PWMDriver *pwmp)
214{
215 (void)pwmp;
216 uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS;
217 // resetting after one period to prevent ugly reset at overflow.
218 breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
219 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
220
221 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
222 ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
223 {
224 breathing_interrupt_disable();
225 }
226
227 uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
228
229 chSysLockFromISR();
230 pwmEnableChannelI(
231 &PWMD4,
232 2,
233 PWM_FRACTION_TO_WIDTH(
234 &PWMD4,
235 0xFFFF,
236 duty
237 )
238 );
239 chSysUnlockFromISR();
240}