Example program of decoding the DF Robotics xbee joystick serial output

Dependencies:   mbed

Weapons and Systems Engineering mbed DFRobotJoystick Interface

/media/uploads/jebradshaw/dfjoystick_xbee.jpg

This page provides an example of reading an ASCII comma separated variable string from the DR Robot Wireless Gamepad using the mbed micro-controller. The DF Robbot Wireless Gamepad is an Arduino programmable game controller with an on-board xbee wireless transceiver module. The gamepads used in the Weapons and Systems Engineering Department have been reprogrammed with the arduino sketch listed below.

/media/uploads/jebradshaw/joes_dfrobot_gamepad_example_20141021_mode.ino

The output of the joystick follows the GPS NMEA 0183 protocol as an ASCII string preceded by an "$JOYSTK", and terminated with an asterisk and a two character checksum (hex representation).

Below is a screen shot of the data streaming in TeraTerminal when the gamepad is connected directly to the PC (through a virtual COM port).

/media/uploads/jebradshaw/df_robot_data_stream.jpg

Revision:
1:bc6098f1dd1a
Parent:
0:ce988e55f00d
--- a/mbed_DFRobot_joystick_parse.cpp	Fri Feb 06 21:04:14 2015 +0000
+++ b/mbed_DFRobot_joystick_parse.cpp	Mon Feb 09 13:42:35 2015 +0000
@@ -1,3 +1,4 @@
+// Example of parsing the serial string from the DF Robot Joystick with the mbed
 #include "mbed.h"
 
 DigitalOut myled(LED1);
@@ -6,8 +7,6 @@
     
 int main() {
     char joystr[100];   //used to store the whole joystick string
-    char header[20];    //stores header - not very useful here
-    char *tok;          //pointer used for string token function
     int buttonsAll;     //16bit representation of all buttons
     int button[16];     //individual button states (normally high)
     int js_lr_L;        //Left Joystick Left/Right movement
@@ -21,49 +20,34 @@
     
     pc.printf("\r\n\r\n%s\r\n", __FILE__);
         
-    while(1) {        
-        //read the serial string from the xbee (starts with '$', ends with \r\n
-        xbee.scanf("$%s\r\n", &joystr);
-        
+    while(1) {                
         while(xbee.readable())      //clear out the remaining characters in the buffer
             char c = xbee.getc();
             
-//        pc.printf("%s ", joystr);     //used for debugging
-        
-        //First pass strtok() the string address to parse, then NULLs to iterate through string
-        tok=strtok(joystr, ",");        //Extract the header
-        strcpy(header, tok);            //copy the header
-
-        tok=strtok(NULL, ",");          //Extract buttons string
-        buttonsAll=strtol(tok, NULL, 16);  //turn the 0xXXXX string into a decimal variable
-        
-        tok=strtok(NULL, ",");          //Extract Left joystick string L/R string
-        js_lr_L=atoi(tok);              //turn ASCII to integer for left joystick Left/Right
-        
-        tok=strtok(NULL, ",");          //Extract Left joystick string U/D string
-        js_ud_L=atoi(tok);              //turn ASCII to integer for left joystick Up/Down
-        
-        tok=strtok(NULL, ",");          //Extract Right joystick string L/R string
-        js_lr_R=atoi(tok);              //turn ASCII to integer for right joystick Left/Right
-        
-        tok=strtok(NULL, "*");          //Extract Right joystick string U/D string when asterisk is reached
-        js_ud_R=atoi(tok);              //turn ASCII to integer for right joystick Up/Down
-                        
-        tok=strtok(NULL, "\0");         //end of string
-        chksum = strtol(tok, NULL, 16); //turn hex string checksumn into integer
-        
-        //print the variables
-        pc.printf("%s %7d %5d %5d %5d %5d 0x%02X ", header, buttonsAll, js_lr_L, js_ud_L, js_lr_R, js_ud_R, chksum);                
-        
-        // iterate through buttonsAll (each bit) to assign value MSB first
-        for(int i=15;i>=0;i--){
-            if(buttonsAll & (1 << i))
-                button[i] = 1;
-            else
-                button[i] = 0;
-            pc.printf("%1d ", button[i]);
-        }
-        pc.printf("\r\n");
+        //read the serial string from the xbee (starts with '$', ends with \r\n            
+        xbee.scanf("$%s\r\n", &joystr);    
+            
+ //       pc.printf("%s\r\n", joystr);     //used for debugging
+  
+        if(!strncmp(joystr, "JOYSTK", 6)){
+            if (sscanf(joystr, "JOYSTK,%X,%d,%d,%d,%d*%X", &buttonsAll, &js_lr_L, &js_ud_L, &js_lr_R, &js_ud_R, &chksum) >= 1) {
+     
+                 // iterate through buttonsAll (each bit) to assign value MSB first
+                for(int i=15;i>=0;i--){
+                    if(buttonsAll & (1 << i))
+                        button[i] = 1;
+                    else
+                        button[i] = 0;
+                    pc.printf("%1d ", button[i]);
+                }
+                
+                //pc.printf("%7 %5d %5d %5d %5d 0x%02X\r\n", buttonsAll, js_lr_L, js_ud_L, js_lr_R, js_ud_R, chksum);
+                pc.printf("%5d %5d %5d %5d\r\n", js_lr_L, js_ud_L, js_lr_R, js_ud_R);
+            }
+            else{
+                pc.printf("BAD parse %s\r\n", joystr);    
+            }
+        }      
         myled = !myled; //toggle LED for activity verification
     }//while(1)
 }//main