Advertisement
RamiLego

ComputerCraft ProjectRed SevenSegment API

Jun 17th, 2015
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | None | 0 0
  1. --Minecraft ProjectRed Seven Segment Library For ComputerCraft By : RamiLego4Game
  2. --
  3. --  CellMap:
  4. --   1  
  5. --  2 3  
  6. --   4  
  7. --  5 6  
  8. --   7  8
  9. --
  10.  
  11. advanced = {}
  12.  
  13.  
  14. advanced.cellsMap = {}
  15. advanced.cellsMap[1] = {colors.lightGray,colors.green,colors.cyan,colors.red,colors.brown,colors.purple,colors.blue,colors.black} --Contains the bundled cable bindings of the left cell
  16. advanced.cellsMap[2] = {colors.white,colors.lime,colors.orange,colors.pink,colors.yellow,colors.magenta,colors.lightBlue,colors.gray} --Contains the bundled cable bindings of the right cell
  17.  
  18. --Contains then numbers & characters map
  19. advanced.numberMap = {}
  20. advanced.numberMap[0] = {true,true,true,false,true,true,true}
  21. advanced.numberMap[1] = {false,false,true,false,false,true,false}
  22. advanced.numberMap[2] = {true,false,true,true,true,false,true}
  23. advanced.numberMap[3] = {true,false,true,true,false,true,true}
  24. advanced.numberMap[4] = {false,true,true,true,false,true,false}
  25. advanced.numberMap[5] = {true,true,false,true,false,true,true}
  26. advanced.numberMap[6] = {true,true,false,true,true,true,true}
  27. advanced.numberMap[7] = {true,false,true,false,false,true,false}
  28. advanced.numberMap[8] = {true,true,true,true,true,true,true}
  29. advanced.numberMap[9] = {true,true,true,true,false,true,true}
  30. advanced.numberMap["-"] = {false,false,false,true,false,false,false}
  31. advanced.numberMap["a"] = {true,true,true,true,true,true,false}
  32. advanced.numberMap["b"] = {false,true,false,true,true,true,true}
  33. advanced.numberMap["c"] = {false,false,false,true,true,false,true}
  34. advanced.numberMap["d"] = {false,false,true,true,true,true,true}
  35. advanced.numberMap["e"] = {true,true,false,true,true,false,true}
  36. advanced.numberMap["f"] = {true,true,false,true,true,false,false}
  37.  
  38. --Internal function, Ignore.
  39. function advanced.getColorsTable(cell,num)
  40.     if not num then return {} end
  41.     local colorsT = {}
  42.     for k,v in pairs(advanced.numberMap[num]) do
  43.         if v then table.insert(colorsT,advanced.cellsMap[cell][k]) end
  44.     end
  45.     return colorsT
  46. end
  47.  
  48. --Internal function, Ignore.
  49. function advanced.combineColorsTable(t)
  50.     return colors.combine(unpack(t))
  51. end
  52.  
  53. --Internal function, Ignore. It sets the characters of the seven segment display, Warning: Doesn't return debug errors !
  54. function advanced.setCells(num1,num2,side,point)
  55.     local colorsT1 = advanced.getColorsTable(1,num1)
  56.     local colorsT2 = advanced.getColorsTable(2,num2)
  57.     local color1 = advanced.combineColorsTable(colorsT1)
  58.     local color2 = advanced.combineColorsTable(colorsT2)
  59.     local color = advanced.combineColorsTable({color1,color2})
  60.     if point then color = advanced.combineColorsTable({color,advanced.cellsMap[1][8]}) end
  61.     rs.setBundledOutput(side,color)
  62. end
  63.  
  64. --Internal function, Ignore. It sets the display number of the seven segment display, Warning: Doesn't return debug errors !
  65. function advanced.setNumber(num,side)
  66.     if num >= 99 or num <= -10 then advanced.setCells("-","-",side) return end
  67.     if num >= 0 then
  68.         local n1, n2, point = nil, nil, nil
  69.         if num >= 10 then
  70.             n1 = math.floor(num/10)
  71.             n2 = math.floor(num)-n1*10
  72.         else
  73.             if num-math.floor(num) > 0 then
  74.                 n1 = math.floor(num)
  75.                 n2 = math.floor((num-math.floor(num))*10)
  76.             else
  77.                 n1 = num
  78.                 n2 = 0
  79.             end
  80.             point = true
  81.         end
  82.         advanced.setCells(n1,n2,side,point)
  83.     else
  84.         advanced.setCells("-",math.floor(-num),side)
  85.     end
  86. end
  87.  
  88. --Use this function ! It returns debugging errors :D
  89. --It creates a new seven segment display object
  90. --Side -> The side that seven segment display is connected to.
  91. function newSevenSegment(side)
  92.     if type(side) ~= "string" then return error("Side can be only a string, Passed as a "..type(side)) end
  93.     local seg = {}
  94.    
  95.     seg.side = side
  96.    
  97.     --It sets the display number of the seven segments display.
  98.     function seg.setNumber(num)
  99.         if type(num) ~= "number" then return error("Number should be only a number, Passed as a "..type(num)) end
  100.         advanced.setNumber(num,seg.side)
  101.     end
  102.    
  103.     --Advanced !: It sets the cells characters of the seven segments.
  104.     --Point -> to turn on the middle point of the display.
  105.     function seg.setCells(character1,character2,point)
  106.         if type(charater1) ~= "number" and type(charater1) ~= "string" then return error("Character #1 can be only a number or a string, Passed as a "..type(character1)) end
  107.         if type(charater2) ~= "number" and type(charater2) ~= "string" then return error("Character #2 can be only a number or a string, Passed as a "..type(character2)) end
  108.         advanced.setCells(character1,character2,seg.side,point)
  109.     end
  110.    
  111.     --Clears the seven segments display.
  112.     function seg.clearCells()
  113.         advanced.setCells(nil,nil,seg.side)
  114.     end
  115.    
  116.     --Plays a demo counting display, It's 112 seconds long !
  117.     function seg.demo()
  118.         local clockTime = -10
  119.         while clockTime < 101 do
  120.             advanced.setNumber(clockTime,seg.side)
  121.             os.sleep(0.1)
  122.             clockTime = clockTime + 0.1
  123.         end
  124.         advanced.setCells("-","-",seg.side)
  125.         os.sleep(1)
  126.         advanced.setCells(nil,nil,seg.side)
  127.     end
  128.    
  129.     return seg
  130. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement