Advertisement
nq4t

Untitled

May 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import RPi.GPIO as GPIO, time
  5. import subprocess
  6. import tm1637
  7. import serial
  8.  
  9. Display = tm1637.TM1637(CLK=4, DIO=5, brightness=1.0)
  10. Display.Clear()
  11. Enc_A = 17 # Encoder input A: input GPIO 17
  12. Enc_B = 18 # Encoder input B: input GPIO 18
  13. lastitem = (0,1,1)
  14. chan = 19
  15. ctable = [26965000, 26965000, 26975000, 26985000, 27005000,
  16. 27015000, 27025000, 27035000, 27055000, 27065000, 27075000,
  17. 27085000, 27105000, 27115000, 27125000, 27135000, 27155000,
  18. 27165000, 27175000, 27185000, 27205000, 27215000, 27225000,
  19. 27255000, 27235000, 27245000, 27265000, 27275000, 27285000,
  20. 27295000, 27305000, 27315000, 27325000, 27335000, 27345000,
  21. 27355000, 27365000, 27375000, 27385000, 27395000, 27405000]
  22. ser = serial.Serial(
  23. port='/dev/ttyAMA0',
  24. baudrate = 1200,
  25. parity=serial.PARITY_NONE,
  26. stopbits=serial.STOPBITS_ONE,
  27. bytesize=serial.EIGHTBITS,
  28. timeout = 1
  29. )
  30.  
  31.  
  32. def channelup():
  33. global chan
  34. global disp
  35. global ctable
  36. if chan==40:
  37. chan = 1
  38. else:
  39. chan += 1
  40.  
  41. def channeldown():
  42. global chan
  43. global disp
  44. if chan==1:
  45. chan = 40
  46. else:
  47. chan -= 1
  48.  
  49. def display():
  50. global chan
  51. disp = [int(x) for x in str(chan).zfill(2)]
  52. Display.Show1(1, disp[0])
  53. Display.Show1(2, disp[1])
  54.  
  55. def setradio():
  56. global chan
  57. global ctable
  58. preamble = b'\xFE\xFE\x28\xE0\x00'
  59. eol = b'\xFD'
  60. freq = str(ctable[chan])
  61. f2 = [freq[i:i+2] for i in range(0, len(freq), 2)][::-1]
  62. freq = ''.join(str(f).zfill(2) for f in f2).decode("hex")
  63. cmd = preamble + freq + eol
  64. ser.write(cmd)
  65.  
  66. def init():
  67. GPIO.setwarnings(True)
  68. GPIO.setmode(GPIO.BCM) # Use BCM mode
  69. GPIO.setup(Enc_A, GPIO.IN) # setup callback thread for the A and B encoder
  70. GPIO.setup(Enc_B, GPIO.IN)
  71. GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotary_interrupt) # NO bouncetime
  72. GPIO.add_event_detect(Enc_B, GPIO.RISING, callback=rotary_interrupt) # NO bouncetime
  73.  
  74. # 'interrupt' handler
  75. def rotary_interrupt(w):
  76. global lastitem
  77.  
  78. if w == Enc_A:
  79. item = (w, 1, GPIO.input(Enc_B))
  80. else:
  81. item = (w, GPIO.input(Enc_A),1)
  82.  
  83. if item == (Enc_A,1,1) and lastitem[1] == 0: # Is it in END position?
  84. channeldown()
  85. elif item == (Enc_B,1,1) and lastitem[2] == 0: # Same but for ENC_B
  86. channelup()
  87. lastitem = item
  88.  
  89. # init and loop forever (stop with CTRL C)
  90. init()
  91. while 1:
  92. setradio()
  93. display()
  94. time.sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement