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

This commit is contained in:
Fabian Schlenz 2019-08-11 17:15:22 +02:00
parent 842d7becf0
commit f29e38160a
7 changed files with 88 additions and 17 deletions

View File

@ -4,13 +4,16 @@
#include "config.h" #include "config.h"
#include "player.h" #include "player.h"
#include <MFRC522.h> #include <MFRC522.h>
#include <MCP23S17/MCP23S17.h>
class Controller { class Controller {
private: private:
MFRC522* _rfid; MFRC522* _rfid;
MCP* _mcp;
bool _rfid_enabled = true; bool _rfid_enabled = true;
void _check_rfid(); void _check_rfid();
void _check_serial(); void _check_serial();
void _check_buttons();
uint32_t _get_rfid_card_uid(); uint32_t _get_rfid_card_uid();
uint32_t _last_rfid_card_uid = 0; uint32_t _last_rfid_card_uid = 0;
Player* _player; Player* _player;
@ -19,7 +22,9 @@ private:
void _execute_serial_command(String cmd); void _execute_serial_command(String cmd);
void _execute_command_ls(String path); void _execute_command_ls(String path);
void _execute_command_help(); void _execute_command_help();
unsigned long _button_last_pressed_at[NUM_BUTTONS];
bool _check_button(uint8_t btn);
public: public:
Controller(Player* p); Controller(Player* p, MCP* m);
void loop(); void loop();
}; };

View File

@ -4,6 +4,7 @@
#include <SD.h> #include <SD.h>
#include <list> #include <list>
#include <map> #include <map>
#include <MCP23S17/MCP23S17.h>
#define SCI_MODE 0x00 #define SCI_MODE 0x00
#define SCI_STATUS 0x01 #define SCI_STATUS 0x01
@ -72,8 +73,9 @@ private:
uint8_t _volume; uint8_t _volume;
uint16_t _stop_delay; uint16_t _stop_delay;
uint32_t _skip_to; uint32_t _skip_to;
MCP* _mcp;
public: public:
Player(); Player(MCP* m);
void vol_up(); void vol_up();
void vol_down(); void vol_down();
void track_next(); void track_next();

View File

