SHOW:
|
|
- or go back to the newest paste.
| 1 | if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
| |
| 2 | if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
| |
| 3 | local w = warpdriveCommons.w | |
| 4 | ||
| 5 | local data | |
| 6 | ----------- Force field support | |
| 7 | ||
| 8 | local ffield_projectorAddresses = {}
| |
| 9 | local ffield_projectors = {}
| |
| 10 | local ffield_projector_indexSelected = 1 | |
| 11 | local ffield_projector_indexFirstLine = 1 | |
| 12 | local ffield_projector_lines = 10 | |
| 13 | ||
| 14 | function ffield_boot(isDetailed) | |
| 15 | if #ffield_projectorAddresses == 0 and #ffield_relayAddresses == 0 then | |
| 16 | return | |
| 17 | end | |
| 18 | ||
| 19 | if isDetailed == nil then | |
| 20 | isDetailed = true | |
| 21 | end | |
| 22 | ||
| 23 | if isDetailed then | |
| 24 | w.write("Booting Cores")
| |
| 25 | ||
| 26 | w.writeLn("...")
| |
| 27 | w.sleep(0.1) | |
| 28 | end | |
| 29 | ||
| 30 | -- getting projectors parameters | |
| 31 | ffield_projectors = {}
| |
| 32 | for key, address in pairs(ffield_projectorAddresses) do | |
| 33 | local device = w.device_get(address) | |
| 34 | name = device.name() | |
| 35 | fn = "" | |
| 36 | file = name:gsub(" ","")
| |
| 37 | stt = "disk/"..file | |
| 38 | f = fs.open(stt,"r"); | |
| 39 | fn = f.readAll(); | |
| 40 | x, y, z = fn | |
| 41 | f.close(); | |
| 42 | local name1 = device.name() | |
| 43 | - | -- local pos = device.getLocalPosition() |
| 43 | + | |
| 44 | address = address, | |
| 45 | device = device, | |
| 46 | position = fn, | |
| 47 | name = name1, | |
| 48 | - | position = fn,--{ x = x, y = y, z = z },
|
| 48 | + | |
| 49 | isEnabled = isEnabled } | |
| 50 | if isDetailed then | |
| 51 | w.writeLn(ffield_projector_getDescription(projector)) | |
| 52 | end | |
| 53 | ||
| 54 | table.insert(ffield_projectors, projector) | |
| 55 | end | |
| 56 | ||
| 57 | end | |
| 58 | ||
| 59 | ||
| 60 | function ffield_read(parData) | |
| 61 | data = parData | |
| 62 | - | function ffield_save() |
| 62 | + | |
| 63 | - | -- nothing |
| 63 | + | |
| 64 | function ffield_projector_getDescription(projector) | |
| 65 | if projector == nil or projector.device == nil then | |
| 66 | return "~invalid~" | |
| 67 | end | |
| 68 | local description = "#" .. w.format_string(projector.name, 20) | |
| 69 | .. " @ " .. w.format_string(projector.position, 30) | |
| 70 | return description | |
| 71 | end | |
| 72 | ||
| 73 | function ffield_projector_getIndexes() | |
| 74 | - | -- local device = w.device_get(address) |
| 74 | + | |
| 75 | - | --local name = device.name() |
| 75 | + | |
| 76 | - | -- fn = "" |
| 76 | + | |
| 77 | - | -- file = name:gsub(" ","")
|
| 77 | + | |
| 78 | - | --stt = "disk/"..file |
| 78 | + | |
| 79 | - | --f = fs.open(stt,"r"); |
| 79 | + | |
| 80 | - | -- fn = f.readAll(); |
| 80 | + | |
| 81 | - | --x, y, z = fn |
| 81 | + | |
| 82 | - | --f.close(); |
| 82 | + | |
| 83 | ffield_projector_indexFirstLine = ffield_projector_indexSelected - ffield_projector_lines | |
| 84 | end | |
| 85 | - | --.. " Y " .. w.format_integer(y1, 3) .. " Z " .. w.format_integer(z1, 7) .. ") " |
| 85 | + | |
| 86 | else | |
| 87 | return 1, 1 | |
| 88 | end | |
| 89 | end | |
| 90 | ||
| 91 | function ffield_projector_get(index) | |
| 92 | local indexToUse = index | |
| 93 | local projector | |
| 94 | ||
| 95 | if ffield_projectors ~= nil then | |
| 96 | if indexToUse > #ffield_projectors then | |
| 97 | indexToUse = 1 | |
| 98 | elseif indexToUse < 1 then | |
| 99 | indexToUse = #ffield_projectors | |
| 100 | end | |
| 101 | projector = ffield_projectors[indexToUse] | |
| 102 | end | |
| 103 | ||
| 104 | if projector == nil then | |
| 105 | ffield_boot(false) | |
| 106 | w.status_showWarning("Invalid projector index " .. index)
| |
| 107 | projector = {
| |
| 108 | address = "-", | |
| 109 | device = nil, | |
| 110 | position = { x = 0, y = 0, z = 0 },
| |
| 111 | name = "-", | |
| 112 | beamFrequency = -1, | |
| 113 | shape = "NONE", | |
| 114 | isEnabled = false } | |
| 115 | end | |
| 116 | return projector | |
| 117 | end | |
| 118 | ||
| 119 | function ffield_projector_getSelected() | |
| 120 | return ffield_projector_get(ffield_projector_indexSelected) | |
| 121 | end | |
| 122 | ||
| 123 | function ffield_enable(projectorOrRelay, enable) | |
| 124 | if projectorOrRelay == nil or projectorOrRelay.device == nil then | |
| 125 | return | |
| 126 | end | |
| 127 | local enableToApply = enable | |
| 128 | if enableToApply == nil then | |
| 129 | enableToApply = not projectorOrRelay.device.enable() | |
| 130 | end | |
| 131 | projectorOrRelay.isEnabled = projectorOrRelay.device.enable(enableToApply) | |
| 132 | return projectorOrRelay.isEnabled | |
| 133 | end | |
| 134 | ||
| 135 | function ffield_projector_key(character, keycode) | |
| 136 | if keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or - | |
| 137 | ffield_projector_indexSelected = ffield_projector_indexSelected - 1 | |
| 138 | return true | |
| 139 | elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or + | |
| 140 | ffield_projector_indexSelected = ffield_projector_indexSelected + 1 | |
| 141 | return true | |
| 142 | end | |
| 143 | return false | |
| 144 | end | |
| 145 | ||
| 146 | function ffield_projector_page() | |
| 147 | w.page_begin(w.data_getName() .. " - Monitored Cores") | |
| 148 | ||
| 149 | -- w.setCursorPos(1, 2) | |
| 150 | if #ffield_projectors == 0 then | |
| 151 | w.setColorDisabled() | |
| 152 | w.writeCentered(2, "No force field projector defined, connect one and reboot!") | |
| 153 | - | if character == 's' or character == 'S' then |
| 153 | + | |
| 154 | - | for key, projector in pairs(ffield_projectors) do |
| 154 | + | |
| 155 | - | ffield_enable(projector, true) |
| 155 | + | |
| 156 | w.writeCentered(2, "Monitored Core " .. indexSelected .. " of " .. #ffield_projectors .. " is selected") | |
| 157 | local indexLastLine = math.min(indexFirstLine + ffield_projector_lines, #ffield_projectors) | |
| 158 | - | elseif character == 'p' or character == 'P' then |
| 158 | + | |
| 159 | - | for key, projector in pairs(ffield_projectors) do |
| 159 | + | |
| 160 | - | ffield_enable(projector, false) |
| 160 | + | |
| 161 | w.clearLine() | |
| 162 | w.write(">")
| |
| 163 | - | elseif character == 'e' or character == 'E' then |
| 163 | + | |
| 164 | - | local projector = ffield_projector_getSelected() |
| 164 | + | |
| 165 | - | if projector ~= nil and projector.device ~= nil then |
| 165 | + | |
| 166 | - | ffield_enable(projector) |
| 166 | + | |
| 167 | local projector = ffield_projector_get(indexCurrent) | |
| 168 | local description = ffield_projector_getDescription(projector) | |
| 169 | - | elseif character == 'c' or character == 'C' then -- C or keycode == 46 |
| 169 | + | |
| 170 | - | ffield_projector_config() |
| 170 | + | |
| 171 | - | w.data_save() |
| 171 | + | |
| 172 | end | |
| 173 | - | elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or - |
| 173 | + | |
| 174 | w.setCursorPos(1, 16) | |
| 175 | w.setColorNormal() | |
| 176 | w.write(" -----------------------------------------------")
| |
| 177 | ||
| 178 | w.setCursorPos(1, 17) | |
| 179 | w.setColorControl() | |
| 180 | end | |
| 181 | ||
| 182 | function ffield_register() | |
| 183 | w.device_register("warpdriveShipController",
| |
| 184 | function(deviceType, address, wrap) table.insert(ffield_projectorAddresses, address) end, | |
| 185 | function() end) | |
| 186 | end | |
| 187 | ||
| 188 | ----------- connections status | |
| 189 | ||
| 190 | function connections_page(isBooting) | |
| 191 | w.page_begin(w.data_getName() .. " - Connections") | |
| 192 | ||
| 193 | w.writeLn("")
| |
| 194 | ||
| 195 | local monitors = w.device_getMonitors() | |
| 196 | if #monitors == 0 then | |
| 197 | w.setColorDisabled() | |
| 198 | w.writeLn("No Monitor detected")
| |
| 199 | elseif #monitors == 1 then | |
| 200 | w.setColorSuccess() | |
| 201 | w.writeLn("1 monitor detected")
| |
| 202 | else | |
| 203 | w.setColorSuccess() | |
| 204 | w.writeLn(#monitors .. " Monitors detected") | |
| 205 | end | |
| 206 | ||
| 207 | if #ffield_projectorAddresses == 0 then | |
| 208 | w.setColorDisabled() | |
| 209 | w.writeLn("No Ship Controllers Detected.")
| |
| 210 | elseif #ffield_projectorAddresses == 1 then | |
| 211 | w.setColorSuccess() | |
| 212 | w.writeLn("1 Ship Controller Detected.")
| |
| 213 | else | |
| 214 | w.setColorSuccess() | |
| 215 | w.writeLn(#ffield_projectorAddresses .. " Ship Controllers Detected.") | |
| 216 | end | |
| 217 | ||
| 218 | if isBooting then | |
| 219 | ffield_boot() | |
| 220 | end | |
| 221 | ||
| 222 | w.writeLn("")
| |
| 223 | w.setColorNormal() | |
| 224 | w.writeLn("This is a keyboard controlled user interface.")
| |
| 225 | w.write("Key controls are written like so: ")
| |
| 226 | w.setColorControl() | |
| 227 | w.write("Action (key)")
| |
| 228 | w.setColorNormal() | |
| 229 | w.writeLn(".")
| |
| 230 | w.write("For example, typing ")
| |
| 231 | w.setColorControl() | |
| 232 | w.write(" 1 ")
| |
| 233 | w.setColorNormal() | |
| 234 | w.writeLn(" will open Cores.")
| |
| 235 | end | |
| 236 | ||
| 237 | ----------- Boot sequence | |
| 238 | ||
| 239 | w.page_setEndText(" Home (0), Cores (1)")
| |
| 240 | w.page_register('0', connections_page, nil)
| |
| 241 | w.page_register('1', ffield_projector_page, ffield_projector_key)
| |
| 242 | ffield_register() | |
| 243 | ||
| 244 | w.boot() | |
| 245 | w.run() | |
| 246 | w.close() |