Clock

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

Fork of Internet_LCD_Clock by jim hamblen

Internet_LCD_Clock.cpp

Committer:
ashea6
Date:
2016-04-11
Revision:
3:b4e7f126c80a
Parent:
1:09fcc9b81f23
Child:
5:818735a07b88

File content as of revision 3:b4e7f126c80a:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "uLCD_4DGL.h"
#include "Speaker.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include <vector>
#include <string>

// Internet of Things clock example: LCD time is set via internet NTP time server
uLCD_4DGL lcd(p28,p27,p29); // create a global LCD object
EthernetNetIf eth;
NTPClient ntp;
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
AnalogOut DACout(p18);  //speaker
wave_player waver(&DACout);
PinDetect snooze(p15);  //snooze button
PinDetect off(p16);     //turn alarm off

#define snoozeTime 420

//Global Variables
//variables for SD sound
vector <string> filenames; 
int current = 0;
bool play = false; sd_insert = false;
static int veclen = 5;
FILE *wave_file;
//variables for alarm 
//system time structure
time_t ctTime; //ctTime = current time
// Base alarm time-24 hour clock 
int baseAlarmHour = 0; //0-23
int baseAlarmMin = 0; 
// Current alarm time
int curAlarmHour = 0; //0-23
int curAlarmMin = 0; 

//Check for SD Card
void sd_check(){
    int sdPre = sdd.read();
    while (sdPre == 0){
        lcd.locate(0,0);
        lcd.printf("Insert SD card");
        sdPre = sdd.read();
        wait (.5);
    }
    lcd.cls();
}

//Read File Names
void read_file_names(char *dir)
{
    DIR *dp;
    struct dirent *dirp;
    dp = opendir(dir);
    //read all directory and file names in current directory into filename vector
    while((dirp = readdir(dp)) != NULL) {
        filenames.push_back(string(dirp->d_name));
    }
}

//Play file from SD card
void play_file()
{
    bool* play_point = &play;
    string file_name("/sd/");
    file_name += filenames[current];
    wave_file = fopen(file_name.c_str(),"r");
    while(play){
        waver.play(wave_file, play_point);
    }
    fclose(wave_file);
}

//Interrupt-Snooze Function
void snooze_hit_callback (void)
{
    play = false;
    time_t = newtime;
    struct tm * timeinfo;
    
    newtime = ctTime + snoozeTime;
    timeinfo = localtime (&newtime);
    curAlarmMin = timeinfo.tm_min;
    curAlarmHour = timeinfo.tm_hour;
}

//Interrupt- Off Function 
void off_hit_callback (void)
{
    play = false;
    curAlarmMin = baseAlarmMin;
    curAlarmHour = baseAlarmHour;
}
//Time Compare Function
void timeCompare()
{
  struct tm * timeinfo;
  
  //time(&ctTime);
  timeinfo = localtime (&ctTime);
  if (timeinfo.tm_min == curAlarmMin && timeinfo.tm_hour == curAlarmHour)
  {
      play = true;
      play_file();
  }
}  


int main() {

    snooze.mode(PullUp);
    off.mode(PullUp);
    wait(.01);
    snooze.attach_deasserted(&snooze_hit_callback);
    off.attach_deasserted(&off_hit_callback);
    snooze.setSampleFrequency();
    off.setSampleFrequency();
    
    //clear LCD
    lcd.cls();
    // lcd.printf prints to LCD display;
    lcd.printf("Get IP addr...");
    EthernetErr ethErr = eth.setup();
    //Get an Internet IP address using DHCP
    if (ethErr) {
        //error or timeout getting an IP address
        lcd.cls();
        lcd.printf("Network Error \n\r %d",ethErr);
        return -1;
    }
    lcd.cls();
    lcd.printf("Reading Time...\n\r");
    //specify time server URL
    Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
    //Read time from server
    ntp.setTime(server);
    lcd.printf("Time set");
    //Delay for human time to read LCD display
    wait(1);
    while (1) {
        // loop and periodically update the LCD's time display
        lcd.cls();
        ctTime = time(NULL);
        lcd.printf("UTC:  %s", ctime(&ctTime));
        timeCompare();
        wait(.25);
    }
}