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/>.
15 #include <avr/interrupt.h>
18 #include <util/delay.h>
20 #include "backlight_led.h"
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)
36 void send_bit_d4(bool bitVal)
40 "sbi %[port], %[bit] \n\t"
41 ".rept %[onCycles] \n\t"
44 "cbi %[port], %[bit] \n\t"
45 ".rept %[offCycles] \n\t"
49 [port] "I" (_SFR_IO_ADDR(PORTD)),
51 [onCycles] "I" (NS_TO_CYCLES(T1H) - 2),
52 [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
55 "sbi %[port], %[bit] \n\t"
56 ".rept %[onCycles] \n\t"
59 "cbi %[port], %[bit] \n\t"
60 ".rept %[offCycles] \n\t"
64 [port] "I" (_SFR_IO_ADDR(PORTD)),
66 [onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
67 [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
71 void send_bit_d6(bool bitVal)
75 "sbi %[port], %[bit] \n\t"
76 ".rept %[onCycles] \n\t"
79 "cbi %[port], %[bit] \n\t"
80 ".rept %[offCycles] \n\t"
84 [port] "I" (_SFR_IO_ADDR(PORTD)),
86 [onCycles] "I" (NS_TO_CYCLES(T1H) - 2),
87 [offCycles] "I" (NS_TO_CYCLES(T1L) - 2));
90 "sbi %[port], %[bit] \n\t"
91 ".rept %[onCycles] \n\t"
94 "cbi %[port], %[bit] \n\t"
95 ".rept %[offCycles] \n\t"
99 [port] "I" (_SFR_IO_ADDR(PORTD)),
101 [onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
102 [offCycles] "I" (NS_TO_CYCLES(T0L) - 2));
108 _delay_us((RES / 1000UL) + 1);
111 void send_value(uint8_t byte, enum Device device)
113 for(uint8_t b = 0; b < 8; b++) {
114 if(device == Device_STATELED) {
115 send_bit_d4(byte & 0b10000000);
117 if(device == Device_PCBRGB) {
118 send_bit_d6(byte & 0b10000000);
124 void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device)
126 send_value(g, device);
127 send_value(r, device);
128 send_value(b, device);