-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
72 lines (60 loc) · 1.4 KB
/
code.cpp
File metadata and controls
72 lines (60 loc) · 1.4 KB
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
#include <avr/io.h>
/*
1 1 1 1 1 1 1 1 : setup (carrier and width follow)
1|x x x x x x x : LSB out first, end before MSB zero
0|x x x x x x|y : X + 1 y
0 0 0 0 0 0 0 0 : end of stream
*/
extern char delay;
extern unsigned int shift;
signed char buffer[100], *ptr;
namespace
{
signed char *rptr;
signed char next() {
signed char c;
if (!TCB0.CTRLA) return 0; // forbid reading rptr
if (!rptr) return 0;
while ((c = *rptr) == -1) {
TCA0.SINGLE.CTRLA = TCB0.CTRLA = 0;
TCA0.SINGLE.CNT = TCB0.CNT = 0;
TCB0.CCMPL = rptr[1] - 1;
TCA0.SINGLE.PER = (int)rptr[2] * (int)rptr[1] - 1;
TCB0.CTRLA = TCB_ENABLE_bm;
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
rptr += 3;
}
rptr++;
if (!c) {
rptr = 0;
if (!delay) if (!shift) TCA0.SINGLE.CTRLA = 0; TCB0.CTRLA = 0;
}
return c;
}
} // anonymous
bool getDuty() {
static signed char x;
signed char y = x | 1;
if (y == -1) x = next();
y = x; // save bit
if (y < 0) x >>= 1;
else x -= 2;
return y & 1;
}
bool transmitting_() { return TCB0.CTRLA; }
void transmit_() {
*ptr++ = 126; *ptr++ = 126; *ptr = 0;
rptr = buffer;
TCB0.CTRLA = TCB_ENABLE_bm; // let go next()
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
}
namespace user
{
void wait(int i) {
while (transmitting_());
ptr = buffer;
while (i--) *ptr++ = 126;
*ptr = 0;
transmit_();
}
} // user