This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jakejakey's profile picture

Binary Encoder

Started by jakejakey, 21 November 2016 - 12:44 AM
jakejakey #1
Posted 21 November 2016 - 01:44 AM
Hello! I have started work on a program that transmits files over a redstone signal, but about half way through its development, I ran into an issue that I cannot figure out.

Code
Spoiler

args = {...}
if #args < 3 then
  printError("Usage: file <send|receive> <file> <side>")
  return
end
function limit(str)
  return string.sub(str,1,51)
end
function center(str,line)
  local maxX,maxY = term.getSize()
  maxX = maxX / 2
  maxX = maxX - (#str/2)
  term.setCursorPos(maxX,line)
  print(str)
end
function toBinary(decimal)
  decimal = decimal - 1
  local binary = {}
  while decimal > 0 do
    local rest = math.fmod(decimal,2)
    binary[#binary+1] = rest
    decimal = (decimal-rest)/2
  end
  local str = ""
  for i=1,#binary do
    if binary[i] == 1 then
	  str = str.."1"
    else
	  str = str.."0"
    end
  end
  while #str ~= 8 do
    str = "-"..str
  end
  for i=1,#str do
    if string.sub(str,i,i) == "1" then
	  binary[i] = 1
    elseif string.sub(str,i,i) == "-" then
	  binary[i] = false
    end
	  binary[i] = 0
    end
  end
  return binary
end
function fromBinary(bin)
  local binary = ""
  for i=1,#bin do
    binary = binary..string.sub(bin,#bin+1-i,#bin+1-i)
  end
  local sum = 0
  for i=1,#binary do
    if string.sub(binary,i,i) == "1" then
	  sum = sum + math.pow(2,i-1)
    end
  end
  sum = sum + 1
  return sum
end
if args[1] == "send" then
  local file = {}
  f = fs.open(args[2],"r")
  while true do
    local line = f.readLine()
    if not line then
	  break
    end
    file[#file+1] = line
  end
  f.close()
  rs.setOutput(args[3],true)
  sleep(0.1)
  rs.setOutput(args[3],false)
  sleep(0.9)
  for ln=1,#file do
    for col=1,#file[ln] do
	  local char = string.sub(file[ln],col,col)
	  local bits = toBinary(string.byte(char))
	  for i=1,8 do
	    if bits[i] == 1 then
		  rs.setOutput(args[3],true)
	    elseif bits[i] == false then
		  rs.setAnalogOutput(args[3],14)
	    else
		  rs.setOutput(args[3],false)
	    end
	    sleep(0.1)
	  end
	  term.setCursorPos(2,19)
	  term.setTextColor(colors.lightGray)
	  write(tostring(string.byte(char)))
	  --term.clear()
	  term.setCursorPos(1,1)
	  for k=1,#file do
	    local line = file[k]
	    for z=1,#line do
		  if k == ln then
		    for j=1,1 do
			  if col > z then
			    term.setTextColor(colors.gray)
			  elseif col < z then
			    term.setTextColor(colors.lightGray)
			  elseif col == z then
			    term.setTextColor(colors.white)
			  end
			  write(string.sub(line,z,z))
		    end
		  end
	    end
	    if ln > k then
		  term.setTextColor(colors.gray)
		  write(limit(line))
	    end
	    print()
	  end
    end
    local bits = toBinary(256)
    for i=1,#bits do
	  if bits[i] == 1 then
	    rs.setOutput(args[3],true)
	  else
	    rs.setOutput(args[3],false)
	  end
	  sleep(0.1)
    end
  end
  term.clear()
  local bits = toBinary(256)
  for i=1,#bits do
    if bits[i] == 1 then
	  rs.setOutput(args[3],true)
    else
	  rs.setOutput(args[3],false)
    end
    sleep(0.1)
  end
  rs.setOutput(args[3],false)
elseif args[1] == "receive" then
  local file = {""}
  local line = 1
  os.pullEvent("redstone")
  local signal = rs.getAnalogInput(args[3])
  sleep(1)
  while true do
    local bits = {}
    for i=1,8 do
	  if rs.getAnalogInput(args[3]) == (signal-1) then
	    bits[i] = nil
	  elseif rs.getInput(args[3]) then
	    bits[i] = 1
	  else
	    bits[i] = 0
	  end
	  sleep(0.1)
    end
    local newBits = {}
    for i,v in pairs(bits) do
	  if v then
	    newBits[#newBits+1] = v
	  end
    end
    local str = ""
    for i=1,#newBits do
	  if newBits[i] == 1 then
	    str = str.."1"
	  else
	    str = str.."0"
	  end
    end
    local num = fromBinary(str)
    term.setCursorPos(2,19)
    term.setTextColor(colors.lightGray)
    write(tostring(num).." : "..str)
    --term.clear()
    term.setCursorPos(1,1)
    term.setTextColor(colors.lightGray)
    for ln=1,#file do
	  print(file[ln])
    end
    if num == 256 then
	  line = line + 1
	  if newLn then
	    break
	  end
	  newLn = true
	  file[line] = ""
    else
	  newLn = false
	  file[line] = file[line]..string.char(num)
    end
  end
end

The error is that on line 45 there is a missing end, as indicated by the "<eof> expected" But there is an end properly placed there, so I'm not sure why Lua is acting up on me. Thanks!
Dog #2
Posted 21 November 2016 - 02:19 AM
It looks to me that right around line 42 you have an extra end that doesn't need to be there - in fact it looks like you meant to put an else on line 40 instead of an end.
jakejakey #3
Posted 21 November 2016 - 02:21 AM
It looks to me that right around line 42 you have an extra end that doesn't need to be there - in fact it looks like you meant to put an else on line 40 instead of an end.
That was the issue, Thanks for the help!