]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/duck/octagon/v2/indicator_leds.c
c24509f514418d050bdcaf674d485eb4eeab2bc9
[qmk_firmware.git] / keyboards / duck / octagon / v2 / indicator_leds.c
1 /*
2 Copyright 2017 MechMerlin <mechmerlin@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 #include "indicator_leds.h"
22
23 #define T1H  900
24 #define T1L  600
25 #define T0H  400
26 #define T0L  900
27 #define RES 6000
28
29 #define NS_PER_SEC (1000000000L)
30 #define CYCLES_PER_SEC (F_CPU)
31 #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC)
32 #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE)
33
34 void send_bit_d4(bool bitVal) {
35   if(bitVal) {
36     asm volatile (
37         "sbi %[port], %[bit] \n\t"
38         ".rept %[onCycles] \n\t"
39         "nop \n\t"
40         ".endr \n\t"
41         "cbi %[port], %[bit] \n\t"
42         ".rept %[offCycles] \n\t"
43         "nop \n\t"
44         ".endr \n\t"
45         ::
46         [port]      "I" (_SFR_IO_ADDR(PORTD)),
47         [bit]       "I" (4),
48         [onCycles]  "I" (NS_TO_CYCLES(T1H) - 2),
49         [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
50   } else {
51     asm volatile (
52         "sbi %[port], %[bit] \n\t"
53         ".rept %[onCycles] \n\t"
54         "nop \n\t"
55         ".endr \n\t"
56         "cbi %[port], %[bit] \n\t"
57         ".rept %[offCycles] \n\t"
58         "nop \n\t"
59         ".endr \n\t"
60         ::
61         [port]      "I" (_SFR_IO_ADDR(PORTD)),
62         [bit]       "I" (4),
63         [onCycles]  "I" (NS_TO_CYCLES(T0H) - 2),
64         [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
65   }
66 }
67
68 void send_bit_d6(bool bitVal)
69 {
70   if(bitVal) {
71     asm volatile (
72         "sbi %[port], %[bit] \n\t"
73         ".rept %[onCycles] \n\t"
74         "nop \n\t"
75         ".endr \n\t"
76         "cbi %[port], %[bit] \n\t"
77         ".rept %[offCycles] \n\t"
78         "nop \n\t"
79         ".endr \n\t"
80         ::
81         [port]      "I" (_SFR_IO_ADDR(PORTD)),
82         [bit]       "I" (6),
83         [onCycles]  "I" (NS_TO_CYCLES(T1H) - 2),
84         [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
85   } else {
86     asm volatile (
87         "sbi %[port], %[bit] \n\t"
88         ".rept %[onCycles] \n\t"
89         "nop \n\t"
90         ".endr \n\t"
91         "cbi %[port], %[bit] \n\t"
92         ".rept %[offCycles] \n\t"
93         "nop \n\t"
94         ".endr \n\t"
95         ::
96         [port]      "I" (_SFR_IO_ADDR(PORTD)),
97         [bit]       "I" (6),
98         [onCycles]  "I" (NS_TO_CYCLES(T0H) - 2),
99         [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
100   }
101 }
102
103 void show(void) {
104   _delay_us((RES / 1000UL) + 1);
105 }
106
107 void send_value(uint8_t byte, enum Device device) {
108   for(uint8_t b = 0; b < 8; b++) {
109     if(device == Device_STATUSLED) {
110       send_bit_d4(byte & 0b10000000);
111     }
112     if(device == Device_PCBRGB) {
113       send_bit_d6(byte & 0b10000000);
114     }
115     byte <<= 1;
116   }
117 }
118
119 void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device) {
120   send_value(g, device);
121   send_value(r, device);
122   send_value(b, device);
123 }
124
125 // Port from backlight_set_state
126 void indicator_leds_set(bool leds[8]) {
127   cli();
128   send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0, Device_STATUSLED);
129   send_color(leds[4] ? 255 : 0, leds[3] ? 255 : 0, leds[5] ? 255 : 0, Device_STATUSLED);
130   leds[6] ? (PORTD &= ~0b10000000) : (PORTD |= 0b10000000);
131   sei();
132   show();
133 }