From 55826823fc1f978e503ffba2b436d91e9efb09d7 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sun, 11 Aug 2019 17:15:22 +0200 Subject: [PATCH] Added MCP23S17 support as well as changed some pins and added more de-bouncing for the buttons. --- include/config.h | 7 ++++-- include/controller.h | 7 +++++- include/player.h | 4 +++- include/spi_master.h | 8 +++++-- platformio.ini | 1 + src/controller.cpp | 56 +++++++++++++++++++++++++++++++++++++++++--- src/main.cpp | 13 +++++++--- src/player.cpp | 16 +++++++------ 8 files changed, 93 insertions(+), 19 deletions(-) diff --git a/include/config.h b/include/config.h index 6a56a65..ed5ea67 100644 --- a/include/config.h +++ b/include/config.h @@ -25,7 +25,8 @@ #define PIN_SD_CS D4 #define PIN_VS1053_XCS D8 -#define PIN_VS1053_XRESET D3 +#define PIN_VS1053_XRESET 16 +#define PIN_MCP D3 #define PIN_VS1053_XDCS D1 #define PIN_VS1053_DREQ D2 #define PIN_RC522_CS D0 @@ -35,7 +36,9 @@ #define PIN_BTN_TRACK_NEXT D6 #define PIN_BTN_TRACK_PREV D7 -#define DEBOUNCE_MILLIS 100 +#define NUM_BUTTONS 4 + +#define DEBOUNCE_MILLIS 200 #define VOLUME_DEFAULT 0xA0 #define VOLUME_MIN 0x60 #define VOLUME_MAX 0xC0 diff --git a/include/controller.h b/include/controller.h index 21216c9..4b8ae2b 100644 --- a/include/controller.h +++ b/include/controller.h @@ -4,13 +4,16 @@ #include "config.h" #include "player.h" #include +#include class Controller { private: MFRC522* _rfid; + MCP* _mcp; bool _rfid_enabled = true; void _check_rfid(); void _check_serial(); + void _check_buttons(); uint32_t _get_rfid_card_uid(); uint32_t _last_rfid_card_uid = 0; Player* _player; @@ -19,7 +22,9 @@ private: void _execute_serial_command(String cmd); void _execute_command_ls(String path); void _execute_command_help(); + unsigned long _button_last_pressed_at[NUM_BUTTONS]; + bool _check_button(uint8_t btn); public: - Controller(Player* p); + Controller(Player* p, MCP* m); void loop(); }; diff --git a/include/player.h b/include/player.h index f8bd5aa..f4993b9 100644 --- a/include/player.h +++ b/include/player.h @@ -4,6 +4,7 @@ #include #include #include +#include #define SCI_MODE 0x00 #define SCI_STATUS 0x01 @@ -72,8 +73,9 @@ private: uint8_t _volume; uint16_t _stop_delay; uint32_t _skip_to; + MCP* _mcp; public: - Player(); + Player(MCP* m); void vol_up(); void vol_down(); void track_next(); diff --git a/include/spi_master.h b/include/spi_master.h index 315b51f..b31ade4 100644 --- a/include/spi_master.h +++ b/include/spi_master.h @@ -11,18 +11,22 @@ public: pinMode(PIN_SD_CS, OUTPUT); pinMode(PIN_VS1053_XCS, OUTPUT); pinMode(PIN_VS1053_XDCS, OUTPUT); + pinMode(PIN_MCP, OUTPUT); } static void enable(uint8_t pin) { digitalWrite(PIN_SD_CS, pin==PIN_SD_CS ? LOW : HIGH); digitalWrite(PIN_VS1053_XCS, pin==PIN_VS1053_XCS ? LOW : HIGH); digitalWrite(PIN_VS1053_XDCS, pin==PIN_VS1053_XDCS ? LOW : HIGH); + digitalWrite(PIN_MCP, pin==PIN_MCP ? LOW : HIGH); + } static void printStatus() { - Serial.printf("CS state: SD:%d, VS1053_XCS:%d, VS1053_XDCS:%d\n", + Serial.printf("CS state: SD:%d, VS1053_XCS:%d, VS1053_XDCS:%d, MCP:%d\n", digitalRead(PIN_SD_CS), digitalRead(PIN_VS1053_XCS), - digitalRead(PIN_VS1053_XDCS)); + digitalRead(PIN_VS1053_XDCS), + digitalRead(PIN_MCP)); } static void disable() { diff --git a/platformio.ini b/platformio.ini index 85f2c5a..07b3b18 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,4 +14,5 @@ board = esp12e framework = arduino upload_speed = 512000 lib_deps = 63 + https://github.com/n0mjs710/MCP23S17.git upload_port = /dev/cu.wchusbserial1420 diff --git a/src/controller.cpp b/src/controller.cpp index 5f7cd38..7c71b9f 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -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; idigitalRead(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; +} diff --git a/src/main.cpp b/src/main.cpp index f0c7efe..8ca0511 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "config.h" #include "controller.h" #include "player.h" @@ -8,6 +9,7 @@ Controller* controller; Player* player; +MCP* mcp; void setup() { delay(500); @@ -20,6 +22,11 @@ void setup() { SPIMaster::init(); INFO("SPI initialized.\n"); + DEBUG("Setting up MCP...\n"); + SPIMaster::enable(PIN_MCP); + mcp = new MCP(0, PIN_MCP); + INFO("MCP initialized."); + DEBUG("Setting up SD card...\n"); SPIMaster::enable(PIN_SD_CS); if (SD.begin(PIN_SD_CS)) { @@ -28,9 +35,9 @@ void setup() { ERROR("Could not initialize SD card. Halting.\n"); while(1); } - player = new Player(); - controller = new Controller(player); - + player = new Player(mcp); + controller = new Controller(player, mcp); + INFO("Initialization completed.\n"); } diff --git a/src/player.cpp b/src/player.cpp index 2e4fcc4..13dcad2 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -5,18 +5,19 @@ //Player::_spi_settings -Player::Player() { - pinMode(XRESET, OUTPUT); - digitalWrite(XRESET, HIGH); +Player::Player(MCP* m) { + _mcp = m; + _mcp->pinMode(XRESET, OUTPUT); + _mcp->digitalWrite(XRESET, HIGH); pinMode(DREQ, INPUT); _init(); } void Player::_reset() { - digitalWrite(XRESET, LOW); + _mcp->digitalWrite(XRESET, LOW); delay(100); - digitalWrite(XRESET, HIGH); + _mcp->digitalWrite(XRESET, HIGH); delay(100); _state = uninitialized; _spi_settings = &_spi_settings_slow; // After reset, communication has to be slow @@ -24,6 +25,7 @@ void Player::_reset() { void Player::_init() { SPIMaster::disable(); + DEBUG("Resetting VS1053...\n"); _reset(); uint16_t result = _read_control_register(SCI_MODE); @@ -59,14 +61,14 @@ void Player::_init() { set_volume(VOLUME_DEFAULT); INFO("VS1053 initialization completed.\n"); - + INFO("Checking system sounds...\n"); SPIMaster::enable(PIN_SD_CS); _check_system_sound("no_prev_song.mp3"); _check_system_sound("no_next_song.mp3"); _check_system_sound("volume_max.mp3"); _check_system_sound("volume_min.mp3"); - + _state = idle; }