From 0be8b39d8cdce12bdb8a600938d3a572d48069c4 Mon Sep 17 00:00:00 2001 From: Oliver Seiler Date: Wed, 24 Apr 2024 12:32:08 +1200 Subject: [PATCH] NeopixelMatrix improvements --- include/neopixelMatrix.h | 7 +++---- src/hub75.cpp | 2 +- src/neopixelMatrix.cpp | 18 +++++++++--------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/neopixelMatrix.h b/include/neopixelMatrix.h index 2188238..b9ab833 100644 --- a/include/neopixelMatrix.h +++ b/include/neopixelMatrix.h @@ -20,7 +20,7 @@ class NeopixelMatrix { private: static void neopixelMatrixLoop(void* pvParameters); - uint8_t ppmToDots(uint16_t ppm); + uint16_t ppmToDots(uint16_t ppm); void showTank(uint16_t ppm, bool showDrip); void showText(); void waveTimer(); @@ -75,7 +75,7 @@ class NeopixelMatrix { uint8_t MATRIX_WIDTH; uint8_t MATRIX_HEIGHT; - uint8_t NUMBER_OF_DOTS; + uint16_t NUMBER_OF_DOTS; uint8_t TANK_WIDTH; uint8_t TANK_HEIGHT; @@ -104,12 +104,11 @@ class NeopixelMatrix { // === SNAKES === const uint8_t NUMBER_OF_SNAKES = 1; const uint8_t SNAKE_LENGTH = 5; - volatile uint8_t snakePos = 0; + volatile uint16_t snakePos = 0; const static uint16_t SNAKE_TICKER_INTERVAL = 333; // === PPM DISPLAY === char txt[6]; - uint16_t textWidth; int16_t scrollWidth; volatile int16_t scrollPosition; volatile int8_t scrollDirection; diff --git a/src/hub75.cpp b/src/hub75.cpp index 635c3e6..742f432 100644 --- a/src/hub75.cpp +++ b/src/hub75.cpp @@ -23,7 +23,7 @@ HUB75::HUB75(Model* _model) { } matrix->begin(); matrix->setRotation(3); - matrix->setBrightness8(config.brightness); + matrix->setBrightness(config.brightness); cyclicTimer = new Ticker(); // https://arduino.stackexchange.com/questions/81123/using-lambdas-as-callback-functions diff --git a/src/neopixelMatrix.cpp b/src/neopixelMatrix.cpp index b8cc1c9..0ba9e47 100644 --- a/src/neopixelMatrix.cpp +++ b/src/neopixelMatrix.cpp @@ -67,9 +67,9 @@ void NeopixelMatrix::stop() { * Return the number of dots that represent the given PPM value, with ppm <= LOWER_LIMIT returning NUMBER_OF_DOTS (= full tank) and * ppm >= UPPER_LIMIT returning 0 (= empty tank) */ -uint8_t NeopixelMatrix::ppmToDots(uint16_t ppm) { +uint16_t NeopixelMatrix::ppmToDots(uint16_t ppm) { ppm = max(min(ppm, UPPER_LIMIT), LOWER_LIMIT); - return NUMBER_OF_DOTS - min((uint8_t)floor((ppm - LOWER_LIMIT) / PPM_PER_DOT), NUMBER_OF_DOTS); + return NUMBER_OF_DOTS - min((uint16_t)floor((ppm - LOWER_LIMIT) / PPM_PER_DOT), NUMBER_OF_DOTS); } /** @@ -103,7 +103,7 @@ void NeopixelMatrix::showText() { } /** - * Render the given PPM value on the matrix-> + * Render the given PPM value on the matrix */ void NeopixelMatrix::showTank(uint16_t ppm, bool showDrip) { if (this->displayMode != SHOW_TANK) return; @@ -111,7 +111,7 @@ void NeopixelMatrix::showTank(uint16_t ppm, bool showDrip) { // takes about 1297 micros // uint32_t start = micros(); - uint8_t dots = ppmToDots(ppm); + uint16_t dots = ppmToDots(ppm); float eps = 1 - ((float)dots / NUMBER_OF_DOTS); matrix->setBrightness(min(int(config.brightness + (eps * 10)), 255)); @@ -132,9 +132,9 @@ void NeopixelMatrix::showTank(uint16_t ppm, bool showDrip) { // if snake(s) are active, draw snake(s) at current positions - dows not move the snake if (snakeTicker->active()) { for (uint8_t s = 0; s < NUMBER_OF_SNAKES; s++) { - uint8_t snakeOffset = s * (((TANK_WIDTH + TANK_HEIGHT - 2) * 2)) / NUMBER_OF_SNAKES; + uint16_t snakeOffset = s * (((TANK_WIDTH + TANK_HEIGHT - 2) * 2)) / NUMBER_OF_SNAKES; for (uint8_t i = 0; i < SNAKE_LENGTH; i++) { - uint8_t p = (snakePos + i + snakeOffset) % ((TANK_WIDTH + TANK_HEIGHT - 2) * 2); + uint16_t p = (snakePos + i + snakeOffset) % ((TANK_WIDTH + TANK_HEIGHT - 2) * 2); if (p < TANK_WIDTH) { // ESP_LOGD(TAG, "snakePos 1 %u =>(%u,%u)", p, p, TANK_HEIGHT - 1); matrix->drawPixel(p, TANK_HEIGHT - 1, RED); @@ -194,7 +194,7 @@ void NeopixelMatrix::update(uint16_t ppm) { sprintf(txt, "%u", ppm); } int16_t x1, y1 = 0; - uint16_t h = 0; + uint16_t textWidth, h = 0; matrix->getTextBounds(txt, 0, 0, &x1, &y1, &textWidth, &h); scrollWidth = textWidth - matrix->width(); if (scrollWidth > 0) { @@ -220,7 +220,7 @@ void NeopixelMatrix::update(uint16_t ppm) { if (previousPpm > currentPpm) { // ppm decreasing dripDirection = 1; - uint8_t dots = ppmToDots(ppm); + uint16_t dots = ppmToDots(ppm); dripCurrentRow = TANK_HEIGHT; dripColumn = max(dots - 1, 0) % TANK_WIDTH; dripFinalRow = min((uint8_t)floor((dots - 1) / TANK_WIDTH), TANK_HEIGHT); @@ -228,7 +228,7 @@ void NeopixelMatrix::update(uint16_t ppm) { } else { // ppm increasing dripDirection = -1; - uint8_t dots = ppmToDots(previousPpm); + uint16_t dots = ppmToDots(previousPpm); dripCurrentRow = min((uint8_t)floor((dots - 1) / TANK_WIDTH), TANK_HEIGHT); dripColumn = max(dots - 1, 0) % TANK_WIDTH; dripFinalRow = TANK_HEIGHT - 1;