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