Hendrik Langer
7 years ago
7 changed files with 90 additions and 8 deletions
@ -0,0 +1,52 @@ |
|||
#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; |
|||
} |
@ -0,0 +1,14 @@ |
|||
#ifndef _COIN_H |
|||
#define _COIN_H |
|||
|
|||
class Coin { |
|||
public: |
|||
Coin(void); |
|||
void setup(void); |
|||
int getMoney(void); |
|||
void setZero(void); |
|||
void subtractMoney(int); |
|||
private: |
|||
}; |
|||
|
|||
#endif /* _COIN_H */ |
Loading…
Reference in new issue