[Keyboard] Quantrik Kyuu 65% Board (#5541)
[jackhill/qmk/firmware.git] / keyboards / duck / jetfire / backlight_led.c
1 /*
2 Copyright 2016 Ralf Schmitt <ralf@bunkertor.net>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11 You should have received a copy of the GNU General Public License
12 along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14
15 #include <avr/interrupt.h>
16 #include <avr/io.h>
17 #include <stdbool.h>
18 #include <util/delay.h>
19 #include <stdint.h>
20 #include "backlight_led.h"
21 #include "quantum.h"
22 // #include "led.h"
23
24
25 #define T1H 900
26 #define T1L 600
27 #define T0H 400
28 #define T0L 900
29 #define RES 6000
30
31 #define NS_PER_SEC (1000000000L)
32 #define CYCLES_PER_SEC (F_CPU)
33 #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC)
34 #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE)
35
36 void send_bit_d4(bool bitVal)
37 {
38 if(bitVal) {
39 asm volatile (
40 "sbi %[port], %[bit] \n\t"
41 ".rept %[onCycles] \n\t"
42 "nop \n\t"
43 ".endr \n\t"
44 "cbi %[port], %[bit] \n\t"
45 ".rept %[offCycles] \n\t"
46 "nop \n\t"
47 ".endr \n\t"
48 ::
49 [port] "I" (_SFR_IO_ADDR(PORTD)),
50 [bit] "I" (4),
51 [onCycles] "I" (NS_TO_CYCLES(T1H) - 2),
52 [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
53 } else {
54 asm volatile (
55 "sbi %[port], %[bit] \n\t"
56 ".rept %[onCycles] \n\t"
57 "nop \n\t"
58 ".endr \n\t"
59 "cbi %[port], %[bit] \n\t"
60 ".rept %[offCycles] \n\t"
61 "nop \n\t"
62 ".endr \n\t"
63 ::
64 [port] "I" (_SFR_IO_ADDR(PORTD)),
65 [bit] "I" (4),
66 [onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
67 [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
68 }
69 }
70
71 void send_bit_d6(bool bitVal)
72 {
73 if(bitVal) {
74 asm volatile (
75 "sbi %[port], %[bit] \n\t"
76 ".rept %[onCycles] \n\t"
77 "nop \n\t"
78 ".endr \n\t"
79 "cbi %[port], %[bit] \n\t"
80 ".rept %[offCycles] \n\t"
81 "nop \n\t"
82 ".endr \n\t"
83 ::
84 [port] "I" (_SFR_IO_ADDR(PORTD)),
85 [bit] "I" (6),
86 [onCycles] "I" (NS_TO_CYCLES(T1H) - 2),
87 [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
88 } else {
89 asm volatile (
90 "sbi %[port], %[bit] \n\t"
91 ".rept %[onCycles] \n\t"
92 "nop \n\t"
93 ".endr \n\t"
94 "cbi %[port], %[bit] \n\t"
95 ".rept %[offCycles] \n\t"
96 "nop \n\t"
97 ".endr \n\t"
98 ::
99 [port] "I" (_SFR_IO_ADDR(PORTD)),
100 [bit] "I" (6),
101 [onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
102 [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
103 }
104 }
105
106 void show(void)
107 {
108 _delay_us((RES / 1000UL) + 1);
109 }
110
111 void send_value(uint8_t byte, enum Device device)
112 {
113 for(uint8_t b = 0; b < 8; b++) {
114 if(device == Device_STATELED) {
115 send_bit_d4(byte & 0b10000000);
116 }
117 if(device == Device_PCBRGB) {
118 send_bit_d6(byte & 0b10000000);
119 }
120 byte <<= 1;
121 }
122 }
123
124 void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device)
125 {
126 send_value(g, device);
127 send_value(r, device);
128 send_value(b, device);
129 }