#include #include "coin.h" #include "hardware.h" #include "esp32-hal-timer.h" #include "freertos/FreeRTOS.h" #include "freertos/xtensa_api.h" #include "freertos/task.h" #include "rom/ets_sys.h" #include "soc/timer_group_struct.h" #include "soc/dport_reg.h" #include "esp_attr.h" #include "esp_intr.h" using namespace std; portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; static volatile int pulseCounter; static unsigned long lastUpdate = 0; static void IRAM_ATTR handleInterrupt() { portENTER_CRITICAL_ISR(&mux); pulseCounter++; lastUpdate = millis(); portEXIT_CRITICAL_ISR(&mux); } Coin::Coin(void) { pulseCounter = 0; } void Coin::setup(void) { pinMode(COIN_PULSE_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(COIN_PULSE_PIN), handleInterrupt, FALLING); } int Coin::getMoney() { if (millis() - lastUpdate <= 200) delay(200); // make sure to wait for the last pulse return pulseCounter*5; } void Coin::setZero() { pulseCounter=0; } void Coin::subtractMoney(int amount) { pulseCounter -= amount/5; }