Added MQTT client, better speaker handling, fixed bug in FTP server, ...

This commit is contained in:
2019-08-14 21:01:01 +02:00
parent c044098c8d
commit 231b8a2bce
55 changed files with 4946 additions and 52 deletions

View File

@ -1,9 +1,10 @@
#include "controller.h"
#include "spi_master.h"
Controller::Controller(Player* p, MCP* m) {
Controller::Controller(Player* p, MCP* m, MQTTClient* mqtt) {
_player = p;
_mcp = m;
_mqtt_client = mqtt;
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN);
SPIMaster::enable(PIN_MCP);
@ -32,6 +33,11 @@ void Controller::loop() {
}
_check_serial();
_check_buttons();
if ((_last_mqtt_report_at < now - MQTT_REPORT_INTERVAL) || (now < _last_mqtt_report_at)) {
_send_mqtt_report();
_last_mqtt_report_at = now;
}
}
uint32_t Controller::_get_rfid_card_uid() {
@ -53,6 +59,7 @@ void Controller::_check_rfid() {
uint32_t uid = _get_rfid_card_uid();
if (uid != _last_rfid_card_uid) {
if (uid > 0) {
_mqtt_client->publish_rfid_uid(uid);
_no_rfid_card_count = 0;
INFO("New RFID card uid: %08x\n", uid);
String s_uid = String(uid, HEX);
@ -157,7 +164,7 @@ void Controller::_check_buttons() {
bool Controller::_check_button(uint8_t index) {
if (index >= NUM_BUTTONS) return false;
bool ret = false;
uint8_t sum = 0;
/*uint8_t sum = 0;
while (1) {
sum = 0;
for (int i=0; i<8; i++) {
@ -165,7 +172,8 @@ bool Controller::_check_button(uint8_t index) {
}
if (sum==0 || sum==8) break;
}
if (sum == 0) {
if (sum == 0) {*/
if (_mcp->digitalRead(index + 1) == LOW) {
if (_button_last_pressed_at[index] + DEBOUNCE_MILLIS < millis()) {
DEBUG("Button %d pressed.\n", index);
ret = true;
@ -174,3 +182,33 @@ bool Controller::_check_button(uint8_t index) {
}
return ret;
}
String Controller::get_status_json() {
String response = String("{");
response.concat("\"state\": \"");
response.concat(_player->is_playing() ? "playing" : "idle");
response.concat("\", ");
if (_player->is_playing()) {
response.concat("\"album\": \"");
response.concat(_player->album());
response.concat("\", \"track\": ");
response.concat(_player->track());
response.concat(", \"position\": ");
response.concat(_player->position());
response.concat(", ");
}
response.concat("\"volume\": ");
response.concat(_player->volume());
response.concat(", \"volume_max\": ");
response.concat(VOLUME_MAX);
response.concat(", \"volume_min\": ");
response.concat(VOLUME_MIN);
response.concat(", \"rfid_uid\": ");
response.concat(String(_last_rfid_card_uid, HEX));
response.concat("}\n");
return response;
}
void Controller::_send_mqtt_report() {
}