Network, SD, Readall. comments added

Dependencies:   Peripherals Buffer_lib_v2 SD_Lib_EffedUP_ERP Time_Lib_v2 Year3_Version5 BMP280 Network_Lib TextLCD BME280

main.cpp

Committer:
cgogay
Date:
2018-11-23
Revision:
6:2ef9c06ce506
Parent:
5:60e116a1e913
Child:
7:981670f59caf

File content as of revision 6:2ef9c06ce506:

#include "mbed.h"
#include "TextLCD.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
#include "sample_hardware.hpp"

#define Day 1
#define Month 2
#define Year 3
#define Hour  4
#define Minute 5
#define Second 6

//Function declarations
void displayOnLcd();
void updateRealTimeClock(char *buffer);
void getLineFromSerial(char *keyBuffer, int bufferLength);
void displayMessageOnConsole();
void SetingTimeWithButtons ();
void FunctionSensor();
void FunctionTime();

TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4-d7
SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
Serial pc(SERIAL_TX, SERIAL_RX);
time_t currentTime;
char lcdBuffer[32];
DigitalOut myled(LED1);
Ticker ticker;
DigitalIn SWUser(USER_BUTTON); 
Mutex date_mutex;
int setting;
int t;
//InterruptIn SW1;

Thread t1;
Thread t2;


//Main thread
int main() {
   
    lcd.printf("Select Time Option\n\n");
    
    post();
    
    //Initialise the SD card
    if ( sd.init() != 0) {
        printf("Init failed \n");
        errorCode(FATAL);
    } 
    
    //Create a filing system for SD Card
    FATFileSystem fs("sd", &sd);     

    //Open to WRITE
    FILE* fp = fopen("/sd/test.csv","a");
    if (fp == NULL) {
        error("Could not open file for write\n");
        errorCode(FATAL);
    }
    
    // run threads
    t1.start(FunctionSensor);           
    t2.start(FunctionTime);    
       
}
    

void displayOnLcd() {
    date_mutex.lock();
    
    myled = !myled;
 
    currentTime = time(NULL);
    strftime(lcdBuffer, 32, "%d/%m/%Y  %H:%M:%S", localtime(&currentTime));
 
    //lcd.cls();
//    lcd.locate(0,0);
    pc.printf("%s\n\r", lcdBuffer);
 
    myled = !myled;   
    date_mutex.unlock();   
};

void updateRealTimeClock(char *buffer) {
    date_mutex.lock();
    
    char *tp;
    char *timeArray[6];
    int arrayIndex;
    struct tm struct_time;
    
    // extract number from string
    arrayIndex = 0;
    tp = strtok( buffer, " /:-" );
    timeArray[arrayIndex++] = tp;
    printf("%d ", atoi(tp));
    while ( tp != NULL && arrayIndex < 6 ) {
        tp = strtok( NULL," /:-" );
        timeArray[arrayIndex++] = tp;
        if ( tp != NULL ) {
            printf("%d ", atoi(tp));
        }
}
    printf("\r\n");
    
    // store number into time struct   
    struct_time.tm_mday = atoi(timeArray[0]);
    struct_time.tm_mon  = atoi(timeArray[1]) - 1;
    struct_time.tm_year = atoi(timeArray[2]) - 1900;
    struct_time.tm_hour = atoi(timeArray[3]);
    struct_time.tm_min  = atoi(timeArray[4]);
    struct_time.tm_sec  = atoi(timeArray[5]);
 
    currentTime = mktime(&struct_time);
    set_time(currentTime);
    
    date_mutex.unlock(); 
}

void getLineFromSerial(char *keyBuffer, int bufferLength)
{
    date_mutex.lock();
    
    char c;
    int index = 0;
 
    for (;;) {
        // break if keyBuffer is full
        if (index >= bufferLength) {
            break;
        }
 
        // read input
        c = pc.getc();
        //printf(" %d ", c);
        pc.putc(c);
        
        // break if end
        if (c == '\r') {
            keyBuffer[index++] = c;
            printf("\n");
            break;
        }
 
        // store in keyBuffer
        keyBuffer[index++] = c;    
        
        date_mutex.unlock();     
    }
}

void displayMessageOnConsole() {
    date_mutex.lock();
    
    currentTime = time(NULL);
    //strftime(lcdBuffer, 32, "%Y/%m/%d %H:%M:%S", localtime(&currentTime));
    strftime(lcdBuffer, 32, "%d/%m/%Y %H:%M:%S", localtime(&currentTime));
 
    printf("Current Time:%s\r\n", lcdBuffer);
    printf("Enter new Time (dd/mm/YYYY HH:MM:SS)\r\n");
    
    date_mutex.unlock(); 
}

void SetingTimeWithButtons () {
        
   int day = 0;
   int month = 0;
   int year = 2017;
   int hour = 0;
   int min = 0;
   int sec = 0;
   
   setting = Day;
   t = 1;
   
   while(t == 1) {
        switch (setting) {
        case Day:
           if (SW1 == 1) {
                wait(0.2);
                day = day + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Day = %d\n", day);
                }
                    if (SW2 == 1) {
                        setting = Month;
                        wait(0.5);
                }
        break;       
                
        case Month:
            if (SW1 == 1) {
                wait(0.2);
                month = month + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Month = %d\n", month);
                }
            if (SW2 == 1) {
                setting = Year;
                wait(0.5);
                        }
        break;                
            
        case Year:
            if (SW1 == 1) {
                wait(0.2);
                year = year + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Year = %d\n", year);
                }
            if (SW2 == 1) {
                setting = Hour;
                wait(0.5);
                }
        break;        
                        
        case Hour:
            if (SW1 == 1) {
                wait(0.2);
                hour = hour + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Hour = %d\n", hour);
                }
            if (SW2 == 1) {
                setting = Minute;
                wait(0.5);
                        } 
             break;                      
                        
        case Minute:
            if (SW1 == 1) {
                wait(0.2);
                min = min + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Minute = %d\n", min);
                }
            if (SW2 == 1) {
                setting = Second;
                wait(0.5);
            } 
         break; 
         
         case Second:
            if (SW1 == 1) {
                wait(0.2);
                sec = sec + 1;
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Second = %d\n", sec);
                }
            if (SW2 == 1) {
                wait(0.5);
                t = 0;
                }                               
                 break;       
            }
      }
        
        struct tm struct_time;  
                
     //store number into time struct   
        struct_time.tm_year = year - 1900;
        struct_time.tm_mon  = month;
        struct_time.tm_mday = day;
        struct_time.tm_hour = hour;
        struct_time.tm_min  = min;
        struct_time.tm_sec  = sec;
     
    // Coverting to time
    time_t currentTime = mktime(&struct_time);
    set_time(currentTime);
        
        }


void FunctionSensor()
{
        while (true) {
            double temp = sensor.getTemperature();
            double pressure = sensor.getPressure();
            lcd.cls();
            lcd.printf("Temp   Pressure\n"); 
            lcd.printf("%6.1f ",temp);
            lcd.printf("%.2f\n",pressure);
            
            //Write to SD
            //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
            
            Thread::wait(15000); //Updates the state every 15 seconds
                        }
}

void FunctionTime()
{
    while (true) {    
        char lineBuffer[32];
        char *pointer;
        pointer = lineBuffer;
        if (SW1 == 1) {                         //Set time on Putty
         // show initial message on console
            displayMessageOnConsole();
            
            // get input from console
            getLineFromSerial(pointer, sizeof(lineBuffer));
             
             myled = !myled;
            // update RTC based on input value
            updateRealTimeClock(pointer);
            }
        if (SW2 == 1) {                     // Set time with Buttons
            pc.printf("Ready");
            wait(0.5);
            SetingTimeWithButtons ();
            }
        while(true) {
            displayOnLcd();
             myled = !myled;
            Thread::wait(1000); 
            }
        }
             
}