def run(): conn = Connection(3) msg = messages.GetBatteryLevel() conn.send(msg) resp = conn.receive() print('received response: %s' % type(resp)) print('battery voltage is %d mV.' % resp.millivolts)
def log(): with open('bluetooth.log', 'w') as log_file: print("Establishing connection...") conn = Connection(PORT, timeout=5) print('Logging output...') while True: print('Listening for output...') try: resp = conn.receive() except struct.error: print("Timed out") conn.close() time.sleep(1) conn.open() else: msg = parse_message(resp) print msg if msg.startswith('DISTANCE'): log_file.write('%s %s\n' % (datetime.now().time(), msg))
x = conn.receive().normalized_value readingList + x return (max(readingList)) def calibrateAVG(conn, port, n): for i in xrange(n): conn.send(messages.GetInputValues(port)) x = conn.receive().normalized_value readingList + x return (sum(readingList) / len(readingList)) try: conn.send(messages.GetInputValues(right_port)) Current_Reading = conn.receive().normalized_value print Current_Reading moveMotor(conn, motor_port_a, 75, .35) moveMotor(conn, motor_port_b, 75, .35) while Current_Reading >= Blue_Reading: conn.send(messages.GetInputValues(light_port)) Current_Reading = conn.receive().normalized_value print Current_Reading while ((Current_Reading < Gray_Reading) and (Current_Reading > Blue_Reading)): cmd = messages.SetOutputState(motor_port_a, motor_on=True, set_power=70, run_state=messages.RunState.running) conn.send(cmd) time.sleep(.35)
x = conn.receive().normalized_value readingList + x return (max(readingList)) def calibrateAVG(conn,port,n): for i in xrange(n): conn.send(messages.GetInputValues(port)) x = conn.receive().normalized_value readingList + x return (sum(readingList)/len(readingList)) try: conn.send(messages.GetInputValues(right_port)) Current_Reading = conn.receive().normalized_value print Current_Reading moveMotor(conn,motor_port_a,75,.35) moveMotor(conn,motor_port_b,75,.35) while Current_Reading >= Blue_Reading: conn.send(messages.GetInputValues(light_port)) Current_Reading = conn.receive().normalized_value print Current_Reading while ((Current_Reading < Gray_Reading) and (Current_Reading > Blue_Reading)): cmd = messages.SetOutputState(motor_port_a, motor_on = True, set_power = 70, run_state = messages.RunState.running) conn.send(cmd) time.sleep(.35) cmd = messages.SetOutputState(motor_port_a, motor_on = False) conn.send(cmd) time.sleep(.05) conn.send(messages.GetInputValues(light_port))