def communicate(comms, frame, args, quiet=False): """ Communicate with the DPS device according to the user's wishes """ bytes_ = frame.get_frame() if not comms: fail("no communication interface specified") if not comms.open(): fail("could not open {}".format(comms.name())) if args.verbose: print("Communicating with {}".format(comms.name())) print("TX {:2d} bytes [{}]".format( len(bytes_), " ".join("{:02x}".format(b) for b in bytes_))) if not comms.write(bytes_): fail("write failed on {}".format(comms.name())) resp = comms.read() if len(resp) == 0: fail("timeout talking to device {}".format(comms._if_name)) elif args.verbose: print("RX {:2d} bytes [{}]\n".format( len(resp), " ".join("{:02x}".format(b) for b in resp))) if not comms.close: print("Warning: could not close {}".format(comms.name())) f = uframe.uFrame() res = f.set_frame(resp) if res < 0: fail("protocol error ({:d})".format(res)) else: return handle_response(frame.get_frame()[1], f, args, quiet)
def create_upgrade_start(window_size, crc): f = uFrame() f.pack8(CMD_UPGRADE_START) f.pack16(window_size) f.pack16(crc) f.end() return f
def create_upgrade_data(data): f = uFrame() f.pack8(CMD_UPGRADE_DATA) for d in data: f.pack8(d) f.end() return f
def create_temperature(temperature): print("Sending temperature {:.1f} and {:.1f}".format( temperature, -temperature)) temperature = int(10 * temperature) f = uFrame() f.pack8(CMD_TEMPERATURE_REPORT) f.pack16(temperature) f.pack16(-temperature) f.end() return f
def create_query_response(v_in, v_out_setting, v_out, i_out, i_limit, power_enabled): f = uFrame() f.pack8(CMD_RESPONSE | CMD_QUERY) f.pack16(v_in) f.pack16(v_out_setting) f.pack16(v_out) f.pack16(i_out) f.pack16(i_limit) f.pack8(power_enabled) f.end() return f
def create_set_parameter(parameter_list): f = uFrame() f.pack8(CMD_SET_PARAMETERS) for p in parameter_list: parts = p.split("=") if len(parts) != 2: return None else: f.pack_cstr(parts[0].lstrip().rstrip()) f.pack_cstr(parts[1].lstrip().rstrip()) f.end() return f
def create_set_calibration(parameter_list): f = uFrame() f.pack8(CMD_SET_CALIBRATION) for p in parameter_list: parts = p.split("=") if len(parts) != 2: return None else: f.pack_cstr(parts[0].lstrip().rstrip()) for t in bytearray( struct.pack("f", float(parts[1].lstrip().rstrip()))): f.pack8(t) f.pack8(0) f.end() return f
def create_set_function(name): f = uFrame() f.pack8(CMD_SET_FUNCTION) f.pack_cstr(name) f.end() return f
def create_cmd(cmd): f = uFrame() f.pack8(cmd) f.end() return f
def create_response(command, success): f = uFrame() f.pack8(CMD_RESPONSE | command) f.pack8(success) f.end() return f
def create_set_brightness(brightness): f = uFrame() f.pack8(CMD_SET_BRIGHTNESS) f.pack8(brightness) f.end() return f
def create_change_screen(screen): f = uFrame() f.pack8(CMD_CHANGE_SCREEN) f.pack8(screen) f.end() return f
def create_ocp(i_cut): f = uFrame() f.pack8(CMD_OCP_EVENT) f.pack16(i_cut) f.end() return f
def create_lock(locked): f = uFrame() f.pack8(CMD_LOCK) f.pack8(locked) f.end() return f
def create_wifi_status(wifi_status): f = uFrame() f.pack8(CMD_WIFI_STATUS) f.pack8(wifi_status) f.end() return f
def create_enable_output(activate): f = uFrame() f.pack8(CMD_ENABLE_OUTPUT) f.pack8(1 if activate == "on" else 0) f.end() return f