Source

fmradio / fmradio.py

Full commit
from twisted.internet import reactor
from txosc import osc
from txosc import dispatch
from txosc import async

import serial
import time

ser = serial.Serial('/dev/tty.usbserial-A8006AtT', 57600)
time.sleep(3)

class UDPReceiverApplication(object):
	def __init__(self):
		self.port = 8000
		self.dest_port = 9000
		self.receiver = dispatch.Receiver()
		self.sender = async.DatagramClientProtocol()
		self._sender_port = reactor.listenUDP(0, self.sender)
		self._server_port = reactor.listenUDP(self.port, async.DatagramServerProtocol(self.receiver))
		self.receiver.addCallback("/radio/tuner", self.radio_tuner_handler)
		self.receiver.addCallback("/radio/volume", self.radio_volume_handler)
		self.receiver.addCallback("/radio/favorite", self.radio_favorite_handler)

		ser.write("x")
		line = ser.readline()
		self.sender.send(osc.Message("/radio/tuner", line), ("192.168.152.101", self.dest_port))
		self.sender.send(osc.Message("/radio/station", str(float(line)/10)), ("192.168.152.101", self.dest_port))
		ser.write("g")
		line = ser.readline()
		self.sender.send(osc.Message("/radio/volume", line), ("192.168.152.101", self.dest_port))

	def radio_tuner_handler(self, message, address):
		station = int(message.getValues()[0])
		st1 = station & 0xff
		st2 = station >> 8
		ser.write("t" + chr(st2) + chr(st1));
		self.sender.send(osc.Message("/radio/station", str(float(station)/10)), ("192.168.152.101", self.dest_port))

	def radio_volume_handler(self, message, address):
		volume = int(message.getValues()[0])
		ser.write("v" + chr(volume))

	def radio_favorite_handler(self, message, address):
		station = int(message.getValues()[0])
		st1 = station & 0xff
		st2 = station >> 8
		ser.write("t" + chr(st2) + chr(st1));
		self.sender.send(osc.Message("/radio/tuner", station), ("192.168.152.101", self.dest_port))
		self.sender.send(osc.Message("/radio/station", str(float(station)/10)), ("192.168.152.101", self.dest_port))

if __name__ == "__main__":
    app = UDPReceiverApplication()
    reactor.run()