Gerrod Ubben / Mbed 2 deprecated ECE4180_Final_Project

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE RPCInterface

main.cpp

Committer:
robo1340
Date:
2019-11-12
Revision:
12:f1856a0b8ced
Parent:
11:0309bef74ba8
Child:
13:f1649dc31b04

File content as of revision 12:f1856a0b8ced:

#include "mbed.h"
#include "rtos.h"

#include "mbed_rpc.h"
#include "uLCD_4DGL.h"

uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
Serial bluetooth(p13,p14);
Serial pc(USBTX, USBRX);

Mutex stdio_mutex;
Mutex lcd_mutex;

Thread bluetooth_thread;
Thread time_thread;

void writeLCD(Arguments *in, Reply *out);
void setTime (Arguments *in, Reply *out);

//rpc function prototypes
RPCFunction rpcWriteLCD(&writeLCD, "writeLCD");
RPCFunction rpcSetTime(&setTime, "setTime");


void time_thread_func() {
 
    while (true) {
        time_t seconds = time(NULL);
        
        stdio_mutex.lock();
        lcd_mutex.lock();
        
        uLCD.locate(0,0);
        uLCD.printf("%s", ctime(&seconds)); //Time as a basic string
        
        stdio_mutex.unlock();
        lcd_mutex.unlock();
        
        //printf("Time as seconds since January 1, 1970 = %d\n", seconds);
 
        //char buffer[32];
        //strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        //printf("Time as a custom formatted string = %s", buffer);
        
        Thread::wait(1000); //only update every second
    }
    
}


void bluetooth_thread_func() {
    
    //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
    // receive commands, and send back the responses
    char buf[256], outbuf[256];
    uint16_t buf_pos = 0;
    
    while(true) {
        //Thread::wait(20);
        
        if (pc.readable() == true) {
        //if (bluetooth.readable() == true) {
            
            stdio_mutex.lock();
            
            buf[buf_pos] = pc.getc();
            //buf[buf_pos] = bluetooth.getc();
            
            stdio_mutex.unlock();
            
            if (buf[buf_pos] == '\n') { //the end of the RPC command has been received
                buf[buf_pos] = '\0';
                buf_pos = 0;
                RPC::call(buf, outbuf);  //make an RPC call  
                
                stdio_mutex.lock();
                pc.printf("%s\n", outbuf); //send the response
                stdio_mutex.unlock();
            }
            else {
                buf_pos++;
            }
            
        } else { 
            Thread::yield();
        }
        
    }
}

int main() {
    
    bluetooth_thread.start(bluetooth_thread_func);
    time_thread.start(time_thread_func);

}


// Make sure the method takes in Arguments and Reply objects.
void setTime (Arguments *in, Reply *out)   {
    static const char * unix_time_str;
    uint32_t unix_time;
    
    //set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
    unix_time_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored
    unix_time = atoll(unix_time_str);
    
    set_time(unix_time);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
    
}


// Make sure the method takes in Arguments and Reply objects.
void writeLCD (Arguments *in, Reply *out)   {
    static const char * msg_str;
    
    msg_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored
    
    stdio_mutex.lock();
    lcd_mutex.lock();
    
    uLCD.locate(0,2);
    uLCD.printf("%s\r\n",msg_str);
    
    stdio_mutex.unlock();
    lcd_mutex.unlock();
    
}