mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-30 16:29:12 +00:00
c4a0847c99
* FuriHal: new speaker HAL * FuriHal: drop PWM * FuriHal: move COMP1 to LL * FuriHal: move COMP1 to LL backport to F6 * FuriHal: remove missing gpio_rfid_carrier from F6 * FurHal: use LL for system controls in flash HAL * Drop F6 source tree * Drop F6 from GitHub workflow * Tie USE_FULL_ASSERT with APP_UNIT_TESTS * Speaker: return to old volume calculation * FreeRTOS: move TCB header to glue Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
181 lines
4.9 KiB
C++
181 lines
4.9 KiB
C++
#include "key_reader.h"
|
|
#include "key_commands.h"
|
|
#include <callback-connector.h>
|
|
#include <maxim_crc.h>
|
|
|
|
KeyReader::Error KeyReader::read(iButtonKey* key) {
|
|
uint8_t tmp_key_data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
iButtonKeyType key_type;
|
|
|
|
KeyReader::Error result = KeyReader::Error::EMPTY;
|
|
|
|
if(read_key(&key_type, tmp_key_data, 8)) {
|
|
switch(key_type) {
|
|
case iButtonKeyType::KeyDallas:
|
|
if(verify_key(key_type, tmp_key_data, 8)) {
|
|
if(maxim_crc8(tmp_key_data, 8) == 0) {
|
|
if(tmp_key_data[0] == 0x01) {
|
|
result = KeyReader::Error::OK;
|
|
} else {
|
|
result = KeyReader::Error::NOT_ARE_KEY;
|
|
}
|
|
} else {
|
|
result = KeyReader::Error::CRC_ERROR;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case iButtonKeyType::KeyCyfral:
|
|
result = KeyReader::Error::OK;
|
|
break;
|
|
case iButtonKeyType::KeyMetakom:
|
|
result = KeyReader::Error::OK;
|
|
break;
|
|
}
|
|
|
|
if(result != KeyReader::Error::EMPTY) {
|
|
key->set_type(key_type);
|
|
key->set_data(tmp_key_data, 8);
|
|
}
|
|
}
|
|
|
|
switch_mode_if_needed();
|
|
|
|
return result;
|
|
}
|
|
|
|
KeyReader::KeyReader(OneWireMaster* _onewire_master) {
|
|
onewire_master = _onewire_master;
|
|
read_mode_switch_time = 0;
|
|
read_mode = ReadMode::DALLAS;
|
|
}
|
|
|
|
KeyReader::~KeyReader() {
|
|
stop();
|
|
}
|
|
|
|
bool KeyReader::read_key(iButtonKeyType* key_type, uint8_t* data, uint8_t data_size) {
|
|
bool readed = false;
|
|
|
|
if(read_mode == ReadMode::DALLAS) {
|
|
FURI_CRITICAL_ENTER();
|
|
if(onewire_master->search(data)) {
|
|
onewire_master->reset_search();
|
|
readed = true;
|
|
*key_type = iButtonKeyType::KeyDallas;
|
|
} else {
|
|
onewire_master->reset_search();
|
|
}
|
|
FURI_CRITICAL_EXIT();
|
|
} else if(read_mode == ReadMode::CYFRAL_METAKOM) {
|
|
if(cyfral_decoder.read(data, 2)) {
|
|
readed = true;
|
|
*key_type = iButtonKeyType::KeyCyfral;
|
|
} else if(metakom_decoder.read(data, 4)) {
|
|
readed = true;
|
|
*key_type = iButtonKeyType::KeyMetakom;
|
|
}
|
|
}
|
|
|
|
return readed;
|
|
}
|
|
|
|
bool KeyReader::verify_key(iButtonKeyType key_type, const uint8_t* const data, uint8_t data_size) {
|
|
bool result = true;
|
|
|
|
if(key_type == iButtonKeyType::KeyDallas) {
|
|
switch_to(ReadMode::DALLAS);
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
if(onewire_master->reset()) {
|
|
onewire_master->write(DS1990::CMD_READ_ROM);
|
|
for(uint8_t i = 0; i < data_size; i++) {
|
|
if(onewire_master->read() != data[i]) {
|
|
result = false;
|
|
}
|
|
}
|
|
} else {
|
|
result = false;
|
|
}
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
} else {
|
|
result = false;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void KeyReader::start_comaparator(void) {
|
|
furi_hal_rfid_pins_reset();
|
|
|
|
// pulldown pull pin, we sense the signal through the analog part of the RFID schematic
|
|
furi_hal_rfid_pin_pull_pulldown();
|
|
|
|
comparator_callback_pointer =
|
|
cbc::obtain_connector(this, &KeyReader::comparator_trigger_callback);
|
|
furi_hal_rfid_comp_set_callback(comparator_callback_pointer, this);
|
|
last_dwt_value = DWT->CYCCNT;
|
|
furi_hal_rfid_comp_start();
|
|
}
|
|
|
|
void KeyReader::stop_comaparator(void) {
|
|
furi_hal_rfid_pins_reset();
|
|
|
|
// rfid_pins_reset will disable ibutton pin
|
|
furi_hal_ibutton_start();
|
|
|
|
furi_hal_rfid_comp_stop();
|
|
furi_hal_rfid_comp_set_callback(NULL, NULL);
|
|
}
|
|
|
|
void KeyReader::comparator_trigger_callback(bool level, void* comp_ctx) {
|
|
KeyReader* _this = static_cast<KeyReader*>(comp_ctx);
|
|
|
|
uint32_t current_dwt_value = DWT->CYCCNT;
|
|
|
|
_this->cyfral_decoder.process_front(level, current_dwt_value - last_dwt_value);
|
|
_this->metakom_decoder.process_front(level, current_dwt_value - last_dwt_value);
|
|
|
|
last_dwt_value = current_dwt_value;
|
|
}
|
|
|
|
void KeyReader::switch_to(ReadMode mode) {
|
|
switch(mode) {
|
|
case ReadMode::DALLAS:
|
|
onewire_master->start();
|
|
stop_comaparator();
|
|
break;
|
|
case ReadMode::CYFRAL_METAKOM:
|
|
onewire_master->stop();
|
|
start_comaparator();
|
|
break;
|
|
}
|
|
|
|
read_mode = mode;
|
|
}
|
|
|
|
void KeyReader::switch_mode_if_needed() {
|
|
if(osKernelGetTickCount() - read_mode_switch_time > (osKernelGetTickFreq() / 5)) {
|
|
read_mode_switch_time = osKernelGetTickCount();
|
|
switch(read_mode) {
|
|
case ReadMode::DALLAS:
|
|
switch_to(ReadMode::CYFRAL_METAKOM);
|
|
break;
|
|
case ReadMode::CYFRAL_METAKOM:
|
|
switch_to(ReadMode::DALLAS);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void KeyReader::start() {
|
|
furi_hal_power_enable_otg();
|
|
switch_to(ReadMode::CYFRAL_METAKOM);
|
|
}
|
|
|
|
void KeyReader::stop() {
|
|
furi_hal_power_disable_otg();
|
|
onewire_master->stop();
|
|
stop_comaparator();
|
|
}
|