From 721ddce721d0e12f965969d0a9c1a79cbf9a145c Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 22 Jan 2018 18:08:11 +0100 Subject: [PATCH] add coin acceptor --- software/src/coin.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ software/src/coin.h | 14 +++++++++++ software/src/hardware.h | 2 ++ software/src/led.cpp | 5 ++++ software/src/led.h | 1 + software/src/main.cpp | 3 +++ software/src/shelf.cpp | 21 ++++++++++------- 7 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 software/src/coin.cpp create mode 100644 software/src/coin.h diff --git a/software/src/coin.cpp b/software/src/coin.cpp new file mode 100644 index 0000000..07a5162 --- /dev/null +++ b/software/src/coin.cpp @@ -0,0 +1,52 @@ +#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; +} diff --git a/software/src/coin.h b/software/src/coin.h new file mode 100644 index 0000000..d866482 --- /dev/null +++ b/software/src/coin.h @@ -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 */ diff --git a/software/src/hardware.h b/software/src/hardware.h index 9d9a0c3..332c936 100644 --- a/software/src/hardware.h +++ b/software/src/hardware.h @@ -21,6 +21,8 @@ static constexpr bool PROGMEM MQTT_SECURE = true; static const char* PROGMEM mqtt_username = "vending"; static const char* PROGMEM mqtt_password = "password"; +static constexpr uint8_t PROGMEM COIN_PULSE_PIN = 15; + #endif /* _HARDWARE_H */ /* ******** pin diagram (esp32-st) ******** diff --git a/software/src/led.cpp b/software/src/led.cpp index 756a644..f89d0c1 100644 --- a/software/src/led.cpp +++ b/software/src/led.cpp @@ -43,6 +43,10 @@ void Led::loop_dispense(void) { leds[NUM_LEDS-2] = CRGB::White; } +void Led::loop_denied(void) { + fill_solid(leds, NUM_LEDS, CRGB::Red); +} + void Led::loop_steady(void) { fill_solid(leds, NUM_LEDS, color); } @@ -55,6 +59,7 @@ void Led::changeAnimation(uint8_t num, uint16_t duration) { else if (num == 1) refresh = &Led::loop_dispense; else if (num == 2) refresh = &Led::loop_confetti; else if (num == 3) refresh = &Led::loop_rainbow; + else if (num == 4) refresh = &Led::loop_denied; } void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) { diff --git a/software/src/led.h b/software/src/led.h index 4e64d6e..b029c09 100644 --- a/software/src/led.h +++ b/software/src/led.h @@ -25,6 +25,7 @@ class Led { void loop_rainbow(void); void loop_confetti(void); void loop_dispense(void); + void loop_denied(void); uint16_t duration; uint8_t index; bool change; diff --git a/software/src/main.cpp b/software/src/main.cpp index f06cf45..d2c1ef8 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -13,6 +13,7 @@ #include "pusher.h" #include "shelf.h" #include "led.h" +#include "coin.h" #include "MyMQTT.h" #define LED_BUILTIN 2 @@ -24,6 +25,7 @@ volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex(); Wifi wifi; //Webserver webserver; MyMQTT mqtt; +Coin coin; Pusher pusher; Shelf* shelf; Led led; @@ -48,6 +50,7 @@ void setup() // delay(1000); shelf = new Shelf(8); pusher.setup(); + coin.setup(); // webserver.start(); } diff --git a/software/src/shelf.cpp b/software/src/shelf.cpp index 6d85f84..629aead 100644 --- a/software/src/shelf.cpp +++ b/software/src/shelf.cpp @@ -2,11 +2,13 @@ #include "item.h" #include "pusher.h" #include "led.h" +#include "coin.h" using namespace std; extern Pusher pusher; extern Led led; +extern Coin coin; Shelf::Shelf(int size) : num_items(size) { @@ -27,14 +29,17 @@ void Shelf::reload(void) { void Shelf::dispenseTask(void* parameters) { Serial.println("dispensing"); - item.at(num)->quantity--; - led.changeAnimation(1, 0); - pusher.push(num); - vTaskDelay(500 / portTICK_PERIOD_MS); - led.changeAnimation(2, 0); - vTaskDelay(7000 / portTICK_PERIOD_MS); - led.changeAnimation(0, 0); - + if (coin.getMoney() > 50) { + item.at(num)->quantity--; + led.changeAnimation(1, 0); + pusher.push(num); + vTaskDelay(500 / portTICK_PERIOD_MS); + led.changeAnimation(2, 0); + vTaskDelay(7000 / portTICK_PERIOD_MS); + led.changeAnimation(0, 0); + } else { + led.changeAnimation(4,500); + } vTaskDelete(NULL); }