-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
148 lines (132 loc) · 3.14 KB
/
main.c
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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define BAUD 38400
#include <util/setbaud.h>
typedef struct __attribute__((packed)) {
uint8_t green;
uint8_t red;
uint8_t blue;
} color;
extern void display(color* pixels);
static color pixelSpinner[] = {
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x05 },
{ 0x00, 0x00, 0x25 },
{ 0x00, 0x00, 0x45 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x05 },
{ 0x00, 0x00, 0x25 },
};
static color off[] = {
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
};
static color on[] = {
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
{ 0xFF, 0xff, 0xff },
};
void ring()
{
PORTA.OUTSET = PIN7_bm;
// see bottom of function
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_FRQ_gc;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm;
uint8_t loc = 7;
for (int i = 0; i < 3 * 8; i++) {
display(pixelSpinner + loc);
loc = (loc + 1) % 8;
_delay_ms(100);
}
display(off);
PORTA.OUTCLR = PIN7_bm;
TCA0.SINGLE.CTRLB = 0; // turn PWM override off, so we don't kill the piezo if it stays at 1
TCA0.SINGLE.CTRLA = 0; // powersaving
}
static color dyn[] = {
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
};
static uint8_t cur = 0xa;
static uint8_t dir = 1;
void breathe()
{
cur = cur + dir;
for (int i = 0; i < 8; i++) {
uint8_t downscale = cur / 2;
dyn[i].red = cur;
dyn[i].green = downscale < 5 ? 5 : downscale;
dyn[i].blue = 0; // downscale < 5 ? 5 : downscale;
}
display(dyn);
if (cur == 0x30) {
dir = -1;
} else if (cur == 0xa) {
dir = 1;
}
for (int i = 0; i < 130 - cur; i++) {
_delay_ms(1);
}
}
inline void uart_setup()
{
USART0.CTRLA = USART_DREIE_bm;
USART0.CTRLB = USART_TXEN_bm;
USART0.CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_CHSIZE_8BIT_gc;
USART0.BAUDH = 0;
USART0.BAUDL = 116;
}
ISR(USART0_DRE_vect)
{
USART0.TXDATAL = 0x31;
}
uint8_t ring_timeout = 0;
int main(void)
{
PORTA.DIRSET = PIN4_bm | PIN7_bm;
PORTB.DIRSET = PIN0_bm | PIN1_bm | PIN2_bm;
TCA0.SINGLE.CMP0L = 22;
TCA0.SINGLE.CMP0H = 1;
uart_setup();
sei();
PORTB.OUTSET = PIN1_bm;
USART0.TXDATAL = 0x31;
while (1) {
if (ring_timeout) {
ring_timeout--;
} else {
if (VPORTC.IN & PIN3_bm) {
ring();
ring_timeout = 0x60;
}
}
breathe();
}
}