[Keyboard] Small Refactor of Duck boards (#5521)
[jackhill/qmk/firmware.git] / keyboards / duck / jetfire / indicator_leds.c
CommitLineData
e16b39f0
M
1/*
2Copyright 2016 Ralf Schmitt <ralf@bunkertor.net>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7This program is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11You should have received a copy of the GNU General Public License
12along 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>
cb2f2fd2 20#include "indicator_leds.h"
e16b39f0 21#include "quantum.h"
e16b39f0 22
cb2f2fd2
M
23#define LED_T1H 900
24#define LED_T1L 600
25#define LED_T0H 400
26#define LED_T0L 900
e16b39f0
M
27
28void send_bit_d4(bool bitVal)
29{
30 if(bitVal) {
31 asm volatile (
32 "sbi %[port], %[bit] \n\t"
33 ".rept %[onCycles] \n\t"
34 "nop \n\t"
35 ".endr \n\t"
36 "cbi %[port], %[bit] \n\t"
37 ".rept %[offCycles] \n\t"
38 "nop \n\t"
39 ".endr \n\t"
40 ::
41 [port] "I" (_SFR_IO_ADDR(PORTD)),
42 [bit] "I" (4),
cb2f2fd2
M
43 [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
44 [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
e16b39f0
M
45 } else {
46 asm volatile (
47 "sbi %[port], %[bit] \n\t"
48 ".rept %[onCycles] \n\t"
49 "nop \n\t"
50 ".endr \n\t"
51 "cbi %[port], %[bit] \n\t"
52 ".rept %[offCycles] \n\t"
53 "nop \n\t"
54 ".endr \n\t"
55 ::
56 [port] "I" (_SFR_IO_ADDR(PORTD)),
57 [bit] "I" (4),
cb2f2fd2
M
58 [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
59 [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
e16b39f0
M
60 }
61}
62
63void send_bit_d6(bool bitVal)
64{
65 if(bitVal) {
66 asm volatile (
67 "sbi %[port], %[bit] \n\t"
68 ".rept %[onCycles] \n\t"
69 "nop \n\t"
70 ".endr \n\t"
71 "cbi %[port], %[bit] \n\t"
72 ".rept %[offCycles] \n\t"
73 "nop \n\t"
74 ".endr \n\t"
75 ::
76 [port] "I" (_SFR_IO_ADDR(PORTD)),
77 [bit] "I" (6),
cb2f2fd2
M
78 [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
79 [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
e16b39f0
M
80 } else {
81 asm volatile (
82 "sbi %[port], %[bit] \n\t"
83 ".rept %[onCycles] \n\t"
84 "nop \n\t"
85 ".endr \n\t"
86 "cbi %[port], %[bit] \n\t"
87 ".rept %[offCycles] \n\t"
88 "nop \n\t"
89 ".endr \n\t"
90 ::
91 [port] "I" (_SFR_IO_ADDR(PORTD)),
92 [bit] "I" (6),
cb2f2fd2
M
93 [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
94 [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
e16b39f0
M
95 }
96}
97
e16b39f0
M
98void send_value(uint8_t byte, enum Device device)
99{
100 for(uint8_t b = 0; b < 8; b++) {
cb2f2fd2 101 if(device == Device_STATUSLED) {
e16b39f0
M
102 send_bit_d4(byte & 0b10000000);
103 }
104 if(device == Device_PCBRGB) {
105 send_bit_d6(byte & 0b10000000);
106 }
107 byte <<= 1;
108 }
109}
110
111void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device)
112{
e16b39f0 113 send_value(r, device);
cb2f2fd2 114 send_value(g, device);
e16b39f0
M
115 send_value(b, device);
116}