You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

57 lines
1.1 KiB

#include <Arduino.h>
#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() {
while (millis() - lastUpdate <= 150) delay(150); // make sure to wait for the last pulse
return pulseCounter*5;
}
int Coin::isMoneyInserted() {
if (millis() - lastUpdate <= 1000) return pulseCounter*5;
else return 0;
}
void Coin::setZero() {
pulseCounter=0;
}
void Coin::subtractMoney(int amount) {
pulseCounter -= amount/5;
}