diff options
Diffstat (limited to 'keyboards/duck/lightsaver/indicator_leds.c')
-rw-r--r-- | keyboards/duck/lightsaver/indicator_leds.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/keyboards/duck/lightsaver/indicator_leds.c b/keyboards/duck/lightsaver/indicator_leds.c new file mode 100644 index 000000000..0a54e151e --- /dev/null +++ b/keyboards/duck/lightsaver/indicator_leds.c | |||
@@ -0,0 +1,91 @@ | |||
1 | /* | ||
2 | Copyright 2016-2017 Ralf Schmitt <ralf@bunkertor.net> Rasmus Schults <rasmusx@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 | #include <avr/interrupt.h> | ||
18 | #include <avr/io.h> | ||
19 | #include <stdbool.h> | ||
20 | #include <util/delay.h> | ||
21 | |||
22 | #define T1H 900 | ||
23 | #define T1L 600 | ||
24 | #define T0H 400 | ||
25 | #define T0L 900 | ||
26 | #define RES 6000 | ||
27 | |||
28 | #define NS_PER_SEC (1000000000L) | ||
29 | #define CYCLES_PER_SEC (F_CPU) | ||
30 | #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC) | ||
31 | #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE) | ||
32 | |||
33 | void send_bit_d4(bool bitVal) { | ||
34 | if(bitVal) { | ||
35 | asm volatile ( | ||
36 | "sbi %[port], %[bit] \n\t" | ||
37 | ".rept %[onCycles] \n\t" | ||
38 | "nop \n\t" | ||
39 | ".endr \n\t" | ||
40 | "cbi %[port], %[bit] \n\t" | ||
41 | ".rept %[offCycles] \n\t" | ||
42 | "nop \n\t" | ||
43 | ".endr \n\t" | ||
44 | :: | ||
45 | [port] "I" (_SFR_IO_ADDR(PORTD)), | ||
46 | [bit] "I" (4), | ||
47 | [onCycles] "I" (NS_TO_CYCLES(T1H) - 2), | ||
48 | [offCycles] "I" (NS_TO_CYCLES(T1L) - 2)); | ||
49 | } else { | ||
50 | asm volatile ( | ||
51 | "sbi %[port], %[bit] \n\t" | ||
52 | ".rept %[onCycles] \n\t" | ||
53 | "nop \n\t" | ||
54 | ".endr \n\t" | ||
55 | "cbi %[port], %[bit] \n\t" | ||
56 | ".rept %[offCycles] \n\t" | ||
57 | "nop \n\t" | ||
58 | ".endr \n\t" | ||
59 | :: | ||
60 | [port] "I" (_SFR_IO_ADDR(PORTD)), | ||
61 | [bit] "I" (4), | ||
62 | [onCycles] "I" (NS_TO_CYCLES(T0H) - 2), | ||
63 | [offCycles] "I" (NS_TO_CYCLES(T0L) - 2)); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | void show(void) { | ||
68 | _delay_us((RES / 1000UL) + 1); | ||
69 | } | ||
70 | |||
71 | void send_value(uint8_t byte) { | ||
72 | for(uint8_t b = 0; b < 8; b++) { | ||
73 | send_bit_d4(byte & 0b10000000); | ||
74 | byte <<= 1; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | void send_color(uint8_t r, uint8_t g, uint8_t b) { | ||
79 | send_value(g); | ||
80 | send_value(r); | ||
81 | send_value(b); | ||
82 | } | ||
83 | |||
84 | void indicator_leds_set(bool leds[8]) { | ||
85 | cli(); | ||
86 | send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0); | ||
87 | send_color(leds[4] ? 255 : 0, leds[5] ? 255 : 0, leds[3] ? 255 : 0); | ||
88 | send_color(leds[6] ? 255 : 0, leds[7] ? 255 : 0, 0); | ||
89 | sei(); | ||
90 | show(); | ||
91 | } | ||