Advertisement
Guest User

braille.lua

a guest
Feb 2nd, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.59 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local fs = require("filesystem")
  4. local gpu = component.gpu
  5. local shell = require("shell")
  6. local unicode = require("unicode")
  7.  
  8. local w, h = 30, 15
  9. local iw, ih = w*2, h*4
  10.  
  11. local ow, oh = gpu.getResolution()
  12. local brailleindex = {{0x1,0x8},{0x2,0x10},{0x4,0x20},{0x40,0x80}}
  13. local idfk = 0
  14. local btn = {[false] = 0, [true] = 1}
  15. local frames = {}
  16. local framepath = shell.resolve("./frames")
  17.  
  18. gpu.setResolution(w, h)
  19.  
  20. local frame = 1
  21. for file in fs.list(framepath) do
  22.   frames[frame] = file
  23.   frame = frame + 1
  24. end
  25. table.sort(frames)
  26. for frame,file in ipairs(frames) do
  27.   local framefile = fs.open(framepath.."/"..file)
  28.   frames[frame] = framefile:read(math.huge)
  29.   framefile:close()
  30.   --print(frame,file)
  31. end
  32. print(frames[1])
  33. local framecount = frame
  34.  
  35. function generatePattern()
  36.   local outputstr = ""
  37.   for y=0, h-1 do
  38.     for x=0, w-1 do
  39.       local c = 0
  40.       for sy=0, 3 do
  41.         for sx=0, 1 do
  42.           local i = brailleindex[sy+1][sx+1]
  43.           local tx, ty = x * 2 + sx, y * 4 + sy
  44.           local pixelindex = math.floor(((tx % iw) + (ty * ih)) / 8) + 1
  45.           local p = (string.byte(frames[idfk+1],pixelindex)>>((tx-(ty%2*4)-1)%8))%2
  46.           --print(pixelindex,p)
  47.           c = c + p * i
  48.         end
  49.       end
  50.       outputstr = outputstr .. unicode.char(0x2800+c)
  51.     end
  52.   end
  53.   return outputstr
  54. end
  55.  
  56. io.write("\n")
  57. while true do
  58.   io.write(generatePattern().."\n")
  59.   idfk = idfk + 1
  60.   idfk = idfk % (framecount-1)
  61.   if event.pull(0.05, "interrupted") == "interrupted" then
  62.     gpu.setResolution(ow, oh)
  63.     return
  64.   end
  65.   --os.sleep(1/15)
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement