central ctrl

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Fork of 4180_lab3_extra_2 by ECE4180 纯纯组

main.cpp

Committer:
taoqiuyang
Date:
2016-04-11
Revision:
4:89459681a5bc
Parent:
3:a87e5247906f

File content as of revision 4:89459681a5bc:

/*
ME 6408 Final Project
Central Controller for the clock
Using Real Time Operating System
*/

#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#define MAX_SPN_SIZE 29
#include <vector>
#include <sstream>

//#include "uLCD_4DGL.h"
Serial pc(USBTX,USBRX);
Serial SPN(p28, p27); // tx, rx, debug serial
Serial arduino(p9, p10); // flip clock
Serial xbee(p13,p14);     // spining LED

SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card

//uLCD_4DGL lcd(p13, p14, p15);

AnalogOut DACout(p18);
wave_player waver(&DACout);

DigitalIn button_min_set(p21);
DigitalIn button_hr_set(p22);
DigitalIn button_mode_set(p23);
DigitalIn button_right(p24);

DigitalOut debugLED1(LED1);
DigitalOut debugLED2(LED2);
DigitalOut debugLED3(LED3);
DigitalOut debugLED4(LED4);

Mutex Sound_mutex;

int day,month,year,hour,minute,second;
int spin_LED_mode=1;
int chg_mode_cmd_counter=0; //how many times header has been sent, ie,"#MOD,3,0,0"


/*char SPN_message[256];
int  SPN_message_counter=0;

string SPN_H="N/A";
string SPN_M="N/A";
string SPN_S="N/A";

double D_SPN_H=0;
double D_SPN_M=0;
double D_SPN_S=0;
*/

/*vector<string> split(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        if (item.empty()) {
            item = "N/A";
        }
        tokens.push_back(item);
    }
    return tokens;
}



void updateSPN(string SPN_data) {
    string SPN_data_string(SPN_data);
    if (SPN_data_string.substr(0,4) == "#HMS" and SPN_data_string.size() <= MAX_SPN_SIZE) {
        SPN_data_string = SPN_data_string.substr(5);
        vector<string> result = split(SPN_data_string, ',');
        SPN_H = result.at(0);
        D_SPN_H = strtod(SPN_H.c_str(), NULL);
        SPN_M = result.at(1);
        D_SPN_M = strtod(SPN_M.c_str(), NULL);       
        SPN_S = result.at(2).substr(0, result.at(2).size()-1);
        D_SPN_S = strtod(SPN_S.c_str(), NULL);  
        }
}
 */
 
/* 
//#HMS=-183.74,-134.27,-114.39
void SPN_serial_ISR() {
    char buf;
    
     while (SPN.readable()) {
        buf = SPN.getc();
        
        //pc.putc(buf);
        SPN_message[SPN_message_counter]=buf;
        SPN_message_counter+=1;
        
        if (buf=='\n'){
            string SPN_copy(SPN_message, SPN_message_counter);
            //pc.printf("%s", SPN_copy.c_str());
            updateSPN(SPN_copy);
            SPN_message_counter=0; 
            SPN_copy[0] = '\0';
        }

    }
    //led2 = !led2;
}
*/

/*void printStateSPN() {
     //pc.printf("SPN_H: %s, SPN_M: %s, SPN_S: %s\n",SPN_H.c_str(), SPN_M.c_str(), SPN_S.c_str());
     SPN.printf("D_SPN_H: %f, D_SPN_M: %f, D_SPN_S: %f\n",D_SPN_H, D_SPN_M, D_SPN_S);                  
 }*/


void sound_thread(void const *args) {
    
    while (true) {
        Sound_mutex.lock();
        FILE *wave_file;
        wave_file=fopen("/sd/wavfiles/BUZZER.wav","r");
        waver.play(wave_file);
        fclose(wave_file);
        Thread::wait(1000);
        Sound_mutex.unlock();
    }
}



//Thread read push buttons
void push_button_thread(void const *args) {
    while (true) {
        if(button_min_set==0)   {time_t seconds = time(NULL);set_time(seconds+60);}
        if(button_hr_set==0)   {time_t seconds = time(NULL);set_time(seconds+3600);}
        
        //three modes, 1,2,3
        if(button_mode_set==0) {
             spin_LED_mode++;
             chg_mode_cmd_counter=0;
             if (spin_LED_mode>3) {spin_LED_mode=1;}  
        }
        
        
        debugLED2=!debugLED2;
        Thread::wait(200);
    }
}


//Read time from real-time clock
void RTC_thread(void const *args) {
    while (true) {
        time_t seconds = time(NULL);
        
        //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);
        
        //pc.printf("Time as a basic string = %s", ctime(&seconds));
 
        //char buffer[32];
        char buffer[40];
        //strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        //strftime(buffer,40, "%a,%d %m %Y.%H:%M:%S\n", localtime(&seconds));
        strftime(buffer,40, "%d %m %Y.%H:%M:%S\n", localtime(&seconds));
        //pc.printf("Time as a custom formatted string = %s", buffer);
        
        day=(buffer[0]-48)*10+(buffer[1]-48);
        month=(buffer[3]-48)*10+(buffer[4]-48);
        year=(buffer[6]-48)*1000+(buffer[7]-48)*100+(buffer[8]-48)*10+(buffer[9]-48);
        hour=(buffer[11]-48)*10+(buffer[12]-48);
        minute=(buffer[14]-48)*10+(buffer[15]-48);
        second=(buffer[17]-48)*10+(buffer[18]-48);
        //pc.printf("Time a = %d\n", second);
        
        Thread::wait(100);
    }
}

//send commands to peripherals
void communication_thread(void const *args) {
    while (true) {
        //send command to flip clock
        arduino.printf("%d,%d,%d\n",hour,minute,second);
        
        //send command to spin LED
        if (spin_LED_mode==3){
            debugLED3=1;
            if (chg_mode_cmd_counter<5){ //send header for multiple times to ensure it receives
                pc.printf("#MOD,3,0,0\n");
                xbee.printf("#MOD,3,0,0\n");
                chg_mode_cmd_counter++;
            }else{
                pc.printf("#HMS,%d,%d,%d\n",hour,minute,second);                    
                xbee.printf("#HMS,%d,%d,%d\n",hour,minute,second);                    
            }
        }else{
            debugLED3=0;
        }
        
        debugLED4=!debugLED4;
        Thread::wait(100);
    }
}


void setup(){
    
    set_time(1459123200);  // Set RTC time to 3/28/2016 0:00
 
}



int main(){
    setup();
    //SPN.attach(&SPN_serial_ISR);
    
    Thread t1(RTC_thread);
    Thread t2(push_button_thread);
    Thread t3(sound_thread);
    Thread t4(communication_thread);
    
    while (true) {
        
    
        /*LCD_mutex.lock();
          if(LED_flag==1){lcd.filled_circle(circle_pos_x,circle_pos_y ,10, 0x00FF00);}
          if(LED_flag==2){lcd.filled_circle(circle_pos_x,circle_pos_y ,10, 0xFFFFFF);}
        */
          //if(button_right==0){lcd.filled_circle(circle_pos_x,circle_pos_y ,10, 0x000000);circle_pos_x+=10;}
          //if(button_left==0) {lcd.filled_circle(circle_pos_x,circle_pos_y ,10, 0x000000);circle_pos_x-=10;}
        
        /*LCD_mutex.unlock();
        
        int SPN_counter=0;
        while (SPN.readable())
        {
            char buf=SPN.getc();
            SPN_message[SPN_counter]=buf;
            SPN_counter++;
            if (buf=='\n'){SPN_counter=0;}
            
            //SPN.scanf("%s\n", &SPN_message); 
            SPN.printf("%s",SPN_message);
        }
        
        int time_from_PC;
        
        
        for (int i=0;i<=12;i++){
            SPN_message[i]=SPN_message[i]-48;
            for (int j=1;j<=12-i;i++){
                SPN_message[i]=SPN_message[i]*10;
            }
        time_from_PC=time_from_PC+SPN_message[i];
        }
        SPN.printf("%d\n",time_from_PC);
        */
        
        debugLED1=!debugLED1;
        Thread::wait(500);
    }
}