Source

faux_vox / configurator.py

Full commit
#!/usr/bin/env python

import argparse
import yaml
import time
import pdb
from midi_tester import test_midi

STARTUP_DELAY = 1.5

CONFIG = False 

from arduino import Arduino

#   0xF0 0x78 0xCMD 0xBANK 0xSWITCH 0xTOGGLE 0xS00 0xS01 0xS02 0xS10 0xS11 0xS12 0xF7
SYSEX_PREAMBLE='\xf0\x78'
SYSEX_RECONFIG = '\x00'
SYSEX_RESET = '\01'
SYSEX_STORE = '\x02'
SYSEX_RETRIVE = '\x03'
SYSEX_POSTAMBLE='\xf7'

def upload_config(data, arduino):
  for b in xrange(len(data['bank'])):
    for s in xrange(len(data['bank'][b]['switch'])):
      switch= data['bank'][b]['switch'][s]
      print switch
      command = build_reconfig_command(b, s, switch['toggle_not_immediate'], switch['state0_message'], switch['state1_message'])
      print 'Configuring bank %d switch %d' % (b, s)
      arduino.flush()
      arduino.send(command)
   
def reset_pedal(arduino):
  command = SYSEX_PREAMBLE + SYSEX_RESET + SYSEX_POSTAMBLE
  arduino.flush()
  arduino.send(command)
 
def store_config(bank, arduino):
  config_command(bank, SYSEX_STORE, arduino)

def retrive_config(bank, arduino):
  config_command(bank, SYSEX_RETRIVE, arduino)

def config_command(bank, sysex_command, arduino):
  command = SYSEX_PREAMBLE + sysex_command + chr(bank) + SYSEX_POSTAMBLE
  arduino.flush()
  if CONFIG:
    arduino.send(command)
  else:
    for b in command:
      print "0x%x " % ord(b)
 
def build_reconfig_command(bank, switch, toggle, message0, message1):
  command = SYSEX_PREAMBLE + SYSEX_RECONFIG
  command += chr(bank) + chr(switch) + chr(toggle) 
  m0 = ''.join( [chr(m) for m in message0] )
  m1 = ''.join( [chr(m) for m in message1] )
  command += m0 + m1 + SYSEX_POSTAMBLE
  return command
  

if __name__ == '__main__':
  a = Arduino('/dev/tty.usbmodem24371', 31250)
  time.sleep(STARTUP_DELAY)
  f=open('config.yaml', 'r')
  
  data = yaml.load(f)
  time.sleep(1)
  store_config(2, a)
  upload_config(data, a)
  store_config(1, a)
  test_midi(a)