Commits

Alex Hixon committed 4f8c664

Start work on sending direction data to controller. Need to send 2 bytes rather than 1!

  • Participants
  • Parent commits 7c4b58c

Comments (0)

Files changed (2)

 import math
 import cwiid
 
-import serial
+import wireless
 
 COLORS = {
     "bg": [0, 0, 0],
         self.pitchroll_a = [0.0, 0.0]       # absolute (unsmoothed) pitch and roll values
         self.alpha = 0.07                   # smoothing constant
         
+        print "Connecting to USB..."
+        self.wireless = wireless.WirelessConnection (COM_PORT)
+        print "Connected!"
+        
         glutInit()
         
         # usually we init the window AFTER we set window size and display mode
             self.cube.axes[0] = self.pitchroll[0] * PITCH_FACTOR
             self.cube.axes[1] = yaw * YAW_FACTOR
             self.cube.axes[2] = self.pitchroll[1] * ROLL_FACTOR
+            
+            # and send that stuff to the controller
+            self.wireless.send ((wireless.MessageType.MOVE_ELEVATOR, 180 - self.cube.axes[0]))
+            self.wireless.send ((wireless.MessageType.MOVE_RUDDER, 180 - self.cube.axes[2]))
+        else:
+            pass
+            # FIXME: send noop polls to the controller every x seconds
         
         glutSwapBuffers()
     
     def keydown (self, *args):
         if args[0] == '\033':
+            self.wireless.disconnect()
             sys.exit()
         elif args[0] == "r":
             # recalibrate
+import serial
+
+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
+
+class WirelessConnection ():
+    def __init__ (self, port, baud=19200):
+        self.port = port
+        self.baud = baud
+        self.version = -1
+        
+        self.device = serial.Serial (port, baud)
+    
+    def send (self, msg):
+        if len(msg) > 1:
+            for char in msg:
+                self.device.write (chr(char))
+        else:
+            self.device.write (chr(msg))
+    
+    @property
+    def response (self):
+        return ord(self.device.read())
+    
+    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