Skip to content

Commit 909d483

Browse files
authored
Merge branch 'master' into master
2 parents a84e197 + 4879082 commit 909d483

File tree

87 files changed

+5776
-20203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+5776
-20203
lines changed

2020_shake/.m4.test.only

Whitespace-only changes.

2020_shake/2020.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define BITMAP_WIDTH 64
2+
#define BITMAP_HEIGHT 32
3+
const uint8_t PROGMEM bitmap_2020[] = {
4+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6+
0x0f, 0xe0, 0x0f, 0xe0, 0x0f, 0xe0, 0x0f, 0xe0, 0x1f, 0xf8, 0x1f, 0xf0,
7+
0x1f, 0xf8, 0x1f, 0xf0, 0x3f, 0xfc, 0x3f, 0xf8, 0x3f, 0xfc, 0x3f, 0xf8,
8+
0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7c, 0x7e, 0x7c, 0x7c,
9+
0x7c, 0x7e, 0x7c, 0x7c, 0xf8, 0x3e, 0x7c, 0x3c, 0xf8, 0x3e, 0x7c, 0x3c,
10+
0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e,
11+
0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e, 0xf8, 0x3e,
12+
0x00, 0x3e, 0xf8, 0x3e, 0x00, 0x3e, 0xf8, 0x3e, 0x00, 0x7c, 0xf8, 0x3e,
13+
0x00, 0x7c, 0xf8, 0x3e, 0x00, 0x7c, 0xf8, 0x3e, 0x00, 0x7c, 0xf8, 0x3e,
14+
0x01, 0xf8, 0xf8, 0x3e, 0x01, 0xf8, 0xf8, 0x3e, 0x03, 0xf0, 0xf8, 0x3e,
15+
0x03, 0xf0, 0xf8, 0x3e, 0x07, 0xe0, 0xf8, 0x3e, 0x07, 0xe0, 0xf8, 0x3e,
16+
0x0f, 0xc0, 0xf8, 0x3e, 0x0f, 0xc0, 0xf8, 0x3e, 0x1f, 0x80, 0xf8, 0x3e,
17+
0x1f, 0x80, 0xf8, 0x3e, 0x1f, 0x00, 0xf8, 0x3e, 0x1f, 0x00, 0xf8, 0x3e,
18+
0x3e, 0x00, 0xf8, 0x3e, 0x3e, 0x00, 0xf8, 0x3e, 0x7c, 0x00, 0x78, 0x7c,
19+
0x7c, 0x00, 0x78, 0x7c, 0x7c, 0x00, 0x7c, 0x7c, 0x7c, 0x00, 0x7c, 0x7c,
20+
0xff, 0xfe, 0x7f, 0xfc, 0xff, 0xfe, 0x7f, 0xfc, 0xff, 0xfe, 0x3f, 0xf8,
21+
0xff, 0xfe, 0x3f, 0xf8, 0xff, 0xfe, 0x3f, 0xf0, 0xff, 0xfe, 0x3f, 0xf0,
22+
0xff, 0xfe, 0x0f, 0xe0, 0xff, 0xfe, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00,
23+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25+
0x00, 0x00, 0x00, 0x00 };

2020_shake/2020_shake.ino

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include <Adafruit_LIS3DH.h> // For accelerometer
2+
#include <Adafruit_PixelDust.h> // For simulation
3+
#include <Adafruit_Protomatter.h> // For LED matrix
4+
#include "2020.h" // 2020 bitmap data
5+
#include "2021.h" // 2021 bitmap data
6+
7+
bool show_2021 = true;
8+
9+
#define SHAKE_ACCEL_G 2.9 // Force (in Gs) to trigger shake
10+
#define SHAKE_ACCEL_MS2 (SHAKE_ACCEL_G * 9.8) // Convert to m/s^2
11+
#define SHAKE_ACCEL_SQ (SHAKE_ACCEL_MS2 * SHAKE_ACCEL_MS2) // Avoid sqrt() in accel check
12+
#define SHAKE_EVENTS 30 // Number of accel readings to trigger sand
13+
#define SHAKE_PERIOD 2000 // Period (in ms) when SHAKE_EVENTS must happen
14+
#define SAND_TIME 6000 // Time (in ms) to run simulation before restarting
15+
16+
uint8_t rgbPins[] = {7, 8, 9, 10, 11, 12};
17+
uint8_t addrPins[] = {17, 18, 19, 20};
18+
uint8_t clockPin = 14;
19+
uint8_t latchPin = 15;
20+
uint8_t oePin = 16;
21+
22+
// 64x32 pixel matrix, 6-bit depth
23+
Adafruit_Protomatter matrix(
24+
64, 6, 1, rgbPins, 4, addrPins, clockPin, latchPin, oePin, true);
25+
26+
Adafruit_LIS3DH accel = Adafruit_LIS3DH(); // Accelerometer
27+
28+
#define MAX_FPS 60 // Maximum redraw rate, frames/second
29+
uint32_t prevTime = 0; // For frames-per-second throttle
30+
uint16_t n_grains = 0; // Number of sand grains (counted on startup)
31+
Adafruit_PixelDust *sand; // Sand object (allocated in setup())
32+
33+
// Error handler used by setup()
34+
void err(int x) {
35+
uint8_t i;
36+
pinMode(LED_BUILTIN, OUTPUT); // Using onboard LED
37+
for(i=1;;i++) { // Loop forever...
38+
digitalWrite(LED_BUILTIN, i & 1); // LED on/off blink to alert user
39+
delay(x);
40+
}
41+
}
42+
43+
// SETUP - RUNS ONCE AT PROGRAM START --------------------------------------
44+
45+
void setup(void) {
46+
uint8_t i, j, bytes;
47+
Serial.begin(115200);
48+
//while (!Serial);
49+
50+
ProtomatterStatus status = matrix.begin();
51+
Serial.printf("Protomatter begin() status: %d\n", status);
52+
53+
// Count number of 'on' pixels (sand grains) in bitmap_2020
54+
for (int i=0; i<sizeof(bitmap_2020); i++) {
55+
for (int b=0; b<8; b++) {
56+
if (bitmap_2020[i] & (1 << b)) {
57+
n_grains++;
58+
}
59+
}
60+
}
61+
Serial.printf("Bitmap has %d grains\n", n_grains);
62+
63+
// Allocate sand object based on matrix size and bitmap 'on' pixels
64+
sand = new Adafruit_PixelDust(matrix.width(), matrix.height(), n_grains, 1);
65+
if (!sand->begin()) {
66+
Serial.println("Couldn't start sand");
67+
err(1000); // Slow blink = malloc error
68+
}
69+
70+
if (!accel.begin(0x19)) {
71+
Serial.println("Couldn't find accelerometer");
72+
err(250); // Fast bink = I2C error
73+
}
74+
Serial.println("Accelerometer OK");
75+
accel.setRange(LIS3DH_RANGE_8_G);
76+
}
77+
78+
void loop() {
79+
Serial.print("Tick");
80+
uint16_t sandColor = show_2021 ? 0xF800 : 0xFFFF; // Red or white
81+
82+
// Set initial sand pixel positions and draw initial matrix state
83+
sand->clear();
84+
matrix.fillScreen(0);
85+
int grain = 0, pixel = 0; // Sand grain and pixel indices
86+
for (int i=0; i<sizeof(bitmap_2020); i++) {
87+
for (int b=0; b<8; b++, pixel++) {
88+
if (bitmap_2020[i] & (1 << (7-b))) {
89+
int x = pixel % BITMAP_WIDTH;
90+
int y = pixel / BITMAP_WIDTH;
91+
//Serial.printf("Set pixel %d @ (%d, %d)\n", grain, x, y);
92+
sand->setPosition(grain++, x, y);
93+
matrix.drawPixel(x, y, sandColor);
94+
}
95+
}
96+
}
97+
matrix.show();
98+
99+
// Wait for shake
100+
uint32_t first_event_time = millis() - SHAKE_PERIOD * 2, last_event_time = first_event_time;
101+
uint8_t num_events = 0;
102+
sensors_event_t event;
103+
for (;;) {
104+
uint32_t t = millis(); // Current time
105+
accel.getEvent(&event);
106+
float mag2 = event.acceleration.x * event.acceleration.x +
107+
event.acceleration.y * event.acceleration.y +
108+
event.acceleration.z * event.acceleration.z;
109+
if (mag2 >= SHAKE_ACCEL_SQ) { // Accel exceeds shake threshold
110+
if ((t - last_event_time) > SHAKE_PERIOD) { // Long time since last event?
111+
first_event_time = t; // Start of new count
112+
num_events = 1;
113+
} else if ((t - first_event_time) < SHAKE_PERIOD) { // Still in shake interval?
114+
if (++num_events >= SHAKE_EVENTS) { // Enough events?
115+
break;
116+
}
117+
}
118+
last_event_time = t;
119+
}
120+
}
121+
122+
// Run sand simulation for a few seconds
123+
uint32_t elapsed, sandStartTime = millis();
124+
125+
while((elapsed = (millis() - sandStartTime)) < SAND_TIME) {
126+
127+
// Limit the animation frame rate to MAX_FPS.
128+
uint32_t t;
129+
while(((t = micros()) - prevTime) < (1000000L / MAX_FPS));
130+
prevTime = t;
131+
132+
// Read accelerometer...
133+
sensors_event_t event;
134+
accel.getEvent(&event);
135+
136+
// Run one frame of the simulation
137+
sand->iterate(event.acceleration.x * 1024, event.acceleration.y * 1024, event.acceleration.z * 1024);
138+
139+
if (elapsed > SAND_TIME * 3 / 4) {
140+
float scale = 1.0 - (float)(elapsed - (SAND_TIME * 3 / 4)) / (float)(SAND_TIME / 4);
141+
if (scale < 0.0) scale = 0.0;
142+
else if (scale > 1.0) scale = 1.0;
143+
scale = pow(scale, 2.6);
144+
uint16_t rb = (int)(31.0 * scale + 0.5);
145+
uint16_t g = (int)(63.0 * scale + 0.5);
146+
if (show_2021)
147+
sandColor = (rb * 0b100000000000); // Just show red
148+
else
149+
sandColor = (rb * 0b100000000001) + (g << 5);
150+
}
151+
152+
// Update pixel data in LED driver
153+
matrix.fillScreen(0);
154+
dimension_t x, y;
155+
for(int i=0; i<n_grains ; i++) {
156+
sand->getPosition(i, &x, &y);
157+
matrix.drawPixel(x, y, sandColor);
158+
}
159+
matrix.show();
160+
}
161+
162+
// If the show_2021 flag is set, don't return to 2020 shake detect,
163+
// instead switch to sparkly '2021' display forever (reset to start over)
164+
if (show_2021) {
165+
uint16_t frame = 0;
166+
matrix.fillScreen(0);
167+
for(;;) {
168+
int pixel = 0;
169+
for (int i=0; i<sizeof(bitmap_2021); i++) {
170+
for (int b=0; b<8; b++, pixel++) {
171+
if (bitmap_2021[i] & (1 << (7-b))) {
172+
int x = pixel % BITMAP_WIDTH;
173+
int y = pixel / BITMAP_WIDTH;
174+
matrix.drawPixel(x, y, (random() & 1) ? ((((x - y + frame) / 8) & 1) ? 0xFFFF : 0x001F) : 0);
175+
}
176+
}
177+
}
178+
matrix.show();
179+
delay(18);
180+
frame++;
181+
}
182+
}
183+
}

