Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- radio = self.CreateLink("radio")
- keyboard = self.CreateLink("keyboard")
- console = self.CreateLink("console")
- local y = 0
- local x = 0
- local Caps = false
- local NumberCaps = '!"£$%^&*()'
- local SpecialKeys = {
- [61] = "\\",
- [62] = "-",
- [63] = "=",
- [65] = " ",
- [58] = ",",
- [59] = ".",
- [60] = "/"
- }
- local SpecialKeys_Caps = {
- [61] = "|",
- [62] = "_",
- [63] = "+",
- [58] = "<",
- [59] = ">",
- [60] = "?"
- }
- function GetCharFromKey(key)
- if key >= 1 and key <= 10 then
- if Caps then return NumberCaps[key - 1] end
- return string.char(key + (48 - 1))
- end
- if key >= 37 and key <= 46 then
- return string.char(key + (48 - 37))
- end
- if key >= 11 and key <= 36 then
- if Caps then
- return string.upper(string.char(key + (97 - 11)))
- end
- return string.char(key + (97 - 11))
- end
- if Caps then
- return SpecialKeys_Caps[key] or SpecialKeys[key]
- end
- return SpecialKeys[key]
- end
- local first = true
- local current = ""
- function ReadKeyboard()
- while true do
- local event = keyboard.NextEvent()
- if not event then break end
- if event.Key == 79 then -- Shift
- Caps = event.Down
- continue
- elseif event.Key == 51 or event.Key == 64 then -- ENTER
- if radio.HasData() then return end
- if not event.Down then continue end
- SendMessage(current)
- console.Draw(string.rep(" ", 61), 0, 61, nil, Color(0,0,0,255))
- current = ""
- continue
- elseif event.Key == 66 then -- BKSP
- if not event.Down then continue end
- current = string.sub(current, 1, string.len(current) - 1)
- console.Draw(string.rep(" ", 61), 0, 61, nil, Color(0,0,0,255))
- console.Draw(current, 0, 61)
- continue
- end
- if not event.Down then continue end
- local char = GetCharFromKey(event.Key)
- if not char then continue end
- current = current .. char
- console.Draw(char, string.len(current) - 1, 61)
- end
- end
- local CurrentStatus = ""
- function SetStatus(status)
- if CurrentStatus == status then return end
- CurrentStatus = status
- local len = string.len(status)
- local spacesl = (63 - math.ceil(len)) / 2
- local spacesr = spacesl
- if len + spacesr + spacesl > 61 then
- spacesr = spacesr - 1
- end
- local str = string.rep(" ", spacesl) .. status .. string.rep(" ", spacesr)
- console.Draw(str, 0, 0, Color(0,0,0,255), Color(255,255,255,255))
- end
- function SendMessage(str)
- if string.StartWith(str, "!") then
- if string.StartWith(str, "!freq ") then
- local freq = tonumber(string.sub(str, string.len("!freq "))) or 200
- radio.SetFrequency(freq)
- AddChatLine("set frequency to " .. tostring(freq) .. "MHz", Color(255, 255, 0))
- return
- elseif string.StartWith(str, "!pwr ") then
- local pwr = tonumber(string.sub(str, string.len("!pwr "))) or 5
- radio.SetOutputPower(pwr)
- AddChatLine("set output power to " .. tostring(pwr) .. "W", Color(255, 255, 0))
- return
- elseif string.StartWith(str, "!scan") then
- local pwr = tonumber(string.sub(str, string.len("!pwr "))) or 5
- radio.SetOutputPower(pwr)
- AddChatLine("scannning..." .. tostring(pwr) .. "W", Color(255, 255, 0))
- return
- end
- AddChatLine("error: unknown command!", Color(255, 255, 0))
- return
- end
- for i = 1, string.len(str) do
- radio.WriteByte(string.byte(str[i]))
- end
- radio.WriteByte(0)
- AddChatLine(str, Color(0, 255, 0))
- end
- function AddChatLine(str, col)
- y = (y + 1) % 58
- local blanky = (y + 1) % 58
- console.Draw(string.rep(" ", 61), 0, blanky + 1, nil, Color(0,0,0,255))
- console.Draw(str, 0, y + 1, col)
- end
- local Recieved = ""
- local lowest_amp = 10000
- function Think()
- if not radio.Connected then return 1 end
- if not keyboard.Connected then return 1 end
- if not console.Connected then return 1 end
- if first then
- first = false
- radio.SetSquelch(1)
- radio.SetOutputPower(5)
- console.Clear(Color(0, 0, 0, 255))
- SetStatus("waiting")
- end
- if keyboard.HasTyper() then
- ReadKeyboard()
- if radio.SendQueueSize() != 0 then
- SetStatus("sending")
- elseif CurrentStatus == "sending" then
- SetStatus("waiting")
- end
- end
- if radio.HasData() then
- SetStatus("recieving data")
- while radio.HasData() do
- local b, amp = radio.ReadByte()
- if amp < lowest_amp then
- lowest_amp = amp
- end
- if b == 0 then
- SetStatus("waiting")
- local col = nil
- if lowest_amp <= 2.5 then
- col = Color(255, 127, 0)
- end
- AddChatLine(Recieved, col)
- Recieved = ""
- lowest_amp = 10000
- else
- Recieved = Recieved .. string.char(b)
- end
- end
- end
- return 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement