#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_log.h"
#define BTN_0 13
#define BTN_1 14
#define BTN_2 27
#define BTN_3 26
#define LED0 2
#define LED1 4
#define LED2 16
#define LED3 17
int counter;
int time = 0;
int auxTime = 0;
int waitPay = 0; //Timers
//de pago extra
int currentState = 2;
int bounce = 400;
QueueHandle_t handlerQueue;
void cambio(uint8_t count);
static void IRAM_ATTR gpio_interrupt_handler(void *args){
time = esp_timer_get_time() / 1000 ;
if((time - auxTime) >= bounce){
int pinNumber = (int)args;
auxTime = esp_timer_get_time()/1000;
xQueueSendFromISR(handlerQueue, &pinNumber, NULL);
}
}
void posibilidad(void *params){
int pinNumber;
while(true){
if((xQueueReceive(handlerQueue, &pinNumber, portMAX_DELAY))){
switch(pinNumber){
case BTN_0: //1 peso
counter++;
break;
case BTN_1: //5 pesos
counter+=5;
break;
case BTN_2: //10 pesos
counter+=10;
break;
case BTN_3: //20 pesos
counter+=20;
break;
}
waitPay = 0;
currentState = 2;
}
}
}
void cambio(uint8_t count){
count -= 15;
while(count>0){
if(count >= 10){
gpio_set_level(LED2,1);
vTaskDelay(500/portTICK_PERIOD_MS);
gpio_set_level(LED2,0);
count-=10;
}
else if(count >= 5){
gpio_set_level(LED1,1);
vTaskDelay(500/portTICK_PERIOD_MS);
gpio_set_level(LED1,0);
count-=5;
}
else if(count > 1){
while(count>1){
gpio_set_level(LED0,1);
vTaskDelay(500/portTICK_PERIOD_MS);
gpio_set_level(LED0,0);
vTaskDelay(500/portTICK_PERIOD_MS);
count--;
}
}
else if(count == 1){
gpio_set_level(LED0,1);
count--;
}
}
vTaskDelay(4000/portTICK_PERIOD_MS);
gpio_set_level(LED0,0);
gpio_set_level(LED1,0);
gpio_set_level(LED2,0);
gpio_set_level(LED3,0);
//la puerta
}
void app_main() {
gpio_reset_pin(LED0);
gpio_set_direction(LED0,GPIO_MODE_OUTPUT);
//gpio_set_level(LED0,1);
gpio_reset_pin(LED1);
gpio_set_direction(LED1,GPIO_MODE_OUTPUT);
gpio_reset_pin(LED2);
gpio_set_direction(LED2,GPIO_MODE_OUTPUT);
gpio_reset_pin(LED3);
gpio_set_direction(LED3,GPIO_MODE_OUTPUT);
//BOTONES
gpio_reset_pin(BTN_0);
gpio_set_direction(BTN_0, GPIO_MODE_INPUT);
gpio_set_pull_mode(BTN_0,GPIO_PULLDOWN_ONLY);
gpio_set_intr_type(BTN_0, 5);
gpio_reset_pin(BTN_1);
gpio_set_direction(BTN_1, GPIO_MODE_INPUT);
gpio_set_pull_mode(BTN_1,GPIO_PULLDOWN_ONLY);
gpio_set_intr_type(BTN_1, 5);
gpio_reset_pin(BTN_2);
gpio_set_direction(BTN_2, GPIO_MODE_INPUT);
gpio_set_pull_mode(BTN_2,GPIO_PULLDOWN_ONLY);
gpio_set_intr_type(BTN_2, 5);
gpio_reset_pin(BTN_3);
gpio_set_direction(BTN_3, GPIO_MODE_INPUT);
gpio_set_pull_mode(BTN_3,GPIO_PULLDOWN_ONLY);
gpio_set_intr_type(BTN_3, 5);
handlerQueue = xQueueCreate(10, sizeof(int));
xTaskCreate(posibilidad, "posibilidad", 2048, NULL, 1, NULL);
gpio_install_isr_service(0);
gpio_isr_handler_add(BTN_0, gpio_interrupt_handler, (void *)BTN_0);
gpio_isr_handler_add(BTN_1, gpio_interrupt_handler, (void *)BTN_1);
gpio_isr_handler_add(BTN_2, gpio_interrupt_handler, (void *)BTN_2);
gpio_isr_handler_add(BTN_3, gpio_interrupt_handler, (void *)BTN_3);
while(1){
switch(currentState){
case 2: // Revisar conteo
if(waitPay >= 15000){
if(counter >= 15)
currentState = 3;
else
waitPay = 0;
}
vTaskDelay(1/portTICK_PERIOD_MS);
waitPay++;
break;
case 3: // Cambio
gpio_set_level(LED3,1);
cambio(counter);
counter = 0;
currentState = 2;
break;
}
}
}