Commits

Alex Hixon committed b37620c

Flesh out wireless api, fix up console. Sends packed string.

Comments (0)

Files changed (2)

-class MessageType:
-    MOVE_THROTTLE = 1
-    MOVE_RUDDER = 2
-    MOVE_ELEVATOR = 3
-    
-    NOOP = 4
-    RESET = 5
-    
-    PROPELLER_ON = 6
-    PROPELLER_OFF = 7
-    
-    VERSION = 8
-    
-    SUCCESS = 0
-    FAILURE = 1
-    UNKNOWN = 2
+import wireless
 
-def get_ret (id):
-    if id == MessageType.SUCCESS:
-        return "Success"
-    elif id == MessageType.FAILURE:
-        return "Failure"
-    elif id == MessageType.UNKNOWN:
-        return "Unknown"
-    else:
-        return "code %d" % id
+#port="/dev/ttyUSB0"
+port="/dev/tty.usbserial-FTR9VFLT"
+#port="COM3"
 
-import serial
+print "Opening %s..." % port
+device = wireless.WirelessConnection (port)
 
-baud=19200
-#port="/dev/ttyUSB1"
-port="COM3"
-
-print "Opening %s (%d baud)..." % (port, baud)
-
-device = serial.Serial (port, baud)
-
-"""print "Opened! Flushing IO..."
-
-device.flushInput()
-device.flushOutput()"""
-
-device.write(MessageType.VERSION)
-version = device.readline()
-
-motd = device.readline()
+version = device.read_string()
+motd = device.read_string()
 
 print "Device uses version", version
 print "Ready for commands!"
             break
         
         if act[0] == "s":
-            device.write(ord(MessageType.RESET))
-            status = ord(device.read())
-            print "Return code:", get_ret(status)
+            device.send(wireless.MessageType.RESET)
+            print "Return code:", wireless.get_response_code(device.response)
             continue
         
         if act[0] != "t" and act[0] != "r" and act[0] != "e":
                 break
             except:
                 print "Please enter a number between 0 and 180."
+
+        deg += 180 # so we can use unsigned
         
         if act[0] == "t":
-            device.write (chr(MessageType.MOVE_THROTTLE))
+            device.send (wireless.MessageType.MOVE_THROTTLE, deg)
         elif act[0] == "r":
-            device.write (chr(MessageType.MOVE_RUDDER))
+            device.send (wireless.MessageType.MOVE_RUDDER, deg)
         elif act[0] == "e":
-            device.write (chr(MessageType.MOVE_ELEVATOR))
+            device.send (wireless.MessageType.MOVE_ELEVATOR, deg)
         
-        # Now, put out the actual movement amount as unsigned int in one byte (will be < 255)
-        device.write(chr(deg))
-        status = ord(device.read())
-        print "Return code:", get_ret(status)
+        print "Return code:", wireless.get_response_code(device.response)
 except Exception, e:
     print e
 
 print "Closing..."
-device.close()
+device.disconnect()
 print "All done!"
 import serial
+import struct
 
 class MessageType:
     MOVE_THROTTLE = 1
     FAILURE = 1
     UNKNOWN = 2
 
+def get_response_code (id):
+    if id == MessageType.SUCCESS:
+        return "Success"
+    elif id == MessageType.FAILURE:
+        return "Failure"
+    elif id == MessageType.UNKNOWN:
+        return "Unknown"
+    else:
+        return "code %d" % id
+
 class WirelessConnection ():
     def __init__ (self, port, baud=19200):
         self.port = port
         
         self.device = serial.Serial (port, baud)
     
-    def send (self, msg):
-        if len(msg) > 1:
-            for char in msg:
-                self.device.write (chr(char))
+    def send (self, msg, args=None):
+        if args is None:
+            self.device.write (chr(msg))
         else:
-            self.device.write (chr(msg))
+            a = struct.pack('<cI', chr(msg), args)
+            print a
+            self.device.write (a)
     
     @property
     def response (self):
     
     def disconnect (self):
         self.device.close()
-        
-    def get_response_code (id):
-        if id == MessageType.SUCCESS:
-            return "Success"
-        elif id == MessageType.FAILURE:
-            return "Failure"
-        elif id == MessageType.UNKNOWN:
-            return "Unknown"
-        else:
-            return "code %d" % id
+
+    def read_string (self):
+        return self.device.readline()
+
+    def read (self):
+        return self.response
+
+    def disconnect (self):
+        self.device.close()