Skip to content

Commit 3b2ecd6

Browse files
committed
Add more things to the renderheap to save memory (fire and attract mode)
1 parent bb9df82 commit 3b2ecd6

4 files changed

Lines changed: 34 additions & 65 deletions

File tree

attract.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,27 @@
33
*/
44

55
#include "effects.h"
6+
#include "renderheap.h"
67

7-
#define POS_PER_PIXEL (32768 / STRIP_LENGTH) // ratio of attract mode pixel-positions to actual LEDs
88

9-
static CRGB dot_colors[ATTRACT_MODE_DOTS];
10-
static uint32_t dot_pos[ATTRACT_MODE_DOTS];
11-
static uint16_t dot_speeds[ATTRACT_MODE_DOTS];
12-
static uint8_t dot_age[ATTRACT_MODE_DOTS];
9+
#define POS_PER_PIXEL (32768 / STRIP_LENGTH) // ratio of attract mode pixel-positions to actual LEDs
1310

1411
static void make_new_dot(uint8_t dot) {
15-
dot_colors[dot] = Wheel(random8());
16-
dot_pos[dot] = random16(10000); // 32768 * 0.2 (so pixels dont start at the very end of the strip)
17-
dot_speeds[dot] = random16(POS_PER_PIXEL/17,POS_PER_PIXEL/5);
18-
dot_age[dot] = 0;
12+
r.am.dot_colors[dot] = Wheel(random8());
13+
r.am.dot_pos[dot] = random16(10000); // 32768 * 0.2 (so pixels dont start at the very end of the strip)
14+
r.am.dot_speeds[dot] = random16(POS_PER_PIXEL/17,POS_PER_PIXEL/5);
15+
r.am.dot_age[dot] = 0;
1916
}
2017

