diff --git a/Universal_Serial_Adapter/Config.cpp b/Universal_Serial_Adapter/Config.cpp index fcfb3cd..6534798 100644 --- a/Universal_Serial_Adapter/Config.cpp +++ b/Universal_Serial_Adapter/Config.cpp @@ -18,6 +18,18 @@ Config::Config() { currentTimeout = never; } +bool Config::isUIEnabled() { + return uiEnabled; +} + +void Config::enableUI() { + uiEnabled = true; +} + +void Config::disableUI() { + uiEnabled = false; +} + serialmode Config::getSerialMode() { return currentMode; } @@ -34,6 +46,20 @@ timeout Config::getTimeout() { return currentTimeout; } +long Config::getTimeoutMilis() { + switch (currentTimeout) { + case 0: // tenseconds + return 10000; + case 1: // thirtyseconds + return 30000; + case 2: // oneminute + return 60000; + case 3: // fiveminutes + return 300000; + } + return -1; +} + void Config::setMode(serialmode mode) { currentMode = mode; } @@ -55,4 +81,4 @@ void Config::setDefaults() { setLineSpeed(oneNineteenTwoK); setVoltage(onePointEight); setTimeout(thirtyseconds); -} \ No newline at end of file +} diff --git a/Universal_Serial_Adapter/Config.h b/Universal_Serial_Adapter/Config.h index 701a45e..2454428 100644 --- a/Universal_Serial_Adapter/Config.h +++ b/Universal_Serial_Adapter/Config.h @@ -20,6 +20,9 @@ private: linespeed currentLineSpeed; ttlvoltage currentVoltage; timeout currentTimeout; + bool uiEnabled; + + public: Config(); @@ -32,6 +35,10 @@ public: linespeed getLineSpeed(); ttlvoltage getVoltage(); timeout getTimeout(); + long getTimeoutMilis(); + void disableUI(); + void enableUI(); + bool isUIEnabled(); }; -#endif \ No newline at end of file +#endif diff --git a/Universal_Serial_Adapter/Project.h b/Universal_Serial_Adapter/Project.h index c646476..df00414 100644 --- a/Universal_Serial_Adapter/Project.h +++ b/Universal_Serial_Adapter/Project.h @@ -21,6 +21,7 @@ #define pspXPin 0 // After GND / @ edge #define pspYPin 1 // Between VCC and gnd // LCD Pinouts +#define LCD_LITE 26 // Backlight pin #define SD_CS 7 // Chip select line for SD card #define LCD_CS 53 // Chip select line for TFT display #define LCD_DC 9 // Data/command line for TFT diff --git a/Universal_Serial_Adapter/UI.cpp b/Universal_Serial_Adapter/UI.cpp index e84cece..5966db4 100644 --- a/Universal_Serial_Adapter/UI.cpp +++ b/Universal_Serial_Adapter/UI.cpp @@ -7,4 +7,68 @@ use this code however you'd like, just keep this license and attribute. */ - \ No newline at end of file + +#include "Project.h" +#include "UI.h" + +UI::UI(Config* aConfig) { + config = aConfig; + + okButton = new UIButton(okButtonPin, okButtonLed); + cancelButton = new UIButton(cancelButtonPin, cancelButtonLed); + pspJoystick = new UIJoystickPSP(pspXPin, pspYPin); + + lcd = new UILCD(config); + + startUI(); +} + +void UI::startUI() { + enableUI(); + lcd->start(); +} + +void UI::disableUI() { + config->disableUI(); + lcd->turnOff(); + okButton->turnOffLed(); + cancelButton->turnOffLed(); +} + +void UI::enableUI() { + config->enableUI(); + lcd->turnOn(); + okButton->turnOnLed(); + cancelButton->turnOnLed(); +} + +void UI::processInputEvents() { + joyStickEvent = pspJoystick->direction(); + if (joyStickEvent != joyNone) { + if (!config->isUIEnabled()) { + enableUI(); + return; + } + if (DEBUG) { + Serial.print("Joystick Event: "); + Serial.println(joyStickEvent); + } + lcd->handleJoystickEvent(joyStickEvent); + } + + if (okButton->isPressed()) { + if (!config->isUIEnabled()) { + enableUI(); + return; + } + lcd->handleOkButtonEvent(); + } + + if (cancelButton->isPressed()) { + if (!config->isUIEnabled()) { + enableUI(); + return; + } + lcd->handleCancelButtonEvent(); + } +} diff --git a/Universal_Serial_Adapter/UI.h b/Universal_Serial_Adapter/UI.h index 51a1e99..9cbded4 100644 --- a/Universal_Serial_Adapter/UI.h +++ b/Universal_Serial_Adapter/UI.h @@ -16,3 +16,31 @@ // Button's controlling UI // UIButton* okButton = new UIButton(22, 23); // UIButton* cancelButton = new UIButton(24, 25); + +#include "Project.h" +#include "UIButton.h" +#include "UIJoystickPSP.h" +#include "UILCD.h" +#include "Config.h" + +class UI { +private: + UIButton* okButton; + UIButton* cancelButton; + UIJoystickPSP* pspJoystick; + + joyDirection joyStickEvent; + + UILCD* lcd; + + Config* config; + +public: + UI(Config* aConfig); + void startUI(); + + void processInputEvents(); + + void disableUI(); + void enableUI(); +}; diff --git a/Universal_Serial_Adapter/UIButton.cpp b/Universal_Serial_Adapter/UIButton.cpp index 8138fb0..c992fe8 100644 --- a/Universal_Serial_Adapter/UIButton.cpp +++ b/Universal_Serial_Adapter/UIButton.cpp @@ -21,8 +21,6 @@ UIButton::UIButton(int buttonPin, int ledPin) { void UIButton::setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); - - turnOnLed(); } void UIButton::turnOnLed() { diff --git a/Universal_Serial_Adapter/UILCD.cpp b/Universal_Serial_Adapter/UILCD.cpp index 178b487..0b37cb6 100644 --- a/Universal_Serial_Adapter/UILCD.cpp +++ b/Universal_Serial_Adapter/UILCD.cpp @@ -20,6 +20,8 @@ #include "UIJoystickPSP.h" UILCD::UILCD(Config* config) { + pinMode(LCD_LITE, OUTPUT); + this->config = config; tft = new Adafruit_ST7735(LCD_CS, LCD_DC, LCD_RST); @@ -31,11 +33,19 @@ UILCD::UILCD(Config* config) { } } -void UILCD::startUI() { +void UILCD::start() { drawSplashScreen(); drawMainScreen(); } +void UILCD::turnOn() { + digitalWrite(LCD_LITE, HIGH); +} + +void UILCD::turnOff() { + digitalWrite(LCD_LITE, LOW); +} + void UILCD::handleJoystickEvent(joyDirection direction) { if (DEBUG) { Serial.println("begin UILCD::handleJoystickEvent"); diff --git a/Universal_Serial_Adapter/UILCD.h b/Universal_Serial_Adapter/UILCD.h index 251aa8d..c025d85 100644 --- a/Universal_Serial_Adapter/UILCD.h +++ b/Universal_Serial_Adapter/UILCD.h @@ -60,10 +60,12 @@ private: public: UILCD(Config* config); void bmpDraw(char *filename, uint8_t x, uint8_t y); - void startUI(); void handleJoystickEvent(joyDirection aDirection); void handleOkButtonEvent(); void handleCancelButtonEvent(); + void turnOff(); + void turnOn(); + void start(); }; #endif diff --git a/Universal_Serial_Adapter/Universal_Serial_Adapter.ino b/Universal_Serial_Adapter/Universal_Serial_Adapter.ino index be60f8e..611c77e 100644 --- a/Universal_Serial_Adapter/Universal_Serial_Adapter.ino +++ b/Universal_Serial_Adapter/Universal_Serial_Adapter.ino @@ -13,20 +13,19 @@ #include "UIButton.h" #include "UIJoystickPSP.h" #include "UILCD.h" +#include "UI.h" #include #include #include #include -UILCD* lcd; +#include + +UI* ui; Config* config; -UIButton* okButton; -UIButton* cancelButton; -UIJoystickPSP* pspJoystick; - -joyDirection joyStickEvent; +Metro* uiTimeout; void setup() { Serial.begin(9600); @@ -35,30 +34,17 @@ void setup() { config = new Config(); config->setDefaults(); - okButton = new UIButton(okButtonPin, okButtonLed); - cancelButton = new UIButton(cancelButtonPin, cancelButtonLed); - pspJoystick = new UIJoystickPSP(pspXPin, pspYPin); - - lcd = new UILCD(config); - lcd->startUI(); + ui = new UI(config); + + uiTimeout = new Metro(config->getTimeoutMilis(), false); } void loop() { - joyStickEvent = pspJoystick->direction(); - if (joyStickEvent != joyNone) { - if (DEBUG) { - Serial.print("Joystick Event: "); - Serial.println(joyStickEvent); - } - lcd->handleJoystickEvent(joyStickEvent); + // FIXME: Move timer to UI under its own method + if (uiTimeout->check()) { + ui->disableUI(); } - if (okButton->isPressed()) { - lcd->handleOkButtonEvent(); - } - - if (cancelButton->isPressed()) { - lcd->handleCancelButtonEvent(); - } + ui->processInputEvents(); }