u16 getCmdUSB(u8 * buf)
{
    u16 len = 0;
    cmd_info wrap;
	u8 * pBuf = buf;
	u8 nrLongPack = 0;
	u16 limit = 2000;

    pBuf++;        //missing pilot
    wrap.cmd  = *pBuf;
    pBuf  += SIZE_WRAP_CMD;

    pBuf  += SIZE_WRAP_AUX;  //missing aux

    wrap.len = *(u16*)pBuf;   //length
    pBuf  += SIZE_WRAP_LEN;

	buf += SIZE_HEADER_PACKET;

    /*Executing commands and handling data*/
    switch(wrap.cmd)
     {
	   case  CMD_ALL_INFO        :  len = genInfo(buf, 0);  			 break;
       case  CMD_READ_CONFIG     :  len = menuDisplay(buf, &nrLongPack, limit);break;
       case  CMD_WRITE_CONFIG    :  len = writeConfig(wrap.len, buf );   break;
       case  CMD_PSW_CHECK       :  len = checkPsw( buf );  			 break;
       case  CMD_BL_ENTER        :  len = blEnter();  					 break;
       case  CMD_BL_PORTION      :  len = addPortion(wrap.len, buf);     break;
       case  CMD_BL_ADD_PAGE     :  len = addPage(wrap.len, buf);        break;
       case  CMD_BL_CHECK_CRC    :  len = blCheckCRC(buf);  			 break;
	   case  CMD_BL_RESTART      :  len = systemRestartDelayed();  	     break;
	   case  CMD_GET_VERSION     :  len = getVersion(buf);  			 break;
	   case  CMD_ACCEL           :  len = accelHandle(buf);  			 break;
	   case  CMD_TU              :  len = controlTU(buf);  			     break;
	   case  CMD_DEBUG           :  len = debScan(buf); 				 break;
	   case  CMD_QUITTANCE       :  len = quittance();  				 break;
	   case  CMD_GET_BALANCE     :  len = getBalance(buf);               break;
	   case  CMD_GET_GPS_PACKET  :  len = getGpsPacket(buf);             break;
	   case  CMD_SET_CALIBR_KOEF :  len = setCalibrKoef(buf);            break;
	   case  CMD_GET_PHONE_NUMBER:  len = getPhoneNumber(buf);           break;
	   case  CMD_GET_RTC_TIME    :  len = getRTCTime(buf);               break;
	   case  CMD_MK_ZERO_MILEAGE :  len = zeroMileage(buf);              break;

       default :    break;
     }

	len = prepareForSendUSB(buf,len,nrLongPack);
     
    /* todo: should make some error codes and results*/

    return len;
}
u16 getCmd(u8 * buf)
{
    u16 len = 0;
    cmd_info wrap;
	u8 nrLongPack = 0;
	u16 limit = 2000;  //exact

    buf  += SIZE_WRAP_BLOCKID;

    wrap.cmd  = *buf;               //cmd
    buf  += SIZE_WRAP_CMD;

    buf  += SIZE_WRAP_AUX;  //aux

    wrap.len = *(u16*)buf;   //length
    buf  += SIZE_WRAP_LEN;
    
    /*Executing commands and handling data*/
    switch(wrap.cmd)
     {
	   case  CMD_ALL_INFO        :     len = genInfo(buf, 1);    		  break;
       case  CMD_READ_CONFIG     :     len = menuDisplay(buf, &nrLongPack, limit);            break;
       case  CMD_WRITE_CONFIG    :     len = writeConfig(wrap.len, buf ); break;
       case  CMD_PSW_CHECK       :     len = checkPsw( buf );    		  break;
       case  CMD_BL_ENTER        :     len = blEnter();                   break;
       case  CMD_BL_PORTION      :     len = addPortion(wrap.len, buf);   break;
       case  CMD_BL_ADD_PAGE     :     len = addPage(wrap.len, buf);      break;
       case  CMD_BL_CHECK_CRC    :     len = blCheckCRC(buf);    		  break;
	   case  CMD_BL_RESTART   	 :     len = systemRestartDelayed();      break;
	   case  CMD_GET_VERSION  	 :     len = getVersion(buf);    		  break;
	   case  CMD_TU           	 :     len = controlTU(buf);     		  break;
	   case  CMD_QUITTANCE    	 :     len = quittance();                 break;
	   case  CMD_GET_BALANCE     :     len = getBalance(buf);             break;
	   case  CMD_GET_GPS_PACKET  :     len = getGpsPacket(buf);           break;
	   case  CMD_GET_PHONE_NUMBER:     len = getPhoneNumber(buf);         break;
	   case  CMD_GET_RTC_TIME    :     len = getRTCTime(buf);             break;
	   case  CMD_MK_ZERO_MILEAGE :     len = zeroMileage(buf);            break;

							   
       default :    break;
     }

	//if(len)
        len = prepareForSend(bufRxOperGSM,len, 0x01, wrap.cmd, nrLongPack);   /* filling cmd,uid,len crc ...*/
    /* todo: should make some error codes and results*/
    return len;
}
Exemple #3
0
mainWindow::mainWindow() : QMainWindow(),
    ui(new Ui::MainWindow)
{
    this->setMinimumSize(1000,500);
    // Creating the general UI
    ui->setupUi(this);

    // Making last minute adjustments and
    // initializing some parts of the UI
    ui->tabWidget->setCurrentIndex(0);
    ui->fileName->setText("log_file");
    ui->actionDisconnect->setEnabled(false);

    // Creating the logger file instance
    log = new QFile();

    // Creating the session file instance
    session = new QFile("session.dat");


    // Creating the QVector of double needed to store the graphs
    // points
    y[0] = new QVector<double>(2);
    x[0] = new QVector<double>(2);

    x[1] = new QVector<double>(200);

    y[1] = new QVector<double>(200);
    y[2] = new QVector<double>(200);
    y[3] = new QVector<double>(200);

    y[4] = new QVector<double>(200);
    y[5] = new QVector<double>(200);
    y[6] = new QVector<double>(200);

    y[7] = new QVector<double>(200);

    // The SettingsDialog instance handles the serial port configuration
    // window
    settings = new SettingsDialog();
    file_viewer = new FileViewer();

    // The Protocole instance handles the communication through the selected
    // serial port and parses the data received through a RegEx string
    protocole = new Protocole();


    // Now that everything's ready, try restoring
    // an existing session
    restoreSession();

    // Connecting all the commands button
    QSignalMapper * cmdMap = new QSignalMapper(this);

    connect(ui->cmdFreq,SIGNAL(clicked()),cmdMap,SLOT(map()));
    connect(ui->cmdBurst,SIGNAL(clicked()),cmdMap,SLOT(map()));
    connect(ui->cmdStream,SIGNAL(clicked()),cmdMap,SLOT(map()));
    cmdMap -> setMapping (ui->cmdFreq, "FRQ") ;
    cmdMap -> setMapping (ui->cmdBurst, "BST") ;
    cmdMap -> setMapping (ui->cmdStream, "STR") ;
    connect(cmdMap,SIGNAL(mapped(QString)),this,SLOT(sendCommandBox(QString)));

    // Connecting the buttons for RTC sync
    connect(ui->sendTime,SIGNAL(clicked()),this,SLOT(sendRTCTime()));
    connect(ui->getTime,SIGNAL(clicked()),this,SLOT(getRTCTime()));
    connect(ui->syncSysTime,SIGNAL(clicked()),this,SLOT(getSysTime()));

    // Connecting the button to fetch the last logged frames
    // into the console
    connect(ui->logUpdate,SIGNAL(clicked()),this,SLOT(update_c()));

    // Connecting the buttons used to connect to and configure
    // the serial port
    connect(ui->actionConnect, SIGNAL(clicked()), this, SLOT(openSerialPort()));
    connect(ui->actionDisconnect, SIGNAL(clicked()), this, SLOT(closeSerialPort()));
    connect(ui->actionConfigure, SIGNAL(clicked()), settings, SLOT(show()));
    connect(ui->cmdCustomSend,SIGNAL(clicked()),this,SLOT(sendCustomCommand()));

    connect(ui->applyLog, SIGNAL(clicked()),this,SLOT(applyLog()));


    logMap = new QSignalMapper(this);
    logMap->setMapping(ui->viewLog,logDirectory);
    connect(ui->viewLog,SIGNAL(clicked()),logMap,SLOT(map()));
    connect(logMap,SIGNAL(mapped(QString)),file_viewer,SLOT(show_dialog(QString)));

    // Connectring the button to change the log directory
    connect(ui->changeDirectory,SIGNAL(clicked()),this,SLOT(changeLogDirectory()));

    // Creating graphs instances by linking them to UI elements
    graph[0] = ui->graphXY;
    graph[1] = ui->graphXAxis;
    graph[2] = ui->graphYAxis;
    graph[3] = ui->graphZAxis;
    graph[4] = ui->gyroX;
    graph[5] = ui->gyroY;
    graph[6] = ui->gyroZ;
    graph[7] = ui->weightGraph;

    // The pen is used to draw a dot on XY graphs
    QPen pen;
    pen.setColor(QColor(229,115,115,255));
    pen.setWidth(10);
    pen.setCapStyle(Qt::RoundCap);
    // The line is used to draw classic graphs
    QPen line;
    line.setColor(QColor(25,118,210,255));
    line.setWidth(1);
    QPen line_red;
    line_red.setColor(QColor(210,25,118,255));
    line_red.setWidth(1);
    QPen line_green;
    line_green.setColor(QColor(118,210,25,255));
    line_green.setWidth(1);


    // Graph 1 to 6 stores the accelerometer and gyroscope
    // axis, and share similar settings.
    // Their label can be set directly in the next array
    QString label[16] = {
        "",
        "X","Y","Z",    // Accelerometer's three axis
        "X","Y","Z"     // Gyroscope's three axis
    };
    for(int i=1;i<8;i++) {
        // Only one curve on those graphs : axis/time
        graph[i]->addGraph();
        // The datas are stored in x[1] (time) and y[i] vectors
        graph[i]->graph(0)->setData(*x[1],*y[i]);
        graph[i]->graph(0)->setPen(line);
        // Setting the labels of each axis, as well
        // as the range
        graph[i]->xAxis->setLabel("t");
        graph[i]->yAxis->setLabel(label[i]);
        graph[i]->yAxis->setRange(-32000,32000);
        // The X Axis should display time (text)
        graph[i]->setLocale(QLocale(QLocale::English, QLocale::Canada));
        graph[i]->xAxis->setTickLabelType(QCPAxis::ltDateTime);
        graph[i]->xAxis->setDateTimeFormat("hh:mm:ss");
        graph[i]->xAxis->setDateTimeSpec(Qt::OffsetFromUTC);
        // Activating the zoom and drag interraction in vertical mode
        graph[i]->setInteraction(QCP::iRangeDrag, true);
        graph[i]->setInteraction(QCP::iRangeZoom, true);
        graph[i]->yAxis->axisRect()->setRangeDrag(Qt::Vertical);
        graph[i]->yAxis->axisRect()->setRangeZoom(Qt::Vertical);
    }
    graph[2]->graph(0)->setPen(line_green);
    graph[3]->graph(0)->setPen(line_red);
    graph[5]->graph(0)->setPen(line_green);
    graph[6]->graph(0)->setPen(line_red);

    graph[7]->yAxis->setRange(0,150);

    // Populating the XY graph
    // Only one curve on this graph : X axis /Y axis of accelerometer
    graph[0]->addGraph();
    // The datas are stored in x[0] and y[0] vectors
    graph[0]->graph(0)->setData(*x[0],*y[0]);
    // Draw a red dot
    graph[0]->graph(0)->setPen(pen);
    // Setting the labels of each axis, as well
    // as the range
    graph[0]->xAxis->setLabel("X");
    graph[0]->yAxis->setLabel("Y");
    graph[0]->xAxis->setRange(-10000,10000);
    graph[0]->yAxis->setRange(-10000,10000);
    // Activating the zoom and drag interraction in vertical and horizontal mode
    graph[0]->setInteraction(QCP::iRangeDrag, true);
    graph[0]->setInteraction(QCP::iRangeZoom, true);

    // For each graph, making the lines lighter in order to clean up the view
    for(int i=0;i<8;i++) {
        graph[i]->xAxis->setBasePen(QPen(QColor(195,195,195)));
        graph[i]->xAxis->setTickPen(QPen(QColor(195,195,195)));
        graph[i]->xAxis->setSubTickPen(QPen(QColor(195,195,195)));
        graph[i]->yAxis->setBasePen(QPen(QColor(195,195,195)));
        graph[i]->yAxis->setTickPen(QPen(QColor(195,195,195)));
        graph[i]->yAxis->setSubTickPen(QPen(QColor(195,195,195)));
    }

    // For every setting that should be saved, connecting the saveSession slot to
    // the state change signal
    connect(ui->appendDate,SIGNAL(stateChanged(int)),this,SLOT(saveSession()));
    connect(ui->logToFile,SIGNAL(stateChanged(int)),this,SLOT(saveSession()));
    connect(ui->fileName,SIGNAL(textChanged(QString)),this,SLOT(saveSession()));
    connect(ui->cmdValue,SIGNAL(valueChanged(int)),this,SLOT(saveSession()));
    connect(settings,SIGNAL(updated()),this,SLOT(saveSession()));

    // Addding Window's title
    setWindowTitle("Doctor's Orders Data Logger");



    // Creating a Timer for frame fetching
    serialFetch = new QTimer();
    serialFetch->setInterval(2);
    connect(serialFetch,SIGNAL(timeout()),protocole,SLOT(fetch()));

    // Creating a Timer for GUI update
    guiUpdate = new QTimer();
    guiUpdate->setInterval(50);
    connect(guiUpdate,SIGNAL(timeout()),this,SLOT(update()));

    // Connecting the serial port to the updateData slot
    connect(protocole,SIGNAL(updateData()),this,SLOT(updateData()));
}
Exemple #4
0
int main(void)
{
    ConfigureOscillator();

    InitApp();
    
    ADCInit();

    LCD_Init(NONE);
    
    __delay_ms(100);
    
    LCD_Clear();
    
    RTCC_Initialize();
    
    InitCustomChars();
    
// ******************************************************************************
    char * WeekDay[7] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
    
    int8_t loopCounter = 12, previousLoopCounter = 12;
    
    int OutAirTemp;                                                     

    int Temp[11];

    int OldTemp[11] = {300};

    _Bool Out[11] = {0};                                                         // Startup values for all outputs, 0 = OFF (Output 0 - 11))
    
    _Bool OutSum = 0, outSumOldState = 0;                                        // Sum of all Outputs, Previous scan OutSum state
    
    unsigned char i = 0;
    
    char reset = 0;

    char TestKey;                                                               // Variable used for Storing Which Menu Key is Pressed

    int internalBGV;
    
    unsigned int backLightTimer = 0;

// ******************************************************************************

    while(1)
    {
        time = getRTCTime();                                                    // get the time
        
        unsigned int timer = 0;                                                 // Used to count up time in a loop, to auto exit if user in a menu too long

// ******************************************************************************
//        OutAirTemp = ((ADCRead(9) - 785)/3.2 - 500);                            // Setup for protoBoard 
//        OutAirTemp = ((ADCRead(9) - 804)/3.178 - 500);                          // Setup for first Board built, Green LED's 
        OutAirTemp = ((ADCRead(9) - 807)/3.196 - 500);                          // Setup for second Board built, Blue LED's  
// ******************************************************************************
        Temp[0] = ADCRead(0);   //Read Deck air temperature Pin 19
// ******************************************************************************
        Temp[1] = ADCRead(4);   //Read Deck floor temperature Pin 23
// ******************************************************************************
        Temp[2] = ADCRead(5);   //Read Utility room floor temperature Pin 24
// ******************************************************************************
        Temp[3] = ADCRead(6);   //Read Entrance floor temperature Pin 25
// ******************************************************************************
        Temp[4] = ADCRead(10);   //Read Master bathroom floor temperature Pin 14
// ******************************************************************************
        Temp[5] = ADCRead(11);   //Read Office floor temperature Pin 11
// ******************************************************************************
        Temp[6] = ADCRead(12);   //Read Craft room floor temperature Pin 10
// ******************************************************************************
        Temp[7] = ADCRead(17);   //Read SE basement bedroom floor temperature Pin 41
// ******************************************************************************
        Temp[8] = ADCRead(18);   //Read Media room floor temperature Pin 42
// ******************************************************************************
        Temp[9] = ADCRead(19);   //Read Garage floor temperature Pin 43
// ******************************************************************************
        Temp[10] = ADCRead(20);   //Read Garage room air temperature Pin 44
// ******************************************************************************
        internalBGV = ADCRead(0x1A);
        
         for(i = 0;i<11;++i)
        {
        Temp[i] = TempCalc(Temp[i]);
        
        if(Temp[i] > OldTemp[i] + 1 || Temp[i] < OldTemp[i] - 1)
        {
            Temp[i] = ((OldTemp[i] + Temp[i])/2);
            OldTemp[i] = Temp[i];
        }
        else Temp[i] = OldTemp[i];
        }
// ******************************************************************************
        for(i=0;i<11;i++)
        {
            if(OutAirTemp <= -250)
            {
                Bias[i] = eepromGetData(biasNeg25[i]);
            }
            
            else if (OutAirTemp > -250 && OutAirTemp <= -240)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -240 && OutAirTemp <= -150)
            {
                Bias[i] = eepromGetData(biasNeg15[i]);
            }
            
            else if (OutAirTemp > -150 && OutAirTemp <= -140)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -140 && OutAirTemp <= -50)
            {
                Bias[i] = BiasNeg5[i];
            }

            else if (OutAirTemp > -50 && OutAirTemp <= -40)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -40 && OutAirTemp <= 0)
            {
                Bias[i] = Bias0[i];
            }
    
            else if (OutAirTemp > 0 && OutAirTemp <= 10)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > 10 && OutAirTemp <= 50)
            {
                Bias[i] = Bias5[i];
            }

            else if (OutAirTemp > 50 && OutAirTemp <= 60)
            {
                Bias[i] = Bias[i];
            }
    
            else 
            {
                Bias[i] = BiasWarm[i];
            }
        }