2118
static void calculate_overtakes(uint8_t dot) {
2219
for(uint8_t i = 0; i < ATTRACT_MODE_DOTS; i++) {
23-
if((dot_pos[dot] < dot_pos[i]) && (dot_pos[dot] + dot_speeds[dot]) >= dot_pos[i]) {
20+
if((r.am.dot_pos[dot] < r.am.dot_pos[i]) && (r.am.dot_pos[dot] + r.am.dot_speeds[dot]) >= r.am.dot_pos[i]) {
2421
// i will overtake j on next render. Let's make them collide!
25-
uint8_t r = ((uint8_t)(dot_colors[i].r) + (uint8_t)(dot_colors[dot].r)) >> 1;
26-
uint8_t g = ((uint8_t)(dot_colors[i].g) + (uint8_t)(dot_colors[dot].g)) >> 1;
27-
uint8_t b = ((uint8_t)(dot_colors[i].b) + (uint8_t)(dot_colors[dot].b)) >> 1;
28-
dot_colors[i] = CRGB(r,g,b);
29-
dot_colors[dot] = dot_colors[i];
22+
uint8_t new_r = blend8(r.am.dot_colors[i].r, r.am.dot_colors[dot].r, 127);
23+
uint8_t new_g = blend8(r.am.dot_colors[i].g, r.am.dot_colors[dot].g, 127);
24+
uint8_t new_b = blend8(r.am.dot_colors[i].b, r.am.dot_colors[dot].b, 127);
25+
r.am.dot_colors[i] = CRGB(new_r,new_g,new_b);
26+
r.am.dot_colors[dot] = r.am.dot_colors[i];
3027
}
3128
}
3229
}
@@ -50,33 +47,33 @@ void render_attract() {
5047
// this loop scales the dots from their "native res" (0-32768) antialiased to the LED strip (0-60)
5148
// and keeps track of which LED pixels have actually been rendered to (which is 2x number of dots)
5249
for(uint8_t dot=0; dot < ATTRACT_MODE_DOTS; dot++) {
53-
if(dot_pos[dot] >= 32768) {
50+
if(r.am.dot_pos[dot] >= 32768) {
5451
make_new_dot(dot);
5552
}
5653
// draw adjusted to strip (trying to do anti-aliasing)
5754
// ratio of pixel in left or right pixels
58-
uint16_t led = dot_pos[dot] / POS_PER_PIXEL;
59-
leds_offsets[dot] = (dot_pos[dot] % (POS_PER_PIXEL));
55+
uint16_t led = r.am.dot_pos[dot] / POS_PER_PIXEL;
56+
leds_offsets[dot] = (r.am.dot_pos[dot] % (POS_PER_PIXEL));
6057
calculate_overtakes(dot);
61-
dot_pos[dot] += dot_speeds[dot];
62-
if(dot_age[dot] < 255) dot_age[dot]++;
58+
r.am.dot_pos[dot] += r.am.dot_speeds[dot];
59+
if(r.am.dot_age[dot] < 255) r.am.dot_age[dot]++;
6360

6461
if(led+1 < STRIP_LENGTH) {
6562
CRGB old_color = leds[led];
6663
uint8_t old_r = old_color.r;
6764
uint8_t old_g = old_color.g;
6865
uint8_t old_b = old_color.b;
69-
uint8_t dot_r = dot_colors[dot].r;
70-
uint8_t dot_g = dot_colors[dot].g;
71-
uint8_t dot_b = dot_colors[dot].b;
66+
uint8_t dot_r = r.am.dot_colors[dot].r;
67+
uint8_t dot_g = r.am.dot_colors[dot].g;
68+
uint8_t dot_b = r.am.dot_colors[dot].b;
7269

73-
uint16_t r = old_r + dot_r * (dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
74-
uint16_t g = old_g + dot_g * (dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
75-
uint16_t b = old_b + dot_b * (dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
70+
uint16_t new_r = old_r + dot_r * (r.am.dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
71+
uint16_t new_g = old_g + dot_g * (r.am.dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
72+
uint16_t new_b = old_b + dot_b * (r.am.dot_age[dot]/255.0) * (POS_PER_PIXEL - leds_offsets[dot])/POS_PER_PIXEL;
7673
leds[led+1] = CRGB(
77-
min(r, 255),
78-
min(g, 255),
79-
min(b, 255)
74+
min(new_r, 255),
75+
min(new_g, 255),
76+
min(new_b, 255)
8077
);
8178
// old_color = strip.getPixelColor(led+1);
8279
// old_r = (uint8_t)(old_color >> 16);

fire.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "config.h"
1010
#include "render.h"
11+
#include "renderheap.h"
1112

1213
#define COOLING 65
1314
#define SPARKING 120
@@ -32,14 +33,14 @@ void setPixelHeatColor (int Pixel, byte temperature) {
3233
}
3334

3435
void render_fire(bool is_beat, unsigned int peakToPeak) {
35-
static byte heat[STRIP_LENGTH]; // todo: this is probably wasteful
36+
byte *heat = r.fm.heat;
3637
int cooldown;
3738

3839
// Step 1. Cool down every cell a little
3940
for( uint8_t i = 0; i < STRIP_LENGTH; i++) {
4041
cooldown = random8(COOLING_RANDOM_FACTOR);
4142

42-
if(cooldown>heat[i]) {
43+
if(cooldown>r.fm.heat[i]) {
4344
heat[i]=0;
4445
} else {
4546
heat[i]=heat[i]-cooldown;

render.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "config.h"
77
#include "render.h"
88
#include "sampler.h"
9+
#include "renderheap.h"
910

1011
#ifndef DEBUG_ONLY
1112

@@ -28,37 +29,6 @@ static const uint8_t PROGMEM _gammaTable[256] = {
2829
243, 244, 245, 245, 246, 247, 248, 249, 249, 250, 251, 252, 253, 253, 254, 255};
2930

3031

31-
typedef struct {
32-
uint8_t hue, beat_offset;
33-
} render_vu_with_beat_strobe_type;
34-
35-
typedef struct {
36-
uint8_t beat_brightness;
37-
} render_vu_plus_beat_interleave_type;
38-
39-
typedef struct {
40-
uint8_t phase;
41-
} render_beat_line_type;
42-
43-
typedef struct {
44-
bool was_beat_2;
45-
uint8_t fade_type;
46-
} render_double_vu_type;
47-
48-
typedef struct {
49-
bool was_beat;
50-
bool top;
51-
uint16_t hue;
52-
} render_beat_bounce_flip_type;
53-
54-
typedef union renderheap_t {
55-
render_vu_with_beat_strobe_type rvuwbs;
56-
render_vu_plus_beat_interleave_type rvupbi;
57-
render_beat_line_type rbl;
58-
render_double_vu_type rdv;
59-
render_beat_bounce_flip_type rbbf;
60-
};
61-
6232
renderheap_t r;
6333

6434
uint8_t static gamma8(uint8_t x) {

vu3.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include "config.h"
66

7+
#include "sampler.h"
8+
#include "ledpwm.h"
9+
#include "tempo.h"
10+
711
// this define is for FastLED
812
#define NO_CORRECTION 1
913
#define FASTLED_ALLOW_INTERRUPTS 1
@@ -14,9 +18,6 @@
1418

1519
struct Framestate F; // the global instance
1620

17-
#include "sampler.h"
18-
#include "ledpwm.h"
19-
#include "tempo.h"
2021

2122
#ifdef BEAT_WITH_INTERRUPTS
2223
// This mode doesn't currently work, because it's all been integrated into this project and isn't needed

0 commit comments

Comments
 (0)