Changed from MCP23S17 to MCP23017. Lots of changes.

Doesn't really work because of timing stuff.
This commit is contained in:
2019-11-10 14:45:33 +01:00
parent 88655b543d
commit 2ea2ec479a
14 changed files with 423 additions and 156 deletions

View File

@ -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\": \"");