// ******************************************************************************
        for (i=0;i<11;i++)
        {
            Out[i] = SetOutput(Out[i], eepromGetData(setpoint[i]), Bias[i], Temp[i], eepromGetData(deadband[i]));
        }
// ******************************************************************************
        for(i=0;i<11;i++)
        {
            if (Temp[i] <= eepromGetData(setpoint[i]) + Bias[i])                //If Out is not Off
            {
                outState[i] = 1;
            }
          
            else if (Temp[i] >= eepromGetData(setpoint[i]) + Bias[i] + eepromGetData(deadband[i]))
            {
                outState[i] = 0;
            }
            
            else
            {
                outState[i] = outState[i];
            }
            
            if(outState[i] != 0)
            {
                if (outState[i] != lastOutState[i])                             //If Out changed since last read
                {
                    outStateCounter[i]+=1;                                      //Increment the OutState Counter
                }
            }
            lastOutState[i] = outState[i];                                      //And set them equal to each other, so, it doesn't count again next time through
        }
// ******************************************************************************
        OutSum = Out[1] + Out[2] + Out[3] + Out[4] + Out[5] + Out[6] + Out[7] + Out[8] + Out[9];
        
        if(outSumOldState != OutSum)                                            // OutSum has changed,
        {
            if(OutSum != 0)                                                     // because an Out is turned on
            {
                for(i=0;i<11;i++)
                {
                    if (Temp[i] < eepromGetData(setpoint[i]) + eepromGetData(deadband[i]) + Bias[i])// Check for other PV's below SP + DB + Bias,
                    {
                        Out[i] = 1;                                             // and turn them on.
                    }

                    else
                    {
                        Out[i] = 0;                                             // Turn them off if they are already too hot!!
                    }
                }
            }
            outSumOldState = OutSum;
        }
