# j4cDAC test code # # Copyright 2011 Jacob Potter # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3. # # 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, see . import socket import time import struct def pack_point(x, y, r, g, b, i = -1, u1 = 0, u2 = 0, flags = 0): """Pack some color values into a struct dac_point. Values must be specified for x, y, r, g, and b. If a value is not passed in for the other fields, i will default to max(r, g, b); the rest default to zero. """ #print(r,g,b) if i < 0: i = max(r, g, b) return struct.pack(" len(self.buf): buffy = self.conn.recv(4096) #print(buffy) self.buf += buffy obuf = self.buf self.buf = obuf[l:] return obuf[:l] def readresp(self, cmd): """Read a response from the DAC.""" data = self.read(22) response = data[0] cmdR = chr(data[1]) status = Status(data[2:]) status.dump() if cmdR != cmd: raise ProtocolError("expected resp for %r, got %r" % (cmd, cmdR)) if response != ord('a'): raise ProtocolError("expected ACK, got %r" % (response, )) self.last_status = status return status def __init__(self, host, port = 7765): """Connect to the DAC over TCP.""" conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((host, port)) conn.settimeout(1) self.conn = conn self.buf = b'' # Read the "hello" message first_status = self.readresp("?") first_status.dump() ''' self.conn.sendall('v') self.firmware_string = self.read(32).replace("\x00", " ").strip() print "Firmware: %s" % (self.firmware_string, ) ''' def begin(self, lwm, rate): cmd = struct.pack("