Advertisement
nq4t

Untitled

May 20th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 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. Display = tm1637.TM1637(CLK=4, DIO=5, brightness=1.0)
  8. Display.Clear()
  9.  
  10. Enc_A = 17                      # Encoder input A: input GPIO 17
  11. Enc_B = 18                      # Encoder input B: input GPIO 18
  12.  
  13. lastitem = (0,1,1)
  14.  
  15. chan = 19
  16. ctable = [26965000, 26965000, 26975000, 26985000, 27005000,
  17. 27015000, 27025000, 27035000, 27055000, 27065000, 27075000,
  18. 27085000, 27105000, 27115000, 27125000, 27135000, 27155000,
  19. 27165000, 27175000, 27185000, 27205000, 27215000, 27225000,
  20. 27255000, 27235000, 27245000, 27265000, 27275000, 27285000,
  21. 27295000, 27305000, 27315000, 27325000, 27335000, 27345000,
  22. 27355000, 27365000, 27375000, 27385000, 27395000, 27405000]
  23.  
  24. def channelup():
  25.         global chan
  26.         global disp
  27.         global ctable
  28.         if chan==40:
  29.                 chan = 1
  30.         else:
  31.                   chan += 1
  32.  
  33. def channeldown():
  34.         global chan
  35.         global disp
  36.         if chan==1:
  37.                 chan = 40
  38.         else:
  39.                 chan -= 1
  40.  
  41. def display():
  42.         global chan
  43.         disp = [int(x) for x in str(chan).zfill(2)]
  44.         Display.Show1(1, disp[0])
  45.         Display.Show1(2, disp[1])
  46.  
  47. def setradio():
  48.         global chan
  49.         global ctable
  50.         freq = str(ctable[chan])
  51.         subprocess.call(["rigctl", "-m", "314", "-r", "/dev/ttyAMA0", "-c", "0x28",  "-s", "1200", "F", str(ctable[chan])])
  52.  
  53. def init():
  54.         GPIO.setwarnings(True)
  55.         GPIO.setmode(GPIO.BCM)                  # Use BCM mode
  56.         GPIO.setup(Enc_A, GPIO.IN)                      # setup callback thread for the A and B encoder
  57.         GPIO.setup(Enc_B, GPIO.IN)
  58.         GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotary_interrupt)    # NO bouncetime
  59.         GPIO.add_event_detect(Enc_B, GPIO.RISING, callback=rotary_interrupt)    # NO bouncetime
  60.  
  61. # 'interrupt' handler
  62. def rotary_interrupt(w):
  63.         global chan
  64.         global disp
  65.         global lastitem
  66.  
  67.         if w == Enc_A:
  68.                 item = (w, 1, GPIO.input(Enc_B))
  69.         else:
  70.                 item = (w, GPIO.input(Enc_A),1)
  71.  
  72.         if item == (Enc_A,1,1) and lastitem[1] == 0:    # Is it in END position?
  73.                 channeldown()
  74.                 setradio()
  75.                 display()
  76.         elif item == (Enc_B,1,1) and lastitem[2] == 0:  # Same but for ENC_B
  77.                 channelup()
  78.                 setradio()
  79.                 display()
  80.         lastitem = item
  81.  
  82. # init and loop forever (stop with CTRL C)
  83. setradio()
  84. display()
  85. init()
  86. while 1:
  87.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement