Skip to content

Commit 987f2c0

Browse files
committed
refactor(firmware): replace magic numbers with constants
Replace all hardcoded numbers with named constants for better maintainability: **New constants:** - NUM_FRAMES (8) - Number of frames per animation - BYTES_PER_COLOR (3) - RGB bytes per color - Reorganize config into logical sections **Replacements:** - NUM_FRAMES_PER_ANIMATION: now calculated as (FPS * ANIMATION_DURATION_SEC) - Frame offsets: use NUM_LEDS instead of hardcoded 64 - Color offsets: use BYTES_PER_COLOR instead of hardcoded 3 - Loop bounds: use NUM_FRAMES and ANIMATION_DURATION_SEC - Serial output: use LED_PIN and ANIMATION_DURATION_SEC constants This makes the code easier to modify (e.g., changing to 16x16 matrix or different FPS) without hunting for magic numbers.
1 parent 8346617 commit 987f2c0

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

seeduino_xiao/src/main.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
#define MATRIX_HEIGHT 8
99
#define NUM_LEDS (MATRIX_WIDTH * MATRIX_HEIGHT)
1010
#define BRIGHTNESS 32 // 0-255, start dim for testing
11-
#define FPS 8
12-
#define FRAME_DELAY (1000 / FPS) // 125ms per frame
13-
#define ANIMATION_DURATION_SEC 8 // Play each animation for 8 seconds (64 frames)
14-
#define NUM_FRAMES_PER_ANIMATION 64 // 8 seconds at 8 FPS
11+
12+
// Animation Configuration
13+
#define NUM_FRAMES 8 // Number of frames per animation
14+
#define FPS 8 // Playback speed
15+
#define FRAME_DELAY (1000 / FPS) // Delay per frame in ms
16+
#define ANIMATION_DURATION_SEC 8 // Duration each animation plays
17+
#define NUM_FRAMES_PER_ANIMATION (FPS * ANIMATION_DURATION_SEC)
18+
19+
// Color Configuration
20+
#define BYTES_PER_COLOR 3 // RGB
1521

1622
Adafruit_NeoPixel matrix(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
1723

@@ -44,8 +50,11 @@ void setup()
4450
// Initialize shuffle
4551
shuffleAnimations();
4652

47-
Serial.println("Matrix initialized on pin 10");
48-
Serial.println("Starting random playback (8 seconds per animation)...");
53+
Serial.print("Matrix initialized on pin ");
54+
Serial.println(LED_PIN);
55+
Serial.print("Starting random playback (");
56+
Serial.print(ANIMATION_DURATION_SEC);
57+
Serial.println(" seconds per animation)...");
4958
}
5059

5160
void loop()
@@ -58,7 +67,7 @@ void loop()
5867
Serial.print(" of ");
5968
Serial.println(NUM_ANIMATIONS);
6069

61-
// Play the animation (64 frames = 8 seconds)
70+
// Play the animation
6271
playAnimation(animIndex);
6372

6473
// Move to next animation
@@ -95,18 +104,19 @@ void shuffleAnimations()
95104
// Get RGB color from palette stored in PROGMEM
96105
uint32_t getPaletteColor(uint8_t animIndex, uint8_t colorIdx)
97106
{
98-
uint8_t r = pgm_read_byte(&PALETTES[animIndex][colorIdx * 3]);
99-
uint8_t g = pgm_read_byte(&PALETTES[animIndex][colorIdx * 3 + 1]);
100-
uint8_t b = pgm_read_byte(&PALETTES[animIndex][colorIdx * 3 + 2]);
107+
uint16_t offset = colorIdx * BYTES_PER_COLOR;
108+
uint8_t r = pgm_read_byte(&PALETTES[animIndex][offset]);
109+
uint8_t g = pgm_read_byte(&PALETTES[animIndex][offset + 1]);
110+
uint8_t b = pgm_read_byte(&PALETTES[animIndex][offset + 2]);
101111
return matrix.Color(r, g, b);
102112
}
103113

104114
// Display a single frame from an animation
105115
void displayFrame(uint8_t animIndex, uint8_t frameIndex)
106116
{
107-
uint16_t frameOffset = frameIndex * 64;
117+
uint16_t frameOffset = frameIndex * NUM_LEDS;
108118

109-
for (uint8_t pixelIdx = 0; pixelIdx < 64; pixelIdx++)
119+
for (uint8_t pixelIdx = 0; pixelIdx < NUM_LEDS; pixelIdx++)
110120
{
111121
uint8_t colorIdx = pgm_read_byte(&FRAMES[animIndex][frameOffset + pixelIdx]);
112122
uint32_t color = getPaletteColor(animIndex, colorIdx);
@@ -116,13 +126,13 @@ void displayFrame(uint8_t animIndex, uint8_t frameIndex)
116126
matrix.show();
117127
}
118128

119-
// Play an animation for 64 frames (8 seconds at 8 FPS)
129+
// Play an animation for the configured duration
120130
void playAnimation(uint8_t animIndex)
121131
{
122-
// Loop through all 8 frames, 8 times = 64 total frames = 8 seconds
123-
for (uint8_t cycle = 0; cycle < 8; cycle++)
132+
// Loop through all frames multiple times to reach desired duration
133+
for (uint8_t cycle = 0; cycle < ANIMATION_DURATION_SEC; cycle++)
124134
{
125-
for (uint8_t frameIdx = 0; frameIdx < 8; frameIdx++)
135+
for (uint8_t frameIdx = 0; frameIdx < NUM_FRAMES; frameIdx++)
126136
{
127137
displayFrame(animIndex, frameIdx);
128138
delay(FRAME_DELAY);

0 commit comments

Comments
 (0)