]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/eagle_viper/v2/indicator_leds.c
Putting my ducks in a row: Octagon V1/V2 (#3765)
[qmk_firmware.git] / keyboards / eagle_viper / 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 RES 6000
24
25 #define LED_T1H 600
26 #define LED_T1L 650
27 #define LED_T0H 250
28 #define LED_T0L 1000
29
30 #define NS_PER_SEC (1000000000L)
31 #define CYCLES_PER_SEC (F_CPU)
32 #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC)
33 #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE)
34
35 void send_bit_d4(bool bitVal) {
36   if(bitVal) {
37     asm volatile (
38         "sbi %[port], %[bit] \n\t"
39         ".rept %[onCycles] \n\t"
40         "nop \n\t"
41         ".endr \n\t"
42         "cbi %[port], %[bit] \n\t"
43         ".rept %[offCycles] \n\t"
44         "nop \n\t"
45         ".endr \n\t"
46         ::
47         [port]      "I" (_SFR_IO_ADDR(PORTD)),
48         [bit]       "I" (4),
49         [onCycles]  "I" (NS_TO_CYCLES(LED_T1H) - 2),
50         [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
51   } else {
52     asm volatile (
53         "sbi %[port], %[bit] \n\t"
54         ".rept %[onCycles] \n\t"
55         "nop \n\t"
56         ".endr \n\t"
57         "cbi %[port], %[bit] \n\t"
58         ".rept %[offCycles] \n\t"
59         "nop \n\t"
60         ".endr \n\t"
61         ::
62         [port]      "I" (_SFR_IO_ADDR(PORTD)),
63         [bit]       "I" (4),
64         [onCycles]  "I" (NS_TO_CYCLES(LED_T0H) - 2),
65         [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
66   }
67 }
68
69 void show(void) {
70   _delay_us((RES / 1000UL) + 1);
71 }
72
73 void send_value(uint8_t byte) {
74   for(uint8_t b = 0; b < 8; b++) {
75     send_bit_d4(byte & 0b10000000);
76     byte <<= 1;
77   }
78 }
79
80 // Send the LED indicators to the WS2811S chips
81 void indicator_leds_set(bool leds[8]) {
82   uint8_t led_cnt;
83
84   cli();
85   for(led_cnt = 0; led_cnt < 8; led_cnt++)
86     send_value(leds[led_cnt] ? 255 : 0);
87   sei();
88   show();
89 }