Implemented menu and presets configuration

This commit is contained in:
Mark van Renswoude 2020-02-01 20:54:05 +01:00
parent 9ab3da2782
commit c8ccd519fc
12 changed files with 435 additions and 177 deletions

View File

@ -18,8 +18,8 @@ https://www.pololu.com/product/2995
| Identifier | Description | Arduino pin | ATMega328p pin | | Identifier | Description | Arduino pin | ATMega328p pin |
|------------|----------------------------|-------------|----------------| |------------|----------------------------|-------------|----------------|
| PWM | Motor PWM | 14 | 23 - PC0 | | PWM | Motor PWM | 14 / A0 | 23 - PC0 |
| DIR | Direction | 15 | 24 - PC1 | | DIR | Direction | 15 / A1 | 24 - PC1 |
| SLP | Sleep (active low) | 16 | 25 - PC2 | | SLP | Sleep (active low) | 16 | 25 - PC2 |
| CS | Current sensing | 17 / A3 | 26 - PC3 | | CS | Current sensing | 17 / A3 | 26 - PC3 |

View File

@ -151,6 +151,9 @@ class Config
static const uint16_t ColorMenuHeaderText = ColorWhite; static const uint16_t ColorMenuHeaderText = ColorWhite;
static const uint16_t ColorMenuHeaderBackground = ColorSoftBlue; static const uint16_t ColorMenuHeaderBackground = ColorSoftBlue;
static const uint16_t ColorMenuText = ColorWhite;
static const uint16_t ColorMenuBackground = ColorBlack;
static const uint16_t ColorMenuSelectedText = ColorWhite; static const uint16_t ColorMenuSelectedText = ColorWhite;
static const uint16_t ColorMenuSelectedBackground = ColorDarkBlue; static const uint16_t ColorMenuSelectedBackground = ColorDarkBlue;

View File

@ -9,7 +9,8 @@ enum class ScreenId
MoveOvercurrent, MoveOvercurrent,
MoveSensorError, MoveSensorError,
Menu, Menu,
Manual Manual,
Presets
}; };
#endif #endif

View File

@ -28,6 +28,7 @@ void motorStop()
{ {
digitalWrite(Config::MotorPinPWM, LOW); digitalWrite(Config::MotorPinPWM, LOW);
digitalWrite(Config::MotorPinSleep, LOW); digitalWrite(Config::MotorPinSleep, LOW);
digitalWrite(Config::MotorPinDirection, LOW);
} }

View File

