kolaf
06-09-2012, 04:37 AM
Hi guys,
I just made my first hardware switch control and events in the flight simulator (P3D) using arduino and lua. I have connected a simple switch to the arduino board which sends commands over a serial link (through USB) to a lua script. The script subscribes to the parking brake event.
Every time I flicked the switch it turns the parking brake off and on, and whenever an internal simulation events (such as pressing the brake pedal) tries to turn the parking brake off, this information is sent to the board which can verify if this is in sync with the hardware switch. If the hardware switch says that the parking brake should still be on, it re-sends the parking brake command.
I plan to expand this to build my own panels with switches, displays and rotary knobs to take care of the most common FSX functions.
This is a picture of the setup. Impressive, huh?
6723
The arduino sketch is here:
#define STRLEN 20
char buffer[STRLEN];
int bufferIndex = 0;
int parking_break = 100;
int parking_break_pin = 4;
int digital_read_value = 100;
long last_debounce=0;
long debounce_delay=100;
void setup()
{
Serial.begin(115200);
Serial.flush();
pinMode(parking_break_pin, INPUT);
digitalWrite(parking_break_pin, HIGH);
}
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if( ch == '\n') // is this the terminating carriage return
{
buffer[ bufferIndex ] = 0; // terminate the strin
bufferIndex = 0; // reset the index ready for another string
parse_string(buffer);
// do something with the string
}
else
buffer[ bufferIndex++ ] = ch; // add the character into the buffer
}
check_input();
}
void parse_string (char* buffer)
{
char *name = NULL, *value = NULL;
//Serial.write(buffer);
name =strtok (buffer, ":");
value =strtok( NULL, ":");
if (name && value) {
handle_command (name, value);
}
}
void handle_command (char* name,char* value)
{
if (strcmp( name, "parkingb") == 0)
{
if(parking_break !=atoi( value))
{
send_value ("parkingb", parking_break);
}
}
}
void send_value (char* name,int value)
{
char buffer [20];
sprintf (buffer, "%s:%d\0", name, value);
Serial.write (buffer);
}
void check_input ()
{
digital_read_value = digitalRead(parking_break_pin);
if (digital_read_value!= parking_break) {
parking_break = digital_read_value;
last_debounce= millis();
}
if (last_debounce!=0 && (millis() - last_debounce) > debounce_delay) {
//Serial.write("changed value");
last_debounce=0;
send_value ("parkingb", parking_break);
}
}
And the lua script is here:
speed = 115200
handshake = 0
dev = com.open("COM8", speed, handshake)
if dev == 0 then
ipc.display("Could not open VRIdevice port")
ipc.exit()
end
function handlecom(handle, str)
ipc.log("com=" .. str)
if string.match(str, "parkingb:(%d)") then
state = tonumber(string.match(str, "parkingb:(%d)"))
if state == 1 then
ipc.writeUD(0x0BC8, 32767)
else
ipc.writeUD(0x0BC8, 0)
end
end
end
-- ----------------------------- Callbacks -------------------
function parking_break (offset, value)
if value == 0 then
com.write(dev, "parkingb:0\n")
else
com.write(dev, "parkingb:1\n")
end
end
ipc.sleep(500)
event.com(dev, 20, 5, 0, "handlecom")
event.offset(0x0BC8, "UW", "parking_break")
I just made my first hardware switch control and events in the flight simulator (P3D) using arduino and lua. I have connected a simple switch to the arduino board which sends commands over a serial link (through USB) to a lua script. The script subscribes to the parking brake event.
Every time I flicked the switch it turns the parking brake off and on, and whenever an internal simulation events (such as pressing the brake pedal) tries to turn the parking brake off, this information is sent to the board which can verify if this is in sync with the hardware switch. If the hardware switch says that the parking brake should still be on, it re-sends the parking brake command.
I plan to expand this to build my own panels with switches, displays and rotary knobs to take care of the most common FSX functions.
This is a picture of the setup. Impressive, huh?
6723
The arduino sketch is here:
#define STRLEN 20
char buffer[STRLEN];
int bufferIndex = 0;
int parking_break = 100;
int parking_break_pin = 4;
int digital_read_value = 100;
long last_debounce=0;
long debounce_delay=100;
void setup()
{
Serial.begin(115200);
Serial.flush();
pinMode(parking_break_pin, INPUT);
digitalWrite(parking_break_pin, HIGH);
}
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if( ch == '\n') // is this the terminating carriage return
{
buffer[ bufferIndex ] = 0; // terminate the strin
bufferIndex = 0; // reset the index ready for another string
parse_string(buffer);
// do something with the string
}
else
buffer[ bufferIndex++ ] = ch; // add the character into the buffer
}
check_input();
}
void parse_string (char* buffer)
{
char *name = NULL, *value = NULL;
//Serial.write(buffer);
name =strtok (buffer, ":");
value =strtok( NULL, ":");
if (name && value) {
handle_command (name, value);
}
}
void handle_command (char* name,char* value)
{
if (strcmp( name, "parkingb") == 0)
{
if(parking_break !=atoi( value))
{
send_value ("parkingb", parking_break);
}
}
}
void send_value (char* name,int value)
{
char buffer [20];
sprintf (buffer, "%s:%d\0", name, value);
Serial.write (buffer);
}
void check_input ()
{
digital_read_value = digitalRead(parking_break_pin);
if (digital_read_value!= parking_break) {
parking_break = digital_read_value;
last_debounce= millis();
}
if (last_debounce!=0 && (millis() - last_debounce) > debounce_delay) {
//Serial.write("changed value");
last_debounce=0;
send_value ("parkingb", parking_break);
}
}
And the lua script is here:
speed = 115200
handshake = 0
dev = com.open("COM8", speed, handshake)
if dev == 0 then
ipc.display("Could not open VRIdevice port")
ipc.exit()
end
function handlecom(handle, str)
ipc.log("com=" .. str)
if string.match(str, "parkingb:(%d)") then
state = tonumber(string.match(str, "parkingb:(%d)"))
if state == 1 then
ipc.writeUD(0x0BC8, 32767)
else
ipc.writeUD(0x0BC8, 0)
end
end
end
-- ----------------------------- Callbacks -------------------
function parking_break (offset, value)
if value == 0 then
com.write(dev, "parkingb:0\n")
else
com.write(dev, "parkingb:1\n")
end
end
ipc.sleep(500)
event.com(dev, 20, 5, 0, "handlecom")
event.offset(0x0BC8, "UW", "parking_break")