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 ebd9a9f24f
commit 55826823fc
8 changed files with 93 additions and 19 deletions

View File

@ -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

View File

@ -4,13 +4,16 @@
#include "config.h"
#include "player.h"
#include <MFRC522.h>
#include <MCP23S17/MCP23S17.h>
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();
};

View File

@ -4,6 +4,7 @@
#include <SD.h>
#include <list>
#include <map>
#include <MCP23S17/MCP23S17.h>
#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();

View File

@ -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() {

View File

@ -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

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;
}

View File

@ -1,6 +1,7 @@
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <MCP23S17/MCP23S17.h>
#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");
}

View File

@ -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;
}