@ -11,18 +11,22 @@ public:
pinMode(PIN_SD_CS, OUTPUT); pinMode(PIN_SD_CS, OUTPUT);
pinMode(PIN_VS1053_XCS, OUTPUT); pinMode(PIN_VS1053_XCS, OUTPUT);
pinMode(PIN_VS1053_XDCS, OUTPUT); pinMode(PIN_VS1053_XDCS, OUTPUT);
pinMode(PIN_MCP, OUTPUT);
} }
static void enable(uint8_t pin) { static void enable(uint8_t pin) {
digitalWrite(PIN_SD_CS, pin==PIN_SD_CS ? LOW : HIGH); digitalWrite(PIN_SD_CS, pin==PIN_SD_CS ? LOW : HIGH);
digitalWrite(PIN_VS1053_XCS, pin==PIN_VS1053_XCS ? LOW : HIGH); digitalWrite(PIN_VS1053_XCS, pin==PIN_VS1053_XCS ? LOW : HIGH);
digitalWrite(PIN_VS1053_XDCS, pin==PIN_VS1053_XDCS ? LOW : HIGH); digitalWrite(PIN_VS1053_XDCS, pin==PIN_VS1053_XDCS ? LOW : HIGH);
digitalWrite(PIN_MCP, pin==PIN_MCP ? LOW : HIGH);
} }
static void printStatus() { 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_SD_CS),
digitalRead(PIN_VS1053_XCS), digitalRead(PIN_VS1053_XCS),
digitalRead(PIN_VS1053_XDCS)); digitalRead(PIN_VS1053_XDCS),
digitalRead(PIN_MCP));
} }
static void disable() { static void disable() {

View File

@ -14,4 +14,5 @@ board = esp12e
framework = arduino framework = arduino
upload_speed = 512000 upload_speed = 512000
lib_deps = 63 lib_deps = 63
https://github.com/n0mjs710/MCP23S17.git
upload_port = /dev/cu.wchusbserial1420 upload_port = /dev/cu.wchusbserial1420

View File

@ -1,10 +1,17 @@
#include "controller.h" #include "controller.h"
#include "spi_master.h" #include "spi_master.h"
Controller::Controller(Player* p) { Controller::Controller(Player* p, MCP* m) {
_player = p; _player = p;
_mcp = m;
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN); _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); SPIMaster::enable(PIN_RC522_CS);
DEBUG("Initializing RC522..."); DEBUG("Initializing RC522...");
_rfid->PCD_Init(); _rfid->PCD_Init();
@ -13,6 +20,8 @@ Controller::Controller(Player* p) {
#endif #endif
SPIMaster::disable(); SPIMaster::disable();
INFO("RC522 initialized.\n"); INFO("RC522 initialized.\n");
for (uint8_t i=0; i<NUM_BUTTONS; i++) _button_last_pressed_at[i]=0;
} }
void Controller::loop() { void Controller::loop() {
@ -22,6 +31,7 @@ void Controller::loop() {
_last_rfid_scan_at = now; _last_rfid_scan_at = now;
} }
_check_serial(); _check_serial();
_check_buttons();
} }
uint32_t Controller::_get_rfid_card_uid() { uint32_t Controller::_get_rfid_card_uid() {
@ -70,8 +80,8 @@ void Controller::_check_serial() {
} }
void Controller::_execute_serial_command(String cmd) { 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")) { if (cmd.equals("ls")) {
_execute_command_ls("/"); _execute_command_ls("/");
} else if (cmd.startsWith("ls ")) { } else if (cmd.startsWith("ls ")) {
@ -116,3 +126,43 @@ void Controller::_execute_command_help() {
INFO(" - / + - Decrease or increase the volume\n"); INFO(" - / + - Decrease or increase the volume\n");
INFO(" p / n - Previous or next track\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;
}

View File

@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include <MCP23S17/MCP23S17.h>
#include "config.h" #include "config.h"
#include "controller.h" #include "controller.h"
#include "player.h" #include "player.h"
@ -8,6 +9,7 @@
Controller* controller; Controller* controller;
Player* player; Player* player;
MCP* mcp;
void setup() { void setup() {
delay(500); delay(500);
@ -20,6 +22,11 @@ void setup() {
SPIMaster::init(); SPIMaster::init();
INFO("SPI initialized.\n"); 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"); DEBUG("Setting up SD card...\n");
SPIMaster::enable(PIN_SD_CS); SPIMaster::enable(PIN_SD_CS);
if (SD.begin(PIN_SD_CS)) { if (SD.begin(PIN_SD_CS)) {
@ -28,9 +35,9 @@ void setup() {
ERROR("Could not initialize SD card. Halting.\n"); ERROR("Could not initialize SD card. Halting.\n");
while(1); while(1);
} }
player = new Player(); player = new Player(mcp);
controller = new Controller(player); controller = new Controller(player, mcp);
INFO("Initialization completed.\n"); INFO("Initialization completed.\n");
} }

View File

@ -5,18 +5,19 @@
//Player::_spi_settings //Player::_spi_settings
Player::Player() { Player::Player(MCP* m) {
pinMode(XRESET, OUTPUT); _mcp = m;
digitalWrite(XRESET, HIGH); _mcp->pinMode(XRESET, OUTPUT);
_mcp->digitalWrite(XRESET, HIGH);
pinMode(DREQ, INPUT); pinMode(DREQ, INPUT);
_init(); _init();
} }
void Player::_reset() { void Player::_reset() {
digitalWrite(XRESET, LOW); _mcp->digitalWrite(XRESET, LOW);
delay(100); delay(100);
digitalWrite(XRESET, HIGH); _mcp->digitalWrite(XRESET, HIGH);
delay(100); delay(100);
_state = uninitialized; _state = uninitialized;
_spi_settings = &_spi_settings_slow; // After reset, communication has to be slow _spi_settings = &_spi_settings_slow; // After reset, communication has to be slow
@ -24,6 +25,7 @@ void Player::_reset() {
void Player::_init() { void Player::_init() {
SPIMaster::disable(); SPIMaster::disable();
DEBUG("Resetting VS1053...\n");
_reset(); _reset();
uint16_t result = _read_control_register(SCI_MODE); uint16_t result = _read_control_register(SCI_MODE);
@ -59,14 +61,14 @@ void Player::_init() {
set_volume(VOLUME_DEFAULT); set_volume(VOLUME_DEFAULT);
INFO("VS1053 initialization completed.\n"); INFO("VS1053 initialization completed.\n");
INFO("Checking system sounds...\n"); INFO("Checking system sounds...\n");
SPIMaster::enable(PIN_SD_CS); SPIMaster::enable(PIN_SD_CS);
_check_system_sound("no_prev_song.mp3"); _check_system_sound("no_prev_song.mp3");
_check_system_sound("no_next_song.mp3"); _check_system_sound("no_next_song.mp3");
_check_system_sound("volume_max.mp3"); _check_system_sound("volume_max.mp3");
_check_system_sound("volume_min.mp3"); _check_system_sound("volume_min.mp3");
_state = idle; _state = idle;
} }