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.
53 lines
1.0 KiB
53 lines
1.0 KiB
7 years ago
|
#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() {
|
||
|
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;
|
||
|
}
|