// ******************************************************************************
        if(Out[0] == 0)                                                         // If Deck Air Temp is NOT calling,
        {
            Out[1] = 0;                                                         // turn OFF Deck Floor Out           
        }
        
        if(Out[10] == 0)                                                        // If Garage Air Temp is NOT calling,
        {
            Out[9] = 0;                                                         // turn OFF Garage Floor Out
        }
        
        DeckFloorOut =          Out[1];
        UtilityRoomFloorOut =   Out[2];
        EntranceFloorOut =      Out[3];
        MasterBathFloorOut =    Out[4];
        OfficeFloorOut =        Out[5];
        CraftRoomFloorOut =     Out[6];
        SEBasementFloorOut =    Out[7]; 
        MediaRoomFloorOut =     Out[8];
        GarageFloorOut =        Out[9];

// ******************************************************************************
        if(previousLoopCounter != loopCounter)
        {
            LCD_Clear();
            mainTimer = 0;
            previousLoopCounter = loopCounter;
        }

        
        if(loopCounter < 11)
        {
            LCD_Set_Cursor(0,0);                                                //LCD Line 0 Display
            LCD_Write_String(desc[loopCounter]);
            LCDWriteStringXY(0,13,"Loop ");
            LCD_Write_Int(loopCounter,2);
            
            LCDWriteStringXY(1,0,"Bs:");                                        //LCD Line 1 SetPoint Display
            LCDWriteDecIntXY(1,3,eepromGetData(setpoint[loopCounter]) + Bias[loopCounter],3);
            LCD_Write_Char(0);
            LCD_Write_Char('C');
            LCDWriteStringXY(1,10,"Set:");
            LCDWriteDecIntXY(1,14,eepromGetData(setpoint[loopCounter]),3);
            LCD_Write_Char(0);
            LCD_Write_Char(67);

            LCDWriteStringXY(2,0,"Temp:");                                      //LCD Line 2 Temperature Display
            LCDWriteDecIntXY(2,5,Temp[loopCounter],3);
            LCD_Write_Char(0);
            LCD_Write_Char(67);

            LCDWriteStringXY(2,12,"Db:");                                       //LCD Line 2 Deadband Display
            LCDWriteDecIntXY(2,15,eepromGetData(deadband[loopCounter]),2);
            LCD_Write_Char(0);
            LCD_Write_Char(67);
            
            LCDWriteIntXY(3,0,outStateCounter[loopCounter],5);
            
            LCDWriteStringXY(3,6,"On: ");                                       //LCD Line 3 Out Display
            
            if(outState[loopCounter] == 1)
            {
                LCD_Write_Char('Y');
            }
            
            else
            {
                LCD_Write_Char('N');
            }

            LCDWriteStringXY(3,12,"FOn: ");
            
            if(outState[loopCounter] == 1 || Out[loopCounter] == 0)
            {
                LCD_Write_Char('N');
            }
            
            else
            {
                LCD_Write_Char('Y');
            }
        }

        if(loopCounter == 11)
        {
            if(mainTimer < 10)
            {
            LCDWriteStringXY(0,0,"Output loop counters");
            }
            if(mainTimer >= 10)
            {
            LCDWriteStringXY(0,0,"Hold Cancel to Reset");
            }
            
            LCDWriteIntXY(1,0,outStateCounter[0],4);
            LCDWriteIntXY(1,5,outStateCounter[1],4);
            LCDWriteIntXY(1,10,outStateCounter[2],4);
            LCDWriteIntXY(1,15,outStateCounter[3],4);
            LCDWriteIntXY(2,0,outStateCounter[4],4);
            LCDWriteIntXY(2,5,outStateCounter[5],4);
            LCDWriteIntXY(2,10,outStateCounter[6],4);
            LCDWriteIntXY(2,15,outStateCounter[7],4);
            LCDWriteIntXY(3,0,outStateCounter[8],4);
            LCDWriteIntXY(3,5,outStateCounter[9],4);
            LCDWriteIntXY(3,10,outStateCounter[10],4);
            LCD_Set_Cursor(3,15);
//            LCD_Write_2VDec_Int(421872/internalBGV,3);                          // Board with Green LCD's
            LCD_Write_2VDec_Int(405070/internalBGV,3);                          // Board with Blue LCD's

            if(TestKey == KEY_CANCEL)
            {
                reset += 1;
                
                if(reset > 10)
                {
                    for(i=0;i<11;i++)
                    {
                        outStateCounter[i]= 0;
                        reset = 0;
                    }
                }
            }
            else
            {
                reset -= 1;
                if(reset < 0)
                {
                    reset = 0;
                }
            }
        }
        
        if(loopCounter == 12)
        {
            if(powerFail == 1)
            {
                if(toggle == 1)
                {
                    LCDWriteIntXY(0,0,time.year,2);
                    LCDWriteStringXY(0,2,"/");
                    LCDWriteIntXY(0,3,time.month,2);
                    LCDWriteStringXY(0,5,"/");
                    LCDWriteIntXY(0,6,time.day,2);
                    LCDWriteStringXY(0,9,WeekDay[time.weekday]);
                    LCDWriteIntXY(0,12,time.hour,2);
                    LCDWriteStringXY(0,14,":");
                    LCDWriteIntXY(0,15,time.minute,2);
                    LCDWriteStringXY(0,17,":");
                    LCDWriteIntXY(0,18,time.second,2);
                }
                
                else 
                {
                    LCDWriteStringXY(0,0,"                    ");
                }
            }
            
            else
            {
                LCDWriteIntXY(0,0,time.year,2);
                LCDWriteStringXY(0,2,"/");
                LCDWriteIntXY(0,3,time.month,2);
                LCDWriteStringXY(0,5,"/");
                LCDWriteIntXY(0,6,time.day,2);
                LCDWriteStringXY(0,9,WeekDay[time.weekday]);
                LCDWriteIntXY(0,12,time.hour,2);
                LCDWriteStringXY(0,14,":");
                LCDWriteIntXY(0,15,time.minute,2);
                LCDWriteStringXY(0,17,":");
                LCDWriteIntXY(0,18,time.second,2);
            }
               
/*            LCDWriteStringXY(1,0,"X Pos:");
            LCDWriteIntXY(1,7,x,5);
            LCDWriteStringXY(1,13,"Col:");
            LCDWriteIntXY(1,18,col,1);

            LCDWriteStringXY(2,0,"Y Pos:");
            LCDWriteIntXY(2,7,y,5);
            LCDWriteStringXY(2,13,"Row:");
            LCDWriteIntXY(2,18,row,1);
*/
            if(mainTimer <= 8)
            {
                LCDWriteStringXY(1,0,"OutSide Temp:");
                LCDWriteSignedDecIntXY(1,13,OutAirTemp,3);
                LCD_Write_Char(0);
                LCD_Write_Char(67);
//                LCDWriteSignedDecIntXY(2,0,ADCRead(6),6);
                LCDWriteStringXY(2,0,"Enter Key Sets Time ");
                LCDWriteStringXY(3,0,"Menu Key Sets Temp ");
            }
            
            if(mainTimer > 8 && mainTimer <= 16)
            {
                LCDWriteStringXY(1,0,"OutSide Temp:");
                LCDWriteSignedDecIntXY(1,13,OutAirTemp,3);
                LCD_Write_Char(0);
                LCD_Write_Char(67);
                LCDWriteStringXY(2,0," <- / -> Keys page ");
                LCDWriteStringXY(3,0," through Loop Info ");
            }
            
            if(mainTimer > 16 && mainTimer <= 20)
            {
                LCDWriteStringXY(1,0,"Left Key to display ");
                LCDWriteStringXY(2,0," all Loop Run-time  ");
                LCDWriteStringXY(3,0,"  Info on One Page ");
            }
        }
        

        if(mainTimer > 19)
        {
            mainTimer = 0;
//            loopCounter = 12;
        }
