forked from ogrodnek/am-nametag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnametag.c
193 lines (162 loc) · 2.78 KB
/
nametag.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
void setup(void);
void loop(void);
void sleep(void);
void blink(void);
void flash(void);
void fade(void);
void pwm(void);
void allOn(void);
#define MAX_PROGRAMS 5
uint8_t EEMEM ProgramConfig = 0;
int program = 0;
unsigned long clock = 0;
int main(void) {
setup();
program = eeprom_read_byte(&ProgramConfig);
if (program >= MAX_PROGRAMS) {
program = 0;
}
eeprom_write_byte(&ProgramConfig, program + 1);
while (1) {
loop();
}
return 0;
}
void setup() {
DDRB = 255; // all output
PORTB = 0;
}
const uint8_t states[] = {
0b00001000, // C
0b00010000, // O
0b00000100, // S
0b00000001, // I
0b00000010, // N
0b00000001, // I
0b00000100, // S
0b00010000, // O
0b00001000, // C
0b00000000, //
0b00011111, // COSIN
0b00000000, //
0b00011111, // COSIN
0b00000000, //
0b00011000, // CO
0b00000111, // SIN
0b00000000, //
0b00001000, // C
0b00011000, // CO
0b00011100, // COS
0b00011101, // COSI
0b00011111, // COSIN
0b00011101, // COSI
0b00011100, // COS
0b00011000, // CO
0b00001000, // C
0b00000000, //
0b00001010, // C N
0b00011011, // CO IN
0b00011111, // COSIN
0b00011011, // CO IN
0b00001010, // C N
0b00000000, //
};
void loop() {
switch (program) {
case 0:
allOn();
break;
case 1:
blink();
break;
case 2:
fade();
break;
case 3:
flash();
break;
case 4:
sleep();
break;
default:
allOn();
}
}
void sleep() {
if (clock > 100000) {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
}
}
uint8_t blinkState = 0;
unsigned long lastBlink = 0;
void blink() {
if (clock - lastBlink > 12500) {
blinkState++;
if (blinkState > 32) {
blinkState = 0;
}
lastBlink = clock;
}
PORTB = states[blinkState];
clock++;
}
uint8_t flashState = 0;
void flash() {
if (clock - lastBlink > 30000) {
flashState = 1 - flashState;
lastBlink = clock;
}
if (flashState) {
blinkState++;
if (blinkState > 4) {
blinkState = 0;
}
PORTB=states[blinkState];
} else {
PORTB = 0;
}
clock++;
}
uint8_t level = 0;
uint8_t pwmCount = 0;
uint8_t dir = 1;
void fade() {
if (clock - lastBlink > 1800) {
lastBlink = clock;
level += dir;
if (level == 0 || level >= 200) {
dir = -1 * dir;
}
}
pwm();
clock++;
}
void pwm() {
static uint8_t pwmCnt = 0;
static uint8_t which = 0;
pwmCnt++;
if (level > pwmCnt) {
PORTB = states[which];
which++;
if (which > 4) {
which = 0;
}
} else {
PORTB = 0;
}
}
void allOn() {
if (clock - lastBlink > 2) {
blinkState++;
if (blinkState > 4) {
blinkState = 0;
}
lastBlink = clock;
}
PORTB = states[blinkState];
clock++;
}