standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

main.cpp

Committer:
vtraveller
Date:
2014-08-10
Revision:
11:96146db429de
Parent:
10:3fcab08717fc
Child:
13:9641bc42db92

File content as of revision 11:96146db429de:

#include "mbed.h"
#include "Adafruit_RGBLCDShield.h"
#include "RTclock.h"
#include "extra_chars.h"

#include "module.h"
#include "DateModule.h"
#include "TempModule.h"
#include "TimeModule.h"
#include "TitleModule.h"

Serial pc(SERIAL_TX, SERIAL_RX);

MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
Adafruit_RGBLCDShield lcd(mcp23017);
RTclock rtc(I2C_SDA, I2C_SCL);

const int k_nWidthLCD = 16;
const int k_nHeightLCD = 2;

enum EModes
{
    eModeMenu = 0,
    eModeSelect,
    eModeChange,
    eModeLast
};

void ChangeModule
(
    Module **   in_pModuleList,
    size_t      in_nModuleListSize,
    size_t      in_nMenuPos,
    int         in_nIndexX,
    int         in_nCursorY,
    bool        in_bUp
)
{
    size_t nModule = (in_nMenuPos + in_nCursorY) % in_nModuleListSize;
    in_pModuleList[nModule]->change(in_nIndexX,in_nCursorY,in_bUp);
}

void CreateChars()
{
    //uint8_t k_aUp[] = { 0x4,0xe,0x1f,0x15,0x4,0x4,0x4,0x4 };
    //uint8_t k_aDown[] = { 0x4,0x4,0x4,0x4,0x15,0x1f,0xe,0x4 };
    
    uint8_t k_aUp[] = { 0x0,0x0,0x4,0xe,0x1f,0x0,0x0 };
    uint8_t k_aDown[] = { 0x0,0x0,0x1f,0xe,0x4,0x0,0x0 };
    uint8_t k_aRight[] = { 0x0,0x8,0xc,0xe,0xc,0x8,0x0 };
    uint8_t k_aLeft[] = { 0x0,0x2,0x6,0xe,0x6,0x2,0x0 };
    uint8_t k_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 };

    lcd.createChar(eUp,k_aUp);
    lcd.createChar(eDown,k_aDown);
    lcd.createChar(eRight,k_aRight);
    lcd.createChar(eLeft,k_aLeft);    
    lcd.createChar(eDegree,k_aDegree);    
}

void Initialise()
{
    // Spin up RTC
    rtc.mapTime();
    
    // Initialise LCD
    lcd.begin(k_nWidthLCD,k_nHeightLCD);
    CreateChars();

    lcd.setCursor(0,0);
    lcd._putc(eUp);

    lcd.setCursor(0,1);
    lcd._putc(eDown);    
}

void SetCursor(bool in_bCursor,bool in_bBlink)
{
    if (in_bCursor) lcd.cursor(); else lcd.noCursor();
    if (in_bBlink) lcd.blink(); else lcd.noBlink();     
}

void ShowModules
(
    Module **   in_pModuleList,
    size_t      in_nModuleListSize,
    size_t      in_nPos,
    bool        in_bRefresh = false
)
{
    lcd.setCursor(2,0);
    
    if (in_pModuleList[in_nPos]->canRefresh() || !in_bRefresh) in_pModuleList[in_nPos]->show();
    
    size_t nPos = (in_nPos + 1) % in_nModuleListSize;
    lcd.setCursor(2,1);
    
    if (in_pModuleList[nPos]->canRefresh() || !in_bRefresh) in_pModuleList[nPos]->show();
}

void ShowTracking(bool in_bShow)
{
    if (in_bShow)
    {
        lcd.setCursor(1,0);
        lcd._putc(eLeft);
        
        lcd.setCursor(1,1);
        lcd._putc(eRight);        
    }
    else
    {
        lcd.setCursor(1,0);
        lcd._putc(' ');
        
        lcd.setCursor(1,1);
        lcd._putc(' ');                                       
    }    
}

