#!/usr/bin/python # fnordlicht serial2i2c client script # # for additional information please # see http://koeln.ccc.de/prozesse/running/fnordlicht # # (c) by Alexander Neumann # and Lars Noschinski # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # # For more information on the GPL, please go to: # http://www.gnu.org/copyleft/gpl.html from select import select from os import fdopen from sys import stdin from tty import setcbreak colors = [ [5,0,0x20,200,000,000], [5,0,0x20,000,200,000], [5,0,0x20,000,000,200], [5,0,0x20,255,255,255] ] colorpointer = 0 if __name__ == "__main__": # current bus command buscmd = "" # current keyboard command inputcmd = "" # open connection to the converter bus = open("/dev/ttyUSB0", "r+", 0) # disable line-buffering setcbreak(bus.fileno()) setcbreak(stdin.fileno()) # multiplex io with stdin inl = [bus, stdin] while 1: # select over all fds inf, ouf, erf = select(inl, [], []) # check every fd for a change if bus in inf: c = bus.read(1) if ord(c) < 32: print "received 0x%x" % ord(c) else: print "received '%0s'" % c buscmd += c while len(buscmd) >= 2: if buscmd[:2]== "bp": print "bootloader detected, starting main application" bus.write('X') elif buscmd[:2] == "bc": print "main application has started" elif buscmd[0] == "T": if ord(buscmd[1]) == 0x18: print "client answers (0x%x)" % ord(buscmd[1]) elif ord(buscmd[1]) == 0x20: print "no client responded (0x%x)" % ord(buscmd[1]) elif ord(buscmd[1]) == 0x28: print "client ack'ed databyte (0x%x)" % ord(buscmd[1]) elif ord(buscmd[1]) == 0x30: print "databyte has been nack'ed (0x%x)" % ord(buscmd[1]) else: print "TWI interrupt, new status: 0x%x" % ord(buscmd[1]) elif buscmd[0] == "D": print "Databytes left: %i" % ord(buscmd[1]) elif buscmd[0] == "A": print "TWI start transmitted, sending address: 0x%x" % ord(buscmd[1]) elif buscmd[0] == "I": print "TWI transmission started: flags 0x%x" % ord(buscmd[1]) elif buscmd[0] == "F": print "TWI no data bytes left, sending stop signal" elif buscmd[0] == "X": print "TWI unknown code: 0x%x" % ord(buscmd[1]) else: print "unknown command sequence: (0x%x, 0x%x)" % (ord(buscmd[0]), ord(buscmd[1])) buscmd = buscmd[2:] elif stdin in inf: inputcmd += stdin.read(1) if inputcmd == "-": print "-" * 76 elif inputcmd == " ": print "colorpointer: %i" % colorpointer for b in colors[colorpointer]: bus.write(chr(b)) colorpointer += 1 if colorpointer == len(colors): colorpointer = 0 else: bus.write(inputcmd) inputcmd = ""