Hendrik Langer
7 years ago
11 changed files with 203 additions and 25 deletions
@ -0,0 +1,27 @@ |
|||
#include <Preferences.h> |
|||
#include <nvs_flash.h> |
|||
|
|||
#include "item.h" |
|||
|
|||
using namespace std; |
|||
|
|||
extern volatile SemaphoreHandle_t xPreferencesSemaphore; |
|||
|
|||
Item::Item(int num) { |
|||
if (xSemaphoreTake(xPreferencesSemaphore, portMAX_DELAY) == pdTRUE) { |
|||
Preferences preferences; |
|||
preferences.begin("vending-items", false); |
|||
size_t len; |
|||
char buffer[50]; |
|||
sprintf(buffer, "item%u_name", num); |
|||
preferences.getString(buffer, name, sizeof(name)); |
|||
sprintf(buffer, "item%u_quantity", num); |
|||
quantity = preferences.getUInt(buffer, 0); |
|||
sprintf(buffer, "item%u_price", num); |
|||
price = preferences.getUInt(buffer, 0); |
|||
preferences.end(); |
|||
xSemaphoreGive(xPreferencesSemaphore); |
|||
} else { |
|||
Serial.print("preferences not available"); |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
#ifndef _ITEM_H |
|||
#define _ITEM_H |
|||
|
|||
#include <Preferences.h> |
|||
|
|||
class Item { |
|||
public: |
|||
Item(int); |
|||
char name[16]; |
|||
unsigned int quantity; |
|||
unsigned int price; |
|||
private: |
|||
}; |
|||
|
|||
#endif /* _ITEM_H */ |
@ -0,0 +1,29 @@ |
|||
#include "shelf.h" |
|||
#include "item.h" |
|||
#include "pusher.h" |
|||
|
|||
using namespace std; |
|||
|
|||
extern Pusher pusher; |
|||
|
|||
Shelf::Shelf(int size) |
|||
: num_items(size) { |
|||
for(int i=0; i<size; i++) { |
|||
item.push_back(new Item(i)); |
|||
} |
|||
} |
|||
|
|||
void Shelf::reload(void) { |
|||
for(int i=0; i<num_items; i++) { |
|||
delete item.at(i); |
|||
} |
|||
item.clear(); |
|||
for(int i=0; i<num_items; i++) { |
|||
item.push_back(new Item(i)); |
|||
} |
|||
} |
|||
|
|||
void Shelf::dispense(int num) { |
|||
item.at(num)->quantity--; |
|||
pusher.push(num); |
|||
} |
@ -0,0 +1,17 @@ |
|||
#ifndef _SHELF_H |
|||
#define _SHELF_H |
|||
|
|||
#include <vector> |
|||
#include "item.h" |
|||
|
|||
class Shelf { |
|||
public: |
|||
Shelf(int); |
|||
int num_items;; |
|||
std::vector<Item*> item; |
|||
void reload(void); |
|||
void dispense(int); |
|||
private: |
|||
}; |
|||
|
|||
#endif /* _SHELF_H */ |
Loading…
Reference in new issue