Richard Parker / EALCD
Revision:
3:24fbf4dbd7e5
Child:
4:f8f7f4f9c58d
diff -r 2b860e764545 -r 24fbf4dbd7e5 extras/EASettings.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extras/EASettings.cpp	Wed Mar 31 22:22:21 2010 +0000
@@ -0,0 +1,556 @@
+// Copyright 2010 Richard Parker
+
+#include "mbed.h"
+
+#include "EASettings.h"
+
+#include "../screen/EALCD.h"
+#include "../screen/EATouch.h"
+#include "../manager/EATouchManager.h"
+#include "../widgets/EAButton.h"
+
+EASettings::EASettings(EALCD& lcd, EATouch& touch, EATouchManager& manager)
+:   _lcd(lcd),
+    _touch(touch),
+    _manager(manager),
+    _time(NULL),
+    _launchBox(NULL),
+    _returnBox(NULL),
+    _touchBox(NULL),
+    _dateBox(NULL),
+    _compassBox(NULL),
+    _nextBox(NULL),
+    _prevBox(NULL),
+    _incBox(NULL),
+    _decBox(NULL),
+    _state(EASettings::STATE_ENDED)
+{
+    // Actually start the RTC without this call RTC is not running.
+    set_time(0);
+    time_t seconds = time(NULL);
+    _time = localtime(&seconds);
+}
+
+EASettings::~EASettings()
+{
+}
+
+void EASettings::start()
+{
+    _state = EASettings::STATE_IDLE;
+    _drawLaunchButton();
+}
+
+void EASettings::actionHandler(EAHitBox* box, short x, short y)
+{
+    State nextState = EASettings::STATE_IDLE;
+
+    // DEBUG
+    Serial pc(USBTX, USBRX);
+    pc.printf("Hello yessd!!!\r\n");
+    
+    // Disable hit boxs from previous screens.
+    _disableHitBoxs();
+    
+    // Do action depending on the state that in.
+    switch(state())
+    {
+        case EASettings::STATE_IDLE:
+            _drawPortal();
+            nextState = EASettings::STATE_PORTAL;
+        break;
+        case EASettings::STATE_PORTAL:
+            if (box == _returnBox->hitBox())
+            {
+                nextState = EASettings::STATE_ENDED;
+            }
+            else if (box == _touchBox->hitBox())
+            {
+                _touch.calibrate();
+                _drawPortal();
+                nextState = EASettings::STATE_PORTAL;
+            }
+            else if (box == _dateBox->hitBox())
+            {
+                _drawDate();
+                _state = EASettings::STATE_DATE_SECOND;
+                nextState = _nextDate();                
+            } else {
+                _drawPortal();
+                nextState = EASettings::STATE_PORTAL;
+            }
+        break;
+        case EASettings::STATE_DATE_DAY:
+        case EASettings::STATE_DATE_MONTH:
+        case EASettings::STATE_DATE_YEAR:
+        case EASettings::STATE_DATE_HOUR:
+        case EASettings::STATE_DATE_MINUTE:
+        case EASettings::STATE_DATE_SECOND:
+            if (box == _returnBox->hitBox())
+            {
+                _drawPortal();
+                nextState = EASettings::STATE_PORTAL;
+            }
+            else if (box == _nextBox->hitBox())
+            {
+                nextState = _nextDate();
+            }
+            else if (box == _prevBox->hitBox())
+            {
+                nextState = _prevDate();
+            }
+            else if (box == _incBox->hitBox())
+            {
+                nextState = _incDate();
+            }
+            else if (box == _decBox->hitBox())
+            {
+                nextState = _decDate();
+            } else {
+                _drawPortal();
+                nextState = EASettings::STATE_PORTAL;
+            }
+            _enableDateButtons();
+        break;
+        default:
+            nextState = EASettings::STATE_ENDED;
+        break;
+    }
+    
+    _state = nextState;    
+}
+
+void EASettings::_disableHitBoxs()
+{   
+    if (_launchBox != NULL)
+    {
+        _launchBox->setEnabled(false);
+    }
+    
+    if (_returnBox != NULL)
+    {
+        _returnBox->setEnabled(false);
+    }
+
+    if (_touchBox != NULL)
+    {
+        _touchBox->setEnabled(false);
+    }
+
+    if (_dateBox != NULL)
+    {
+        _dateBox->setEnabled(false);
+    }
+    
+    if (_compassBox != NULL)
+    {
+        _compassBox->setEnabled(false);
+    }
+    
+    if (_nextBox != NULL)
+    {
+        _nextBox->setEnabled(false);
+    }
+
+    if (_prevBox != NULL)
+    {
+        _prevBox->setEnabled(false);
+    }
+
+    if (_incBox != NULL)
+    {
+        _incBox->setEnabled(false);
+    }
+
+    if (_decBox != NULL)
+    {
+        _decBox->setEnabled(false);
+    }
+}
+
+void EASettings::_drawLaunchButton()
+{
+    // Setup colours and clear screen to known background.    
+    EAColor white = EAColor(0xFF, 0xFF, 0xFF);
+    EAColor black = EAColor(0x00, 0x00, 0x00);
+
+    EABrush brush = _lcd.brush();
+    EAPen pen = _lcd.pen();
+      
+    brush.setColor(black);
+    _lcd.setBrush(brush);
+
+    pen.setColor(white);
+    _lcd.setPen(pen);
+
+    // Create hit box if not already available.
+    if (_launchBox == NULL)
+    {
+        _launchBox = new EAButton(_manager, _lcd.width()-size(), _lcd.height()-size(), size(), size(), "S", this);
+    } else {
+        _launchBox->setEnabled(true);
+    }
+    _launchBox->paint(_lcd);
+}
+
+void EASettings::_drawPortal()
+{
+    // Setup colours and clear screen to known background.    
+    EAColor white = EAColor(0xFF, 0xFF, 0xFF);
+    EAColor black = EAColor(0x00, 0x00, 0x00);
+    
+    EAFont font;
+    EABrush brush = _lcd.brush();
+    EAPen pen = _lcd.pen();
+      
+    font.load("/local/large.fnt");   
+    _lcd.setFont(font);
+
+    brush.setColor(black);
+    _lcd.setBrush(brush);
+
+    pen.setColor(white);
+    _lcd.setPen(pen);
+    
+    // Clear the screen to draw the buttons on.
+    _lcd.clearScreen();
+
+    // Draw title.
+    _lcd.drawText((_lcd.width()/2)-(size()*2)+50, 10, "Settings");
+   
+    // Create hit box if not already available.
+    if (_compassBox == NULL)
+    {
+        _compassBox = new EAButton(_manager, (_lcd.width()/2)-(size()*2), _lcd.height()-(size()*5), size()*4, size(), "Calibrate Compass", this);
+    } else {
+        _compassBox->setEnabled(true);
+    }
+    _compassBox->paint(_lcd);
+
+    // Create hit box if not already available.
+    if (_dateBox == NULL)
+    {
+        _dateBox = new EAButton(_manager, (_lcd.width()/2)-(size()*2), _lcd.height()-(size()*4), size()*4, size(), "Set Date", this);
+    } else {
+        _dateBox->setEnabled(true);
+    }
+    _dateBox->paint(_lcd);
+
+    // Create hit box if not already available.
+    if (_touchBox == NULL)
+    {
+        _touchBox = new EAButton(_manager, (_lcd.width()/2)-(size()*2), _lcd.height()-(size()*3), size()*4, size(), "Touch Calibration", this);
+    } else {
+        _touchBox->setEnabled(true);
+    }
+    _touchBox->paint(_lcd);  
+
+    // Create hit box if not already available.
+    if (_returnBox == NULL)
+    {
+        _returnBox = new EAButton(_manager, (_lcd.width()/2)-(size()*2), _lcd.height()-(size()*2), size()*4, size(), "Return...", this);
+    } else {
+        _returnBox->setEnabled(true);
+    }
+    _returnBox->paint(_lcd);
+    
+}
+
+void EASettings::_drawDate()
+{
+    // Setup colours and clear screen to known background.    
+    EAColor white = EAColor(0xFF, 0xFF, 0xFF);
+    EAColor black = EAColor(0x00, 0x00, 0x00);
+    
+    EAFont font;
+    EABrush brush = _lcd.brush();
+    EAPen pen = _lcd.pen();
+      
+    font.load("/local/large.fnt");   
+    _lcd.setFont(font);
+
+    brush.setColor(black);
+    _lcd.setBrush(brush);
+
+    pen.setColor(white);
+    _lcd.setPen(pen);
+    
+    // Clear the screen to draw the buttons on.
+    _lcd.clearScreen();
+
+    // Draw title.
+    _lcd.drawText((_lcd.width()/2)-(size()*2)+50, 10, "Date/Time");
+
+    // Draw date.
+    char buffer[5];
+    short y = 60;
+    short x = (_lcd.width()/2)-(size()*2)-25;
+
+    // Update cached time.    
+    time_t seconds = time(NULL);
+    _time = localtime(&seconds);
+    
+    // Put the day of the month into the buffer and display.
+    sprintf(buffer, "%02d", _time->tm_mday);
+    _lcd.drawText(x, y, buffer);
+    
+    // Display spacer.
+    _lcd.drawText(x+20, y, "/");  
+    
+    // Put the month into the buffer and display.
+    sprintf(buffer, "%02d", _time->tm_mon+1);
+    _lcd.drawText(x+30, y, buffer);
+
+    // Display spacer.
+    _lcd.drawText(x+50, y, "/");  
+    
+    // Put the month into the buffer and display.
+    sprintf(buffer, "%04d", _time->tm_year+1900);
+    _lcd.drawText(x+60, y, buffer);
+
+    // Put the hour into the buffer and display.
+    sprintf(buffer, "%02d", _time->tm_hour);
+    _lcd.drawText(x+120, y, buffer);
+    
+    // Display spacer.
+    _lcd.drawText(x+140, y, ":");  
+    
+    // Put the minute into the buffer and display.
+    sprintf(buffer, "%02d", _time->tm_min);
+    _lcd.drawText(x+150, y, buffer);
+
+    // Display spacer.
+    _lcd.drawText(x+170, y, ":");  
+    
+    // Put the minute into the buffer and display.
+    sprintf(buffer, "%02d", _time->tm_sec);
+    _lcd.drawText(x+180, y, buffer);
+
+    _enableDateButtons();     
+    _prevBox->paint(_lcd);
+    _nextBox->paint(_lcd);
+    _decBox->paint(_lcd);
+    _incBox->paint(_lcd);
+    _returnBox->paint(_lcd);
+}
+
+void EASettings::_enableDateButtons()
+{
+    // Create hit box if not already available.
+    if (_prevBox == NULL)
+    {
+        _prevBox = new EAButton(_manager, (_lcd.width()/2)-(size()*3), _lcd.height()-(size()*3.5), size(), size(), "<", this);
+    } else {
+        _prevBox->setEnabled(true);
+    }
+
+    // Create hit box if not already available.
+    if (_nextBox == NULL)
+    {
+        _nextBox = new EAButton(_manager, (_lcd.width()/2)-(size()*1.5), _lcd.height()-(size()*3.5), size(), size(), ">", this);
+    } else {
+        _nextBox->setEnabled(true);
+    }
+
+    // Create hit box if not already available.
+    if (_decBox == NULL)
+    {
+        _decBox = new EAButton(_manager, (_lcd.width()/2)+(size()*0.5), _lcd.height()-(size()*3.5), size(), size(), "-", this);
+    } else {
+        _decBox->setEnabled(true);
+    }
+
+    // Create hit box if not already available.
+    if (_incBox == NULL)
+    {
+        _incBox = new EAButton(_manager, (_lcd.width()/2)+(size()*2), _lcd.height()-(size()*3.5), size(), size(), "+", this);
+    } else {
+        _incBox->setEnabled(true);
+    }
+
+    // Create hit box if not already available.
+    if (_returnBox == NULL)
+    {
+        _returnBox = new EAButton(_manager, (_lcd.width()/2)-(size()*2), _lcd.height()-(size()*2), size()*4, size(), "Return...", this);
+    } else {
+        _returnBox->setEnabled(true);
+    }
+}
+
+EASettings::State EASettings::_incDate()
+{    
+    return state();
+}
+
+EASettings::State EASettings::_decDate()
+{
+
+    return state();
+}
+
+EASettings::State EASettings::_nextDate()
+{
+    short y = 60;
+    short x = (_lcd.width()/2)-(size()*2)-25;
+
+    EASettings::State nextState = EASettings::STATE_IDLE;
+
+    // Setup colours and clear screen to known background.    
+    EAColor white = EAColor(0xFF, 0xFF, 0xFF);
+    EAColor black = EAColor(0x00, 0x00, 0x00);
+    
+    EABrush brush = _lcd.brush();
+    EAPen pen = _lcd.pen();
+      
+    brush.setColor(white);
+    _lcd.setBrush(brush);
+
+    pen.setColor(black);
+    _lcd.setPen(pen);
+
+    // Now clear selection box.
+    switch (state())
+    {
+        default:
+        case EASettings::STATE_DATE_DAY:
+            _lcd.drawRect(x, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MONTH:
+            _lcd.drawRect(x+30, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_YEAR:
+            _lcd.drawRect(x+60, y, 40, 20);
+        break;
+        case EASettings::STATE_DATE_HOUR:
+            _lcd.drawRect(x+120, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MINUTE:
+            _lcd.drawRect(x+150, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_SECOND:
+            _lcd.drawRect(x+180, y, 20, 20);
+        break;
+    }
+
+    brush.setColor(black);
+    _lcd.setBrush(brush);
+
+    pen.setColor(white);
+    _lcd.setPen(pen);
+
+    // Now draw selection box.
+    switch (state())
+    {
+        default:
+        case EASettings::STATE_DATE_DAY:
+            nextState = EASettings::STATE_DATE_MONTH;
+            _lcd.drawRect(x+30, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MONTH:
+            nextState = EASettings::STATE_DATE_YEAR;
+            _lcd.drawRect(x+60, y, 40, 20);
+        break;
+        case EASettings::STATE_DATE_YEAR:
+            nextState = EASettings::STATE_DATE_HOUR;
+            _lcd.drawRect(x+120, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_HOUR:
+            nextState = EASettings::STATE_DATE_MINUTE;
+            _lcd.drawRect(x+150, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MINUTE:
+            nextState = EASettings::STATE_DATE_SECOND;
+            _lcd.drawRect(x+180, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_SECOND:
+            nextState = EASettings::STATE_DATE_DAY;
+            _lcd.drawRect(x, y, 20, 20);
+        break;
+    }
+    
+    return nextState;
+}
+
+EASettings::State EASettings::_prevDate()
+{
+    short y = 60;
+    short x = (_lcd.width()/2)-(size()*2)-25;
+
+    EASettings::State prevState = EASettings::STATE_IDLE;
+
+    // Setup colours and clear screen to known background.    
+    EAColor white = EAColor(0xFF, 0xFF, 0xFF);
+    EAColor black = EAColor(0x00, 0x00, 0x00);
+    
+    EABrush brush = _lcd.brush();
+    EAPen pen = _lcd.pen();
+      
+    brush.setColor(white);
+    _lcd.setBrush(brush);
+
+    pen.setColor(black);
+    _lcd.setPen(pen);
+
+    // Now clear selection box.
+    switch (state())
+    {
+        default:
+        case EASettings::STATE_DATE_DAY:
+            _lcd.drawRect(x, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MONTH:
+            _lcd.drawRect(x+30, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_YEAR:
+            _lcd.drawRect(x+60, y, 40, 20);
+        break;
+        case EASettings::STATE_DATE_HOUR:
+            _lcd.drawRect(x+120, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MINUTE:
+            _lcd.drawRect(x+150, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_SECOND:
+            _lcd.drawRect(x+180, y, 20, 20);
+        break;
+    }
+
+    brush.setColor(black);
+    _lcd.setBrush(brush);
+
+    pen.setColor(white);
+    _lcd.setPen(pen);
+
+    // Now draw selection box.
+    switch (state())
+    {
+        default:
+        case EASettings::STATE_DATE_DAY:
+            prevState = EASettings::STATE_DATE_SECOND;
+            _lcd.drawRect(x+180, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_MONTH:
+            prevState = EASettings::STATE_DATE_DAY;
+            _lcd.drawRect(x, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_YEAR:
+            prevState = EASettings::STATE_DATE_MONTH;
+            _lcd.drawRect(x+30, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_HOUR:
+            prevState = EASettings::STATE_DATE_YEAR;
+            _lcd.drawRect(x+60, y, 40, 20);
+        break;
+        case EASettings::STATE_DATE_MINUTE:
+            prevState = EASettings::STATE_DATE_HOUR;
+            _lcd.drawRect(x+120, y, 20, 20);
+        break;
+        case EASettings::STATE_DATE_SECOND:
+            prevState = EASettings::STATE_DATE_MINUTE;
+            _lcd.drawRect(x+150, y, 20, 20);
+        break;
+    }
+    
+    return prevState;
+}
+