encoder-hid-h10301.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "encoder-hid-h10301.h"
  2. #include "protocols/protocol-hid-h10301.h"
  3. #include <furi.h>
  4. void EncoderHID_H10301::init(const uint8_t* data, const uint8_t data_size) {
  5. ProtocolHID10301 hid;
  6. hid.encode(data, data_size, reinterpret_cast<uint8_t*>(&card_data), sizeof(card_data) * 3);
  7. card_data_index = 0;
  8. bit_index = 0;
  9. }
  10. void EncoderHID_H10301::write_bit(bool bit, uint8_t position) {
  11. write_raw_bit(bit, position + 0);
  12. write_raw_bit(!bit, position + 1);
  13. }
  14. void EncoderHID_H10301::write_raw_bit(bool bit, uint8_t position) {
  15. if(bit) {
  16. card_data[position / 32] |= 1UL << (31 - (position % 32));
  17. } else {
  18. card_data[position / 32] &= ~(1UL << (31 - (position % 32)));
  19. }
  20. }
  21. void EncoderHID_H10301::get_next(bool* polarity, uint16_t* period, uint16_t* pulse) {
  22. // hid 0 is 6 cycles by 8 clocks
  23. const uint8_t hid_0_period = 8;
  24. const uint8_t hid_0_count = 6;
  25. // hid 1 is 5 cycles by 10 clocks
  26. const uint8_t hid_1_period = 10;
  27. const uint8_t hid_1_count = 5;
  28. bool bit = (card_data[card_data_index / 32] >> (31 - (card_data_index % 32))) & 1;
  29. *polarity = true;
  30. if(bit) {
  31. *period = hid_1_period;
  32. *pulse = hid_1_period / 2;
  33. bit_index++;
  34. if(bit_index >= hid_1_count) {
  35. bit_index = 0;
  36. card_data_index++;
  37. if(card_data_index >= (32 * card_data_max)) {
  38. card_data_index = 0;
  39. }
  40. }
  41. } else {
  42. *period = hid_0_period;
  43. *pulse = hid_0_period / 2;
  44. bit_index++;
  45. if(bit_index >= hid_0_count) {
  46. bit_index = 0;
  47. card_data_index++;
  48. if(card_data_index >= (32 * card_data_max)) {
  49. card_data_index = 0;
  50. }
  51. }
  52. }
  53. }