#include <vector>
// input task handler
TaskHandle_t xInputTask;
// timer handlers
TimerHandle_t xOneShotCookingTimer, xOneShotWaitingTimer;
// vector of timers
std::vector<TimerHandle_t> timers;
// utils
uint64_t timerID = 0, currTime, prevTime;
int16_t steps = -1;
bool IDLE = true;
// handle egg type input
void vInputTask(void * param){
String input;
while(true){
if(IDLE){
IDLE = false;
Serial.println("Input jenis telur : ");
Serial.println("- Pipit");
Serial.println("- Puyuh");
Serial.println("- Hantu");
Serial.println("- Onta");
while(Serial.available() <= 0);
input = Serial.readStringUntil('\n');
if(input == "Pipit"){
pipit();
}else if(input == "Puyuh"){
puyuh();
}else if(input == "Hantu"){
hantu();
}else if(input == "Onta"){
onta();
}else{
Serial.println("Wtf telur apa ini!!!");
IDLE = true;
}
}
}
}
// timer finish callbacks
void cooking_cb(TimerHandle_t xTimer){
Serial.println("Masak selesai");
return;
}
void waiting_cb(TimerHandle_t xTimer){
Serial.println("Nunggu selesai");
return;
}
// start timers from timer vector and print progress
void start_timers(){
while(++steps < timers.size()){
xTimerStart(timers.at(steps), 0);
vTaskDelay(1 / portTICK_PERIOD_MS);
// check if timer is running
while(xTimerIsTimerActive(timers.at(steps))){
// calculate time remaining
currTime = (int) std::ceil(pdTICKS_TO_MS(xTimerGetExpiryTime(timers.at(steps)) - xTaskGetTickCount()) / 1000.00);
if(prevTime != currTime && currTime != 0)
Serial.println(String(pcTimerGetName(timers.at(steps))) + String(currTime));
prevTime = currTime;
}
}
}
// cook pipit with the correct time
void pipit(){
xOneShotCookingTimer = xTimerCreate("Cooking Pipit : ", pdMS_TO_TICKS(8000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Pipit : ", pdMS_TO_TICKS(5000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
xOneShotCookingTimer = xTimerCreate("Cooking Pipit : ", pdMS_TO_TICKS(4000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
Serial.println("Pipit dimulai");
start_timers();
Serial.println("Pipit selesai");
timers.clear();
steps = -1;
vTaskDelay(5000 / portTICK_PERIOD_MS);
IDLE = true;
}
// cook puyuh
void puyuh(){
xOneShotCookingTimer = xTimerCreate("Cooking Puyuh : ", pdMS_TO_TICKS(9000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Puyuh : ", pdMS_TO_TICKS(7000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
xOneShotCookingTimer = xTimerCreate("Cooking Puyuh : ", pdMS_TO_TICKS(2000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
Serial.println("Puyuh dimulai");
start_timers();
Serial.println("Puyuh selesai");
timers.clear();
steps = -1;
vTaskDelay(5000 / portTICK_PERIOD_MS);
IDLE = true;
}
// cook hantu
void hantu(){
xOneShotCookingTimer = xTimerCreate("Cooking Hantu : ", pdMS_TO_TICKS(3000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Hantu : ", pdMS_TO_TICKS(11000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
xOneShotCookingTimer = xTimerCreate("Cooking Hantu : ", pdMS_TO_TICKS(7000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Hantu : ", pdMS_TO_TICKS(3000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
Serial.println("Hantu dimulai");
start_timers();
Serial.println("Hantu selesai");
timers.clear();
steps = -1;
vTaskDelay(5000 / portTICK_PERIOD_MS);
IDLE = true;
}
// cook onta
void onta(){
xOneShotCookingTimer = xTimerCreate("Cooking Onta : ", pdMS_TO_TICKS(10000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Onta : ", pdMS_TO_TICKS(5000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
xOneShotCookingTimer = xTimerCreate("Cooking Onta : ", pdMS_TO_TICKS(6000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Onta : ", pdMS_TO_TICKS(3000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
xOneShotCookingTimer = xTimerCreate("Cooking Onta : ", pdMS_TO_TICKS(3000), pdFALSE, (void *)timerID++, cooking_cb);
timers.push_back(xOneShotCookingTimer);
xOneShotWaitingTimer = xTimerCreate("Waiting Onta : ", pdMS_TO_TICKS(2000), pdFALSE, (void *)timerID++, waiting_cb);
timers.push_back(xOneShotWaitingTimer);
Serial.println("Onta dimulai");
start_timers();
Serial.println("Onta selesai");
timers.clear();
steps = -1;
vTaskDelay(5000 / portTICK_PERIOD_MS);
IDLE = true;
}
void setup() {
Serial.begin(115200);
// create task for input
xTaskCreatePinnedToCore(vInputTask, "Input Task", 1000, NULL, 1, &xInputTask, 1);
vTaskDelete(NULL);
}
void loop() {
}