const byte KEY = 27;
SemaphoreHandle_t xSemaphore;
void vTaskKey(void *pvParam){
pinMode(KEY, INPUT_PULLUP);
bool key_status = false;
uint32_t key_during = 0;
while(1){
if((digitalRead(KEY) == LOW) && (key_status == false)){
vTaskDelay(pdMS_TO_TICKS(10));
if(digitalRead(KEY) == LOW){
key_status = true;
key_during = micros();
Serial.println("KEY DOWN!");
}
}
if((digitalRead(KEY) == LOW) && (key_status == true)){
if(micros() - key_during > 1000000){
key_during = micros();
Serial.println("KEY LONG PRESSED!");
}
}
if(digitalRead(KEY) == HIGH) {
if(key_status == true){
Serial.println("KEY UP!");
xSemaphoreGive(xSemaphore);
}
key_status = false;
}
}
}
void vTaskDisplay(void *pvParam){
while(1){
if(xSemaphoreTake(xSemaphore, 1000) == pdPASS){
Serial.println("Test OK!");
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
xSemaphore = xSemaphoreCreateBinary();
if(xSemaphore != NULL){
xTaskCreate(vTaskKey,"KEY", 1024, NULL, 1, NULL);
xTaskCreate(vTaskDisplay,"DIS", 1024 *4, NULL , 1, NULL);
}
vTaskDelete(NULL);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}