More jolly refactoring

This commit is contained in:
Mark van Renswoude 2017-12-07 17:32:59 +01:00
parent 5efc605d1d
commit 4551744a7c
10 changed files with 112 additions and 66 deletions

View File

@ -2,15 +2,49 @@
#include "config.h" #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++;
}
}
*/
} }

View File

@ -1,4 +1,5 @@
#include "intensity.h" #include "intensity.h"
#include "state.h"
/* /*
LiquidCrystal* display = getDisplay(); 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;
} }

View File

@ -8,8 +8,8 @@ class IntensityMenuItem : public MenuItem
public: public:
IntensityMenuItem() : MenuItem() { } IntensityMenuItem() : MenuItem() { }
char* getTitle(); const char* getTitle();
char* getValue(); const char* getValue();
bool editable() { return true; } bool editable() { return true; }

View File

@ -32,13 +32,13 @@
ExposureTime -= ExposureTime > LargeStepTreshold ? LargeStep : SmallStep; 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; return NULL;
} }

View File

@ -8,8 +8,8 @@ class TimeMenuItem : public MenuItem
public: public:
TimeMenuItem() : MenuItem() { } TimeMenuItem() : MenuItem() { }
char* getTitle(); const char* getTitle();
char* getValue(); const char* getValue();
bool editable() { return true; } bool editable() { return true; }

View File

@ -8,18 +8,15 @@
void CountdownScreen::printRemainingTime() void CountdownScreen::printRemainingTime()
{ {
const char* time = FormatTime(ExposureTime - ((getCurrentTime() - ExposureTimerStart) / 1000)); LCDPrintLineCentered(getDisplay(), 1, FormatTime(mLastDisplayed));
// TODO blank out, center, etc
LCDPrintLineCentered(getDisplay(), 1, time);
} }
void CountdownScreen::onShow() void CountdownScreen::onShow()
{ {
mLastDisplayed = (uint32_t)-1;
LCDPrintLine(getDisplay(), 0, "Exposing..."); LCDPrintLine(getDisplay(), 0, "Exposing...");
mLastDisplayed = GetExposureTimeRemaining(getCurrentTime()) / 1000;
printRemainingTime(); printRemainingTime();
digitalWrite(PinLED, HIGH); digitalWrite(PinLED, HIGH);
@ -48,25 +45,25 @@ void CountdownScreen::onEncoder(long lastPosition, long newPosition)
void CountdownScreen::onTick() 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); mLastDisplayed = 0;
getDisplay()->print("Done! ");
LCDPrintLine(getDisplay(), 0, "Done!");
printRemainingTime(); printRemainingTime();
digitalWrite(PinLED, LOW); digitalWrite(PinLED, LOW);
Buzzer::completed(); Buzzer::completed();
ExposureTimerStart = 0; ResetExposureTimer();
getScreenManager()->show<MenuScreen>(); getScreenManager()->show<MenuScreen>();
} }
else if (elapsed != mLastDisplayed) else if (remaining != mLastDisplayed)
{ {
mLastDisplayed = remaining;
printRemainingTime(); printRemainingTime();
mLastDisplayed = elapsed;
} }
} }

View File

@ -1,6 +1,7 @@
#include "screen/menu.h" #include "screen/menu.h"
#include "config.h" #include "config.h"
#include "buzzer.h" #include "buzzer.h"
#include "display.h"
#include "menu/time.h" #include "menu/time.h"
@ -46,41 +47,7 @@ void MenuScreen::printFullUpdate()
void MenuScreen::printTitle() void MenuScreen::printTitle()
{ {
LiquidCrystal* display = getDisplay(); LCDPrintLineCentered(getDisplay(), 0, mItems[mSelected]->getTitle(), 1);
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++;
}
}
} }

View File

@ -8,8 +8,8 @@ class MenuItem
public: public:
virtual ~MenuItem() { } virtual ~MenuItem() { }
virtual char* getTitle() = 0; virtual const char* getTitle() = 0;
virtual char* getValue() { return NULL; } virtual const char* getValue() { return NULL; }
virtual bool editable() { return false; } virtual bool editable() { return false; }

View File

@ -7,6 +7,30 @@ uint8_t ExposureIntensity = DefaultExposureIntensity;
uint32_t ExposureTimerStart = 0; 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() void LoadSettings()
{ {
uint16_t offset = 0; uint16_t offset = 0;
@ -31,4 +55,17 @@ void StartExposureTimer(unsigned long currentTime)
{ {
SaveSettings(); SaveSettings();
ExposureTimerStart = currentTime; ExposureTimerStart = currentTime;
}
void ResetExposureTimer()
{
ExposureTimerStart = 0;
}
uint32_t GetExposureTimeRemaining(unsigned long currentTime)
{
uint32_t elapsed = (currentTime - ExposureTimerStart);
return elapsed <= ExposureTime ? ExposureTime - elapsed : 0;
} }

View File

@ -3,15 +3,18 @@
#include <Arduino.h> #include <Arduino.h>
extern uint32_t ExposureTime; uint32_t GetExposureTime();
extern uint8_t ExposureIntensity; void SetExposureTime(uint32_t value);
extern uint32_t ExposureTimerStart; uint8_t GetExposureIntensity();
void SetExposureIntensity(uint8_t value);
void LoadSettings(); void LoadSettings();
void SaveSettings(); void SaveSettings();
void StartExposureTimer(unsigned long currentTime); void StartExposureTimer(unsigned long currentTime);
void ResetExposureTimer();
uint32_t GetExposureTimeRemaining(unsigned long currentTime);
#endif #endif