diff --git a/.gitignore b/.gitignore index 3e451a4..2f16478 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ *.sym sourceLineMap.txt tags +*.pyc +*.sw? diff --git a/juggled/main.c b/juggled/main.c index f641718..b7ae43a 100755 --- a/juggled/main.c +++ b/juggled/main.c @@ -61,7 +61,7 @@ U32 adxl_transmit_count = 0; READ_WRITE_FLAG__FLAG_IMP(read_adxl_flag); READ_WRITE_FLAG__FLAG_IMP_DEFAULT(adxl_flag, false); READ_WRITE_FLAG__FLAG_IMP_DEFAULT(print_flag, false); -READ_WRITE_FLAG__FLAG_IMP(transmit_flag); +READ_WRITE_FLAG__FLAG_IMP_DEFAULT(transmit_flag, true); static bool transmit_adxl = true; static U16 time_to_transmit = false; @@ -271,5 +271,8 @@ void cmd_send_test_message(U8 argc, char **argv) chb_write(STATIONARY_SHORT_ADDRESS, str, strlen(str)); } -READ_WRITE_FLAG__CMD_IMPL(read_adxl_flag) +READ_WRITE_FLAG__CMD_IMPL(read_adxl_flag); +READ_WRITE_FLAG__CMD_IMPL(adxl_flag); +READ_WRITE_FLAG__CMD_IMPL(print_flag); +READ_WRITE_FLAG__CMD_IMPL(transmit_flag); diff --git a/stationary/reader.py b/pc/reader.py similarity index 100% rename from stationary/reader.py rename to pc/reader.py diff --git a/pc/server.py b/pc/server.py new file mode 100755 index 0000000..3e449e4 --- /dev/null +++ b/pc/server.py @@ -0,0 +1,85 @@ +#!/usr/bin/python + +""" +Serve a single serial port to multiple clients. That's it - no parsing +is done here. +""" + +import sys +import os +import time +from twisted.internet import reactor, protocol +from twisted.protocols import basic +from twisted.internet import task +from twisted.internet.serialport import SerialPort + +import pdb +brk = pdb.set_trace + +JUGGLING_SERVER_PORT = 7500 + +class LineBasedTeeProtocol(basic.LineReceiver): + + delimiter='\n' # I always fall on this point. grrr! + + def connectionMade(self): + print "connected" + self.factory.clients.append(self) + + def lineReceived(self, line): + print "got", repr(line) + self.factory.client_commands.append(line) # hope this is atomic :\ + reactor.callLater(0.0, self.factory.flush_lines_from_clients) + + +class TeeFactory(protocol.ServerFactory): + """ Protocol for all connectors, and a convenient place for our single serial + port, or rather serial protocol. + """ + + debug = False + protocol = LineBasedTeeProtocol + serial = None + clients = [] + client_commands = [] + + def on_line_from_serial(self, line): + if self.debug: + print "serial said", repr(line) + for c in self.clients: + c.transport.write(line+'\n') + + def flush_lines_from_clients(self): + for cmd in self.client_commands: + cmd = cmd.strip()+'\r' + print "sending", repr(cmd) + self.serial.transport.write(cmd) + self.client_commands = [] + +class StationaryProtocol(basic.LineReceiver): + delimiter = '\r' # extremely important. + + def __init__(self, teefactory): + self.tee = teefactory + self.tee.serial = self + def connectionMade(self): + pass + def lineReceived(self, line): + print line + reactor.callLater(0.0, self.tee.on_line_from_serial, line) + def request_full_scan(self): + self.transport.write(self.full_scan_request) + +def listenToTCPAndSerial(device): + teefactory = TeeFactory() + protocol = StationaryProtocol(teefactory) + SerialPort(protocol, device, reactor, baudrate=115200) + reactor.listenTCP(JUGGLING_SERVER_PORT, teefactory) + return protocol + +if __name__ == '__main__': + device = '/dev/ttyACM0' if len(sys.argv) == 1 or not os.path.exists(sys.argv[-1]) else sys.argv[-1] + print "Reading stationary from %s" % device + protocol = listenToTCPAndSerial(device) + reactor.run() + diff --git a/stationary/viewer.py b/pc/viewer.py old mode 100644 new mode 100755 similarity index 100% rename from stationary/viewer.py rename to pc/viewer.py