2020_shake/2021.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const uint8_t PROGMEM bitmap_2021[] = {
2+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4+
0x01, 0xfc, 0x01, 0xfc, 0x01, 0xfc, 0x00, 0x78, 0x03, 0xff, 0x03, 0xfe,
5+
0x03, 0xff, 0x00, 0x78, 0x07, 0xff, 0x87, 0xff, 0x07, 0xff, 0x80, 0xf8,
6+
0x0f, 0xff, 0x8f, 0xff, 0x8f, 0xff, 0x81, 0xf8, 0x0f, 0x8f, 0xcf, 0x8f,
7+
0x8f, 0x8f, 0xdf, 0xf8, 0x1f, 0x07, 0xcf, 0x87, 0x9f, 0x07, 0xdf, 0xf8,
8+
0x1f, 0x07, 0xdf, 0x07, 0xdf, 0x07, 0xdf, 0xf8, 0x1f, 0x07, 0xdf, 0x07,
9+
0xdf, 0x07, 0xdf, 0xf8, 0x1f, 0x07, 0xdf, 0x07, 0xdf, 0x07, 0xc0, 0xf8,
10+
0x00, 0x07, 0xdf, 0x07, 0xc0, 0x07, 0xc0, 0xf8, 0x00, 0x0f, 0x9f, 0x07,
11+
0xc0, 0x0f, 0x80, 0xf8, 0x00, 0x0f, 0x9f, 0x07, 0xc0, 0x0f, 0x80, 0xf8,
12+
0x00, 0x3f, 0x1f, 0x07, 0xc0, 0x3f, 0x00, 0xf8, 0x00, 0x7e, 0x1f, 0x07,
13+
0xc0, 0x7e, 0x00, 0xf8, 0x00, 0xfc, 0x1f, 0x07, 0xc0, 0xfc, 0x00, 0xf8,
14+
0x01, 0xf8, 0x1f, 0x07, 0xc1, 0xf8, 0x00, 0xf8, 0x03, 0xf0, 0x1f, 0x07,
15+
0xc3, 0xf0, 0x00, 0xf8, 0x03, 0xe0, 0x1f, 0x07, 0xc3, 0xe0, 0x00, 0xf8,
16+
0x07, 0xc0, 0x1f, 0x07, 0xc7, 0xc0, 0x00, 0xf8, 0x0f, 0x80, 0x0f, 0x0f,
17+
0x8f, 0x80, 0x00, 0xf8, 0x0f, 0x80, 0x0f, 0x8f, 0x8f, 0x80, 0x00, 0xf8,
18+
0x1f, 0xff, 0xcf, 0xff, 0x9f, 0xff, 0xc0, 0xf8, 0x1f, 0xff, 0xc7, 0xff,
19+
0x1f, 0xff, 0xc0, 0xf8, 0x1f, 0xff, 0xc7, 0xfe, 0x1f, 0xff, 0xc0, 0xf8,
20+
0x1f, 0xff, 0xc1, 0xfc, 0x1f, 0xff, 0xc0, 0xf8, 0x00, 0x00, 0x00, 0x00,
21+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23+
0x00, 0x00, 0x00, 0x00 };

0 commit comments

Comments
 (0)