arduino stuffs
feat(mp3player:memory|splash): less ram usage, slower splash speed
bendn 2022-10-20
parent 12d2f50 · commit d58a0e4
-rw-r--r--mp3player/arduino/display/display.h35
-rw-r--r--mp3player/arduino/error.h5
-rw-r--r--mp3player/arduino/math.h18
-rw-r--r--mp3player/arduino/player.h8
4 files changed, 42 insertions, 24 deletions
diff --git a/mp3player/arduino/display/display.h b/mp3player/arduino/display/display.h
index 2445934..764e7f1 100644
--- a/mp3player/arduino/display/display.h
+++ b/mp3player/arduino/display/display.h
@@ -4,13 +4,11 @@
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
+#include "../math.h"
#include "freeserif.h"
#include "splash.h"
#include <Adafruit_SSD1306.h>
#include <Wire.h>
-
-static inline float lerp(float from, float to, float weight) { return from + (to - from) * weight; }
-
namespace OLED {
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
@@ -21,8 +19,8 @@ inline void clear() { oled.clearDisplay(); }
inline void flush() { oled.display(); }
// Adafruit_GFX::drawBitmap(..., size_x, size_y). adds multiplication factors
-void draw_bitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint8_t size_x,
- uint8_t size_y) {
+void draw_bitmap(const int8_t x, const int8_t y, const uint8_t *bitmap, const int8_t w, const int8_t h,
+ const uint16_t color, const uint8_t size_x, const uint8_t size_y) {
int16_t i, j, byteWidth = (w + 7) / 8;
for (j = 0; j < h; j++)
@@ -35,21 +33,22 @@ void draw_bitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t
}
}
-#define SPLASH_SCALE 2
-#define SPLASH_BOTTOM (SCREEN_HEIGHT - SPLASH_SIZE) * (SPLASH_SCALE * SPLASH_SCALE)
-#define SPLASH_MIDDLE SCREEN_WIDTH / SPLASH_SCALE - SPLASH_SIZE
+static constexpr uint8_t SPLASH_SCALE = 2;
+static constexpr uint8_t SPLASH_BOTTOM = SCREEN_HEIGHT + (SPLASH_SIZE * SPLASH_SCALE);
+static constexpr uint8_t SPLASH_MIDDLE = SCREEN_WIDTH / SPLASH_SCALE - SPLASH_SIZE;
+
+static constexpr uint8_t splash_step = SPLASH_BOTTOM - lerp(SPLASH_BOTTOM, 0 - (SPLASH_SIZE * SPLASH_SCALE), 0.1);
+static constexpr int8_t SPLASH_TOP = -splash_step - (SPLASH_SIZE * SPLASH_SCALE);
void show_splash() {
- for (unsigned int c = 0; c < 2; c++) {
- for (unsigned int i = 0; i < 9; i++) {
- clear();
- const int16_t y_pos = lerp(SPLASH_BOTTOM, 0 - (SPLASH_SIZE * SPLASH_SCALE) - 30, (i + 1) / 10.);
- const int16_t x_pos = (rand() % 10 - 5) + SPLASH_MIDDLE;
- draw_bitmap(x_pos, y_pos, ships[i], SPLASH_SIZE, SPLASH_SIZE, WHITE, SPLASH_SCALE, SPLASH_SCALE);
- flush();
- delay(20);
- }
- delay(40);
+ int8_t x_pos = SPLASH_MIDDLE;
+ for (uint8_t i = 0; i < 100; i++) {
+ clear();
+ const int8_t y_pos = lerp(SPLASH_BOTTOM, SPLASH_TOP, (i + 1) / 100.0);
+ x_pos = clamp(x_pos + (rand() % 4 - 2), SPLASH_MIDDLE - 50, SPLASH_MIDDLE + 50);
+ draw_bitmap(x_pos, y_pos, ships[wrapi(i, 0, 8)], SPLASH_SIZE, SPLASH_SIZE, WHITE, SPLASH_SCALE, SPLASH_SCALE);
+ flush();
+ delay(20);
}
}
diff --git a/mp3player/arduino/error.h b/mp3player/arduino/error.h
index eb30a2f..89d6a2e 100644
--- a/mp3player/arduino/error.h
+++ b/mp3player/arduino/error.h
@@ -8,9 +8,10 @@
class Error {
public:
Error(String err) {
- Serial.println("err: " + err);
+ Serial.print(F("E: "));
+ Serial.println(err);
OLED::clear();
- OLED::println("err: " + err);
+ OLED::println("E: " + err);
OLED::flush();
blink_err_light();
};
diff --git a/mp3player/arduino/math.h b/mp3player/arduino/math.h
new file mode 100644
index 0000000..51e88d1
--- /dev/null
+++ b/mp3player/arduino/math.h
@@ -0,0 +1,18 @@
+#ifndef MATH_H
+#define MATH_H
+static constexpr inline float lerp(float from, float to, float weight) { return from + (to - from) * weight; }
+
+static inline int wrapi(int value, int min, int max) {
+ int range = max - min;
+ return range == 0 ? min : min + ((((value - min) % range) + range) % range);
+}
+
+static inline int clamp(long value, long minv, long maxv) {
+ if (value < minv)
+ return minv;
+ else if (value > maxv)
+ return maxv;
+ return value;
+}
+
+#endif
diff --git a/mp3player/arduino/player.h b/mp3player/arduino/player.h
index 7ea9b6e..6cf2957 100644
--- a/mp3player/arduino/player.h
+++ b/mp3player/arduino/player.h
@@ -5,18 +5,18 @@
#include <SoftwareSerial.h>
namespace Player {
-SoftwareSerial s(10, 11); // RX, TX
DFRobotDFPlayerMini p;
+SoftwareSerial s(10, 11); // RX, TX
void begin() {
- Serial.println("Initializing DFPlayer");
+ Serial.println(F("init p"));
s.begin(9600);
if (!p.begin(s))
- Error err("init failed");
+ Error err(F("init p fail"));
- Serial.println("DFPlayer online.");
+ Serial.println(F("p online."));
p.volume(5); // Set volume value. From 0 to 30
p.play(1); // Play the first mp3