void UpdateDisplay
(
    Module **   in_pModuleList,
    size_t      in_nModuleListSize,
    size_t      in_nMenuPos,
    int &       inout_nIndexX,
    int         in_nCursorX,
    int &       inout_nCursorY
)
{
    ShowModules(in_pModuleList,in_nModuleListSize,in_nMenuPos);
    
    size_t nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize;
    inout_nIndexX = in_pModuleList[nCurrent]->setCursor(inout_nIndexX,in_nCursorX,inout_nCursorY);
                
    // If we didn't have anything, move the line
    if (-1 == inout_nIndexX)
    {
        inout_nCursorY = (inout_nCursorY + 1) % k_nHeightLCD;
        
        nCurrent = (in_nMenuPos  + inout_nCursorY) % in_nModuleListSize;
        inout_nIndexX = in_pModuleList[nCurrent]->setCursor(0,in_nCursorX,inout_nCursorY);
    }
    
    // If nothing to show - hide everything
    if (-1 == inout_nIndexX) SetCursor(false,false);
}

int main()
{        
    pc.printf("\r\nInitialise LCD\r\n");    
    
    Initialise();    
        
    EModes eMode = eModeMenu;
    size_t nMenuPos = 0;

    // Set up display modules    
    Module * aModules[] =
    {
        new TitleModule(lcd),
        new TimeModule(lcd,rtc),
        new DateModule(lcd,rtc),
        new TempModule(lcd)
    };
    
    ShowModules(aModules,_countof(aModules),nMenuPos);
    
    int nIndexX = 0;
    int nCursorY = 0;
    
    while (true)
    {
        uint8_t nKeys = lcd.readButtons();
        if (nKeys)
        {
            // Change mode based on select
            if (nKeys & BUTTON_SELECT)
            {
                eMode = (EModes)((eMode + 1) % eModeLast);
                
                // Start at top corner
                if (eModeSelect == eMode)
                {
                    nIndexX = 0;
                    nCursorY = 0;
                }
            }

            switch (eMode)
            {                
                case eModeMenu:
                    SetCursor(false,false);
                    ShowTracking(false);
                                        
                    if (nKeys & BUTTON_UP) nMenuPos--;
                    if (nKeys & BUTTON_DOWN) nMenuPos++;
                    
                    nMenuPos = nMenuPos % _countof(aModules);                    
                    break;
                    
                case eModeSelect:
                    SetCursor(true,false);
                    ShowTracking(true);
                                                            
                    if (nCursorY > 0 && (nKeys & BUTTON_UP)) nCursorY--;
                    if ((nCursorY < k_nHeightLCD - 1) && (nKeys & BUTTON_DOWN)) nCursorY++;
                                        
                    if (nKeys & BUTTON_LEFT) nIndexX--;
                    if (nKeys & BUTTON_RIGHT) nIndexX++;
                    break;
                    
                case eModeChange:
                    SetCursor(false,true);
                    ShowTracking(true);
                    
                    if (nKeys & (BUTTON_UP | BUTTON_DOWN))
                    {                    
                        bool bUp = (nKeys & BUTTON_UP) ? true : false;
                        ChangeModule(aModules,_countof(aModules),nMenuPos,nIndexX,nCursorY,bUp);
                    }
                    
                    if (nKeys & BUTTON_LEFT) nIndexX--;
                    if (nKeys & BUTTON_RIGHT) nIndexX++;
                    break;
            }
                        
            UpdateDisplay(aModules,_countof(aModules),nMenuPos,nIndexX,2,nCursorY);
        }
        else
        {
            switch (eMode)
            {
                case eModeMenu:
                    ShowModules(aModules,_countof(aModules),nMenuPos,true);
                    aModules[nMenuPos]->setCursor(nIndexX,2,nCursorY);
                    break;
            }
        }
        
        wait(0.2);
    }
}