I'm playing the game - hack that code, and I'm guessing the owners will recognise the code - so hoping you can help.
I've done some programming in the past, but LUA is new to me, and Arduino I'm not fully up to speed, but can understand code...
I'm also not sure how to enter code in the forum...
So without further ado please see my lua script
speed = 115200
handshake = 0
serial_wait = 100
inited = 0
states = {}
dev = com.open("COM5", speed, handshake)
if dev == 0 then
ipc.display("Could not open device port")
ipc.exit()
end
function com1s_radio(offset, value)
ipc.log("Sent:- com1s:"..string.format("%04x",value).."\n")
com.write (dev,"com1s:"..string.format("%04x",value).."\n")
ipc.sleep(serial_wait)
end
event.offset (0x311A, "UW", "com1s_radio") -- Com1 Radio
And my Arduino script...
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int Radio_Rotary = 0;
char* Radio_Frequency[] = {"1850","1850","1850","1850","0830","0830","0830","0830"};
int com1_master=0;
int com1_slave=1;
#define STRLEN 30
char buffer[STRLEN];
int bufferIndex = 0;
void setup()
{
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.clear();
Serial.begin(115200);
Serial.flush();
}
void loop() {
if( Serial.available())
{
char ch = Serial.read();
if( ch == '\n') // is this the terminating carriage return
{
buffer[ bufferIndex ] = 0; // terminate the string
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
}
}// end of void loop
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, "com1s")== 0)
{
Radio_Frequency[com1_slave]= value;
update_display();
}
}
void my_copy (char* target,char* source,int len)
{
for(int i=0; i<len;i++){
target =source ;
}
}
void update_display()
{
lcd.setCursor(0,0);
delay(11);
lcd.print(Radio_Frequency[com1_master]);
lcd.setCursor(9,0);
delay(11);
lcd.print(Radio_Frequency[com1_slave]);
}
Lua output is -
369812 LUA.0: Sent:- com1s:2830
383375 LUA.0: Sent:- com1s:2827
395359 LUA.0: Sent:- com1s:2872
But each time I change, it sends 55555 to the LCD, and when I move a second time, it shows 5555555555 etc etc etc.
Anyone able to assist?
Random, it's working now :)
Issue I've now got...
So the displays now show 1850 (I've since modded the script) to now show 11850 - how can I put the . in
As Radio_Frequency is a char*
I was hoping something like freq = "1".substr(1,2).".".substr(3,2);
I know that's not exactly right, but how would you do it?
LOL.
Looks very familiar :)
Quote
function call_oat (offset, value)
value = (value/256)
ipc.log ("writes com = ?c"..string.format("%04d", value).. "\n")
com.write (dev, "?c" .. string.format("%04d", value) .. "\n")
ipc.sleep(serial_wait)
end
The Difference with my code and yours, is if you look above at mine, I have a different "String" for every offset I am calling, this allows me to sort through all the offsets I am pulling from the lua Script, and this might be problematic for you as you add more offsets to pull from the lua script. because you are using the "\n" Carriage return to goto the next variable. What I did was send a "?c" or other variable at the beginning of my offset data, and read each character after the symbol for each bit the offset is sending, really you should be reading "5" characters after the radio offset. 121.00 I do this by reading each like this..
Quote
vsi = ""; // Empty data string
vsi += char(Serial.read()); //Read the first data charactor sent
vsi += char(Serial.read()); //Read the second data charactor sent and add it to the first
vsi += char(Serial.read()); //Read the third data charactor sent and add it to the other two.)
vsi += char(Serial.read());
vsi += char(Serial.read());
vsi += char(Serial.read());
This lets me read 6 characters for the vsi offset. (I know there is 6 because I have watched what number appears in the fsuipc log. If you do not want to do this to identify all your charachters you can keep doing it the way you are, but I think you may have issues in the future when you are pulling more offsets.
In case I missed it in your code, you are treating the frequency as a String ("TEXT") what you should do is convert it to an integer
Quote
int VSIFSX ;
int VSIDECIMAL;
Quote
VSIFSX = vsi.toInt();
VSIDECIMAL = (VSIFSX/100);
Than you will do something like I did above and divide the integer 100. This will move your decimal place and you can
then print the VSIDECIMAL variable on your LCD.
I think LOL.
That's where I went wrong with two different scripts and trying to make an salad roll....
I think as your example script was more for steppers I thought hmmmm.
And you can recognise your script as yes, that's where I stole most of the code - as it was easiest for me to understand. I think where some of my code is different, and had to be changed is to get around coding that I can understand sometimes.
Will play, and let you know how I get on. I'm trying to recreate the FSlink multi radio first, and then will look at AP. Eventually I wouldn't mind getting this all working for the pmdg.....
You are right. My example was for servos. But the way I collect the code is the same for every offset. Discrete or variable.
My servo code is the part with all the math.
Anyway let us know if you need anymore help.
Quote from: Trevor Hale on December 04, 2015, 03:51:31 AM
Anyway let us know if you need anymore help.
Never say that :)
Ok, so I now have a pretty panel that shows me all the com, and nav radio, as well as ADF, and XPR.
Question is - how do I send data back?
I can't find a way to send the data back, well I have, but not how lua reacts to it - or even looks for it.
LOL. Yeah, about that.. I haven't mastered sending the data back.... I haven't had a need to do that yet. But I would think that you would basically reverse the operation.
Someone else may need to chime in here, I don't even use my adruino for switch inputs..
Sorry bud.
You might want to have a look at this project at the Prosim737 forum: http://prosim-ar.com/forum/viewtopic.php?f=12&t=7995 (http://prosim-ar.com/forum/viewtopic.php?f=12&t=7995)
Although specifically written for the Prosim737 suite, the Arduino code might give you new ideas on how to proceed.