Minh Nguyen / Pushbutton

Pushbutton.cpp

Committer:
khaiminhvn
Date:
2021-03-17
Revision:
7:1dbc0dfc85e0
Parent:
6:8a33efda4eb2
Child:
8:9aaeab58e561

File content as of revision 7:1dbc0dfc85e0:

//INCLUDES
#include "Pushbutton.h"

#define timer_read_s(x)     chrono::duration_cast<chrono::seconds>((x).elapsed_time()).count()

DigitalIn Pushbutton::lastPressed(PIN_ACCEL_PANEL);

//Constructor
Pushbutton::Pushbutton(PinName bt) : button(bt),buttonInt(bt){
    busy = 0;
}
Pushbutton::Pushbutton(PinName bt, int* state, int* flagIn) : buttonInt(bt),button(bt){
    busy = 0;
    flag = flagIn;
    mode = state;
    if(INPUT_LOGIC == 0)
    {
        buttonInt.fall(callback(this,&Pushbutton::updateMode));
    }
    else
    {
        buttonInt.rise(callback(this,&Pushbutton::updateMode));
    }
}

//Methods
//read()
int Pushbutton::read(){
    if(Pushbutton::lastPressed == INPUT_LOGIC && Pushbutton::lastPressed != button)
        return 0;
    else if(Pushbutton::lastPressed == INPUT_LOGIC && Pushbutton::lastPressed == button)
        return 1;
    
    wait_us(DEBOUNCE_TIME);
    
    Pushbutton::lastPressed = (button == INPUT_LOGIC) ? button : Pushbutton::lastPressed;
    busy == (button == INPUT_LOGIC) ? 1 : 0;
    return (button == INPUT_LOGIC) ? 1 : 0;
}

//updateMode()
void Pushbutton::updateMode()
{   
    Timer t;
    t.start();
    
    wait_us(DEBOUNCE_TIME);
    if(read()) //If received input
    {
        *flag = 1;
        while((int)timer_read_s(t) <= TIME_WSETTING) //check if button is held to the threshold
        {
            if(buttonInt.read() != INPUT_LOGIC) //check if button is released
            {
                if(*mode == OP_NORMAL || *mode == OP_WIND)
                {
                    *mode = OP_MANUAL1;
                    return;
                }
                else if(*mode == OP_MANUAL1)
                {
                    *mode = OP_MANUAL2;
                    return;
                }
                else if(*mode == OP_MANUAL2)
                {
                    *mode = OP_MANUAL_ALL;
                    return;
                }
                else if(*mode == OP_MANUAL_ALL || *mode == OP_WSETTING || *mode == OP_CONFIRM)
                {
                    *mode = OP_NORMAL;
                    return;
                }
                else if(*mode == OP_PLACEMENT){
                    *mode = OP_CONFIRM;
                    return;
                }
                else if(*mode == OP_OVERHEAT){
                    return;
                }
                else if(*mode == OP_OVERHEAT_MAN){
                    *mode = OP_OVERHEAT;
                    return;
                }
                else{
                    *mode = OP_PLACEMENT;
                    return;
                }
                t.stop();
                return;
            }
        }
        *mode = OP_WSETTING;
        return;
    }
}