@ -0,0 +1,162 @@
#include "./calibrate.h"
#include "./home.h"
#include "include/metrics.h"
#include "lib/settings.h"
void BaseHeightEntryScreen::onShow()
{
this->display->setFont(Metrics::LargeFont);
this->display->setTextSize(Metrics::LargeFontTextSize);
this->display->fillScreen(Config::ColorHomeBackground);
this->drawTitle();
this->initHeights();
this->drawArrowUp(Metrics::ArrowMargin, Metrics::LargeTextLineHeight + Metrics::LargeTextLineVArrowYOffset, Config::ColorCalibrateIndicators);
this->drawArrowRight(Metrics::ArrowMargin, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineHArrowYOffset, Config::ColorCalibrateIndicators);
this->display->setTextColor(Config::ColorCalibrateIndicators);
this->display->setCursor(Metrics::ArrowMargin, Config::DisplayHeight - Metrics::LargeTextLineHeight + Metrics::LargeTextLineYOffset);
this->display->print("OK");
this->drawSetHeight();
}
// Credit: https://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int
uint16_t ipow(uint16_t base, uint8_t exp)
{
int result = 1;
for (;;)
{
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}
return result;
}
void BaseHeightEntryScreen::onButton(Button button)
{
switch (button)
{
case Button::Top:
{
uint16_t height = this->getHeight();
uint16_t increment = ipow(10, 3 - this->editingDigit);
uint16_t modulus = increment * 10;
uint16_t remainder = height % modulus;
uint16_t offset = height - remainder;
height = offset + ((remainder + increment) % modulus);
if (height > 1999)
height %= 2000;
this->setHeight(height);
this->drawSetHeight();
break;
}
case Button::Middle:
{
this->editingDigit++;
if (this->editingDigit > 3)
this->editingDigit = 0;
this->drawSetHeight();
break;
}
case Button::Bottom:
{
if (!this->isValidHeight())
return;
if (this->nextPage())
{
this->editingDigit = 0;
this->drawTitle();
this->drawSetHeight();
}
break;
}
}
}
void BaseHeightEntryScreen::onTick()
{
}
void BaseHeightEntryScreen::drawTitle()
{
this->drawLargeTextLineCentered(this->getTitle(), 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground);
}
void BaseHeightEntryScreen::drawSetHeight()
{
char heightText[7];
uint16_t height = this->getHeight();
if (height > 999)
heightText[0] = '0' + ((height / 1000) % 10);
else
heightText[0] = '0';
heightText[1] = '.';
heightText[2] = '0' + ((height / 100) % 10);
heightText[3] = '0' + ((height / 10) % 10);
heightText[4] = '0' + (height % 10);
heightText[5] = 'm';
heightText[6] = 0;
int16_t textX;
int16_t textY;
uint16_t textW;
uint16_t textH;
this->display->getTextBounds(&heightText[0], 0, 0, &textX, &textY, &textW, &textH);
textX = (Config::DisplayWidth - textW) / 2;
uint8_t editingChar = this->editingDigit;
// Skip the dot
if (editingChar > 0)
editingChar++;
if (this->lastTextWidth > 0)
this->display->fillRect((Config::DisplayWidth - this->lastTextWidth) / 2, Metrics::MiddleLargeTextLineY, this->lastTextWidth, Metrics::LargeTextLineHeight, Config::ColorCalibrateBackground);
this->lastTextWidth = textW;
this->display->setTextColor(this->isValidHeight() ? Config::ColorCalibrateValue : Config::ColorCalibrateInvalidValue);
this->display->setCursor(textX, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineYOffset);
// Draw each character ourselves so we can keep track of where the line should be drawn
for (uint8_t i = 0; i < sizeof(heightText); i++)
{
int16_t cursorStart = this->display->getCursorX();
this->display->print(heightText[i]);
if (i == editingChar)
this->display->drawFastHLine(cursorStart, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineHeight - 1, this->display->getCursorX() - cursorStart, Config::ColorCalibrateDigitMarker);
}
}

View File

@ -0,0 +1,34 @@
#ifndef __screen_baseheightentry
#define __screen_baseheightentry
#include "../screen.h"
#include "../control.h"
class BaseHeightEntryScreen : public BaseScreen
{
public:
BaseHeightEntryScreen(ScreenManager* screenManager, Adafruit_GFX* display) : BaseScreen(screenManager, display) { }
void onShow();
void onButton(Button button);
void onTick();
protected:
virtual void initHeights() = 0;
virtual uint16_t getHeight() = 0;
virtual void setHeight(uint16_t value) = 0;
virtual bool nextPage() = 0;
virtual bool isValidHeight() = 0;
virtual const char* getTitle() = 0;
private:
void drawTitle();
void drawSetHeight();
uint16_t lastTextWidth = 0;
uint8_t editingDigit = 0;
};
#endif

View File

