Skip to content

Commit

Permalink
NeopixelMatrix improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
oseiler2 committed Apr 24, 2024
1 parent 2d662b5 commit 0be8b39
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
7 changes: 3 additions & 4 deletions include/neopixelMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/hub75.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/neopixelMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -103,15 +103,15 @@ 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;
uint16_t color = ppmToColour(ppm); //showDrip ? GREEN : GREEN; //ppmToColour(ppm);

// 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));
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -220,15 +220,15 @@ 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);
// ESP_LOGD(TAG, "dripTimer=> dripColumn %u, dripFinalRow %u", dripColumn, dripFinalRow);
} 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;
Expand Down

0 comments on commit 0be8b39

Please sign in to comment.