Welcome to Cockpitbuilders.com. Please login or sign up.

April 19, 2024, 03:06:40 PM

Login with username, password and session length

PROUDLY ENDORSING


Fly Elise-ng
268 Guests, 0 Users
Members
  • Total Members: 4,154
  • Latest: xyligo
Stats
  • Total Posts: 59,640
  • Total Topics: 7,853
  • Online today: 286
  • Online ever: 582
  • (January 22, 2020, 08:44:01 AM)
Users Online
Users: 0
Guests: 268
Total: 268

COUNTDOWN TO WF2022


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Welcome

Arduino Help Please

Started by Binkles, December 02, 2015, 04:41:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Binkles

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?


Binkles


Binkles

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?

Trevor Hale

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.
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Binkles

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.....

Trevor Hale

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.
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Binkles

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.

Trevor Hale

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.
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Volante

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

Although specifically written for the Prosim737 suite, the Arduino code might give you new ideas on how to proceed.

Like the Website ?
Support Cockpitbuilders.com and Click Below to Donate