-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_year_lightning.ino
132 lines (107 loc) · 2.55 KB
/
new_year_lightning.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
#include "SnakeGame.hpp"
#include "snowfall.hpp"
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 100
CRGB leds[NUM_LEDS];
void setup()
{
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 150);
FastLED.clear();
FastLED.show();
randomSeed(analogRead(5));
}
namespace
{
int8_t const COLS = 10;
int8_t const ROWS = 10;
int const SNOWFALL_ITERATIONS = 240;
int const RANDOM_LIGHTS_ITERATIONS = 2400;
int const FIRE_ITERATIONS = 240;
auto BLUE_COLOR = CRGB(0, 0, 255);
auto GREEN_COLOR = CRGB(0, 255, 0);
auto RED_COLOR = CRGB(255, 0, 0);
auto WHITE_COLOR = CRGB(255, 255, 255);
auto NONE_COLOR = CRGB(0, 0, 0);
} // namespace
int convert_to_local(int i)
{
int left = (i / COLS * COLS);
int right = (i / COLS + 1) * COLS;
int index = right - i - 1;
return (i / COLS % 2) ? i : left + index;
}
void show(SnakeGame::GameObjects const* const grid)
{
for (size_t i = 0; i < NUM_LEDS; ++i)
{
auto const target = convert_to_local(i);
switch (grid[i])
{
case SnakeGame::GameObjects::EMPTY:
leds[target] = NONE_COLOR;
break;
case SnakeGame::GameObjects::HEAD:
leds[target] = BLUE_COLOR;
break;
case SnakeGame::GameObjects::BODY:
leds[target] = GREEN_COLOR;
break;
case SnakeGame::GameObjects::SHEEP:
leds[target] = RED_COLOR;
break;
}
}
}
void play_snake()
{
SnakeGame::GameObjects grid[NUM_LEDS];
SnakeGame snake_game{COLS, ROWS};
while (!snake_game.isOver())
{
FastLED.clear();
snake_game.draw(grid);
show(grid);
FastLED.show();
delay(500);
snake_game.update();
}
}
void show(bool* grid)
{
for (size_t i = 0; i < NUM_LEDS; ++i)
{
auto const target = convert_to_local(i);
leds[target] = grid[i] ? WHITE_COLOR : NONE_COLOR;
}
}
void run_snowfall()
{
bool grid[NUM_LEDS] = {false};
for (size_t count = 0; count != SNOWFALL_ITERATIONS; ++count)
{
snowfall::simulate_snowfall(grid, COLS, ROWS);
show(grid);
FastLED.show();
delay(500);
}
}
void run_random_lights()
{
for (size_t count = 0; count != RANDOM_LIGHTS_ITERATIONS; ++count)
{
leds[random(NUM_LEDS)] = CRGB(random(256), random(256), random(256));
FastLED.show();
delay(50);
}
}
void loop()
{
while (true)
{
run_random_lights();
run_snowfall();
play_snake();
}
}