Hi All,
I'm currently working on controlling an Arduino over the internet with an XBox 360 controller. I have a working ActiveX Object that can poll the state of the controller, format it into a message, send it to the Arduino and wait on a response. The ActiveX isn't perfect yet as it's requiring me to relax my internet security settings. Still working on it. Anyway, the formatted message would look like this...
LX:0|LY:0|RX:0|RY:0|LT:255|RT:255|AB:0|BB:0|XB:0|YB:0|RS:0|LS:0|DU:0|DD:0|DL:0|DR:0|ST:0|BK:0|LJ:0|RJ:0|BG:0
This successfully arrives at the Arduino and is parsed into individual variables. For testing purposes, the sketch is currently taking the trigger values (LT & RT) and sending them back to the web page as vibration values for the left & right vibrating motors. The sketch may iterate the loop 100 times before it seizes. My assumption is a memory leak but I don't know. I'm very new to C++. Coming from a web programming backround (PHP, javascript) I'm finding the way C++ handles variables totally baffling. After some googling, I learned that memory held by variables is released when the function they were declared in loses focus. I tried to arrange my loops and functions to facilitate this but it's still seizing.
My hope is that it's just going to jump out at someone. Without further ado...
My Code:
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x07, 0xBC };
IPAddress ip(192,168,1, 111);
EthernetServer server(12345);
EthernetClient client;
int iterations = 0;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
}
void theState()
{
// GamePad State Variables
int LX = 0; int LY = 0;
int RX = 0; int RY = 0;
int LT = 0; int RT = 0;
bool AB = false; bool BB = false; bool XB = false; bool YB = false;
bool RS = false; bool LS = false;
bool DU = false; bool DD = false; bool DL = false; bool DR = false;
bool ST = false; bool BK = false;
bool LJ = false; bool RJ = false;
bool BG = false;
char c;
char theState[128] = "";
int LV = 0;
int RV = 0;
float PS = 0;
int i = 0;
c = client.read();
while (c != '^')
{
theState[i] = c;
c = client.read();
i++;
}
// Parse the GamePad State
char *p = theState;
char *str;
while ((str = strtok_r(p, "|", &p)) != NULL)
{
char *q = str;
char *var;
int j = 0;
String _name;
String _value;
while ((var = strtok_r(q, ":", &q)) != NULL)
{
//char _var[8] = {char(*var)};
//_var = char(*var);
if (j == 0) _name = var; else _value = var;
j++;
}
char name[8];
_name.toCharArray(name, sizeof(name));
char value[8];
_value.toCharArray(value, sizeof(value));
//Serial.println("Name: " + name);
//Serial.println("Value: " + value);
if (strcmp(name, "LX") == 0){LX = atoi(value);}
if (strcmp(name, "LY") == 0){LY = atoi(value);}
if (strcmp(name, "RX") == 0){RX = atoi(value);}
if (strcmp(name, "RY") == 0){RY = atoi(value);}
if (strcmp(name, "LT") == 0){LT = atoi(value);}
if (strcmp(name, "RT") == 0){RT = atoi(value);}
if (strcmp(name, "AB") == 0){AB = (value == "1") ? true : false;}
if (strcmp(name, "BB") == 0){BB = (value == "1") ? true : false;}
if (strcmp(name, "XB") == 0){XB = (value == "1") ? true : false;}
if (strcmp(name, "YB") == 0){YB = (value == "1") ? true : false;}
if (strcmp(name, "LS") == 0){LS = (value == "1") ? true : false;}
if (strcmp(name, "RS") == 0){RS = (value == "1") ? true : false;}
if (strcmp(name, "DU") == 0){DU = (value == "1") ? true : false;}
if (strcmp(name, "DD") == 0){DD = (value == "1") ? true : false;}
if (strcmp(name, "DL") == 0){DL = (value == "1") ? true : false;}
if (strcmp(name, "DR") == 0){DR = (value == "1") ? true : false;}
if (strcmp(name, "ST") == 0){ST = (value == "1") ? true : false;}
if (strcmp(name, "BK") == 0){BK = (value == "1") ? true : false;}
if (strcmp(name, "LJ") == 0){LJ = (value == "1") ? true : false;}
if (strcmp(name, "RJ") == 0){RJ = (value == "1") ? true : false;}
if (strcmp(name, "BG") == 0){BG = (value == "1") ? true : false;}
}
// Set Pins with Variables
LV = LT; // Proof of Concept... Triggers controlling vibration
RV = RT;
// Read Any Sensors & Assemble the Response
PS = 14.05;
char buffer[10];
String _PS = dtostrf(PS, 5, 2, buffer);
String _LV = String(LV);
String _RV = String(RV);
String _response = "|LV:" + _LV + "|RV:" + _RV + "|PS:" + _PS;
char response[50];
_response.toCharArray(response, 50);
// send the response
client.write(response);
iterations++;
Serial.print(iterations);
Serial.print(" ");
Serial.println(response);
}
void loop()
{
client = server.available();
if (client)
{
if (client.connected() && client.available())
{
theState();
}
}
client.flush();
client.stop();
delay(1);
}
Any help at all would be greatly appreciated. If anyone is interested in the ActiveX Object and client-side javascript, I'm more than willing to share.
Thanks,
Don