infrared_signal_viewer.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import flipperzero as f0
  2. import time
  3. terminate = False
  4. index = 0
  5. signal = []
  6. def draw_signal():
  7. global index
  8. global signal
  9. f0.canvas_clear()
  10. level = False if index % 2 else True
  11. x = 0
  12. y_low = 32
  13. y_high = 40
  14. y_level = y_low
  15. for i in range(100):
  16. i += index
  17. if i > len(signal):
  18. break
  19. duration = int(signal[i] / 100)
  20. if level:
  21. f0.canvas_draw_line(x, y_low, x, y_high)
  22. y_level = y_high
  23. else:
  24. f0.canvas_draw_line(x, y_high, x, y_low)
  25. y_level = y_low
  26. f0.canvas_draw_line(x, y_level, x + duration, y_level)
  27. x += duration
  28. level = not level
  29. if x > f0.canvas_width():
  30. break
  31. f0.canvas_update()
  32. @f0.on_input
  33. def on_input(button, type):
  34. global terminate
  35. global index
  36. global signal
  37. # terminate upon back button press
  38. if button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_SHORT:
  39. terminate = True
  40. return
  41. # transmit signal upon ok button
  42. if button == f0.INPUT_BUTTON_OK and type == f0.INPUT_TYPE_SHORT and len(signal) > 0:
  43. f0.infrared_transmit(signal)
  44. return
  45. # scroll left upon button left
  46. if button == f0.INPUT_BUTTON_LEFT and type == f0.INPUT_TYPE_SHORT:
  47. index -= 1 if index > 0 else 0
  48. draw_signal()
  49. return
  50. # scroll right upon button left
  51. if button == f0.INPUT_BUTTON_RIGHT and type == f0.INPUT_TYPE_SHORT:
  52. index += 1
  53. index %= len(signal)
  54. draw_signal()
  55. return
  56. f0.canvas_set_text(10, 32, "Waiting for IR signal ...")
  57. f0.canvas_update()
  58. # receive signal
  59. signal = f0.infrared_receive()
  60. if len(signal):
  61. draw_signal()
  62. else:
  63. terminate = True
  64. # wait upon termination
  65. while not terminate:
  66. time.sleep_ms(1)