@ -4,110 +4,28 @@
#include "lib/settings.h" #include "lib/settings.h"
void CalibrateScreen::onShow()
void CalibrateScreen::initHeights()
{ {
this->display->setFont(Metrics::LargeFont);
this->display->setTextSize(Metrics::LargeFontTextSize);
this->display->fillScreen(Config::ColorHomeBackground);
this->drawTitle();
this->height[CalibrateStepCurrent] = Control.getCurrentHeight(); this->height[CalibrateStepCurrent] = Control.getCurrentHeight();
this->height[CalibrateStepMax] = Settings.Height.Maximum; this->height[CalibrateStepMax] = Settings.Height.Maximum;
this->height[CalibrateStepMin] = Settings.Height.Minimum; this->height[CalibrateStepMin] = Settings.Height.Minimum;
this->drawArrowUp(Metrics::ArrowMargin, Metrics::LargeTextLineHeight + Metrics::LargeTextLineVArrowYOffset, Config::ColorCalibrateIndicators);
this->drawArrowRight(Metrics::ArrowMargin, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineHArrowYOffset, Config::ColorCalibrateIndicators);
this->display->setTextColor(Config::ColorCalibrateIndicators);
this->display->setCursor(Metrics::ArrowMargin, Config::DisplayHeight - Metrics::LargeTextLineHeight + Metrics::LargeTextLineYOffset);
this->display->print("OK");
this->drawSetHeight();
} }
// Credit: https://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int bool CalibrateScreen::nextPage()
uint16_t ipow(uint16_t base, uint8_t exp)
{ {
int result = 1; this->step = (CalibrateStep)(this->step + 1);
for (;;) if (this->step < __CalibrateStepCount)
{ return true;
if (exp & 1)
result *= base;
exp >>= 1; Settings.Height.Offset = this->height[CalibrateStepCurrent] - Control.getCurrentMeasurement();
if (!exp) Settings.Height.Maximum = this->height[CalibrateStepMax];
break; Settings.Height.Minimum = this->height[CalibrateStepMin];
writeSettingsHeights();
base *= base; this->screenManager->show<HomeScreen>();
} return false;
return result;
}
void CalibrateScreen::onButton(Button button)
{
switch (button)
{
case Button::Top:
{
uint16_t height = this->height[this->step];
uint16_t increment = ipow(10, 3 - this->editingDigit);
uint16_t modulus = increment * 10;
uint16_t remainder = height % modulus;
uint16_t offset = height - remainder;
height = offset + ((remainder + increment) % modulus);
if (height > 1999)
height %= 2000;
this->height[this->step] = height;
this->drawSetHeight();
break;
}
case Button::Middle:
{
this->editingDigit++;
if (this->editingDigit > 3)
this->editingDigit = 0;
this->drawSetHeight();
break;
}
case Button::Bottom:
{
if (!this->isValidHeight())
return;
this->step = (CalibrateStep)(this->step + 1);
if (this->step == CalibrateStepCount)
{
Settings.Height.Offset = this->height[CalibrateStepCurrent] - Control.getCurrentMeasurement();
Settings.Height.Maximum = this->height[CalibrateStepMax];
Settings.Height.Minimum = this->height[CalibrateStepMin];
writeSettingsHeights();
this->screenManager->show<HomeScreen>();
}
else
{
this->drawTitle();
this->drawSetHeight();
}
break;
}
}
}
void CalibrateScreen::onTick()
{
} }
@ -132,78 +50,20 @@ bool CalibrateScreen::isValidHeight()
} }
void CalibrateScreen::drawTitle() const char* CalibrateScreen::getTitle()
{ {
switch (this->step) switch (this->step)
{ {
case CalibrateStepCurrent: case CalibrateStepCurrent:
this->drawLargeTextLineCentered("Calibrate", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground); return "Calibrate";
break;
case CalibrateStepMin: case CalibrateStepMin:
this->drawLargeTextLineCentered("Minimum", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground); return "Minimum";
break;
case CalibrateStepMax: case CalibrateStepMax:
this->drawLargeTextLineCentered("Maximum", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground); return "Maximum";
break;
default: default:
break; return nullptr;
}
}
void CalibrateScreen::drawSetHeight()
{
char heightText[7];
uint16_t height = this->height[this->step];
if (height > 999)
heightText[0] = '0' + ((height / 1000) % 10);
else
heightText[0] = '0';
heightText[1] = '.';
heightText[2] = '0' + ((height / 100) % 10);
heightText[3] = '0' + ((height / 10) % 10);
heightText[4] = '0' + (height % 10);
heightText[5] = 'm';
heightText[6] = 0;
int16_t textX;
int16_t textY;
uint16_t textW;
uint16_t textH;
this->display->getTextBounds(&heightText[0], 0, 0, &textX, &textY, &textW, &textH);
textX = (Config::DisplayWidth - textW) / 2;
uint8_t editingChar = this->editingDigit;
// Skip the dot
if (editingChar > 0)
editingChar++;
if (this->lastTextWidth > 0)
this->display->fillRect((Config::DisplayWidth - this->lastTextWidth) / 2, Metrics::MiddleLargeTextLineY, this->lastTextWidth, Metrics::LargeTextLineHeight, Config::ColorCalibrateBackground);
this->lastTextWidth = textW;
this->display->setTextColor(this->isValidHeight() ? Config::ColorCalibrateValue : Config::ColorCalibrateInvalidValue);
this->display->setCursor(textX, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineYOffset);
// Draw each character ourselves so we can keep track of where the line should be drawn
for (uint8_t i = 0; i < sizeof(heightText); i++)
{
int16_t cursorStart = this->display->getCursorX();
this->display->print(heightText[i]);
if (i == editingChar)
this->display->drawFastHLine(cursorStart, Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineHeight - 1, this->display->getCursorX() - cursorStart, Config::ColorCalibrateDigitMarker);
} }
} }

