Advertisement
C0BRA

EI Radio Test

Dec 30th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. local radio = self.CreateLink("radio")
  2. local keyboard = self.CreateLink("keyboard")
  3. local console = self.CreateLink("console")
  4.  
  5. local y = 0
  6. local x = 0
  7.  
  8. local Caps = false
  9. local NumberCaps = '!"£$%^&*()'
  10. local SpecialKeys = {
  11.     [61] = "\\",
  12.     [62] = "-",
  13.     [63] = "=",
  14.     [65] = " ",
  15.     [58] = ",",
  16.     [59] = ".",
  17.     [60] = "/"
  18. }
  19. local SpecialKeys_Caps = {
  20.     [61] = "|",
  21.     [62] = "_",
  22.     [63] = "+",
  23.     [58] = "<",
  24.     [59] = ">",
  25.     [60] = "?"
  26. }
  27.  
  28. function GetCharFromKey(key)
  29.     if key >= 1 and key <= 10 then
  30.         if Caps then return NumberCaps[key - 1] end
  31.         return string.char(key + (48 - 1))
  32.     end
  33.    
  34.     if key >= 37 and key <= 46 then
  35.         return string.char(key + (48 - 37))
  36.     end
  37.    
  38.     if key >= 11 and key <= 36 then
  39.         if Caps then
  40.             return string.upper(string.char(key + (97 - 11)))
  41.         end
  42.         return string.char(key + (97 - 11))
  43.     end
  44.    
  45.     if Caps then
  46.         return SpecialKeys_Caps[key] or SpecialKeys[key]
  47.     end
  48.     return SpecialKeys[key]
  49. end
  50.  
  51. local first = true
  52. local current = ""
  53. local towrite = {}
  54.  
  55. function ReadKeyboard()
  56.     while true do
  57.         local event = keyboard.NextEvent()
  58.         if not event then break end
  59.        
  60.         if event.Key == 79 then -- Shift
  61.             Caps = event.Down
  62.             continue
  63.         elseif event.Key == 51 or event.Key == 64 then -- ENTER
  64.            
  65.             if string.len(current) == 0 then continue end
  66.            
  67.             for i = 1, string.len(current) do
  68.                 table.insert(towrite, string.byte(current[i]))
  69.             end
  70.            
  71.             table.insert(towrite, 0)
  72.            
  73.             y = y + 1
  74.             if y == 60 then y = 0 console.Clear(Color(0, 0, 0, 255)) end
  75.            
  76.             console.Draw(":" .. current, 0, y)
  77.            
  78.             console.Draw(string.rep(" ", 61), 0, 61, Color(0, 0, 0), Color(0, 0, 0, 255)) -- clear the last line
  79.             current = ""
  80.             continue
  81.         end
  82.        
  83.         if not event.Down then continue end
  84.        
  85.         local char = GetCharFromKey(event.Key)
  86.         if not char then continue end
  87.        
  88.         current = current .. char
  89.         console.Draw(char, string.len(current), 61)
  90.     end
  91. end
  92.  
  93. function Think()   
  94.     if not radio.Connected then return 1 end
  95.     if not keyboard.Connected then return 1 end
  96.     if not console.Connected then return 1 end
  97.    
  98.     if first then
  99.         first = false
  100.         console.Clear(Color(0, 0, 0, 255))
  101.     end
  102.    
  103.     if keyboard.HasTyper() then
  104.         ReadKeyboard()
  105.     end
  106.    
  107.     if radio.HasData() then
  108.         local str = ""
  109.         while radio.HasData() do
  110.             local b, amp = radio.ReadByte()
  111.            
  112.             if b == 0 then
  113.                 y = y + 1
  114.                 if y == 60 then y = 0 console.Clear(Color(0, 0, 0, 255)) end
  115.                
  116.                 console.Draw(str, x, y)
  117.                
  118.                 x = 0
  119.                
  120.                 str = ""
  121.             else
  122.                 str = str .. string.char(b)
  123.             end
  124.         end
  125.        
  126.         if str != "" then
  127.             console.Draw(str, x, y + 1)
  128.            
  129.             x = x + string.len(str)
  130.         end
  131.     else
  132.         if (#towrite) >= 1 then
  133.             local b = towrite[1]
  134.             table.remove(towrite, 1)
  135.             radio.WriteByte(b)
  136.         end
  137.     end
  138.    
  139.     return 0
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement