Kojo / AlarmClock

Dependents:   IF-SmartClock

AlarmClock.cpp

Committer:
takashikojo
Date:
2015-06-25
Revision:
0:d69dc4537754
Child:
2:86a5b9235f16

File content as of revision 0:d69dc4537754:

#include "mbed.h"
#include "DigitalClock.h"
#include "AlarmClock.h"

#if 0
static    DigitalIn alarmButton(D13) ;
static    DigitalIn hourButton(D14) ;
static    DigitalIn minButton(D15) ;
static    PwmOut    tone(PTC11) ;
#endif

AlarmClock::AlarmClock (PinName seg0, PinName seg1, PinName seg2, PinName seg3, 
    PinName seg4, PinName seg5, PinName seg6, PinName dot, 
    PinName digit0, PinName digit1, PinName digit2, PinName digit3,
    PinName alarmB, PinName hourB, PinName minB, PinName toneP
) :
    DigitalClock(seg0, seg1, seg2, seg3, seg4, seg5, seg6, dot, digit0, digit1, digit2, digit3)
{
    struct tm at ;
    alarmB_stat = false ;
    hourB_stat  = false ;
    minB_stat   = false ;
    alarmActive = false ;
    newAlarmTime= false ;

    alarmButton = new DigitalIn(alarmB) ;
    hourButton = new DigitalIn(hourB) ;
    minButton  = new DigitalIn(minB) ;
    tone       = new PwmOut(toneP) ;

    #define JST (9*60*60)
    alarmTime = time(NULL) + JST ;
    at = *localtime(&alarmTime) ;
    alarmTime -= (at.tm_hour*60*60 + at.tm_min*60) ;  /* Alarm time set to 00:00 */
}

AlarmClock::~AlarmClock(){ } ;
#define ALARM_RANGE 60
#define JST (9*60*60)
void AlarmClock::checkAlarmTime(void) {
    time_t ctTime;
    ctTime = time(NULL) + JST ;
    struct tm ct ; struct tm at ;
    //printf("alarmActive=%d, timeMatched=%d, ctTime=%d, alarmTime=%d\n", alarmActive, timeMatched, ctTime, alarmTime) ;
    ct = *localtime(&ctTime) ;
    at = *localtime(&alarmTime) ;
    if((ct.tm_hour == at.tm_hour) && (ct.tm_min == at.tm_min)) 
         timeMatched = true ;
    else timeMatched = false ;
    if(alarmActive && timeMatched)
         alarmTone(true) ;
    else alarmTone(false) ;
}

void AlarmClock::checkAlarmButton() {
    struct tm * alarmT ;
    
    //printf("alarmB_stat=%d, alarmButton=%d", alarmB_stat, *alarmButton) ;
    if(!alarmB_stat && !*alarmButton) {
         stop() ; /* stop updating LED */
         alarmT = localtime(&alarmTime);
         //printf("Alarm Time = %d:%d\n", alarmT->tm_hour, alarmT->tm_min) ;
         setLED(alarmT->tm_hour, alarmT->tm_min) ;
         alarmB_stat = true ;
         newAlarmTime = false ;
         return ;
    } else if(alarmB_stat && *alarmButton) {
        start() ; /* show current time on LED */
        alarmB_stat = false ;
        if(!newAlarmTime)alarmActive = !alarmActive ;
        else             alarmActive = true ;
        //printf("alarmActive=%d\n", alarmActive) ;
        setDot(0, alarmActive) ;
        flashLED() ;
    }
}

#define HOUR (60*60)
void AlarmClock::checkHourButton() {
    struct tm * alarmT ;
    
    //printf("    hourB_stat=%d, hourButton=%d", hourB_stat, hourButton->read()) ;    
    if((!hourB_stat && !*hourButton) && !*alarmButton) {
         alarmTime += HOUR ;
         alarmT = localtime(&alarmTime);
         setLED(alarmT->tm_hour, alarmT->tm_min) ;
         newAlarmTime = true ;
    }
}

#define MIN 60
void AlarmClock::checkMinButton() {
    struct tm * alarmT ;
    
    //printf("    minB_stat=%d, minButton=%d\n", minB_stat, minButton->read()) ;    
    if((!minB_stat && !*minButton) && !*alarmButton) {
         alarmTime += MIN ;
         alarmT = localtime(&alarmTime);
         setLED(alarmT->tm_hour, alarmT->tm_min) ;
         newAlarmTime = true ;
    } 
}

void AlarmClock::checkButtons(void) {
    if((alarmActive && timeMatched) && (!*alarmButton || !*hourButton || !*minButton)){
        alarmActive = false ; 
        setDot(0, alarmActive) ;
    }
    if(alarmActive && timeMatched)
        alarmTone(true) ;
    
    checkAlarmButton() ;
    checkHourButton() ;
    checkMinButton() ;
}

void AlarmClock::alarmTone(bool t)
{
    if(t) {
        tone->write(0.5) ;
        tone->period(0.002) ;
    } else {
        tone->write(0.0) ;
        tone->period(0.0) ;
    }
}

static int count ;
void AlarmClock::poll(void) {
    checkButtons() ; 
    if((count++ % 10) == 0){
        checkAlarmTime() ;
    }
}