#define ANIMATED
#include <MD_MAX72xx.h> // https://github.com/MajicDesigns/MD_MAX72XX
#include <MD_Parola.h> // https://github.com/MajicDesigns/MD_Parola
#include <SPI.h>
using Millis_t = decltype(millis());
using WeightStr_t = char[sizeof("0,000 kg")];
namespace gc {
constexpr uint8_t ClkPin {13};
constexpr uint8_t DataPin {11};
constexpr uint8_t CsPin {10};
constexpr uint8_t MaxLedDevices {5};
constexpr uint32_t DisplayDelay_ms {3000};
} // namespace gc
class Timer {
public:
Timer() : TimeStamp(0), Reached(true) {}
void start() {
TimeStamp = millis();
Reached = false;
}
void reset() { Reached = true; }
bool operator()(const Millis_t Duration) {
if (!Reached) { Reached = millis() - TimeStamp >= Duration; }
return Reached;
}
private:
Millis_t TimeStamp;
bool Reached;
};
// Display
MD_Parola Mx = MD_Parola(MD_MAX72XX::PAROLA_HW, gc::CsPin, gc::MaxLedDevices);
Timer Delay;
void getWeightStr(WeightStr_t WeightStr) {
long WeightTotalInGrams = random(0, 10000);
uint8_t WeightKg = WeightTotalInGrams / 1000;
uint16_t WeigtGrams = WeightTotalInGrams % 1000;
snprintf(WeightStr, sizeof(WeightStr_t), "%1d,%03d kg", WeightKg, WeigtGrams);
}
void showWeightStr(WeightStr_t WeightStr, textEffect_t Effect = PA_NO_EFFECT) {
Mx.displayClear();
Mx.displayText(WeightStr, PA_CENTER, 50, 0, Effect);
while (!Mx.displayAnimate()) { ; }
}
void setup() {
// Serial.begin(115200);
Mx.begin();
Mx.setIntensity(15);
randomSeed(A0);
}
void loop() {
static WeightStr_t Buffer;
#ifdef ANIMATED
static textEffect_t Animation[2] {PA_SCROLL_DOWN, PA_SCROLL_UP};
static uint8_t AIdx {0};
#endif
if (Delay(gc::DisplayDelay_ms)) {
Delay.start();
getWeightStr(Buffer);
#ifdef ANIMATED
showWeightStr(Buffer, Animation[AIdx ^= 1]);
#else
showWeightStr(Buffer);
#endif
}
}