flipperzero-firmware/applications/lfrfid/helpers/rfid-timer-emulator.cpp
SG 22e1ecb642
New LF-RFID app (#534)
* Hal lfrfid: add read timer pulse and period config fns
* New debug application for lfrfid subsystem
* New lfrfid: app, fix naming
* App lfrfid: assets
* Container view module
* App ibutton: remove unused header
* App lfrfid scenes
* App notification, add yield to blocking operations, add speaker volume control
* App lfrfid: reading key scene
* Assets: placeholder icon
* App lfrfid: reworked container view module
* App lfrfid: new scenes
* App lfrfid: write scene
* App lfrfid: write hid
* App lfrfid: emulate scene
* App lfrfid: save name scene
* App lfrfid: add missing file
2021-06-28 17:42:30 +03:00

65 lines
1.8 KiB
C++

#include "rfid-timer-emulator.h"
extern TIM_HandleTypeDef htim1;
RfidTimerEmulator::RfidTimerEmulator() {
}
RfidTimerEmulator::~RfidTimerEmulator() {
std::map<LfrfidKeyType, EncoderGeneric*>::iterator it;
for(it = encoders.begin(); it != encoders.end(); ++it) {
delete it->second;
encoders.erase(it);
}
}
void RfidTimerEmulator::start(LfrfidKeyType type, const uint8_t* data, uint8_t data_size) {
if(encoders.count(type)) {
current_encoder = encoders.find(type)->second;
if(data_size >= lfrfid_key_get_type_data_count(type)) {
current_encoder->init(data, data_size);
api_hal_rfid_tim_emulate(125000);
api_hal_rfid_pins_emulate();
api_interrupt_add(timer_update_callback, InterruptTypeTimerUpdate, this);
api_hal_rfid_tim_emulate_start();
}
} else {
// not found
}
}
void RfidTimerEmulator::stop() {
api_hal_rfid_tim_emulate_stop();
api_interrupt_remove(timer_update_callback, InterruptTypeTimerUpdate);
api_hal_rfid_tim_reset();
api_hal_rfid_pins_reset();
}
void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
if(api_hal_rfid_is_tim_emulate(hw)) {
bool result;
bool polarity;
uint16_t period;
uint16_t pulse;
do {
_this->current_encoder->get_next(&polarity, &period, &pulse);
result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
} while(result == false);
_this->pulse_joiner.pop_pulse(&period, &pulse);
api_hal_rfid_set_emulate_period(period - 1);
api_hal_rfid_set_emulate_pulse(pulse);
}
}