Advertisement
MrDionesalvi

Sender - I/O

Jun 20th, 2021 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. -- Sender by CFG
  2.  
  3. local sendSide = "back"
  4. local clockSide = "right"
  5. local parityBit = 0
  6. local counter = 0
  7.  
  8. local filename = "text"
  9.  
  10. local maxw, maxh = term.getSize()
  11.  
  12. local function splitByChunk(text, chunkSize)
  13.     local s = {}
  14.     for i=1, #text, chunkSize do
  15.         s[#s+1] = text:sub(i,i+chunkSize - 1)
  16.     end
  17.     return s
  18. end
  19.  
  20. function string.tohex(str)
  21.     return (str:gsub('.', function (c)
  22.         return string.format('%02X', string.byte(c))
  23.     end))
  24. end
  25.  
  26. function hex2bin(str)
  27.     local map = {
  28.         ["0"]="0000",
  29.         ["1"]="0001",
  30.         ["2"]="0010",
  31.         ["3"]="0011",
  32.         ["4"]="0100",
  33.         ["5"]="0101",
  34.         ["6"]="0110",
  35.         ["7"]="0111",
  36.         ["8"]="1000",
  37.         ["9"]="1001",
  38.         ["a"]="1010",
  39.         ["b"]="1011",
  40.         ["c"]="1100",
  41.         ["d"]="1101",
  42.         ["e"]="1110",
  43.         ["f"]="1111",
  44.         ["A"]="1010",
  45.         ["B"]="1011",
  46.         ["C"]="1100",
  47.         ["D"]="1101",
  48.         ["E"]="1110",
  49.         ["F"]="1111",
  50.     }
  51.     return str:gsub('[0-9A-F]', map)
  52. end
  53.  
  54. if fs.exists(filename) == true then
  55.         h = fs.open(filename, "r")
  56.         text = h.readAll()
  57.         h.close()
  58.         strHex = string.tohex(text)
  59.         strBin = hex2bin(strHex)
  60.  
  61.     else
  62.         print("file does not exist, check path..")
  63.     end
  64.  
  65.  
  66. local function error(errore)
  67.     term.clear()
  68.     term.setCursorPos(1, 1)
  69.     term.write("Error!!!!!")
  70.     term.setCursorPos((maxw - #errore) / 2, 6)
  71.     term.write(errore)
  72.     os.sleep(5)
  73.     os.reboot()
  74. end
  75.  
  76. local function toBinary(num)
  77.     return hexToBin_lut[num]
  78. end
  79.  
  80. local function sendStartSequence()
  81.     -- Start of TEXT: 10
  82.     rs.setOutput(clockSide, true)
  83.     rs.setOutput(sendSide, true)
  84.     os.sleep(0.2)
  85.     rs.setOutput(clockSide, false)
  86.     rs.setOutput(sendSide, false)
  87.  
  88. end
  89.  
  90. local function sendStopSequence()
  91.     -- End of TEXT: 11
  92.     rs.setOutput(sendSide, false)
  93.  
  94.     rs.setOutput(clockSide, true)
  95.     os.sleep(0.2)
  96.     rs.setOutput(clockSide, false)
  97.     if parityBit % 2 == 0 then
  98.         rs.setOutput(sendSide, true)
  99.         os.sleep(0.2)
  100.         rs.setOutput(sendSide, false)
  101.     end
  102.    
  103. end
  104.  
  105. term.clear()
  106. term.setCursorPos(1,1)
  107. print("Calafrica Group -- Calaofeng UV5R+Plus\n\n")
  108. print("Bin to send..".. strBin)
  109.  
  110.  
  111. while true do
  112.     os.sleep(0.1)
  113.     sendStartSequence()
  114.     os.sleep(0.2)
  115.  
  116.     -- Qui in poi legge il file
  117.     for i = 1, #strBin do
  118.         local c = strBin:sub(i,i)
  119.         if c == "1" then
  120.             rs.setOutput(sendSide, true)
  121.             parityBit = parityBit + 1
  122.         else
  123.             rs.setOutput(sendSide, false)
  124.         end
  125.         os.sleep(0.2)
  126.     end
  127.     -- Finisce il file
  128.    
  129.     sendStopSequence()
  130.     parityBit = 0
  131.     os.sleep(1)
  132.  
  133.  
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement