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:
emilytrembeth
Date:
2018-11-14
Revision:
4:da63962bc0f1
Parent:
3:ef46c1ddefed
Child:
5:60e116a1e913

File content as of revision 4:da63962bc0f1:

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

//Function declarations
void displayOnLcd();
void updateRealTimeClock(char *buffer);
void getLineFromSerial(char *keyBuffer, int bufferLength);
void displayMessageOnConsole();
void Function1();
void Function2();

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;

Thread t1;
Thread t2;


void displayOnLcd() {
    date_mutex.lock();
    
    myled = !myled;
 
    currentTime = time(NULL);
    strftime(lcdBuffer, 32, "%Y/%m/%d  %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_year = atoi(timeArray[0]) - 1900;
    struct_time.tm_mon  = atoi(timeArray[1]) - 1;
    struct_time.tm_mday = atoi(timeArray[2]);
    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));
 
    printf("Current Time:%s\r\n", lcdBuffer);
    printf("Enter new Time (YYYY/mm/dd HH:MM:SS)\r\n");
    
    date_mutex.unlock(); 
}


void Function1()
{
        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 Function2()
{
    while (true) {    
        char lineBuffer[32];
        char *pointer;
        pointer = lineBuffer;
         // show initial message on console
            displayMessageOnConsole();
            
            // get input from console
            getLineFromSerial(pointer, sizeof(lineBuffer));
             
             myled = !myled;
            // update RTC based on input value
            updateRealTimeClock(pointer);
        while(true) {
            displayOnLcd();
             myled = !myled;
            Thread::wait(1000); 
            }
        }
             
}  

//Main thread
int main() {
   
    lcd.printf("Press a Switch\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(Function1);           
    t2.start(Function2);    
       
}