zurbo

cc_serialRednet_API_WIP

Dec 20th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dataIsSended= false
  2.  
  3. function binToDec(bin) -- this function is used to convert a binary number into a decimal one
  4.   for i=1,#bin do
  5.     dec=(string.sub(bin,i,i)*2^i)+dec
  6.   end
  7.   return dec
  8. end
  9.  
  10.  
  11. function hexToDec(hex) -- this function is used to convert an hexadecimal number into a decimal one
  12.   for i=1,#hex do
  13.     if string.byte(hex,i)>64 then
  14.       dec=((string.byte(string.sub(hex,i,i),i)-55)*16^i)+dec
  15.     else
  16.       dec=(string.sub(hex,i,i)*16^i)+dec
  17.     end
  18.   end
  19.   return dec
  20. end  
  21.  
  22. function decTobin(dec) -- this function is used to convert a decimal number into a binary one
  23.   local tab={}
  24.   while dec>0 do
  25.     rest=dec%2
  26.     tab[#tab+1]=rest
  27.     dec=(dec-rest)/2
  28.   end
  29.   return table.concat(tab)
  30. end
  31.  
  32. function decToHex(dec) -- this function is used to convert a decimal number into an hexadecimal one
  33.   local tab={}
  34.   while dec>0 do
  35.     rest=dec%16
  36.     tab[#tab+1]=rest
  37.     dec=(dec-rest)/16
  38.   end
  39.   for i=1,#tab do
  40.     if tab[i]>9 then
  41.     tab[i]=string.char(tab[i]+55)
  42.     end
  43.   end
  44.   return table.concat(tab)
  45. end
  46.  
  47. function sendPMR(data, tick, sideClk, sideDat) -- this function is used to send a serial signal to a programable rednet controler from MFR
  48.   for i=1,#data do
  49.     bit=string.sub(data,i,i)
  50.     redstone.setOutput(sideClk,true)
  51.     os.sleep(tick*0.05)
  52.     redstone.setOutput(sideClk,false)
  53.     if bit=="1" then
  54.       redstone.setOutput(sideDat,true)
  55.       os.sleep(tick*0.05)
  56.     else
  57.       redstone.setOutput(sideDat,false)
  58.       os.sleep(tick*0.05)
  59.     end
  60.   end
  61.   dataIsSended= true
  62. end
Advertisement
Add Comment
Please, Sign In to add comment