Changed from MCP23S17 to MCP23017. Lots of changes.
Doesn't really work because of timing stuff.
This commit is contained in:
@ -1,24 +1,25 @@
|
||||
#include "controller.h"
|
||||
#include "spi_master.h"
|
||||
#include "config.h"
|
||||
|
||||
Controller::Controller(Player* p, MCP* m) {
|
||||
Controller::Controller(Player* p, Adafruit_MCP23017* m, SPIMaster* s) {
|
||||
_player = p;
|
||||
_mcp = m;
|
||||
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN);
|
||||
_spi = s;
|
||||
_rfid = new MFRC522(D0, 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);
|
||||
BTN_NEXT_SETUP();
|
||||
BTN_PREV_SETUP();
|
||||
BTN_VOL_UP_SETUP();
|
||||
BTN_VOL_DOWN_SETUP();
|
||||
|
||||
SPIMaster::enable(PIN_RC522_CS);
|
||||
DEBUG("Initializing RC522...");
|
||||
_spi->select_rc522();
|
||||
DEBUG("Initializing RC522...\n");
|
||||
_rfid->PCD_Init();
|
||||
#ifdef SHOW_DEBUG
|
||||
_rfid->PCD_DumpVersionToSerial();
|
||||
#endif
|
||||
SPIMaster::disable();
|
||||
_spi->select_rc522(false);
|
||||
INFO("RC522 initialized.\n");
|
||||
|
||||
for (uint8_t i=0; i<NUM_BUTTONS; i++) _button_last_pressed_at[i]=0;
|
||||
@ -44,7 +45,7 @@ void Controller::loop() {
|
||||
}
|
||||
|
||||
uint32_t Controller::_get_rfid_card_uid() {
|
||||
SPIMaster::enable(PIN_RC522_CS);
|
||||
_spi->select_rc522();
|
||||
if (!_rfid->PICC_ReadCardSerial()) {
|
||||
if (!_rfid->PICC_IsNewCardPresent()) {
|
||||
return 0;
|
||||
@ -53,8 +54,8 @@ uint32_t Controller::_get_rfid_card_uid() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
_spi->select_rc522(false);
|
||||
uint32_t uid = _rfid->uid.uidByte[0]<<24 | _rfid->uid.uidByte[1]<<16 | _rfid->uid.uidByte[2]<<8 | _rfid->uid.uidByte[3];
|
||||
SPIMaster::disable();
|
||||
return uid;
|
||||
}
|
||||
|
||||
@ -146,46 +147,26 @@ void Controller::_execute_command_help() {
|
||||
}
|
||||
|
||||
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)) {
|
||||
if (BTN_PREV() && _debounce_button(0)) {
|
||||
_player->track_prev();
|
||||
} else if (_check_button(1)) {
|
||||
} else if (BTN_VOL_UP() && _debounce_button(1)) {
|
||||
_player->vol_up();
|
||||
} else if (_check_button(2)) {
|
||||
} else if (BTN_VOL_DOWN() && _debounce_button(2)) {
|
||||
_player->vol_down();
|
||||
} else if (_check_button(3)) {
|
||||
} else if (BTN_NEXT() && _debounce_button(3)) {
|
||||
_player->track_next();
|
||||
}
|
||||
SPI.endTransaction();
|
||||
SPIMaster::disable();
|
||||
}
|
||||
|
||||
bool Controller::_check_button(uint8_t index) {
|
||||
if (index >= NUM_BUTTONS) return false;
|
||||
bool Controller::_debounce_button(uint8_t index) {
|
||||
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 (_mcp->digitalRead(index + 1) == LOW) {
|
||||
if (_button_last_pressed_at[index] + DEBOUNCE_MILLIS < millis()) {
|
||||
DEBUG("Button %d pressed.\n", index);
|
||||
ret = true;
|
||||
}
|
||||
_button_last_pressed_at[index] = millis();
|
||||
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;
|
||||
}
|
||||
|
||||
String Controller::get_status_json() {
|
||||
String response = String("{");
|
||||
response.concat("\"state\": \"");
|
||||
|
74
src/main.cpp
74
src/main.cpp
@ -1,7 +1,8 @@
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
#include <MCP23S17/MCP23S17.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_MCP23017.h>
|
||||
#include "config.h"
|
||||
#include "controller.h"
|
||||
#include "player.h"
|
||||
@ -12,7 +13,7 @@
|
||||
|
||||
Controller* controller;
|
||||
Player* player;
|
||||
MCP* mcp;
|
||||
Adafruit_MCP23017* mcp;
|
||||
HTTPServer* http_server;
|
||||
FtpServer* ftp_server;
|
||||
MQTTClient* mqtt_client;
|
||||
@ -21,6 +22,8 @@ unsigned long last_mqtt_report = 0;
|
||||
void setup() {
|
||||
delay(500);
|
||||
Serial.begin(74880);
|
||||
/*Serial.println("Starting...");
|
||||
Serial.println("Started.");
|
||||
INFO("Starting.\n");
|
||||
#ifdef VERSION
|
||||
INFO("ESMP3 version %s\n", VERSION);
|
||||
@ -28,7 +31,47 @@ void setup() {
|
||||
INFO("ESMP3, version unknown\n");
|
||||
#endif
|
||||
INFO("Initializing...\n");
|
||||
|
||||
|
||||
DEBUG("Setting up MCP...\n");*/
|
||||
Wire.begin();
|
||||
Wire.setClock(1700000);
|
||||
|
||||
uint8_t addr = MCP23017_ADDRESS + MCP_I2C_ADDR;
|
||||
while(true) {
|
||||
Wire.beginTransmission(addr);
|
||||
byte status = Wire.endTransmission();
|
||||
if (status==0) {
|
||||
DEBUG("I2C device found.");
|
||||
break;
|
||||
}
|
||||
DEBUG("No I2C device found.");
|
||||
delay(100);
|
||||
}
|
||||
|
||||
mcp = new Adafruit_MCP23017();
|
||||
mcp->begin(MCP_I2C_ADDR);
|
||||
INFO("MCP initialized.\n");
|
||||
|
||||
DEBUG("Setting up SPI...\n");
|
||||
SPI.begin();
|
||||
SPI.setHwCs(false);
|
||||
SPIMaster* spi = new SPIMaster(mcp);
|
||||
INFO("SPI initialized.\n");
|
||||
|
||||
DEBUG("Setting up SD card...\n");
|
||||
spi->select_sd();
|
||||
if (SD.begin(42)) {
|
||||
INFO("SD card initialized.\n");
|
||||
} else {
|
||||
ERROR("Could not initialize SD card.\n");
|
||||
}
|
||||
spi->select_sd(false);
|
||||
|
||||
DEBUG("Initializing Player and Controller...\n");
|
||||
player = new Player(mcp, spi);
|
||||
controller = new Controller(player, mcp, spi);
|
||||
INFO("Player and controller initialized.\n");
|
||||
|
||||
DEBUG("Connecting to wifi \"%s\"...\n", WIFI_SSID);
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
@ -42,30 +85,7 @@ void setup() {
|
||||
mqtt_client = new MQTTClient();
|
||||
MDNS.begin("esmp3");
|
||||
|
||||
|
||||
|
||||
DEBUG("Setting up SPI...\n");
|
||||
SPI.begin();
|
||||
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)) {
|
||||
INFO("SD card initialized.\n");
|
||||
} else {
|
||||
ERROR("Could not initialize SD card.\n");
|
||||
}
|
||||
|
||||
DEBUG("Initializing Player and Controller...\n");
|
||||
player = new Player(mcp);
|
||||
controller = new Controller(player, mcp, mqtt_client);
|
||||
INFO("Player and controller initialized.\n");
|
||||
controller->set_mqtt_client(mqtt_client);
|
||||
|
||||
DEBUG("Setting up WiFi and web server...\n");
|
||||
http_server = new HTTPServer(player, controller);
|
||||
|
@ -2,50 +2,50 @@
|
||||
|
||||
#include "player.h"
|
||||
#include "spi_master.h"
|
||||
#include "tools.h"
|
||||
|
||||
//Player::_spi_settings
|
||||
|
||||
Player::Player(MCP* m) {
|
||||
SPIMaster::enable(PIN_MCP);
|
||||
Player::Player(Adafruit_MCP23017* m, SPIMaster* s) {
|
||||
_mcp = m;
|
||||
_mcp->pinMode(XRESET, OUTPUT);
|
||||
_mcp->digitalWrite(XRESET, HIGH);
|
||||
_mcp->pinMode(SPEAKER_L, OUTPUT);
|
||||
_mcp->pinMode(SPEAKER_R, OUTPUT);
|
||||
_spi = s;
|
||||
PIN_VS1053_XRESET_SETUP();
|
||||
PIN_VS1053_XRESET(HIGH);
|
||||
PIN_SPEAKER_L_SETUP();
|
||||
PIN_SPEAKER_R_SETUP();
|
||||
_speaker_off();
|
||||
SPIMaster::disable();
|
||||
pinMode(DREQ, INPUT);
|
||||
_spi->disable();
|
||||
PIN_VS1053_DREQ_SETUP();
|
||||
|
||||
_init();
|
||||
}
|
||||
|
||||
void Player::_reset() {
|
||||
SPIMaster::enable(PIN_MCP);
|
||||
_mcp->digitalWrite(XRESET, LOW);
|
||||
PIN_VS1053_XRESET(LOW);
|
||||
delay(100);
|
||||
_mcp->digitalWrite(XRESET, HIGH);
|
||||
SPIMaster::disable();
|
||||
PIN_VS1053_XRESET(HIGH);
|
||||
delay(100);
|
||||
_state = uninitialized;
|
||||
_spi_settings = &_spi_settings_slow; // After reset, communication has to be slow
|
||||
}
|
||||
|
||||
void Player::_init() {
|
||||
SPIMaster::disable();
|
||||
DEBUG("Resetting VS1053...\n");
|
||||
_reset();
|
||||
|
||||
uint16_t result = _read_control_register(SCI_MODE);
|
||||
DEBUG("SCI_MODE: 0x%04X\n", result);
|
||||
if (result != 0x4800) {
|
||||
ERROR("SCI_MODE was 0x%04X, expected was 0x4800.\n", result);
|
||||
return;
|
||||
ERROR("SCI_MODE was 0x%04X, expected was 0x4800. Rebooting.\n", result);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
result = _read_control_register(SCI_STATUS);
|
||||
DEBUG("SCI_STATUS: 0x%04X\n", result);
|
||||
if (result != 0x0040 && result != 0x0048) {
|
||||
ERROR("SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048.\n", result);
|
||||
return;
|
||||
ERROR("SCI_STATUS was 0x%04X, expected was 0x0040 or 0x0048. Rebooting.\n", result);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
result = _read_control_register(SCI_CLOCKF);
|
||||
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
|
||||
@ -61,8 +61,9 @@ void Player::_init() {
|
||||
result = _read_control_register(SCI_CLOCKF);
|
||||
DEBUG("SCI_CLOCKF: 0x%04X\n", result);
|
||||
if (result != 0x6000) {
|
||||
ERROR("Error: SCI_CLOCKF was 0x%04X, expected was 0x6000.\n", result);
|
||||
return;
|
||||
ERROR("Error: SCI_CLOCKF was 0x%04X, expected was 0x6000. Rebooting.\n", result);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
set_volume(VOLUME_DEFAULT);
|
||||
@ -70,29 +71,26 @@ void Player::_init() {
|
||||
INFO("VS1053 initialization completed.\n");
|
||||
|
||||
INFO("Checking system sounds...\n");
|
||||
SPIMaster::enable(PIN_SD_CS);
|
||||
_spi->select_sd();
|
||||
_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");
|
||||
_spi->select_sd(false);
|
||||
|
||||
_state = idle;
|
||||
}
|
||||
|
||||
void Player::_speaker_off() {
|
||||
DEBUG("Speaker off\n");
|
||||
SPIMaster::enable(PIN_MCP);
|
||||
_mcp->digitalWrite(SPEAKER_R, LOW);
|
||||
_mcp->digitalWrite(SPEAKER_L, LOW);
|
||||
SPIMaster::disable();
|
||||
PIN_SPEAKER_L(LOW);
|
||||
PIN_SPEAKER_R(LOW);
|
||||
}
|
||||
|
||||
void Player::_speaker_on() {
|
||||
DEBUG("Speaker on\n");
|
||||
SPIMaster::enable(PIN_MCP);
|
||||
_mcp->digitalWrite(SPEAKER_R, HIGH);
|
||||
_mcp->digitalWrite(SPEAKER_L, HIGH);
|
||||
SPIMaster::disable();
|
||||
PIN_SPEAKER_L(HIGH);
|
||||
PIN_SPEAKER_R(HIGH);
|
||||
}
|
||||
|
||||
void Player::_sleep() {
|
||||
@ -128,12 +126,12 @@ void Player::_check_system_sound(String filename) {
|
||||
}
|
||||
|
||||
inline void Player::_wait() {
|
||||
while(!digitalRead(DREQ));
|
||||
while(!PIN_VS1053_DREQ());
|
||||
}
|
||||
|
||||
uint16_t Player::_read_control_register(uint8_t address) {
|
||||
_wait();
|
||||
SPIMaster::enable(XCS);
|
||||
_spi->select_vs1053_xcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
SPI.transfer(CMD_READ);
|
||||
SPI.transfer(address);
|
||||
@ -142,7 +140,7 @@ uint16_t Player::_read_control_register(uint8_t address) {
|
||||
uint8_t b2 = SPI.transfer(0xFF);
|
||||
_wait();
|
||||
SPI.endTransaction();
|
||||
SPIMaster::disable();
|
||||
_spi->select_vs1053_xcs(false);
|
||||
|
||||
return (b1 << 8) | b2;
|
||||
}
|
||||
@ -151,7 +149,7 @@ void Player::_write_control_register(uint8_t address, uint16_t value) {
|
||||
uint8_t b1 = value >> 8;
|
||||
uint8_t b2 = value & 0xFF;
|
||||
_wait();
|
||||
SPIMaster::enable(XCS);
|
||||
_spi->select_vs1053_xcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
SPI.transfer(CMD_WRITE);
|
||||
SPI.transfer(address);
|
||||
@ -159,17 +157,17 @@ void Player::_write_control_register(uint8_t address, uint16_t value) {
|
||||
SPI.transfer(b2);
|
||||
_wait();
|
||||
SPI.endTransaction();
|
||||
SPIMaster::disable();
|
||||
_spi->select_vs1053_xcs(false);
|
||||
}
|
||||
|
||||
void Player::_write_data(uint8_t* buffer) {
|
||||
SPIMaster::enable(XDCS);
|
||||
_spi->select_vs1053_xdcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
for (uint i=0; i<sizeof(_buffer); i++) {
|
||||
SPI.transfer(_buffer[i]);
|
||||
}
|
||||
SPI.endTransaction();
|
||||
SPIMaster::disable();
|
||||
_spi->select_vs1053_xdcs(false);
|
||||
}
|
||||
|
||||
uint16_t Player::_read_wram(uint16_t address) {
|
||||
@ -264,7 +262,7 @@ bool Player::is_playing() {
|
||||
}
|
||||
|
||||
std::list<String> Player::ls(String path, bool withFiles, bool withDirs, bool withHidden) {
|
||||
SPIMaster::enable(PIN_SD_CS);
|
||||
_spi->select_sd();
|
||||
std::list<String> result;
|
||||
if (!SD.exists(path)) return result;
|
||||
File dir = SD.open(path);
|
||||
@ -277,11 +275,13 @@ std::list<String> Player::ls(String path, bool withFiles, bool withDirs, bool wi
|
||||
if (entry.isDirectory()) filename.concat("/");
|
||||
result.push_back(filename);
|
||||
}
|
||||
_spi->select_sd(false);
|
||||
result.sort();
|
||||
return result;
|
||||
}
|
||||
|
||||
String Player::_find_album_dir(String id) {
|
||||
_spi->select_sd();
|
||||
if (id.endsWith("/")) id = id.substring(0, id.length() - 1);
|
||||
String id_with_divider = id + " - ";
|
||||
File root = SD.open("/");
|
||||
@ -295,10 +295,12 @@ String Player::_find_album_dir(String id) {
|
||||
entry.close();
|
||||
}
|
||||
root.close();
|
||||
_spi->select_sd(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::list<String> Player::_files_in_dir(String path) {
|
||||
_spi->select_sd();
|
||||
DEBUG("Examining folder %s...\n", path.c_str());
|
||||
if (!path.startsWith("/")) path = String("/") + path;
|
||||
if (!path.endsWith("/")) path.concat("/");
|
||||
@ -323,6 +325,7 @@ std::list<String> Player::_files_in_dir(String path) {
|
||||
entry.close();
|
||||
}
|
||||
dir.close();
|
||||
_spi->select_sd(false);
|
||||
result.sort();
|
||||
|
||||
return result;
|
||||
@ -393,8 +396,13 @@ void Player::play_system_sound(String filename) {
|
||||
|
||||
void Player::_play_file(String file, uint32_t file_offset) {
|
||||
INFO("play_file('%s', %d)\n", file.c_str(), file_offset);
|
||||
_spi->select_sd();
|
||||
_file = SD.open(file);
|
||||
if (!_file) return;
|
||||
_spi->select_sd(false);
|
||||
if (!_file) {
|
||||
DEBUG("Could not open file %s", file.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG("Resetting SCI_DECODE_TIME...\n");
|
||||
_write_control_register(SCI_DECODE_TIME, 0);
|
||||
@ -438,13 +446,14 @@ uint32_t Player::_id3_tag_offset(File f) {
|
||||
}
|
||||
|
||||
void Player::_flush(uint count, int8_t byte) {
|
||||
SPIMaster::enable(XDCS);
|
||||
_spi->select_vs1053_xdcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
for(uint i=0; i<count; i++) {
|
||||
_wait();
|
||||
SPI.transfer(byte);
|
||||
}
|
||||
SPI.endTransaction();
|
||||
_spi->select_vs1053_xdcs(false);
|
||||
}
|
||||
|
||||
void Player::_finish_playing() {
|
||||
@ -498,10 +507,11 @@ void Player::_finish_stopping(bool turn_speaker_off) {
|
||||
}
|
||||
|
||||
void Player::_refill() {
|
||||
SPIMaster::enable(PIN_SD_CS);
|
||||
_spi->select_sd();
|
||||
_refills++;
|
||||
if (_refills % 1000 == 0) DEBUG(".");
|
||||
uint8_t result = _file.read(_buffer, sizeof(_buffer));
|
||||
_spi->select_sd(false);
|
||||
if (result == 0) {
|
||||
// File is over.
|
||||
DEBUG("EOF reached.\n");
|
||||
@ -550,7 +560,7 @@ bool Player::_refill_needed() {
|
||||
}
|
||||
|
||||
bool Player::loop() {
|
||||
if (digitalRead(DREQ) && _refill_needed()) {
|
||||
if (PIN_VS1053_DREQ() && _refill_needed()) {
|
||||
_refill();
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user