Advertisement
hbar

lumiere

Mar 15th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 KB | None | 0 0
  1. --[[
  2. Lumiere v 0.9.0
  3. Matti Vapa, 2014
  4. https://github.com/mattijv/fresco-and-lumiere
  5. http://pastebin.com/DbZkz5KT
  6.  
  7. This program was made to control a screen of Glowstone Illuminators. Required mods are
  8.  
  9.     * ComputerCraft (duh),
  10.     * Thermal Expansion,
  11.     * OpenPeripheral.
  12.  
  13. In itself this program contains only rudimentary functions for demo purposes. Lumiere is
  14. ment to be used as an API, like so
  15.  
  16.         os.load("lumiere")
  17.  
  18.         screen = lumiere.initScreen(16,16,"back")
  19.  
  20.         screen:setColor("0xFF0000")
  21.  
  22. More detailed tutorial on the capabilities can be found here: http://pastebin.com/B6juP6wJ
  23.  
  24. To run this file as a standalone program, use
  25.  
  26.         lumiere run
  27.  
  28. Changelog:
  29.  
  30.     0.9.0:
  31.                 * First release.
  32.  
  33. ]]--
  34. if not http then
  35.   print("HTTP must be enabled.")
  36.   return
  37. end
  38.  
  39. local VERSION = "0.9.0"
  40.  
  41. local apiURL = "http://lakka.kapsi.fi:62096"
  42.  
  43. --not currently used
  44. --local playerURL = "http://s3.amazonaws.com/MinecraftSkins/"
  45.  
  46.  
  47. -- PRIVATE FUNCTIONS
  48. -------------------------------------------------------------
  49. local function index(x,y,width,height)
  50.   if x%2 == 1 then
  51.     return height*(x-1)+(height-y)+1
  52.   else
  53.     return height*(x-1)+y
  54.   end
  55. end
  56.  
  57. -- Returns HEX representation of num
  58. -- posted in snipplr.com by ukpyr
  59. -- stolen shamelessly, and tweaked a litte
  60. local function num2hex(num)
  61.     local hexstr = '0123456789abcdef'
  62.     local s = ''
  63.     while num > 0 do
  64.         local mod = math.fmod(num, 16)
  65.         s = string.sub(hexstr, mod+1, mod+1) .. s
  66.         num = math.floor(num / 16)
  67.     end
  68.     if s == '' then
  69.       s = '00'
  70.     elseif #s < 2 then
  71.       s = '0'..s
  72.     end
  73.     return s
  74. end
  75. -------------------------------------------------------------
  76.  
  77. -- PUBLIC FUNCTIONS AND CLASSES
  78. -------------------------------------------------------------
  79. Screen = {}
  80.  
  81. Screen.setColor = function(self,color)
  82.   for r = 1, self.height do
  83.     for c = 1, self.width do
  84.       self.illuminators[index(c,r,self.width,self.height)].setColor(tonumber(color,16))
  85.     end
  86.   end
  87. end
  88.  
  89. Screen.showImage = function(self,image,x,y)
  90.   local x0 = x or 1
  91.   local y0 = y or 1
  92.   for r = math.max(y0,1), math.min(self.height, y0+#image-1) do
  93.     for c = math.max(x0,1), math.min(self.width, x0+(#image[1]/8)-1) do
  94.       self.illuminators[index(c,r,self.width,self.height)].setColor(tonumber(image[r-y0+1]:sub(1+8*(c-x0),8+8*(c-x0)),16))
  95.     end
  96.   end
  97. end
  98.  
  99. Screen.showAnimation = function(self,frames,x,y,speed)
  100.   for i = 1,#frames do
  101.     self:showImage(frames[i],x,y)
  102.     sleep(1/speed)
  103.   end
  104. end
  105.  
  106. initScreen = function(width,height,side)
  107.     local screen = {}
  108.     setmetatable(screen,{__index=Screen})
  109.     screen.width = width
  110.     screen.height = height
  111.     local modem = peripheral.wrap(side)
  112.     local periplist = modem.getNamesRemote()
  113.  
  114.     local min_id = 1000000000000
  115.  
  116.     for _,name in pairs(periplist) do
  117.       if name:find("cofh_thermalexpansion_lamp") then
  118.         local id = tonumber(name:sub(28,#name))
  119.         if id < min_id then
  120.           min_id = id
  121.         end
  122.       end
  123.     end
  124.  
  125.     local illuminators = {}
  126.     for i = min_id,min_id+height*width-1 do
  127.       local l = peripheral.wrap("cofh_thermalexpansion_lamp_"..tostring(i))
  128.       table.insert(illuminators,l)
  129.     end
  130.     screen.illuminators = illuminators
  131.     return screen
  132. end
  133.  
  134. getImage = function(url,size)
  135.     local vars = "url="..url
  136.     local vars = vars.."&maxsize="..tostring(size)
  137.     local resp = http.post(apiURL,vars)
  138.  
  139.     if not resp then
  140.         print("No response from image server.")
  141.         return nil
  142.     elseif resp.getResponseCode() ~= 200 then
  143.         print("Error while connecting to server!")
  144.         print("Server response")
  145.         print("Code: "..resp.getResponseCode())
  146.         print("Message:")
  147.         print(resp.readAll())
  148.         return nil
  149.     end
  150.  
  151.     local oldSize = resp.readLine()
  152.     local newSize = resp.readLine()
  153.  
  154.     if oldSize ~= newSize then
  155.         print("Original image size was (w,h): "..oldSize)
  156.         print("New size is (w,h): "..newSize)
  157.     else
  158.         print("Image size is (w,h): "..newSize)
  159.     end
  160.  
  161.     local sizes = {}
  162.     for word in newSize:gmatch("%w+") do table.insert(sizes, word) end
  163.  
  164.     local picWidth = tonumber(sizes[1])
  165.     local picHeight = tonumber(sizes[2])
  166.  
  167.     local img = {}
  168.  
  169.     local line = resp.readLine()
  170.     while line ~= nil do
  171.         table.insert(img,line)
  172.         line = resp.readLine()
  173.     end
  174.     resp.close()
  175.  
  176.     return img
  177. end
  178.  
  179. getAnimation = function(url,size)
  180.     vars = "url="..url
  181.     vars = vars.."&maxsize="..tostring(size)
  182.     vars = vars.."&animate=true"
  183.     local resp = http.post(apiURL,vars)
  184.  
  185.     if not resp then
  186.         print("No response from image server.")
  187.         return nil
  188.     elseif resp.getResponseCode() ~= 200 then
  189.         print("Error while connecting to server!")
  190.         print("Server response")
  191.         print("Code: "..resp.getResponseCode())
  192.         print("Message:")
  193.         print(resp.readAll())
  194.         return nil
  195.     end
  196.  
  197.     local oldSize = resp.readLine()
  198.     local newSize = resp.readLine()
  199.  
  200.     if oldSize ~= newSize then
  201.         print("Original image size was (w,h): "..oldSize)
  202.         print("New size is (w,h): "..newSize)
  203.     else
  204.         print("Image size is (w,h): "..newSize)
  205.     end
  206.  
  207.     local sizes = {}
  208.     for word in newSize:gmatch("%w+") do table.insert(sizes, word) end
  209.  
  210.     local picWidth = tonumber(sizes[1])
  211.     local picHeight = tonumber(sizes[2])
  212.  
  213.     local img = {}
  214.  
  215.     local line = resp.readLine()
  216.     while line ~= nil do
  217.         table.insert(img,line)
  218.         line = resp.readLine()
  219.     end
  220.     resp.close()
  221.  
  222.     frames = {}
  223.     frame = {}
  224.     for _,line in pairs(img) do
  225.         if line == "~" then
  226.             table.insert(frames,frame)
  227.             frame = {}
  228.         else
  229.             table.insert(frame,line)
  230.         end
  231.     end
  232.  
  233.     return frames
  234. end
  235.  
  236. getColor = function(width,height,color)
  237.     local img = {}
  238.     for r = 1,height do
  239.         local line = string.rep(color,width)
  240.         table.insert(img,line)
  241.     end
  242.     return img
  243. end
  244.  
  245. fade = function(screen,start,final,steps,speed)
  246.  
  247.   for i = 1,steps do
  248.     local current = {}
  249.     for r = 1,#start do
  250.       line = ""
  251.       for c = 1,(#start[1]/8) do
  252.         local s_px = start[r]:sub(1+8*(c-1),8+8*(c-1))
  253.         local e_px = final[r]:sub(1+8*(c-1),8+8*(c-1))
  254.         local s_r, s_g, s_b = tonumber(s_px:sub(3,4),16), tonumber(s_px:sub(5,6),16), tonumber(s_px:sub(7,8),16)
  255.         local e_r, e_g, e_b = tonumber(e_px:sub(3,4),16), tonumber(e_px:sub(5,6),16), tonumber(e_px:sub(7,8),16)
  256.         local d_r = ((e_r-s_r)/(steps-1))*(i-1)
  257.         local d_g = ((e_g-s_g)/(steps-1))*(i-1)
  258.         local d_b = ((e_b-s_b)/(steps-1))*(i-1)
  259.         local c_r, c_g, c_b = s_r + d_r, s_g + d_g, s_b + d_b
  260.         line = line.."0x"..num2hex(c_r)..num2hex(c_g)..num2hex(c_b)
  261.       end
  262.       table.insert(current,line)
  263.     end
  264.     screen:showImage(current,1,1)
  265.     sleep(1/speed)
  266.   end
  267. end
  268.  
  269. -------------------------------------------------------------
  270.  
  271. args = {...}
  272.  
  273. if #args > 0 then
  274.  
  275.     print("Lumiere v "..VERSION)
  276.     print("Please input the screen size.")
  277.     local width
  278.     local height
  279.     while true do
  280.         term.write("Screen width: ")
  281.         width = tonumber(io.read())
  282.         if not width or width < 1 then
  283.             print("Bad input!")
  284.         else
  285.             break
  286.         end
  287.     end
  288.     while true do
  289.         term.write("Screen height: ")
  290.         height = tonumber(io.read())
  291.         if not height or height < 1 then
  292.             print("Bad input!")
  293.         else
  294.             break
  295.         end
  296.     end
  297.    
  298.     print("Initializing screen...")
  299.     local screen = initScreen(width, height)
  300.     screen:setColor("0xFFFFFF")
  301.     print("Testing...")
  302.     print("* white")
  303.     screen:setColor("0xFFFFFF")
  304.     sleep(1)
  305.     print("* red")
  306.     screen:setColor("0xFF0000")
  307.     sleep(1)
  308.     print("* green")
  309.     screen:setColor("0x00FF00")
  310.     sleep(1)
  311.     print("* blue")
  312.     screen:setColor("0x0000FF")
  313.     sleep(1)
  314.     print("* black")
  315.     screen:setColor("0x000000")
  316.     sleep(1)
  317.     print("Good to go!")
  318.     sleep(1)
  319.     while true do
  320.         print("Give URL to show!")
  321.         term.write("URL: ")
  322.         local url = io.read()
  323.         local img = getImage(url,math.max(screen.width,screen.height))
  324.         if img then
  325.             screen:showImage(img,1,1)
  326.             sleep(2)
  327.         else
  328.             print("Try again.")
  329.         end
  330.     end
  331. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement