wiegand_play.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "../wiegand.h"
  2. void single_vibro() {
  3. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  4. notification_message(notification, &sequence_single_vibro);
  5. furi_record_close(RECORD_NOTIFICATION);
  6. }
  7. void wiegand_play() {
  8. uint32_t* delays = malloc(sizeof(uint32_t) * bit_count * 2);
  9. for(int i = 0; i < bit_count - 1; i++) {
  10. delays[i * 2] = (data_rise[i] - data_fall[i]) / 64;
  11. delays[i * 2 + 1] = (data_fall[i + 1] - data_rise[i]) / 64;
  12. }
  13. delays[(bit_count - 1) * 2] = (data_rise[bit_count - 1] - data_fall[bit_count - 1]) / 64;
  14. delays[(bit_count - 1) * 2 + 1] = 1;
  15. for(int i = 0; i < bit_count; i++) {
  16. // Delays are always at least 1 tick.
  17. if(delays[i * 2] == 0) delays[i * 2] = 1;
  18. if(delays[i * 2 + 1] == 0) delays[i * 2 + 1] = 1;
  19. if(delays[i * 2 + 1] > 5) delays[i * 2 + 1] -= 1;
  20. }
  21. furi_hal_gpio_write(pinD0, true);
  22. furi_hal_gpio_init(pinD0, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  23. furi_hal_gpio_write(pinD0mosfet, false);
  24. furi_hal_gpio_init(pinD0mosfet, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  25. furi_hal_gpio_write(pinD1, true);
  26. furi_hal_gpio_init(pinD1, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  27. furi_hal_gpio_write(pinD1mosfet, false);
  28. furi_hal_gpio_init(pinD1mosfet, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  29. single_vibro();
  30. furi_delay_ms(500);
  31. int j = 0;
  32. for(int i = 0; i < bit_count; i++) {
  33. if(data[i]) {
  34. furi_hal_gpio_write(pinD1mosfet, true); // Activate the mosfet to ground wire
  35. furi_hal_gpio_write(pinD1, false); // Ground the open-drain wire
  36. furi_delay_us(delays[j++]);
  37. furi_hal_gpio_write(pinD1, true); // Float the wire
  38. furi_hal_gpio_write(pinD1mosfet, false); // Deactivate the mosfet
  39. furi_delay_us(delays[j++]);
  40. } else {
  41. furi_hal_gpio_write(pinD0mosfet, true); // Activate the mosfet to ground wire
  42. furi_hal_gpio_write(pinD0, false); // Ground the open-drain wire
  43. furi_delay_us(delays[j++]);
  44. furi_hal_gpio_write(pinD0, true); // Float the wire
  45. furi_hal_gpio_write(pinD0mosfet, false); // Deactivate the mosfet
  46. furi_delay_us(delays[j++]);
  47. }
  48. }
  49. furi_hal_gpio_init_simple(pinD0, GpioModeAnalog);
  50. furi_hal_gpio_init_simple(pinD1, GpioModeAnalog);
  51. furi_hal_gpio_init_simple(pinD0mosfet, GpioModeAnalog);
  52. furi_hal_gpio_init_simple(pinD1mosfet, GpioModeAnalog);
  53. }