From 65338e6b33a2953528c2d5221d5e3cca53495914 Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 26 Aug 2021 12:48:02 +0200 Subject: [PATCH] factorized code --- watchfaces/tetris-2.0/Watchy_Tetris.cpp | 45 +++++++++++++------------ watchfaces/tetris-2.0/Watchy_Tetris.h | 1 + 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/watchfaces/tetris-2.0/Watchy_Tetris.cpp b/watchfaces/tetris-2.0/Watchy_Tetris.cpp index 6b1aca4..5b6b5c9 100644 --- a/watchfaces/tetris-2.0/Watchy_Tetris.cpp +++ b/watchfaces/tetris-2.0/Watchy_Tetris.cpp @@ -4,11 +4,16 @@ const unsigned char *tetris_nums[10] = {tetris0, tetris1, tetris2, tetris3, tetr const unsigned char *tetris_small_nums[10] = {tetrissmall0, tetrissmall1, tetrissmall2, tetrissmall3, tetrissmall4, tetrissmall5, tetrissmall6, tetrissmall7, tetrissmall8, tetrissmall9}; +const float MAX_VBAT = 4.33; +const float MIN_VBAT = 3.80; + WatchyTetris::WatchyTetris() {} //constructor void WatchyTetris::drawWatchFace() { display.fillScreen(GxEPD_WHITE); + + // Background display.drawBitmap(0, 0, tetrisbg, DISPLAY_WIDTH, DISPLAY_HEIGHT, GxEPD_BLACK); //Hour @@ -25,30 +30,28 @@ void WatchyTetris::drawWatchFace() sensor.resetStepCounter(); } uint32_t stepCount = sensor.getCounter(); - if (stepCount > 1000000) - display.drawBitmap(131, 41, tetris_small_nums[(stepCount / 1000000) % 10], 8, 8, GxEPD_BLACK); - if (stepCount > 100000) - display.drawBitmap(141, 41, tetris_small_nums[(stepCount / 100000) % 10], 8, 8, GxEPD_BLACK); - if (stepCount > 10000) - display.drawBitmap(151, 41, tetris_small_nums[(stepCount / 10000) % 10], 8, 8, GxEPD_BLACK); - if (stepCount > 1000) - display.drawBitmap(161, 41, tetris_small_nums[(stepCount / 1000) % 10], 8, 8, GxEPD_BLACK); - if (stepCount > 100) - display.drawBitmap(171, 41, tetris_small_nums[(stepCount / 100) % 10], 8, 8, GxEPD_BLACK); - if (stepCount > 10) - display.drawBitmap(181, 41, tetris_small_nums[(stepCount / 10) % 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(191, 41, tetris_small_nums[(stepCount) % 10], 8, 8, GxEPD_BLACK); + drawNumber(191, 41, stepCount); //Voltage float VBAT = getBatteryVoltage(); - display.drawBitmap(161, 81, tetris_small_nums[(int)(VBAT * 1) % 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(171, 81, tetris_small_nums[(int)(VBAT * 10) % 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(181, 81, tetris_small_nums[(int)(VBAT * 100) % 10], 8, 8, GxEPD_BLACK); + uint32_t percent = (int)(100.0 * (VBAT - MIN_VBAT) / MAX_VBAT); + if (percent < 0) + percent = 0; + if (percent > 100) + percent = 100; + drawNumber(181, 81, percent); //Date - if (currentTime.Month > 10) - display.drawBitmap(151, 111, tetris_small_nums[currentTime.Month / 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(161, 111, tetris_small_nums[currentTime.Month % 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(171, 111, tetris_small_nums[currentTime.Day / 10], 8, 8, GxEPD_BLACK); - display.drawBitmap(181, 111, tetris_small_nums[currentTime.Day % 10], 8, 8, GxEPD_BLACK); + drawNumber(181, 111, currentTime.Month * 100 + currentTime.Day); +} + +void WatchyTetris::drawNumber(uint32_t x, uint32_t y, uint32_t v) +{ + for(int8_t i = 0; i < 8; i++){ + if (v == 0) { + break; + } + display.drawBitmap(x - i * 10, y, tetris_small_nums[v % 10], 8, 8, GxEPD_BLACK); + v /= 10; + } } \ No newline at end of file diff --git a/watchfaces/tetris-2.0/Watchy_Tetris.h b/watchfaces/tetris-2.0/Watchy_Tetris.h index 953f14d..3d9c3c1 100644 --- a/watchfaces/tetris-2.0/Watchy_Tetris.h +++ b/watchfaces/tetris-2.0/Watchy_Tetris.h @@ -9,6 +9,7 @@ class WatchyTetris : public Watchy public: WatchyTetris(); void drawWatchFace(); + void drawNumber(uint32_t x, uint32_t y, uint32_t v); }; #endif \ No newline at end of file