I probably did not understand the task, according to your second topic, but I will place here a code anyway.
#include "mbed.h"
#define VARIANTSMAX 2
#define LOWTIME 200
#define HIGHTIME 500ms
DigitalOut led1(LED1);
InterruptIn button(BUTTON1);
Timeout tOut;
volatile bool buttonFlag = false;
void PressHandler(){
if(!buttonFlag){
buttonFlag = true;
button.disable_irq();
}
}
volatile bool timeOutFlag = false;
void TimeOutHandler(){
if(!timeOutFlag ){
timeOutFlag = true;
}
}
long Millis(void) {
using namespace std::chrono;
auto now_ms = time_point_cast<milliseconds>(Kernel::Clock::now()); // Convert time_point to one in microsecond accuracy
return now_ms.time_since_epoch().count();
}
int main()
{
printf("Running on Mbed OS %d.%d.%d.\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
printf("Multiple button press test\n");
button.fall(callback(&PressHandler));
int counter = 0;
long last_ms = Millis();
while (true)
{
if(buttonFlag){
long timeTemp = Millis();
if((timeTemp - last_ms) > LOWTIME){
tOut.attach(callback(&TimeOutHandler),HIGHTIME);
if(++counter >= VARIANTSMAX){
tOut.detach();
timeOutFlag = true;
}
}
last_ms = timeTemp;
buttonFlag= false;
button.enable_irq();
}
if(timeOutFlag){
button.disable_irq();
switch (counter) {
case 1:{
printf("Case %d\n", counter);
break;
}
case 2:{
printf("Case %d\n", counter);
break;
}
}
counter = 0;
timeOutFlag = false;
button.enable_irq();
}
//led1 = !led1;
//thread_sleep_for(WAIT_TIME_MS);
}
}
BR, Jan