-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_attiny.cpp
149 lines (110 loc) · 4.6 KB
/
led_attiny.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
* SOURCE CODE BASED ON: https://github.com/bigjosh/SimpleNeoPixelDemo
********************************************************************************
* LICENSE:
* https://github.com/bigjosh/SimpleNeoPixelDemo/blob/master/licence.txt
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Josh Levine
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
//INCLUDE FILES
////////////////////////////////////////////////////////////////////////////////
#include <arduino.h>
#include "led.h"
////////////////////////////////////////////////////////////////////////////////
//THIS FILE ONLY APPLIES TO THE ATTINY AND DIGISPARK AVR BOARDS
////////////////////////////////////////////////////////////////////////////////
#if defined ARDUINO_attiny || defined ARDUINO_AVR_DIGISPARK
////////////////////////////////////////////////////////////////////////////////
//BIT CALCULATIONS
////////////////////////////////////////////////////////////////////////////////
#define T1H 900 // Width of a 1 bit in ns
#define T1L 600 // Width of a 1 bit in ns
#define T0H 400 // Width of a 0 bit in ns
#define T0L 900 // Width of a 0 bit in ns
#define RES 6000 // Width of the low gap between bits to cause a frame to latch
#define NS_PER_SEC (1000000000L)
#define CYCLES_PER_SEC (F_CPU)
#define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC)
#define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE)
////////////////////////////////////////////////////////////////////////////////
// DYNAMIC CLOCK CALCULATIONS
// NOT NEEDED, THIS MODULE USES STATIC CALCULATIONS FOR PERFORMANCE REASONS
// THERE ISN'T ENOUGH CLOCK CYCLES TO CALCULATE THIS ON THE ATTINY CHIPS
////////////////////////////////////////////////////////////////////////////////
void led::clock() {}
////////////////////////////////////////////////////////////////////////////////
// BITBANG SEND A SINGLE BIT TO THE LED STRIP
////////////////////////////////////////////////////////////////////////////////
static INLINE void led_send_bit(const bool value) {
if (value) {
asm volatile (
"sbi %[port], %[bit] \n\t"
".rept %[onCycles] \n\t"
"nop \n\t"
".endr \n\t"
"cbi %[port], %[bit] \n\t"
".rept %[offCycles] \n\t"
"nop \n\t"
".endr \n\t"
::
[port] "I" (_SFR_IO_ADDR(PORTB)),
[bit] "I" (4),
[onCycles] "I" (NS_TO_CYCLES(T1H) - 2),
[offCycles] "I" (NS_TO_CYCLES(T1L) - 2)
);
} else {
asm volatile (
"sbi %[port], %[bit] \n\t"
".rept %[onCycles] \n\t"
"nop \n\t"
".endr \n\t"
"cbi %[port], %[bit] \n\t"
".rept %[offCycles] \n\t"
"nop \n\t"
".endr \n\t"
::
[port] "I" (_SFR_IO_ADDR(PORTB)),
[bit] "I" (4),
[onCycles] "I" (NS_TO_CYCLES(T0H) - 2),
[offCycles] "I" (NS_TO_CYCLES(T0L) - 2)
);
}
}
////////////////////////////////////////////////////////////////////////////////
// SEND A SINGLE BYTE TO THE LED STRIP
////////////////////////////////////////////////////////////////////////////////
static INLINE void led_send_byte(const uint8_t byte) {
for (int8_t bit=7; bit>=0; bit--) {
led_send_bit(bitRead(byte, bit));
}
}
////////////////////////////////////////////////////////////////////////////////
// SEND A FULL PIXEL TO THE LED STRIP
////////////////////////////////////////////////////////////////////////////////
void led::pixel(const color_t &color) {
led_send_byte(color.g);
led_send_byte(color.r);
led_send_byte(color.b);
}
#endif // ARDUINO_attiny || ARDUINO_AVR_DIGISPARK