Group assignment alarm clock

Dependencies:   mbed TextLCD

main.cpp

Committer:
mmlhein
Date:
2019-05-22
Revision:
4:61d3a7c1b411
Parent:
0:65f054e83dac
Child:
5:fc218b8312eb

File content as of revision 4:61d3a7c1b411:

#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p19, p20, p21, p22, p23, p24);
Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer;
// Initially we set joystick to intterupt.
InterruptIn joycenter(p14);
InterruptIn up(p15);
InterruptIn down(p12);
InterruptIn left(p13);
InterruptIn right(p16);
Serial pc(USBTX, USBRX); // tx, rx

int row, col, r_hour, r_min, r_sec, r_apm, epoch;
bool state_init;

void blink_cursor(){
    lcd.locate(col, row);
    lcd.printf("_");
    }
void blink_num()
{
    
    if (r_hour > 9){
        lcd.locate(3,0);
        lcd.printf("%d:", r_hour);
    }
    else{
        lcd.locate(3,0);
        lcd.printf("0%d:", r_hour);
    }
    if (r_min > 9){
        lcd.locate(6,0);
        lcd.printf("%d:", r_min);
    }
    else{
        lcd.locate(6,0);
        lcd.printf("0%d:", r_min);
    }
    if (r_sec > 9){
        lcd.locate(9,0);
        lcd.printf("%d", r_sec);
    }
    else{
        lcd.locate(9,0);
        lcd.printf("0%d", r_sec);
    }
    lcd.locate(12,0);
    if (r_apm ==0)
        lcd.printf("AM");
    else
        lcd.printf("PM");
    lcd.locate(3,1);
    lcd.printf("Done");
    return;
}


void cursor_left()
{
    if(state_init) {
        if (col >0 && col <13)
            if (col == 12)
                col = 10;
            else if (col ==10)
                col = 7;
            else if (col ==7)
                col = 4;
            else if (col == 4) {
                col =3;
                row =1;
            } else if (col ==3 && row == 1) {
                col = 12;
                row = 0;
            } else
                col = 4;
        else
            col = 4;
    }
    pc.printf("Cursor left: %i,%i\r\n", col, row);

}


void cursor_right()
{
    if(state_init) {
        if (col >0 && col <13)
            if (col == 12){
                col =3;
                row =1;
                }
                
            else if (col ==10)
                col = 12;
            else if (col ==7)
                col = 10;
            else if (col == 4) {
                col = 7;
            } else if (col ==3 && row == 1) {
                col = 4;
                row = 0;
            } else
                col = 4;
        else
            col = 4;
    }
    pc.printf("Cursor right: %i,%i\r\n", col, row);
}

void chg_time_up(){
       if (col ==10){
            r_sec = r_sec +1;
            if (r_sec > 59)
                r_sec = 0;      
        }
        else if (col ==7){
            r_min = r_min + 1;
            if (r_min > 59)
                r_min = 0; }
        else if (col ==4){
            r_hour = r_hour + 1;
            if (r_hour > 12)
                r_hour = 1;} 
        else if (col == 12){
            if(r_apm==0)
                r_apm=1;
            else
                r_apm=0;
            }
        else
            pc.printf("lmfao");
        pc.printf("\nUP: %d:%d:%d\r\n",  r_hour, r_min, r_sec);
}
void chg_time_down()
{
        if (col ==10){
            r_sec = r_sec - 1;
            if (r_sec < 0)
                r_sec = 59;      
        }
        else if (col ==7){
            r_min = r_min - 1;
            if (r_min < 0)
                r_min = 59;} 
        else if (col ==4){
            r_hour = r_hour - 1;
            if (r_hour < 1)
                r_hour = 12; }
        else if (col == 12){
            if(r_apm==0)
                r_apm=1;
            else
                r_apm=0;
            }
        else
            pc.printf("lmfao");
        pc.printf("\nDOWN: %d:%d:%d\r\n",  r_hour, r_min, r_sec);
}

void setup_init(){
    state_init = true;
    
    // Change the cursor value.
    left.rise(&cursor_left);
    right.rise(&cursor_right);
    up.rise(&chg_time_up);
    down.rise(&chg_time_down);
    blinker_cursor.attach(&blink_cursor, 0.1);
    blinker_numer.attach(&blink_num, 0.5);
    while (1){
        // This is where, we select "Done", and we press joycenter to end the initial setup.
        if(col==3 && row==1){
            if(joycenter){
                blinker_cursor.detach();
                blinker_numer.detach();
                
                //Calculate the unix timestamp
                //1h : 3600, 1m: 60, 1s: 1
                if(r_apm==0){// AM
                    if(r_hour ==12)
                        epoch = (0 *3600) + (r_min *60) +(r_sec);
                    else
                        epoch = (r_hour *3600) + (r_min *60) +(r_sec);
                }
                else if (r_apm ==1){//PM
                    if(r_hour ==12)
                        epoch = (r_hour *3600) + (r_min *60) +(r_sec);
                    else
                        epoch = 43200+(r_hour *3600) + (r_min *60) +(r_sec);
                    }
               
                pc.printf("%d", epoch);
                state_init = false;
                break;    
            }
        }    
    }
}
void view_clock()
{
    lcd.locate(3,0);
    time_t seconds = time(NULL);
    char buffer[32];
    strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
    lcd.printf("%s", buffer);
}

int main(){

    
    // Initial value
    row =0 ;
    col=0 ;
    r_hour=12 ;
    r_min=0 ;
    r_sec = 0;
    r_apm = 0; //0-AM, 1-PM
    epoch = 0; // 12:00:00 AM
    
    setup_init();
    set_time(epoch);
    clock_viewer.attach(&view_clock,1);
    lcd.cls();
    
    
    
}