// ******************************************************************************
        TestKey = menuRead();
// ******************************************************************************
        heartBeat();                                                            // HeartBeat displays the HeartBeat on the LCD,
// ******************************************************************************  but, also increments mainTimer every second 
     
        if(TestKey == KEY_NONE)             // If no key is pressed for 60 seconds
        {                                   // Turn OFF the LCD Backlight
            backLightTimer += 1;
        }
        
        else
        {
            backLightTimer = 0;
        }
        
        if (backLightTimer < 4450)
        {
            backLightOn = 1;
            
        }
        else
        {
            backLightTimer = 4450;
            backLightOn = 0;
        }
            
        
        if (TestKey == KEY_ENTER)
        {
//            TestKey = 9;
            SetTime();
        }

        if (TestKey == KEY_RESET_LCD)
        {
            LCD_Cmd(0x08);
            LCD_Cmd(0x00);
            LCD_Cmd(0x0C|0);      //Enable Display ON with style selected (BLINK, ULINE, BLUL, or NONE))
            LCD_Cmd(0x00);
        }

        if(TestKey == KEY_LEFT)
        {
            loopCounter -=1;
            
            if(loopCounter < 0)
            {
                loopCounter = 12;
            }
        }

        if(TestKey == KEY_RIGHT)
        {
            loopCounter +=1;
            
            if(loopCounter >12)
            {
                loopCounter = 0;
            }
        }

        if (TestKey == KEY_MENU)
        {
            signed char choice = 5;

            while(TestKey != KEY_ENTER)
            {
                TestKey = menuRead();
                
                if(timer > 1000)
                {
                    timer = 0;
                    goto Exit;                                                  //This uses less memory than TestKey = KEY_ENTER
                    
//                    TestKey = KEY_ENTER;                                      // This functions fine, but forces a write to EEProm
                }

                switch(TestKey)
                {
                    case KEY_DOWN:
                    {
                        choice -=1;
                            
                        if (choice < 0)
                        {
                            choice = 0;
                        }
                    }
                    break;

                    case KEY_LEFT:
                    {
                        choice -=1;
                            
                        if (choice < 0)
                        {
                            choice = 0;
                        }
                    }
                    break;
                        
                    case KEY_UP:
                    {
                        choice += 1;
                            
                        if(choice > 10)
                        {
                            choice = 10;
                        }
                    }
                    break;
                        
                    case KEY_RIGHT:
                    {
                        choice += 1;
                            
                        if(choice > 10)
                        {
                            choice = 10;
                        }
                    }
                    break;
                    
                    case KEY_CANCEL:
                    {
                        goto Exit;
                    }
                    break;
                        
                }

                if(timer < 2)
                {
                    LCD_Clear();
                }

                LCD_Set_Cursor(0,0);
                LCD_Write_String("Set Temperature for");
                LCD_Set_Cursor(1,0);
                LCD_Write_String(desc[choice]);
                LCDWriteStringXY(2,0,"Up/Dn Keys to change");
                LCDWriteStringXY(3,0,"Enter Key for Yes   ");
                
                heartBeat();                                                    // HeartBeat displays the HeartBeat on the LCD,
                                                                                // but, also increments mainTimer every second
                timer += 1;
            }
            
//            TestKey = 9;
            
            LCD_Clear();


            LCD_Set_Cursor(0,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(0,strlen(desc[choice]),"Set Tmp");
            eepromPutData(setpoint[choice], TempSetpoint(eepromGetData(setpoint[choice])));
            
            LCD_Set_Cursor(2,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(2,strlen(desc[choice]),"Set DB");
            eepromPutData(deadband[choice], SetDeadband(eepromGetData(deadband[choice])));            

            LCD_Clear();

            LCD_Set_Cursor(0,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(0,strlen(desc[choice]),"15 Bias");
            eepromPutData(biasNeg15[choice], SetBiasNeg15(eepromGetData(biasNeg15[choice])));

            LCD_Set_Cursor(2,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(2,strlen(desc[choice]),"25 Bias");
            eepromPutData(biasNeg25[choice], SetBiasNeg25(eepromGetData(biasNeg25[choice])));
            
            Exit:
                        
            LCD_Clear();
        }
// ******************************************************************************
        ClrWdt();                                                               //Clr (Re-Set) the WatchDog Timer
    }
    return(0);
}