aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/lightsaver/indicator_leds.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/duck/lightsaver/indicator_leds.c')
-rw-r--r--keyboards/duck/lightsaver/indicator_leds.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/keyboards/duck/lightsaver/indicator_leds.c b/keyboards/duck/lightsaver/indicator_leds.c
index 0a54e151e..5d2e1ad9f 100644
--- a/keyboards/duck/lightsaver/indicator_leds.c
+++ b/keyboards/duck/lightsaver/indicator_leds.c
@@ -18,17 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include <avr/io.h> 18#include <avr/io.h>
19#include <stdbool.h> 19#include <stdbool.h>
20#include <util/delay.h> 20#include <util/delay.h>
21#include "duck_led/duck_led.h"
21 22
22#define T1H 900 23#define LED_T1H 900
23#define T1L 600 24#define LED_T1L 600
24#define T0H 400 25#define LED_T0H 400
25#define T0L 900 26#define LED_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 27
33void send_bit_d4(bool bitVal) { 28void send_bit_d4(bool bitVal) {
34 if(bitVal) { 29 if(bitVal) {
@@ -44,8 +39,8 @@ void send_bit_d4(bool bitVal) {
44 :: 39 ::
45 [port] "I" (_SFR_IO_ADDR(PORTD)), 40 [port] "I" (_SFR_IO_ADDR(PORTD)),
46 [bit] "I" (4), 41 [bit] "I" (4),
47 [onCycles] "I" (NS_TO_CYCLES(T1H) - 2), 42 [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
48 [offCycles] "I" (NS_TO_CYCLES(T1L) - 2)); 43 [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
49 } else { 44 } else {
50 asm volatile ( 45 asm volatile (
51 "sbi %[port], %[bit] \n\t" 46 "sbi %[port], %[bit] \n\t"
@@ -59,33 +54,31 @@ void send_bit_d4(bool bitVal) {
59 :: 54 ::
60 [port] "I" (_SFR_IO_ADDR(PORTD)), 55 [port] "I" (_SFR_IO_ADDR(PORTD)),
61 [bit] "I" (4), 56 [bit] "I" (4),
62 [onCycles] "I" (NS_TO_CYCLES(T0H) - 2), 57 [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
63 [offCycles] "I" (NS_TO_CYCLES(T0L) - 2)); 58 [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
64 } 59 }
65} 60}
66 61
67void show(void) { 62void send_value(uint8_t byte, enum Device device) {
68 _delay_us((RES / 1000UL) + 1);
69}
70
71void send_value(uint8_t byte) {
72 for(uint8_t b = 0; b < 8; b++) { 63 for(uint8_t b = 0; b < 8; b++) {
73 send_bit_d4(byte & 0b10000000); 64 if(device == Device_STATUSLED) {
74 byte <<= 1; 65 send_bit_d4(byte & 0b10000000);
66 byte <<= 1;
67 }
75 } 68 }
76} 69}
77 70
78void send_color(uint8_t r, uint8_t g, uint8_t b) { 71void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device) {
79 send_value(g); 72 send_value(r, device);
80 send_value(r); 73 send_value(g, device);
81 send_value(b); 74 send_value(b, device);
82} 75}
83 76
84void indicator_leds_set(bool leds[8]) { 77void indicator_leds_set(bool leds[8]) {
85 cli(); 78 cli();
86 send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0); 79 send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0, Device_STATUSLED);
87 send_color(leds[4] ? 255 : 0, leds[5] ? 255 : 0, leds[3] ? 255 : 0); 80 send_color(leds[4] ? 255 : 0, leds[5] ? 255 : 0, leds[3] ? 255 : 0, Device_STATUSLED);
88 send_color(leds[6] ? 255 : 0, leds[7] ? 255 : 0, 0); 81 send_color(leds[6] ? 255 : 0, leds[7] ? 255 : 0, 0, Device_STATUSLED);
89 sei(); 82 sei();
90 show(); 83 show();
91} 84}