-
Notifications
You must be signed in to change notification settings - Fork 445
/
facemask.ino
181 lines (152 loc) · 3.61 KB
/
facemask.ino
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
/* Written guide by bennyd93 (https://github.com/bennyd93): https://github.com/hansjny/Natural-Nerd/blob/master/arduino/readme.md */
#include <SPI.h>
#include <SD.h>
#include <FastLED.h>
#define DATA_PIN 6
#define BUTTON_PIN 2
/* If LEDS_PER_SECTION is 1, the number of sections is your amount of LEDs */
#define ROWS 12
/* LED PIN */
#define LEDS_PER_ROW 30
#define NUM_LEDS LEDS_PER_ROW*ROWS
#define BRIGHTNESS_DIVIDER 3
//The CS pin on the SDCARD
#define CHIP_SELECT SS
CRGB leds[NUM_LEDS];
uint8_t stepsInAnimation = 0;
uint8_t currentStep = 1;
uint16_t bytesRead = 0;
File root;
File readFile;
volatile bool change_animation = false;
unsigned long last_interrupt;
char fname[12];
bool selectFile() {
File entry = root.openNextFile();
while(!entry) {
root.rewindDirectory();
entry.close();
entry = root.openNextFile();
}
bool found = false;
memcpy(&fname, entry.name(), strlen(entry.name()) + 1);
if (fname[strlen(fname) - 1] == 'N' && fname[strlen(fname) - 2] == 'I' && fname[strlen(fname) - 3] == 'B' && fname[strlen(fname) - 4] == '.') {
found = true;
}
entry.close();
return found;
}
void find_new_animation() {
int i = 0;
while(!selectFile()) {
if (i++ == 100) {
//This is ugly. It's here so it tries a few times before giving up.
break;
}
}
readFile.close();
readFile = SD.open(fname);
bytesRead = 0;
animationInit(readFile);
}
void button_click()
{
if (millis() - last_interrupt > 250) {
last_interrupt = millis();
change_animation = true;
}
}
void readNextStep(File *file)
{
uint16_t numColors;
file->read(&numColors, 2);
bytesRead += 2;
for (int i = 0; i < numColors; i++)
{
CRGB color;
uint16_t num_leds;
file->read(&color, 3);
bytesRead+= 3;
file->read(&num_leds, 2);
bytesRead += 2;
uint16_t led_idx;
for (int j = 0; j < num_leds; j++)
{
file->read(&led_idx, 2);
bytesRead += 2;
set_led(led_idx, CRGB(color.r / BRIGHTNESS_DIVIDER, color.g / BRIGHTNESS_DIVIDER, color.b / BRIGHTNESS_DIVIDER));
}
}
}
void animationInit(File dataFile) {
if (dataFile)
{
if (dataFile.available())
{
dataFile.read(&stepsInAnimation, 1);
bytesRead++;
readNextStep(&dataFile);
}
}
}
void led_clear()
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
}
}
/* This function is here to fix the issue with led strips
* going in a snake pattern, and not all starting on the same side,
* as well as fixing that the "top" of the led matrix, is the
* bottom when it was soldered. Basically flipping every second
* row horizontally, and flipping the entire thing vertically.
*/
void set_led(uint16_t i, CRGB color) {
uint8_t x;
uint8_t y;
y = (ROWS - 1) - (i / LEDS_PER_ROW);
x = i % LEDS_PER_ROW;
if (y % 2 == 0)
leds[y * LEDS_PER_ROW + x] = color;
else
leds[y * LEDS_PER_ROW + (LEDS_PER_ROW - 1) - x] = color;
}
void setup()
{
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
leds[0] = CRGB(255, 255, 255);
while (!SD.begin(CHIP_SELECT))
{
delay(1000);
}
root = SD.open("/");
find_new_animation();
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), button_click, FALLING);
}
void readNext(File f)
{
//Start from beginning
if (currentStep >= stepsInAnimation)
{
f.seek(1);
currentStep = 1;
bytesRead = 1;
}
else
{
currentStep++;
}
readNextStep(&f);
}
void loop() {
if (change_animation) {
find_new_animation();
change_animation = false;
}
led_clear();
readNext(readFile);
FastLED.show();
delay(100);
}