Compare commits
2 Commits
12978e9ffc
...
0d64366241
Author | SHA1 | Date |
---|---|---|
Fabian Schlenz | 0d64366241 | |
Fabian Schlenz | ed73d003f9 |
|
@ -17,6 +17,8 @@ private:
|
|||
void _check_buttons();
|
||||
bool _debounce_button(uint8_t index);
|
||||
uint32_t _get_rfid_card_uid();
|
||||
String _read_rfid_data();
|
||||
bool _rfid_present = false;
|
||||
uint32_t _last_rfid_card_uid = 0;
|
||||
uint8_t _no_rfid_card_count = 0;
|
||||
Player* _player;
|
||||
|
|
|
@ -48,8 +48,8 @@ private:
|
|||
void _reset();
|
||||
void _init();
|
||||
void _wait();
|
||||
uint16_t _read_control_register(uint8_t address);
|
||||
void _write_control_register(uint8_t address, uint16_t value);
|
||||
uint16_t _read_control_register(uint8_t address, bool do_wait=true);
|
||||
void _write_control_register(uint8_t address, uint16_t value, bool do_wait=true);
|
||||
void _write_direct(uint8_t address, uint16_t value);
|
||||
void _write_data(uint8_t* data);
|
||||
uint16_t _read_wram(uint16_t address);
|
||||
|
|
|
@ -59,11 +59,28 @@ uint32_t Controller::_get_rfid_card_uid() {
|
|||
}
|
||||
|
||||
void Controller::_check_rfid() {
|
||||
uint32_t uid = _get_rfid_card_uid();
|
||||
if (uid != _last_rfid_card_uid) {
|
||||
MFRC522::StatusCode status;
|
||||
if (_rfid_present) {
|
||||
byte buffer[2];
|
||||
byte buffer_size = 2;
|
||||
_spi->select_rc522();
|
||||
status = _rfid->PICC_WakeupA(buffer, &buffer_size);
|
||||
if (status == MFRC522::STATUS_OK) {
|
||||
// Card is still present.
|
||||
_rfid->PICC_HaltA();
|
||||
_spi->select_rc522(false);
|
||||
return;
|
||||
}
|
||||
_spi->select_rc522(false);
|
||||
// Card is now gone
|
||||
_rfid_present = false;
|
||||
INFO("No more RFID card.\n");
|
||||
_player->stop();
|
||||
} else {
|
||||
uint32_t uid = _get_rfid_card_uid();
|
||||
if (uid > 0) {
|
||||
_mqtt_client->publish_rfid_uid(uid);
|
||||
_no_rfid_card_count = 0;
|
||||
//_no_rfid_card_count = 0;
|
||||
String temp = String(uid, HEX);
|
||||
String s_uid = "";
|
||||
for (int i=0; i<(8-temp.length()); i++) {
|
||||
|
@ -71,65 +88,67 @@ void Controller::_check_rfid() {
|
|||
}
|
||||
s_uid.concat(temp);
|
||||
INFO("New RFID card uid: %s\n", s_uid.c_str());
|
||||
_rfid_present = true;
|
||||
|
||||
String data = _read_rfid_data();
|
||||
|
||||
_player->play_id(s_uid);
|
||||
|
||||
|
||||
DEBUG("Trying to read RFID data...");
|
||||
_spi->select_rc522();
|
||||
String data = "";
|
||||
MFRC522::MIFARE_Key key;
|
||||
for (int i=0; i<6; i++) key.keyByte[i]=0xFF;
|
||||
MFRC522::PICC_Type type = _rfid->PICC_GetType(_rfid->uid.sak);
|
||||
MFRC522::StatusCode status;
|
||||
uint8_t sectors = 0;
|
||||
switch(type) {
|
||||
case MFRC522::PICC_TYPE_MIFARE_MINI: sectors = 5; break;
|
||||
case MFRC522::PICC_TYPE_MIFARE_1K: sectors = 16; break;
|
||||
case MFRC522::PICC_TYPE_MIFARE_4K: sectors = 40; break;
|
||||
default: INFO("Unknown PICC type %s\n", String(MFRC522::PICC_GetTypeName(type)).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String Controller::_read_rfid_data() {
|
||||
_spi->select_rc522();
|
||||
DEBUG("Trying to read RFID data...\n");
|
||||
|
||||
String data = "";
|
||||
MFRC522::MIFARE_Key key;
|
||||
key.keyByte[0] = 0xD3;
|
||||
key.keyByte[1] = 0xF7;
|
||||
key.keyByte[2] = 0xD3;
|
||||
key.keyByte[3] = 0xF7;
|
||||
key.keyByte[4] = 0xD3;
|
||||
key.keyByte[5] = 0xF7;
|
||||
MFRC522::PICC_Type type = _rfid->PICC_GetType(_rfid->uid.sak);
|
||||
|
||||
uint8_t sectors = 0;
|
||||
switch(type) {
|
||||
case MFRC522::PICC_TYPE_MIFARE_MINI: sectors = 5; break;
|
||||
case MFRC522::PICC_TYPE_MIFARE_1K: sectors = 16; break;
|
||||
case MFRC522::PICC_TYPE_MIFARE_4K: sectors = 40; break;
|
||||
default: INFO("Unknown PICC type %s\n", String(MFRC522::PICC_GetTypeName(type)).c_str());
|
||||
}
|
||||
|
||||
for (uint8_t sector=1; sector<sectors; sector++) {
|
||||
uint8_t blocks = (sector < 32) ? 4 : 16;
|
||||
uint8_t block_offset = (sector < 32) ? sector * 4 : 128 + (sector - 32) * 16;
|
||||
|
||||
MFRC522::StatusCode status;
|
||||
status = _rfid->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block_offset, &key, &_rfid->uid);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
DEBUG("PCD_Authenticate() for sector %d failed: %s\n", sector, String(_rfid->GetStatusCodeName(status)).c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint8_t block=0; block<blocks-1; block++) {
|
||||
byte buffer[18];
|
||||
uint8_t byte_count = 18;
|
||||
status = _rfid->MIFARE_Read(block_offset + block, buffer, &byte_count);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
DEBUG("MIFARE_Read() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint8_t sector=0; sector<sectors; sector++) {
|
||||
uint8_t blocks = (sector < 32) ? 4 : 16;
|
||||
uint8_t block_offset = (sector < 32) ? sector * 4 : 128 + (sector - 32) * 16;
|
||||
|
||||
status = _rfid->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block_offset, &key, &_rfid->uid);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
DEBUG("PCD_Authenticate() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint8_t block=0; block<blocks-1; block++) {
|
||||
byte buffer[16];
|
||||
uint8_t byte_count = 16;
|
||||
status = _rfid->MIFARE_Read(block_offset + block, buffer, &byte_count);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
DEBUG("MIFARE_Read() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
|
||||
continue;
|
||||
}
|
||||
for (int i=0; i<16; i++) {
|
||||
if (buffer[i]!=0x00) data.concat(buffer[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_rfid->PICC_HaltA();
|
||||
_rfid->PCD_StopCrypto1();
|
||||
_spi->select_rc522(false);
|
||||
|
||||
DEBUG("Data from RFID: %s", data.c_str());
|
||||
|
||||
} else {
|
||||
if (_no_rfid_card_count >= 1) {
|
||||
INFO("No more RFID card.\n");
|
||||
_player->stop();
|
||||
} else {
|
||||
_no_rfid_card_count++;
|
||||
return;
|
||||
for (int i=0; i<16; i++) {
|
||||
if (buffer[i]>=0x20 && buffer[i]<0x7F) data.concat((char)buffer[i]);
|
||||
}
|
||||
}
|
||||
_last_rfid_card_uid = uid;
|
||||
}
|
||||
|
||||
_rfid->PICC_HaltA();
|
||||
_rfid->PCD_StopCrypto1();
|
||||
DEBUG("Data from RFID: %s", data.c_str());
|
||||
_spi->select_rc522(false);
|
||||
return data;
|
||||
}
|
||||
|
||||
void Controller::_check_serial() {
|
||||
|
|
|
@ -134,17 +134,18 @@ void Player::_record() {
|
|||
|
||||
_patch_adpcm();
|
||||
|
||||
_write_control_register(SCI_MODE, _read_control_register(SCI_MODE) | SM_ADPCM | SM_LAYER12);
|
||||
_write_control_register(SCI_MODE, SM_ADPCM);
|
||||
|
||||
_write_control_register(SCI_AICTRL0, 0x8000); // Mono VU meter
|
||||
_write_control_register(SCI_AICTRL1, 1024); // Manual gain, 1x
|
||||
_write_control_register(SCI_AICTRL2, 0); // Maximum gain for autogain - ignored
|
||||
_write_control_register(SCI_AICTRL3, 0); // status: record
|
||||
|
||||
_write_control_register(SCI_AIADDR, 0x0034);
|
||||
_write_control_register(SCI_AIADDR, 0x0034, false);
|
||||
delay(1);
|
||||
_wait();
|
||||
|
||||
DEBUG("Recording.\n");
|
||||
delay(10);
|
||||
_state = recording;
|
||||
}
|
||||
|
||||
|
@ -161,8 +162,8 @@ inline void Player::_wait() {
|
|||
while(!PIN_VS1053_DREQ());
|
||||
}
|
||||
|
||||
uint16_t Player::_read_control_register(uint8_t address) {
|
||||
_wait();
|
||||
uint16_t Player::_read_control_register(uint8_t address, bool do_wait) {
|
||||
if (do_wait) _wait();
|
||||
_spi->select_vs1053_xcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
SPI.transfer(CMD_READ);
|
||||
|
@ -177,7 +178,7 @@ uint16_t Player::_read_control_register(uint8_t address) {
|
|||
return (b1 << 8) | b2;
|
||||
}
|
||||
|
||||
void Player::_write_control_register(uint8_t address, uint16_t value) {
|
||||
void Player::_write_control_register(uint8_t address, uint16_t value, bool do_wait) {
|
||||
_wait();
|
||||
_spi->select_vs1053_xcs();
|
||||
SPI.beginTransaction(*_spi_settings);
|
||||
|
@ -187,7 +188,7 @@ void Player::_write_control_register(uint8_t address, uint16_t value) {
|
|||
SPI.transfer(value & 0xFF);
|
||||
SPI.endTransaction();
|
||||
_spi->select_vs1053_xcs(false);
|
||||
_wait();
|
||||
if (do_wait) _wait();
|
||||
}
|
||||
|
||||
void Player::_patch_adpcm() {
|
||||
|
@ -892,13 +893,14 @@ bool Player::loop() {
|
|||
}
|
||||
|
||||
if (_state == recording) {
|
||||
uint16_t samples_available = _read_control_register(SCI_HDAT1);
|
||||
uint16_t vu_value = _read_control_register(SCI_AICTRL0);
|
||||
DEBUG("r");
|
||||
uint16_t samples_available = _read_control_register(SCI_HDAT1, false);
|
||||
uint16_t vu_value = _read_control_register(SCI_AICTRL0, false);
|
||||
DEBUG("Samples available: %4d, VU meter: 0x%04X\n", samples_available, vu_value);
|
||||
if (samples_available >= 500) {
|
||||
unsigned long sum = 0;
|
||||
for (int i=0; i<500; i++) {
|
||||
uint16_t sample = _read_control_register(SCI_HDAT0);
|
||||
uint16_t sample = _read_control_register(SCI_HDAT0, false);
|
||||
sum += sample * sample;
|
||||
}
|
||||
double result = sqrt(sum / 500);
|
||||
|
@ -907,8 +909,8 @@ bool Player::loop() {
|
|||
}
|
||||
|
||||
if (_state == idle && _stopped_at < millis() - VS1053_SLEEP_DELAY) {
|
||||
//_sleep();
|
||||
_record();
|
||||
_sleep();
|
||||
//_record();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue