wave cancel

Dependencies:   4DGL-uLCD-SE EthernetInterface HTTPClient NTPClient PinDetect SDFileSystem mbed-rtos mbed wave_player

Fork of 4180_Final_Project by Adam Zuravleff

main.cpp

Committer:
agamemaker
Date:
2016-04-28
Revision:
11:89e778e985d5
Parent:
10:6b27fafad047
Child:
12:27470e8c0556

File content as of revision 11:89e778e985d5:

//WavePlayer_HelloWorld4180
//internet_clock

#include "mbed.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "uLCD_4DGL.h"
#include "rtos.h"
#include "PinDetect.h"
#include "HTTPClient.h"

//pinouts
// Graphic LCD - TX, RX, and RES pins
uLCD_4DGL uLCD(p28,p27,p29);
SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
Serial pc(USBTX, USBRX);
Serial time_weth(p9, p10);
AnalogOut DACout(p18);
wave_player waver(&DACout);
PinDetect snooze(p19);  //snooze button
PinDetect off(p20);     //turn alarm off
PinDetect settings(p17);     //turn alarm off
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
//Mutex lcd_mutex;

// Parameters
char* time_domain_name = "0.uk.pool.ntp.org";
//char* weth_domain_name = "http://weather.yahooapis.com/forecastrss?w=2502265";
//char* weth_domain_name = "http://developer.mbed.org/media/uploads/mbed_official/hello.txt";
//char* weth_domain_name = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
int port_number = 123;

// Networking
EthernetInterface eth;
#define snoozeTime 10
#define locationLength 50

//global variables
time_t ct_time;
// Base alarm time-24 hour clock
int baseAlarmHour = 0; //0-23
int baseAlarmMin = 0;
// Current alarm time
int curAlarmHour = 18; //0-23
int curAlarmMin = 9;
bool play = true;
//traffic
char location[locationLength];
char destination[locationLength];


//time thread
void time_thread(void const *args)
{
    char time_buffer[80];
    // Loop and update clock
    while (1) {
        //lcd_mutex.lock();
        uLCD.locate(0, 1);
        ct_time = time(NULL);
        strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
                 localtime(&ct_time));
        uLCD.printf("    UTC/GMT:\n%s", time_buffer);
        //lcd_mutex.unlock();
        Thread::wait(100);
    }
}
/*
//communication thread
void comm_thread(void const *args)
{
    time_weth.putc("w");
    time_weth.putc("e");
    time_weth.putc("t");
    time_weth.putc("h");
    for(i = 0; i < locationLength; i++){
        time_weth.putc(location[i]);
        time_weth.putc(destination[i]);
    }  
}
*/

//pushbutton (p19)
void snooze_hit_callback (void)
{
    myled1 = !myled1;
    play = false;
    time_t newtime;
    struct tm * timeinfo;
    newtime = ct_time + snoozeTime;
//time (&newtime);
    timeinfo = localtime (&newtime);
    curAlarmMin = timeinfo->tm_min;
    curAlarmHour = timeinfo->tm_hour;
}

void off_hit_callback (void)
{
    myled2 = !myled2;
    play = false;
    curAlarmMin = baseAlarmMin;
    curAlarmHour = baseAlarmHour;
}

void settings_hit_callback (void)
{
    while(1) {
        char buff[100];
        int i = 0;
        bool flag = false;
        while(!flag) {
            buff[i] = pc.getc();
            pc.putc(buff[i]);
            flag = buff[i] == '^';
            i++;
        }
        uLCD.printf("%s", buff);

        mkdir("/sd/mydir", 0777);
        FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
        if(fp == NULL) {
            error("Could not open file for write\n");
        }
        fprintf(fp, buff);
        fclose(fp);
    }
}

void play_file()
{
    bool* play_point = &play;
    FILE *wave_file;
    printf("\n\n\nHello, wave world!\n");
    wave_file=fopen("/sd/bob.wav","r");
    waver.play(wave_file, play_point);
    fclose(wave_file);
}

void timeCompare()
{
    struct tm * timeinfo;
    timeinfo = localtime (&ct_time);
    if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
        play = true;
        myled3 = true;
        play_file();
    }
}

int main()
{
    snooze.mode(PullUp);
    off.mode(PullUp);
    settings.mode(PullUp);
    wait(0.01);
    snooze.attach_deasserted(&snooze_hit_callback);
    off.attach_deasserted(&off_hit_callback);
    settings.attach_deasserted(&off_hit_callback);
    snooze.setSampleFrequency();
    off.setSampleFrequency();
    settings.setSampleFrequency();

    //play_file();


    // Initialize LCD
    uLCD.baudrate(115200);
    uLCD.background_color(BLACK);
    uLCD.cls();

    // Connect to network and wait for DHCP
    uLCD.locate(0,0);
    uLCD.printf("Getting IP Address\n");
    eth.init();
    if ( eth.connect(60000) == -1 ) {
        uLCD.printf("ERROR: Could not\nget IP address");
        return -1;
    }
    uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
    // Read time from server
    {
        uLCD.printf("Reading time...\n\r");
        NTPClient ntp_client;
        ntp_client.setTime(time_domain_name, port_number);
        uLCD.printf("Time set\n");
    }
    Thread::wait(2000);
    eth.disconnect();

    // Reset LCD
    uLCD.background_color(WHITE);
    uLCD.textbackground_color(WHITE);
    uLCD.color(RED);
    uLCD.cls();
    uLCD.text_height(2);

    Thread t1(time_thread);
    //Thread t2(comm_thread);
    while(true) {
        timeCompare();
        Thread::wait(100);
    }

}