View File

@ -2,17 +2,18 @@
#define __screen_calibrate #define __screen_calibrate
#include "include/screenids.h" #include "include/screenids.h"
#include "./baseheightentry.h"
#include "../screen.h" #include "../screen.h"
#include "../control.h" #include "../control.h"
enum CalibrateStep : uint8_t enum CalibrateStep
{ {
CalibrateStepCurrent = 0, CalibrateStepCurrent = 0,
CalibrateStepMax, CalibrateStepMax,
CalibrateStepMin, CalibrateStepMin,
CalibrateStepCount __CalibrateStepCount
}; };
@ -21,27 +22,26 @@ enum CalibrateStep : uint8_t
* Configures the absolute height (calculates the offset) and * Configures the absolute height (calculates the offset) and
* the allowed minimum and maximum absolute height. * the allowed minimum and maximum absolute height.
*/ */
class CalibrateScreen : public BaseScreen class CalibrateScreen : public BaseHeightEntryScreen
{ {
public: public:
CalibrateScreen(ScreenManager* screenManager, Adafruit_GFX* display) : BaseScreen(screenManager, display) { } CalibrateScreen(ScreenManager* screenManager, Adafruit_GFX* display) : BaseHeightEntryScreen(screenManager, display) { }
void onShow();
void onButton(Button button);
void onTick();
ScreenId screenId() { return ScreenId::Calibrate; }; ScreenId screenId() { return ScreenId::Calibrate; };
private: protected:
bool isValidHeight(); void initHeights();
void drawTitle(); uint16_t getHeight() { return this->height[this->step]; }
void drawSetHeight(); void setHeight(uint16_t value) { this->height[this->step] = value; };
bool nextPage();
bool isValidHeight();
const char* getTitle();
private:
CalibrateStep step = CalibrateStepCurrent; CalibrateStep step = CalibrateStepCurrent;
uint16_t height[CalibrateStepCount]; uint16_t height[__CalibrateStepCount];
uint16_t lastTextWidth = 0;
uint8_t editingDigit = 0;
}; };
#endif #endif

View File

