rfid-timer-emulator.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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(lfrfid_key_get_type_data_count(type) == data_size) {
  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. // TODO make api for interrupts priority
  21. for(size_t i = WWDG_IRQn; i <= DMAMUX1_OVR_IRQn; i++) {
  22. HAL_NVIC_SetPriority(static_cast<IRQn_Type>(i), 15, 0);
  23. }
  24. HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 5, 0);
  25. HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  26. api_hal_rfid_tim_emulate_start();
  27. }
  28. } else {
  29. // not found
  30. }
  31. }
  32. void RfidTimerEmulator::stop() {
  33. api_hal_rfid_tim_emulate_stop();
  34. api_interrupt_remove(timer_update_callback, InterruptTypeTimerUpdate);
  35. api_hal_rfid_tim_reset();
  36. api_hal_rfid_pins_reset();
  37. }
  38. void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
  39. RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
  40. TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
  41. if(api_hal_rfid_is_tim_emulate(hw)) {
  42. bool result;
  43. bool polarity;
  44. uint16_t period;
  45. uint16_t pulse;
  46. do {
  47. _this->current_encoder->get_next(&polarity, &period, &pulse);
  48. result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
  49. } while(result == false);
  50. _this->pulse_joiner.pop_pulse(&period, &pulse);
  51. api_hal_rfid_set_emulate_period(period - 1);
  52. api_hal_rfid_set_emulate_pulse(pulse);
  53. }
  54. }