Guest User

Untitled

a guest
Oct 7th, 2013
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.06 KB | None | 0 0
  1. foot =3.28083989501312
  2. speed = 115200
  3. handshake = 0
  4. serial_wait = 100
  5. inited = 0
  6. states = {}
  7. dev = com.open("COM9", speed, handshake)
  8. if dev == 0 then
  9.     ipc.display("Could not open device port")
  10.     ipc.exit()
  11. end
  12. red_string = ""
  13.  
  14.  
  15. -- index from 1
  16. commands={}
  17. table.insert(commands, {0x0bc8, 0x0bc8, "parkingb", ipc.writeUW, ipc.readUW, 32767, 0})
  18. table.insert(commands, {0x0d0c, 0x0d0c, "lights", ipc.writeUW, ipc.readUW, -1, -1})
  19. table.insert(commands, {0x07bc, 0x07bc, "master", ipc.writeUD, ipc.readUD, 1, 0})
  20. table.insert(commands, {0x07c0, 0x07c0, "winglevel", ipc.writeUD, ipc.readUD, 1, 0})
  21. table.insert(commands, {0x07c8, 0x07c8, "hhold", ipc.writeUD, ipc.readUD, 1, 0})
  22. table.insert(commands, {0x07d0, 0x07d0, "ahold", ipc.writeUD, ipc.readUD, 1, 0})
  23. table.insert(commands, {0x07c4, 0x07c4, "navhold", ipc.writeUD, ipc.readUD, 1, 0})
  24. table.insert(commands, {0x07ec, 0x07ec, "vshold", ipc.writeUD, ipc.readUD, 1, 0})
  25. table.insert(commands, {0x0800, 0x0800, "approachhold", ipc.writeUD, ipc.readUD, 1, 0})
  26. table.insert(commands, {0x07dc, 0x07dc, "speedhold", ipc.writeUD, ipc.readUD, 1, 0})
  27. table.insert(commands, {0x0810, 0x0810, "throttle", ipc.writeUD, ipc.readUD, 1, 0})
  28. table.insert(commands, {0x0892, 0x0892, "magneto", ipc.writeUW, ipc.readUW, -1, -1})
  29.  
  30. table.insert(commands, {0x281c, 0x281c, "battery", ipc.writeUW, ipc.readUW, 1, 0})
  31. table.insert(commands, {0x2e80, 0x2e80, "avionics", ipc.writeUW, ipc.readUW, 1, 0})
  32. table.insert(commands, {0x3101, 0x3101, "alternator", ipc.writeUB, ipc.readUB, 1, 0})
  33. table.insert(commands, {0x3104, 0x3104, "fuel", ipc.writeUB, ipc.readUB, 1, 0})
  34. table.insert(commands, {0x337c, 0x337c, "deice", ipc.writeUB, ipc.readUB, 1, 0})
  35. table.insert(commands, {0x337d, 0x337d, "deice", ipc.writeUB, ipc.readUB, 1, 0})
  36. table.insert(commands, {0x029c, 0x029c, "pitot", ipc.writeUB, ipc.readUB, 1, 0})
  37.  
  38. table.insert(commands, {66079, 66080, "gear", ipc.control, ipc.readUD, 0, 0})
  39.  
  40. function do_action (name, action)
  41.     second = 0
  42.     for i,v in ipairs(commands) do
  43.         if string.match (name,v[3]) then
  44.             ipc.log(v[2])
  45.             if string.match (action, "on") or second == 1 then
  46.                 v[4](v[1],v[6])
  47.                 states [v[1]] =v[6]
  48.             elseif string.match (action, "off") then
  49.                 v[4](v[2],v[7])
  50.                 states [v[2]] =v[7]
  51.             elseif string.match (action, "toggle") then
  52.                 current_value =v[5](v[1])
  53.                 if current_value  ~=v[6] then
  54.                     v[4](v[1],v[6])
  55.                 elseif current_value  ~=v[6] then
  56.                     v[4](v[2],v[7])
  57.                 end
  58.             else
  59.                 v[4](v[1], tonumber (action))
  60.                 second=1
  61.                 states [v[1]] = tonumber (action)
  62.             end
  63.  
  64.         end
  65.     end
  66. end
  67.  
  68. function call_offset (offset, value)
  69.     for i,v in ipairs(commands) do
  70.         if offset ==v[1] then
  71.             if states[offset] and value ~= states[offset] and inited==1 then
  72.                 v[4](offset, states[offset])
  73.             end
  74.         end
  75.         break
  76.     end
  77. end
  78.  
  79. function string:split( inSplitPattern, outResults )
  80.   if not outResults then
  81.     outResults = { }
  82.   end
  83.   local theStart = 1
  84.   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern,
  85. theStart )
  86.   while theSplitStart do
  87.     table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
  88.     theStart = theSplitEnd + 1
  89.     theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  90.   end
  91.   table.insert( outResults, string.sub( self, theStart ) )
  92.   return outResults
  93. end
  94.  
  95.  
  96. function handlecom(handle, str)
  97.     red_string =red_string..str
  98.     ipc.log("com=" .. str)
  99.     while(com.test(dev)>0) do
  100.         news, num=com.read (dev, 10000,10)
  101.         ipc.log(".. " .. news)
  102.     red_string =red_string .. news
  103.     end
  104.     if not string.match(red_string, ".*\n") then
  105.     return
  106.     end
  107.     data=string.split(red_string, "\n")
  108.     for i,c in ipairs(data) do
  109.        ipc.log("command=" .. c)
  110.     command_name, command_value = string.match (c, "(%a+):(.*)")
  111.     if command_name then
  112.         do_action (command_name, command_value)
  113.  
  114.         if string.match(command_name, "init") then
  115.         if string.match(command_value, "on") then
  116.             inited=1
  117.             else
  118.             inited=0
  119.             end
  120.     end
  121.    
  122.     end
  123.     if string.match(str, "nav1s:(%d+)") then
  124.         state = "0x"..string.match(str, "nav1s:(%d+)")
  125.         ipc.writeUW(0x311e, state)
  126.     end
  127.     if string.match(str, "nav2s:(%d+)") then
  128.         state = "0x"..string.match(str, "nav2s:(%d+)")
  129.         ipc.writeUW(0x3120, state)
  130.     end
  131.  
  132. if string.match(str, "com1s:(%d+)") then
  133.         state = "0x"..string.match(str, "com1s:(%d+)")
  134.         ipc.writeUW(0x311a, state)
  135.     end
  136. if string.match(str, "com2s:(%d+)") then
  137.         state = "0x"..string.match(str, "com2s:(%d+)")
  138.         ipc.writeUW(0x311c, state)
  139.     end
  140.  
  141.     if string.match(str, "adf1:(%d+)") then
  142.         state = "0x"..string.match(str, "adf1:(%d+)")
  143.         ipc.writeUW(0x034c, state)
  144.     end
  145.     if string.match(str, "adf2:(%d+)") then
  146.         state = "0x"..string.match(str, "adf2:(%d+)")
  147.         ipc.writeUW(0x02d4, state)
  148.     end
  149.     if string.match(str, "trans:(%d+)") then
  150.         state = "0x"..string.match(str, "trans:(%d+)")
  151.         ipc.writeUW(0x0354, state)
  152.     end
  153.  
  154.     if string.match(str, "flaps:(-?%d+)") then
  155.         state = tonumber(string.match(str, "flaps:(-?%d+)"))
  156.         ipc.control(66534, state)
  157.     end
  158. if string.match(str, "ailtrim:(-?%d+)") then
  159.         state = tonumber(string.match(str, "ailtrim:(-?%d+)"))
  160.         ipc.writeSW(0x0c02, state)
  161.     end
  162. if string.match(str, "ruddertrim:(-?%d+)") then
  163.         state = tonumber(string.match(str, "ruddertrim:(-?%d+)"))
  164.         ipc.writeSW(0x0c04, state)
  165.     end
  166.  
  167.     if string.match(str, "spoilers:(-?%d+)") then
  168.         state = tonumber(string.match(str, "spoilers:(-?%d+)"))
  169.         ipc.control(66382, state)
  170.     end
  171.     if string.match(str, "altitude:(-?%d+)") then
  172.         state = tonumber(string.match(str, "altitude:(-?%d+)"))
  173.         current =  ipc.readUD(0x07d4) + ((state*100*65536)/foot)
  174.         ipc.writeUD(0x07d4, current )
  175.     end
  176.     if string.match(str, "obs2:(-?%d+)") then
  177.         state = tonumber(string.match(str, "obs2:(-?%d+)"))
  178.         current =  ipc.readUW(0xc5e) + state
  179.         ipc.writeUW(0x0c5e, current )
  180.     end
  181.     if string.match(str, "obs1:(-?%d+)") then
  182.         state = tonumber(string.match(str, "obs1:(-?%d+)"))
  183.         current =  ipc.readUW(0x0c4e) + state
  184.         if current >359 then
  185.             current = current -360
  186.         end
  187.         if current <0 then
  188.            current = current +360
  189.         end
  190.         ipc.writeUW(0x0c4e, current )
  191.     end
  192.  
  193.     if string.match(str, "heading:(-?%d+)") then
  194.         state = tonumber(string.match(str, "heading:(-?%d+)"))
  195.         current =  ipc.readUW(0x07cc) + state*65536/360
  196.         ipc.writeUW(0x07cc, current )
  197.     end
  198.     if string.match(str, "vertical:(-?%d)") then
  199.         state = tonumber(string.match(str, "vertical:(-?%d)"))
  200.         current =  ipc.readSW(0x07f2)+ state*100
  201.         if current > 7000 then
  202.             current = 7000
  203.         elseif current <-7000 then
  204.             current = -7000
  205.         end
  206.     current = current
  207.         ipc.writeSW(0x07f2, current )
  208.     end
  209.     if string.match (str,"swap:(%x)") then
  210.         state =tonumber(string.match(str, "swap:(%x)"))
  211.         ipc.writeUB (0x3123, state)
  212.     end
  213. end
  214. red_string = string.match (red_string, ".*\n(.*)")
  215.  
  216. end
  217.  
  218.  
  219. -- ----------------------------- Callbacks -------------------
  220.  
  221.  
  222. function call_radio_DME (offset, value)
  223.     if offset ==0x0300 then
  224.         command = "dme1:" .. string.format("%04d", value) .. "\n"
  225.     elseif offset ==0x0304 then
  226.         command = "dme1time:" .. string.format("%04d", value) .. "\n"
  227.     elseif offset ==0x0306 then
  228.         command = "dme2:" .. string.format("%04d", value) .. "\n"
  229.     elseif offset ==0x030a then
  230.         command = "dme2time:" .. string.format("%04d", value) .. "\n"
  231.     end
  232.     ipc.log ("writes com =".. command)
  233.     com.write (dev, command)
  234.     ipc.sleep(serial_wait)
  235. end
  236.  
  237.  
  238. function call_radio_degrees (offset, value)
  239.     if offset ==0x0c4e then
  240.         command = "obs1:" .. string.format("%04d", value) .. "\n"
  241.     elseif offset ==0x0c5e then
  242.         command = "obs2:" .. string.format("%04d", value) .. "\n"
  243.     end
  244.     ipc.log ("writes com =".. command)
  245.     com.write (dev, command)
  246.     ipc.sleep(serial_wait)
  247. end
  248.  
  249.  
  250. function call_altitude (offset, value)
  251.     value = (value/65536)* foot/100
  252.     command = "altitude:" .. string.format("%04d", value) .. "\n"
  253.     ipc.log ("writes com =".. command)
  254.     com.write (dev, command)
  255.     ipc.sleep(serial_wait)
  256. end
  257.  
  258.  
  259. function call_heading (offset, value)
  260.     value = (value/65536)*360
  261.     command = "heading:" .. string.format("%04d", value) .. "\n"
  262.     ipc.log ("writes com =".. command)
  263.     com.write (dev, command)
  264.     ipc.sleep(serial_wait)
  265. end
  266.  
  267.  
  268. function call_vertical (offset, value)
  269.     ipc.log ("writes com = vertical:"..string.format("%04d", value/10).. "\n")
  270.     com.write (dev, "vertical:" .. string.format("%04d", value/10) .. "\n")
  271.     ipc.sleep(serial_wait)
  272. end
  273.  
  274.  
  275. function call_radio (offset, value)
  276.     if offset ==0x311e then
  277.         command = "nav1s:" .. string.format("%04x", value) .. "\n"
  278.     elseif offset ==0x034c then
  279.         command = "adf1:" .. string.format("%04x", value) .. "\n"
  280.     elseif offset ==0x02d4 then
  281.         command = "adf2:" .. string.format("%04x", value) .. "\n"
  282.     elseif offset ==0x034e then
  283.         command = "com1m:" .. string.format("%04x", value) .. "\n"
  284.     elseif offset ==0x0350 then
  285.         command = "nav1m:" .. string.format("%04x", value) .. "\n"
  286.     elseif offset ==0x0352 then
  287.         command = "nav2m:" .. string.format("%04x", value) .. "\n"
  288.     elseif offset ==0x0354 then
  289.         command = "trans:" .. string.format("%04x", value) .. "\n"
  290.     elseif offset ==0x3118 then
  291.         command = "com2m:" .. string.format("%04x", value) .. "\n"
  292.     elseif offset ==0x311a then
  293.         command = "com1s:" .. string.format("%04x", value) .. "\n"
  294.     elseif offset ==0x311c then
  295.         command = "com2s:" .. string.format("%04x", value) .. "\n"
  296.     elseif offset ==0x311e then
  297.         command = "nav1s:" .. string.format("%04x", value) .. "\n"
  298.     elseif offset ==0x3120 then
  299.         command = "nav2s:" .. string.format("%04x", value) .. "\n"
  300.     end
  301.     ipc.log ("writes com =".. command)
  302.     com.write (dev, command)
  303.     ipc.sleep(serial_wait)
  304. end
  305.  
  306.  
  307. event.com(dev, 10000, 8, "handlecom")
  308. com.write(dev,"resend:1\n")
  309.  
  310. ipc.sleep(10000)
  311.  
  312. event.offset(0x0BC8, "UW", "call_offset") -- parking brake
  313. event.offset(0x0d0c, "UW", "call_offset") -- lights
  314. event.offset(0x0Be8, "UD", "call_offset") -- gear
  315. event.offset(0x07BC, "UD", "call_offset") -- master ap
  316. event.offset(0x0810, "UD", "call_offset") -- ap at
  317.  
  318.  
  319. event.offset (0x02D4, "UW", "call_radio") -- ADF two main
  320. event.offset (0x0300, "UW", "call_radio_DME") -- VOR1 DME distance
  321. event.offset (0x0304, "UW", "call_radio_DME") -- VOR1 DME time
  322. event.offset (0x0306, "UW", "call_radio_DME") -- VOR2 DME distance
  323. event.offset (0x030a, "UW", "call_radio_DME") -- VOR2 DME time
  324. event.offset (0x034c, "UW", "call_radio") -- ADF one frequency main
  325. event.offset (0x034e, "UW", "call_radio") -- com one main
  326. event.offset (0x0350, "UW", "call_radio") -- nav one main
  327. event.offset (0x0352, "UW", "call_radio") -- nav two main
  328. event.offset (0x0354, "UW", "call_radio") -- transponder
  329. event.offset (0x0c4e, "UW", "call_radio_degrees") -- nav one obs
  330. event.offset (0x0c5e, "UW", "call_radio_degrees") -- nav two obs
  331. event.offset (0x3118, "UW", "call_radio") -- com two
  332. event.offset (0x311a, "UW", "call_radio") -- com one standby
  333. event.offset (0x311c, "UW", "call_radio") -- com two standby
  334. event.offset (0x311e, "UW", "call_radio") -- nav 1 standby
  335. event.offset (0x3120, "UW", "call_radio") -- nav two standby
  336. event.offset (0x07cc, "UW", "call_heading") -- autopilot heading
  337. event.offset (0x07d4, "UD", "call_altitude") -- autopilot altitude
  338. event.offset (0x07f2, "SW", "call_vertical") -- autopilot vertical speed
Advertisement
Add Comment
Please, Sign In to add comment