@ -1,5 +1,9 @@
#include "./menu.h" #include "./menu.h"
#include "./home.h" #include "./home.h"
#include "./calibrate.h"
#include "./manual.h"
#include "./presets.h"
#include "include/config.h"
#include "include/metrics.h" #include "include/metrics.h"
@ -11,16 +15,102 @@ void MenuScreen::onShow()
this->display->fillScreen(Config::ColorHomeBackground); this->display->fillScreen(Config::ColorHomeBackground);
this->drawLargeTextLineCentered("Menu", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground); this->drawLargeTextLineCentered("Menu", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground);
// TODO: implement MenuScreen for (uint8_t i = 0; i < (uint8_t)MenuItem::__Count; i++)
this->drawMenuItem(i);
} }
void MenuScreen::onButton(Button button) void MenuScreen::onButton(Button button)
{ {
this->screenManager->show<HomeScreen>(); uint8_t previousIndex = this->itemIndex;
switch (button)
{
case Button::Middle:
this->activateMenuItem((MenuItem)this->itemIndex);
return;
case Button::Top:
if (this->itemIndex == 0)
this->itemIndex = (uint8_t)MenuItem::__Count;
this->itemIndex--;
break;
case Button::Bottom:
this->itemIndex++;
if (this->itemIndex == (uint8_t)MenuItem::__Count)
this->itemIndex = 0;
break;
}
this->drawMenuItem(previousIndex);
this->drawMenuItem(this->itemIndex);
} }
void MenuScreen::onTick() void MenuScreen::onTick()
{ {
}
void MenuScreen::drawMenuItem(uint8_t index)
{
uint16_t y = (index + 2) * Metrics::LargeTextLineHeight;
bool isSelected = index == this->itemIndex;
this->drawLargeTextLineCentered(
this->getMenuItemTitle((MenuItem)index), y,
isSelected ? Config::ColorMenuSelectedText : Config::ColorMenuText,
isSelected ? Config::ColorMenuSelectedBackground : Config::ColorMenuBackground);
}
const char* MenuScreen::getMenuItemTitle(MenuItem item)
{
switch (item)
{
case MenuItem::Manual:
return "Manual";
case MenuItem::Presets:
return "Presets";
case MenuItem::Calibrate:
return "Calibrate";
case MenuItem::Exit:
return "Exit";
default:
return nullptr;
}
}
void MenuScreen::activateMenuItem(MenuItem item)
{
switch (item)
{
case MenuItem::Manual:
// TODO: show manual screen
//this->screenManager->show<ManualScreen>();
break;
case MenuItem::Presets:
this->screenManager->show<PresetsScreen>();
break;
case MenuItem::Calibrate:
this->screenManager->show<CalibrateScreen>();
break;
case MenuItem::Exit:
this->screenManager->show<HomeScreen>();
break;
default:
break;
}
} }

View File

@ -6,6 +6,18 @@
#include "../Control.h" #include "../Control.h"
enum class MenuItem
{
Manual = 0,
Presets,
Calibrate,
Exit,
__Count
};
/* /*
* Menu screen * Menu screen
* Shows the menu which allows access to the calibration and * Shows the menu which allows access to the calibration and
@ -23,6 +35,11 @@ class MenuScreen : public BaseScreen
ScreenId screenId() { return ScreenId::Menu; }; ScreenId screenId() { return ScreenId::Menu; };
private: private:
void drawMenuItem(uint8_t index);
const char* getMenuItemTitle(MenuItem item);
void activateMenuItem(MenuItem item);
uint8_t itemIndex = 0;
}; };
#endif #endif

View File

@ -0,0 +1,55 @@
#include "./presets.h"
#include "./home.h"
#include "include/metrics.h"
#include "lib/settings.h"
void PresetsScreen::initHeights()
{
this->height[0] = Settings.Height.Preset[0];
this->height[1] = Settings.Height.Preset[1];
}
bool PresetsScreen::nextPage()
{
this->preset++;
if (this->preset < 2)
return true;
Settings.Height.Preset[0] = this->height[0];
Settings.Height.Preset[1] = this->height[1];
writeSettingsHeights();
this->screenManager->show<HomeScreen>();
return false;
}
void PresetsScreen::setHeight(uint16_t value)
{
if (value < Settings.Height.Minimum)
this->height[this->preset] = Settings.Height.Minimum;
else if (value > Settings.Height.Maximum)
this->height[this->preset] = Settings.Height.Maximum;
else
this->height[this->preset] = value;
}
const char* PresetsScreen::getTitle()
{
switch (this->preset)
{
case 0:
return "Preset 1";
case 1:
return "Preset 2";
default:
return nullptr;
}
}

35
src/lib/screen/presets.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __screen_presets
#define __screen_presets
#include "include/screenids.h"
#include "./baseheightentry.h"
#include "../screen.h"
#include "../control.h"
/*
* Presets screen
* Configures the presets.
*/
class PresetsScreen : public BaseHeightEntryScreen
{
public:
PresetsScreen(ScreenManager* screenManager, Adafruit_GFX* display) : BaseHeightEntryScreen(screenManager, display) { }
ScreenId screenId() { return ScreenId::Presets; };
protected:
void initHeights();
uint16_t getHeight() { return this->height[this->preset]; }
void setHeight(uint16_t value);
bool nextPage();
bool isValidHeight() { return true; }
const char* getTitle();
private:
uint8_t preset = 0;
uint16_t height[2];
};
#endif