MrDionesalvi

Reciver - I/O

Jun 20th, 2021 (edited)
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- Reciver by CFG
  2.  
  3. local reciveSide = "back"
  4. local clockSide = "left"
  5. local listen = false
  6. local parityBit = 0
  7. local counter = 0
  8. local Byte = ""
  9.  
  10.  
  11. local bin2hex = {
  12.     ["0000"] = "0",
  13.     ["0001"] = "1",
  14.     ["0010"] = "2",
  15.     ["0011"] = "3",
  16.     ["0100"] = "4",
  17.     ["0101"] = "5",
  18.     ["0110"] = "6",
  19.     ["0111"] = "7",
  20.     ["1000"] = "8",
  21.     ["1001"] = "9",
  22.     ["1010"] = "A",
  23.     ["1011"] = "B",
  24.     ["1100"] = "C",
  25.     ["1101"] = "D",
  26.     ["1110"] = "E",
  27.     ["1111"] = "F"
  28. }
  29.  
  30. function Bin2Hex(s)
  31.  
  32.     local l = 0
  33.     local h = ""
  34.     local b = ""
  35.     local rem
  36.  
  37.     l = string.len(s)
  38.     rem = l % 4
  39.     l = l-1
  40.     h = ""
  41.  
  42.     -- need to prepend zeros to eliminate mod 4
  43.     if (rem > 0) then
  44.         s = string.rep("0", 4 - rem)..s
  45.     end
  46.  
  47.     for i = 1, l, 4 do
  48.         b = string.sub(s, i, i+3)
  49.         h = h..bin2hex[b]
  50.     end
  51.  
  52.     return h
  53.  
  54. end
  55.  
  56.  
  57. function fromhex(str)
  58.     return (str:gsub('..', function (cc)
  59.         return string.char(tonumber(cc, 16))
  60.     end))
  61. end
  62.  
  63. term.clear()
  64. term.setCursorPos(1,1)
  65. print("Calafrica Group -- Calaofeng FrSky XM+ 1CH SBUS 2021 Full Range\n\n")
  66. print("Waiting for start of text")
  67.  
  68. while true do
  69.     if rs.getInput(clockSide) and rs.getInput(reciveSide) then
  70.         listen = true
  71.         print("Listening:\n")
  72.         os.sleep(0.2)
  73.         while listen do
  74.             os.sleep(0.2)
  75.  
  76.             if rs.getInput(clockSide) and rs.getInput(reciveSide) ~= true then
  77.                 os.sleep(0.2)
  78.                 if rs.getInput(reciveSide) and parityBit % 2 == 0 then
  79.                     print("String validated")
  80.                 elseif not rs.getInput(reciveSide) and parityBit % 2 ~=  0 then
  81.                     print("String validated")
  82.                 else
  83.                     print("Stringa not validated")
  84.                 end
  85.                 os.sleep(0.2)
  86.                 listen = false
  87.             else
  88.                 if rs.getInput(reciveSide) then
  89.                     byte = byte .. "1"
  90.                     parityBit = parityBit + 1
  91.                 else
  92.                     byte = byte .. "0"
  93.                 end
  94.                 counter = counter + 1
  95.             end
  96.         end
  97.         hexText = Bin2Hex(byte)
  98.         print("Recived text: "..fromhex(hexText))
  99.  
  100.     end
  101.     parityBit = 0
  102.     counter = 0
  103.     byte = ""
  104.     os.sleep(0.1)
  105. end
Add Comment
Please, Sign In to add comment