rfid-timer-emulator.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "rfid-timer-emulator.h"
  2. extern TIM_HandleTypeDef htim1;
  3. RfidTimerEmulator::RfidTimerEmulator() {
  4. }
  5. RfidTimerEmulator::~RfidTimerEmulator() {
  6. std::map<Type, 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(Type type) {
  13. if(encoders.count(type)) {
  14. current_encoder = encoders.find(type)->second;
  15. uint8_t em_data[5] = {0x53, 0x00, 0x5F, 0xB3, 0xC2};
  16. uint8_t hid_data[3] = {0xED, 0x87, 0x70};
  17. uint8_t indala_data[3] = {0x1F, 0x2E, 0x3D};
  18. switch(type) {
  19. case Type::EM:
  20. current_encoder->init(em_data, 5);
  21. break;
  22. case Type::HID_H10301:
  23. current_encoder->init(hid_data, 3);
  24. break;
  25. case Type::Indala_40134:
  26. current_encoder->init(indala_data, 3);
  27. break;
  28. }
  29. api_hal_rfid_tim_emulate(125000);
  30. api_hal_rfid_pins_emulate();
  31. api_interrupt_add(timer_update_callback, InterruptTypeTimerUpdate, this);
  32. for(size_t i = WWDG_IRQn; i <= DMAMUX1_OVR_IRQn; i++) {
  33. HAL_NVIC_SetPriority(static_cast<IRQn_Type>(i), 15, 0);
  34. }
  35. HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 5, 0);
  36. HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  37. api_hal_rfid_tim_emulate_start();
  38. } else {
  39. // not found
  40. }
  41. }
  42. void RfidTimerEmulator::stop() {
  43. api_hal_rfid_tim_emulate_stop();
  44. api_hal_rfid_tim_reset();
  45. api_hal_rfid_pins_reset();
  46. api_interrupt_remove(timer_update_callback, InterruptTypeTimerUpdate);
  47. }
  48. void RfidTimerEmulator::timer_update_callback(void* _hw, void* ctx) {
  49. RfidTimerEmulator* _this = static_cast<RfidTimerEmulator*>(ctx);
  50. TIM_HandleTypeDef* hw = static_cast<TIM_HandleTypeDef*>(_hw);
  51. if(api_hal_rfid_is_tim_emulate(hw)) {
  52. bool result;
  53. bool polarity;
  54. uint16_t period;
  55. uint16_t pulse;
  56. do {
  57. _this->current_encoder->get_next(&polarity, &period, &pulse);
  58. result = _this->pulse_joiner.push_pulse(polarity, period, pulse);
  59. } while(result == false);
  60. _this->pulse_joiner.pop_pulse(&period, &pulse);
  61. api_hal_rfid_set_emulate_period(period - 1);
  62. api_hal_rfid_set_emulate_pulse(pulse);
  63. }
  64. }