Skip to content

Instantly share code, notes, and snippets.

@jodal
Created November 4, 2013 19:33
Show Gist options
  • Save jodal/7307943 to your computer and use it in GitHub Desktop.
Save jodal/7307943 to your computer and use it in GitHub Desktop.
A Python DSL and command line util for controlling the subset of functionality I use on my NAD C355BEE analog stereo amp through its RS232 connection
#!/usr/bin/python
import serial
import sys
class Amp(object):
def __init__(self):
self._port = serial.Serial(
port='/dev/ttyUSB0', baudrate=115200, timeout=0.2)
def __getattr__(self, name):
key = 'Main.%s' % name
self._port.write('\n%s?\n' % key)
return self._port.readline().strip().replace('%s=' % key, '')
def __setattr__(self, name, value):
if name.startswith('_'):
self.__dict__[name] = value
return
key = 'Main.%s' % name
self._port.write('\n%s=%s\n' % (key, value))
result = self._port.readline().strip().replace('%s=' % key, '')
print '%s=%s %s' % (
name, value,
'OK' if result.lower() == value.lower() else 'failed')
def __add__(self, other):
for i in range(other):
self._port.write('\nMain.Volume+\n')
result = self._port.readline().strip() == 'Main.Volume+'
print 'Volume+ %s' % ('OK' if result else 'failed')
def __sub__(self, other):
for i in range(other):
self._port.write('\nMain.Volume-\n')
result = self._port.readline().strip() == 'Main.Volume-'
print 'Volume- %s' % ('OK' if result else 'failed')
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit('Usage: %s (video|disc|cd|+N|-N)' % sys.argv[0])
arg = sys.argv[1]
amp = Amp()
if arg.lower() in ('video', 'disc', 'cd'):
amp.Power = 'On'
amp.SpeakerA = 'On'
amp.SpeakerB = 'Off'
amp.Source = arg
elif arg.startswith('+'):
amp + int(arg[1:])
elif arg.startswith('-'):
amp - int(arg[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment