rfid-timer-emulator.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "rfid-timer-emulator.h"
  2. extern TIM_HandleTypeDef htim1;
  3. RfidTimerEmulator::RfidTimerEmulator() {
  4. }
  5. RfidTimerEmulator::~RfidTimerEmulator() {
  6. std::map<LfrfidKeyType, EncoderGeneric*>::iterator it;
  7. for(it = encoders.begin(); it != encoders.end(); ++it) {
  8. delete it->second;
  9. encoders.erase(it);
  10. }
  11. }
  12. void RfidTimerEmulator::start(LfrfidKeyType type, const uint8_t* data, uint8_t data_size) {
  13. if(encoders.count(type)) {
  14. current_encoder = encoders.find(type)->second;
  15. if(data_size >= lfrfid_key_get_type_data_count(type)) {
  16. current_encoder->init(data, data_size);
  17. api_hal_rfid_tim_emulate(125000);
  18. api_hal_rfid_pins_emulate();
  19. api_interrupt_add(timer_update_callback, InterruptTypeTimerUpdate, this);
  20. api_hal_rfid_tim_emulate_start();
  21. }
  22. } else {
  23. // not found
  24. }
  25. }
  26. void RfidTimerEmulator::stop() {
  27. api_hal_rfid_tim_emulate_stop();
  28. api_interrupt_remove(timer_update_callback, InterruptTypeTimerUpdate);
  29. api_hal_rfid_tim_reset();
  30. api_hal_rfid_pins_reset();
  31. }
  32. void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
  33. RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
  34. TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
  35. if(api_hal_rfid_is_tim_emulate(hw)) {
  36. bool result;
  37. bool polarity;
  38. uint16_t period;
  39. uint16_t pulse;
  40. do {
  41. _this->current_encoder->get_next(&polarity, &period, &pulse);
  42. result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
  43. } while(result == false);
  44. _this->pulse_joiner.pop_pulse(&period, &pulse);
  45. api_hal_rfid_set_emulate_period(period - 1);
  46. api_hal_rfid_set_emulate_pulse(pulse);
  47. }
  48. }