From 4551744a7cda049bc70fa38dfd76fd199135ddb2 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Thu, 7 Dec 2017 17:32:59 +0100 Subject: [PATCH] More jolly refactoring --- src/display.cpp | 38 ++++++++++++++++++++++++++++++++++++-- src/menu/intensity.cpp | 16 ++++++++++++---- src/menu/intensity.h | 4 ++-- src/menu/time.cpp | 6 +++--- src/menu/time.h | 4 ++-- src/screen/countdown.cpp | 23 ++++++++++------------- src/screen/menu.cpp | 37 ++----------------------------------- src/screen/menu.h | 4 ++-- src/state.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/state.h | 9 ++++++--- 10 files changed, 112 insertions(+), 66 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index b059947..bf10138 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2,15 +2,49 @@ #include "config.h" -void LCDPrintLine(LiquidCrystal* display, uint8_t y, char* value, uint8_t margin) +void LCDPrintLine(LiquidCrystal* display, uint8_t y, const char* value, uint8_t margin) { } -void LCDPrintLineCentered(LiquidCrystal* display, uint8_t y, char* value, uint8_t margin) +void LCDPrintLineCentered(LiquidCrystal* display, uint8_t y, const char* value, uint8_t margin) { +/* + const char* title = mItems[mSelected]->getTitle(); + uint8_t titleLength = strlen(title); + uint8_t maxWidth = LCDWidth - 2; + display->setCursor(1, 0); + + if (titleLength >= maxWidth) + { + // Title too long, cut off + char* character = (char*)title; + for (uint8_t i = 0; i < maxWidth; i++) + { + display->write(byte(*character)); + character++; + } + } + else + { + // Center title + uint8_t offset = (maxWidth - titleLength) / 2; + + for (uint8_t i = 0; i < offset; i++) + display->write(' '); + + display->print(title); + offset += titleLength; + + while (offset < LCDWidth - 2) + { + display->print(' '); + offset++; + } + } +*/ } diff --git a/src/menu/intensity.cpp b/src/menu/intensity.cpp index 9dbab6e..6fb26f9 100644 --- a/src/menu/intensity.cpp +++ b/src/menu/intensity.cpp @@ -1,4 +1,5 @@ #include "intensity.h" +#include "state.h" /* LiquidCrystal* display = getDisplay(); @@ -24,15 +25,22 @@ */ -char* IntensityMenuItem::getTitle() +const char* IntensityMenuItem::getTitle() { - return NULL; + return "Intensity"; } -char* IntensityMenuItem::getValue() +const char* IntensityMenuItem::getValue() { - return NULL; + char value[5]; + itoa(GetExposureIntensity(), value, 10); + + uint8_t length = strlen(value); + value[length] = '%'; + value[length + 1] = 0; + + return value; } diff --git a/src/menu/intensity.h b/src/menu/intensity.h index a6b7522..f133794 100644 --- a/src/menu/intensity.h +++ b/src/menu/intensity.h @@ -8,8 +8,8 @@ class IntensityMenuItem : public MenuItem public: IntensityMenuItem() : MenuItem() { } - char* getTitle(); - char* getValue(); + const char* getTitle(); + const char* getValue(); bool editable() { return true; } diff --git a/src/menu/time.cpp b/src/menu/time.cpp index d1dba89..47de1fc 100644 --- a/src/menu/time.cpp +++ b/src/menu/time.cpp @@ -32,13 +32,13 @@ ExposureTime -= ExposureTime > LargeStepTreshold ? LargeStep : SmallStep; */ -char* TimeMenuItem::getTitle() +const char* TimeMenuItem::getTitle() { - return NULL; + return "Time"; } -char* TimeMenuItem::getValue() +const char* TimeMenuItem::getValue() { return NULL; } diff --git a/src/menu/time.h b/src/menu/time.h index 39823ef..9f46bc0 100644 --- a/src/menu/time.h +++ b/src/menu/time.h @@ -8,8 +8,8 @@ class TimeMenuItem : public MenuItem public: TimeMenuItem() : MenuItem() { } - char* getTitle(); - char* getValue(); + const char* getTitle(); + const char* getValue(); bool editable() { return true; } diff --git a/src/screen/countdown.cpp b/src/screen/countdown.cpp index 6d0a3a0..e51e834 100644 --- a/src/screen/countdown.cpp +++ b/src/screen/countdown.cpp @@ -8,18 +8,15 @@ void CountdownScreen::printRemainingTime() { - const char* time = FormatTime(ExposureTime - ((getCurrentTime() - ExposureTimerStart) / 1000)); - - // TODO blank out, center, etc - LCDPrintLineCentered(getDisplay(), 1, time); + LCDPrintLineCentered(getDisplay(), 1, FormatTime(mLastDisplayed)); } void CountdownScreen::onShow() { - mLastDisplayed = (uint32_t)-1; - LCDPrintLine(getDisplay(), 0, "Exposing..."); + + mLastDisplayed = GetExposureTimeRemaining(getCurrentTime()) / 1000; printRemainingTime(); digitalWrite(PinLED, HIGH); @@ -48,25 +45,25 @@ void CountdownScreen::onEncoder(long lastPosition, long newPosition) void CountdownScreen::onTick() { - uint32_t elapsed = (getCurrentTime() - ExposureTimerStart) / 1000; + uint32_t remaining = GetExposureTimeRemaining(getCurrentTime()) / 1000; - if (elapsed >= ExposureTime) + if (remaining == 0) { - getDisplay()->setCursor(0, 0); - getDisplay()->print("Done! "); + mLastDisplayed = 0; + LCDPrintLine(getDisplay(), 0, "Done!"); printRemainingTime(); digitalWrite(PinLED, LOW); Buzzer::completed(); - ExposureTimerStart = 0; + ResetExposureTimer(); getScreenManager()->show(); } - else if (elapsed != mLastDisplayed) + else if (remaining != mLastDisplayed) { + mLastDisplayed = remaining; printRemainingTime(); - mLastDisplayed = elapsed; } } diff --git a/src/screen/menu.cpp b/src/screen/menu.cpp index 5a4c47f..e6b7b4f 100644 --- a/src/screen/menu.cpp +++ b/src/screen/menu.cpp @@ -1,6 +1,7 @@ #include "screen/menu.h" #include "config.h" #include "buzzer.h" +#include "display.h" #include "menu/time.h" @@ -46,41 +47,7 @@ void MenuScreen::printFullUpdate() void MenuScreen::printTitle() { - LiquidCrystal* display = getDisplay(); - - const char* title = mItems[mSelected]->getTitle(); - uint8_t titleLength = strlen(title); - uint8_t maxWidth = LCDWidth - 2; - - display->setCursor(1, 0); - - if (titleLength >= maxWidth) - { - // Title too long, cut off - char* character = (char*)title; - for (uint8_t i = 0; i < maxWidth; i++) - { - display->write(byte(*character)); - character++; - } - } - else - { - // Center title - uint8_t offset = (maxWidth - titleLength) / 2; - - for (uint8_t i = 0; i < offset; i++) - display->write(' '); - - display->print(title); - offset += titleLength; - - while (offset < LCDWidth - 2) - { - display->print(' '); - offset++; - } - } + LCDPrintLineCentered(getDisplay(), 0, mItems[mSelected]->getTitle(), 1); } diff --git a/src/screen/menu.h b/src/screen/menu.h index aa6ac32..6e4ad64 100644 --- a/src/screen/menu.h +++ b/src/screen/menu.h @@ -8,8 +8,8 @@ class MenuItem public: virtual ~MenuItem() { } - virtual char* getTitle() = 0; - virtual char* getValue() { return NULL; } + virtual const char* getTitle() = 0; + virtual const char* getValue() { return NULL; } virtual bool editable() { return false; } diff --git a/src/state.cpp b/src/state.cpp index 2e68493..24ed24f 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -7,6 +7,30 @@ uint8_t ExposureIntensity = DefaultExposureIntensity; uint32_t ExposureTimerStart = 0; +uint32_t GetExposureTime() +{ + return ExposureTime; +} + + +void SetExposureTime(uint32_t value) +{ + ExposureTime = value; +} + + +uint8_t GetExposureIntensity() +{ + return ExposureIntensity; +} + + +void SetExposureIntensity(uint8_t value) +{ + ExposureIntensity = (value <= 100) ? value : 100; +} + + void LoadSettings() { uint16_t offset = 0; @@ -31,4 +55,17 @@ void StartExposureTimer(unsigned long currentTime) { SaveSettings(); ExposureTimerStart = currentTime; +} + + +void ResetExposureTimer() +{ + ExposureTimerStart = 0; +} + + +uint32_t GetExposureTimeRemaining(unsigned long currentTime) +{ + uint32_t elapsed = (currentTime - ExposureTimerStart); + return elapsed <= ExposureTime ? ExposureTime - elapsed : 0; } \ No newline at end of file diff --git a/src/state.h b/src/state.h index 787a152..6856305 100644 --- a/src/state.h +++ b/src/state.h @@ -3,15 +3,18 @@ #include -extern uint32_t ExposureTime; -extern uint8_t ExposureIntensity; +uint32_t GetExposureTime(); +void SetExposureTime(uint32_t value); -extern uint32_t ExposureTimerStart; +uint8_t GetExposureIntensity(); +void SetExposureIntensity(uint8_t value); void LoadSettings(); void SaveSettings(); void StartExposureTimer(unsigned long currentTime); +void ResetExposureTimer(); +uint32_t GetExposureTimeRemaining(unsigned long currentTime); #endif