Skip to content

Commit

Permalink
* Remove Pushover support (please not me know if this was still needed!)
Browse files Browse the repository at this point in the history
* Get rid of "instance" in ShelfWeb
* Add Alexa support!
  • Loading branch information
TheNitek committed Mar 2, 2020
1 parent 47d46cc commit e5ad2a9
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 117 deletions.
12 changes: 0 additions & 12 deletions RfidShelf/RfidShelf.ino
Expand Up @@ -20,11 +20,6 @@
#include "ShelfButtons.h"
#endif

#ifdef PUSHOVER_ENABLE
#include "ShelfPushover.h"
#endif


WiFiManager wifiManager;

sdfat::SdFat sdCard;
Expand All @@ -35,9 +30,6 @@ ShelfWeb webInterface(playback, rfid, sdCard);
#ifdef BUTTONS_ENABLE
ShelfButtons buttons(playback);
#endif
#ifdef PUSHOVER_ENABLE
ShelfPushover pushover;
#endif

void timeCallback() {
Sprint("Time updated: ");
Expand Down Expand Up @@ -143,9 +135,5 @@ void loop() {

rfid.handleRfid();

#ifdef PUSHOVER_ENABLE
pushover.sendPoweredNotification();
#endif

webInterface.work();
}
20 changes: 0 additions & 20 deletions RfidShelf/ShelfConfig.h
Expand Up @@ -59,26 +59,6 @@
#define Sprintf(a, b)
#endif

// -------------------------
// NOTIFICTATION SETTINGS
// -------------------------
// enable sending of notifications using pushover
// #define PUSHOVER_ENABLE
#ifdef PUSHOVER_ENABLE
// your pushover user key
#define PUSHOVER_USER "user"
// your pushover application token
#define PUSHOVER_TOKEN "key"
// receiving device(s), leave empty to send notifications to all devices
#define PUSHOVER_DEVICE ""
// name of this RfidShelf (in case you have multiple)
#define PUSHOVER_RFIDSHELD_NAME "RfidShelf"
// timeout for periodic sending of the powered notification in ms (defaults to 30 min)
#define PUSHOVER_POWERED_NOTIFICATION_TIME 30 * 60 * 1000
// sound to play for powered notification
#define PUSHOVER_POWERED_SOUND "climb"
#endif

#if defined(BUTTONS_ENABLE) && defined(DEBUG_ENABLE)
#error "BUTTONS_ENABLE cannot be used with DEBUG_ENABLE"
#endif
Expand Down
2 changes: 1 addition & 1 deletion RfidShelf/ShelfHtml.cpp
Expand Up @@ -2,7 +2,7 @@
#include "ShelfHtml.h"

const char ShelfHtml::INDEX[] PROGMEM = {
0x1f, 0x8b, 0x08, 0x00, 0xcc, 0x1b, 0x47, 0x5e, 0x02, 0xff, 0xc5, 0x3c,
0x1f, 0x8b, 0x08, 0x00, 0xf2, 0x35, 0x5d, 0x5e, 0x02, 0xff, 0xc5, 0x3c,
0x69, 0x77, 0xdb, 0xb6, 0x96, 0xdf, 0xe7, 0x9c, 0xf9, 0x0f, 0x08, 0xdb,
0x3e, 0xc9, 0xcf, 0x12, 0x25, 0x79, 0x4b, 0x2c, 0xcb, 0xea, 0xd8, 0x8e,
0x9d, 0xa5, 0xf1, 0x12, 0xcb, 0x71, 0xdb, 0xd7, 0x76, 0x7a, 0x28, 0x12,
Expand Down
40 changes: 0 additions & 40 deletions RfidShelf/ShelfPushover.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions RfidShelf/ShelfPushover.h

This file was deleted.

66 changes: 48 additions & 18 deletions RfidShelf/ShelfWeb.cpp
@@ -1,29 +1,46 @@
#include "ShelfWeb.h"

// This sucks - Maybe refactor ShelfWeb to singleton
ShelfWeb *ShelfWeb::_instance;

ShelfWeb::ShelfWeb(ShelfPlayback &playback, ShelfRfid &rfid, sdfat::SdFat &sd) : _playback(playback), _rfid(rfid), _SD(sd) {
_instance = this;
}

void ShelfWeb::defaultCallback() {
_instance->_handleDefault();
}

void ShelfWeb::fileUploadCallback() {
_instance->_handleFileUpload();
}

void ShelfWeb::begin() {
_server.on("/", HTTP_POST, defaultCallback, fileUploadCallback);
std::function<void(void)> defaultCallback = std::bind(&ShelfWeb::_handleDefault, this);
_server.on(
"/",
HTTP_POST,
defaultCallback,
std::bind(&ShelfWeb::_handleFileUpload, this)
);
_server.onNotFound(defaultCallback);

_server.begin();
BrightnessCallbackFunction brightnessCallback = std::bind(&ShelfWeb::_handleBrightness, this, std::placeholders::_1);
espalexa.addDevice(ShelfConfig::config.hostname, brightnessCallback, _playback.volume()*5); //simplest definition, default state off
espalexa.begin(&_server);

MDNS.addService("http", "tcp", 80);
}

void ShelfWeb::_handleBrightness(uint8_t brightness) {
Sprintf("Got brightness: %d\n", brightness);
if(brightness == 0) {
if(_playback.playbackState() == PLAYBACK_FILE) {
_playback.pausePlayback();
}
return;
}

// Don't change volume on 255 because it's most likely "on" and we don't want on to turn up the volume to maxium then
if(brightness < 255) {
uint8_t volume = brightness/5;
if(volume > 50) {
volume = 50;
}
_playback.volume(50-volume);
}

// Resume playback AFTER we set the volume to avoid loud surprises
if(_playback.playbackState() == PLAYBACK_PAUSED) {
_playback.resumePlayback();
}
}

void ShelfWeb::_returnOK() {
_server.send_P(200, "text/plain", NULL);
}
Expand Down Expand Up @@ -60,6 +77,9 @@ void ShelfWeb::_sendJsonStatus() {
_playback.currentFile(buffer, sizeof(buffer));
strcat(output, ",\"currentFile\":\"");
strcat(output, buffer);
_playback.currentFolder(buffer, sizeof(buffer));
strcat(output, "\",\"currentFolder\":\"");
strcat(output, buffer);
strcat(output, "\"");
}

Expand Down Expand Up @@ -294,6 +314,11 @@ void ShelfWeb::_handleFileUpload() {
return;
}

if(_playback.playbackState() == PLAYBACK_FILE) {
Sprintln("Pausing playback for upload");
_playback.pausePlayback();
}

_uploadFile.open(filename.c_str(), sdfat::O_WRITE | sdfat::O_CREAT);
_uploadStart = millis();
Sprint("Upload start: ");
Expand All @@ -316,6 +341,9 @@ void ShelfWeb::_handleFileUpload() {
void ShelfWeb::_handleDefault() {
String path = _server.urlDecode(_server.uri());
Sprintf("Request to: %s\n", path.c_str());
if (espalexa.handleAlexaApiCall(_server.uri(), _server.arg(0))) {
return;
}
if (_server.method() == HTTP_GET) {
if (_server.hasArg("status")) {
_sendJsonStatus();
Expand Down Expand Up @@ -476,5 +504,7 @@ void ShelfWeb::_handleDefault() {
}

void ShelfWeb::work() {
_server.handleClient();
// Not needed because espalexa does it
//_server.handleClient();
espalexa.loop();
}
8 changes: 4 additions & 4 deletions RfidShelf/ShelfWeb.h
Expand Up @@ -6,6 +6,7 @@
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266httpUpdate.h>
#include <Espalexa.h>
#include <Adafruit_VS1053.h>
#include <SdFat.h>
#include <time.h>
Expand All @@ -17,16 +18,14 @@

class ShelfWeb {
public:
ShelfWeb(ShelfPlayback &playback, ShelfRfid &rfid, sdfat::SdFat &sd);
ShelfWeb(ShelfPlayback &playback, ShelfRfid &rfid, sdfat::SdFat &sd) : _playback(playback), _rfid(rfid), _SD(sd) {}
void begin();
void work();
static void defaultCallback();
static void fileUploadCallback();
private:
static ShelfWeb *_instance;
ShelfPlayback &_playback;
ShelfRfid &_rfid;
sdfat::SdFat &_SD;
Espalexa espalexa;
ESP8266WebServer _server;
sdfat::SdFile _uploadFile;
uint32_t _uploadStart;
Expand All @@ -40,6 +39,7 @@ class ShelfWeb {
void _handleWriteRfid(const char *folder);
void _handleFileUpload();
void _handleDefault();
void _handleBrightness(uint8_t brightness);
void _downloadPatch();
void _updateOTA();
};
Expand Down
Binary file modified RfidShelf/build/latest.bin
Binary file not shown.
1 change: 1 addition & 0 deletions RfidShelf/platformio.ini
Expand Up @@ -29,6 +29,7 @@ lib_deps =
MFRC522@1.4.6
SPI
WifiManager@0.15.0
TheNitek/Espalexa
extra_scripts = platformio_script.py
monitor_speed = 115200
upload_speed = 921600
Expand Down

0 comments on commit e5ad2a9

Please sign in to comment.