Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import RPi.GPIO as GPIO, time
- import subprocess
- import tm1637
- import serial
- Display = tm1637.TM1637(CLK=4, DIO=5, brightness=1.0)
- Display.Clear()
- Enc_A = 17 # Encoder input A: input GPIO 17
- Enc_B = 18 # Encoder input B: input GPIO 18
- lastitem = (0,1,1)
- chan = 19
- ctable = [26965000, 26965000, 26975000, 26985000, 27005000,
- 27015000, 27025000, 27035000, 27055000, 27065000, 27075000,
- 27085000, 27105000, 27115000, 27125000, 27135000, 27155000,
- 27165000, 27175000, 27185000, 27205000, 27215000, 27225000,
- 27255000, 27235000, 27245000, 27265000, 27275000, 27285000,
- 27295000, 27305000, 27315000, 27325000, 27335000, 27345000,
- 27355000, 27365000, 27375000, 27385000, 27395000, 27405000]
- ser = serial.Serial(
- port='/dev/ttyAMA0',
- baudrate = 1200,
- parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS,
- timeout = 1
- )
- def channelup():
- global chan
- global disp
- global ctable
- if chan==40:
- chan = 1
- else:
- chan += 1
- def channeldown():
- global chan
- global disp
- if chan==1:
- chan = 40
- else:
- chan -= 1
- def display():
- global chan
- disp = [int(x) for x in str(chan).zfill(2)]
- Display.Show1(1, disp[0])
- Display.Show1(2, disp[1])
- def setradio():
- global chan
- global ctable
- preamble = b'\xFE\xFE\x28\xE0\x00'
- eol = b'\xFD'
- freq = str(ctable[chan])
- f2 = [freq[i:i+2] for i in range(0, len(freq), 2)][::-1]
- freq = ''.join(str(f).zfill(2) for f in f2).decode("hex")
- cmd = preamble + freq + eol
- ser.write(cmd)
- def init():
- GPIO.setwarnings(True)
- GPIO.setmode(GPIO.BCM) # Use BCM mode
- GPIO.setup(Enc_A, GPIO.IN) # setup callback thread for the A and B encoder
- GPIO.setup(Enc_B, GPIO.IN)
- GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotary_interrupt) # NO bouncetime
- GPIO.add_event_detect(Enc_B, GPIO.RISING, callback=rotary_interrupt) # NO bouncetime
- # 'interrupt' handler
- def rotary_interrupt(w):
- global lastitem
- if w == Enc_A:
- item = (w, 1, GPIO.input(Enc_B))
- else:
- item = (w, GPIO.input(Enc_A),1)
- if item == (Enc_A,1,1) and lastitem[1] == 0: # Is it in END position?
- channeldown()
- elif item == (Enc_B,1,1) and lastitem[2] == 0: # Same but for ENC_B
- channelup()
- lastitem = item
- # init and loop forever (stop with CTRL C)
- init()
- while 1:
- setradio()
- display()
- time.sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement