Browse Source

add coin acceptor

main
Hendrik Langer 7 years ago
parent
commit
721ddce721
  1. 52
      software/src/coin.cpp
  2. 14
      software/src/coin.h
  3. 2
      software/src/hardware.h
  4. 5
      software/src/led.cpp
  5. 1
      software/src/led.h
  6. 3
      software/src/main.cpp
  7. 21
      software/src/shelf.cpp

52
software/src/coin.cpp

@ -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;
}

14
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 */

2
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_username = "vending";
static const char* PROGMEM mqtt_password = "password"; static const char* PROGMEM mqtt_password = "password";
static constexpr uint8_t PROGMEM COIN_PULSE_PIN = 15;
#endif /* _HARDWARE_H */ #endif /* _HARDWARE_H */
/* ******** pin diagram (esp32-st) ******** /* ******** pin diagram (esp32-st) ********

5
software/src/led.cpp

@ -43,6 +43,10 @@ void Led::loop_dispense(void) {
leds[NUM_LEDS-2] = CRGB::White; leds[NUM_LEDS-2] = CRGB::White;
} }
void Led::loop_denied(void) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
}
void Led::loop_steady(void) { void Led::loop_steady(void) {
fill_solid(leds, NUM_LEDS, color); 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 == 1) refresh = &Led::loop_dispense;
else if (num == 2) refresh = &Led::loop_confetti; else if (num == 2) refresh = &Led::loop_confetti;
else if (num == 3) refresh = &Led::loop_rainbow; 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) { void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) {

1
software/src/led.h

@ -25,6 +25,7 @@ class Led {
void loop_rainbow(void); void loop_rainbow(void);
void loop_confetti(void); void loop_confetti(void);
void loop_dispense(void); void loop_dispense(void);
void loop_denied(void);
uint16_t duration; uint16_t duration;
uint8_t index; uint8_t index;
bool change; bool change;

3
software/src/main.cpp

@ -13,6 +13,7 @@
#include "pusher.h" #include "pusher.h"
#include "shelf.h" #include "shelf.h"
#include "led.h" #include "led.h"
#include "coin.h"
#include "MyMQTT.h" #include "MyMQTT.h"
#define LED_BUILTIN 2 #define LED_BUILTIN 2
@ -24,6 +25,7 @@ volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex();
Wifi wifi; Wifi wifi;
//Webserver webserver; //Webserver webserver;
MyMQTT mqtt; MyMQTT mqtt;
Coin coin;
Pusher pusher; Pusher pusher;
Shelf* shelf; Shelf* shelf;
Led led; Led led;
@ -48,6 +50,7 @@ void setup()
// delay(1000); // delay(1000);
shelf = new Shelf(8); shelf = new Shelf(8);
pusher.setup(); pusher.setup();
coin.setup();
// webserver.start(); // webserver.start();
} }

21
software/src/shelf.cpp

@ -2,11 +2,13 @@
#include "item.h" #include "item.h"
#include "pusher.h" #include "pusher.h"
#include "led.h" #include "led.h"
#include "coin.h"
using namespace std; using namespace std;
extern Pusher pusher; extern Pusher pusher;
extern Led led; extern Led led;
extern Coin coin;
Shelf::Shelf(int size) Shelf::Shelf(int size)
: num_items(size) { : num_items(size) {
@ -27,14 +29,17 @@ void Shelf::reload(void) {
void Shelf::dispenseTask(void* parameters) { void Shelf::dispenseTask(void* parameters) {
Serial.println("dispensing"); Serial.println("dispensing");
item.at(num)->quantity--; if (coin.getMoney() > 50) {
led.changeAnimation(1, 0); item.at(num)->quantity--;
pusher.push(num); led.changeAnimation(1, 0);
vTaskDelay(500 / portTICK_PERIOD_MS); pusher.push(num);
led.changeAnimation(2, 0); vTaskDelay(500 / portTICK_PERIOD_MS);
vTaskDelay(7000 / portTICK_PERIOD_MS); led.changeAnimation(2, 0);
led.changeAnimation(0, 0); vTaskDelay(7000 / portTICK_PERIOD_MS);
led.changeAnimation(0, 0);
} else {
led.changeAnimation(4,500);
}
vTaskDelete(NULL); vTaskDelete(NULL);
} }

Loading…
Cancel
Save