Added MCP23S17 support as well as changed some pins and added more de-bouncing for the buttons.

This commit is contained in:
2019-08-11 17:15:22 +02:00
parent ebd9a9f24f
commit 55826823fc
8 changed files with 93 additions and 19 deletions

View File

@ -1,10 +1,17 @@
#include "controller.h"
#include "spi_master.h"
Controller::Controller(Player* p) {
Controller::Controller(Player* p, MCP* m) {
_player = p;
_mcp = m;
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN);
SPIMaster::enable(PIN_MCP);
_mcp->pinMode(1, INPUT); _mcp->pullupMode(1, HIGH);
_mcp->pinMode(2, INPUT); _mcp->pullupMode(2, HIGH);
_mcp->pinMode(3, INPUT); _mcp->pullupMode(3, HIGH);
_mcp->pinMode(4, INPUT); _mcp->pullupMode(4, HIGH);
SPIMaster::enable(PIN_RC522_CS);
DEBUG("Initializing RC522...");
_rfid->PCD_Init();
@ -13,6 +20,8 @@ Controller::Controller(Player* p) {
#endif
SPIMaster::disable();
INFO("RC522 initialized.\n");
for (uint8_t i=0; i<NUM_BUTTONS; i++) _button_last_pressed_at[i]=0;
}
void Controller::loop() {
@ -22,6 +31,7 @@ void Controller::loop() {
_last_rfid_scan_at = now;
}
_check_serial();
_check_buttons();
}
uint32_t Controller::_get_rfid_card_uid() {
@ -70,8 +80,8 @@ void Controller::_check_serial() {
}
void Controller::_execute_serial_command(String cmd) {
DEBUG("Executing command: %s", cmd.c_str());
DEBUG("Executing command: %s", cmd.c_str());
if (cmd.equals("ls")) {
_execute_command_ls("/");
} else if (cmd.startsWith("ls ")) {
@ -116,3 +126,43 @@ void Controller::_execute_command_help() {
INFO(" - / + - Decrease or increase the volume\n");
INFO(" p / n - Previous or next track\n");
}
void Controller::_check_buttons() {
SPIMaster::enable(PIN_MCP);
SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE0));
/*if (millis()%100==0) {
Serial.printf("Buttons: %d %d %d %d\n", _mcp->digitalRead(1), _mcp->digitalRead(2), _mcp->digitalRead(3), _mcp->digitalRead(4));
}*/
if (_check_button(0)) {
_player->play_album("23f5762e");
} else if (_check_button(1)) {
_player->vol_up();
} else if (_check_button(2)) {
_player->vol_down();
} else if (_check_button(3)) {
_player->track_prev();
}
SPI.endTransaction();
SPIMaster::disable();
}
bool Controller::_check_button(uint8_t index) {
if (index >= NUM_BUTTONS) return false;
bool ret = false;
uint8_t sum = 0;
while (1) {
sum = 0;
for (int i=0; i<8; i++) {
sum += _mcp->digitalRead(index + 1) == HIGH ? 1 : 0;
}
if (sum==0 || sum==8) break;
}
if (sum == 0) {
if (_button_last_pressed_at[index] + DEBOUNCE_MILLIS < millis()) {
DEBUG("Button %d pressed.\n", index);
ret = true;
}
_button_last_pressed_at[index] = millis();
}
return ret;
}