diff --git a/main.lua b/main.lua index 07d03dc..fec03c8 100644 --- a/main.lua +++ b/main.lua @@ -160,25 +160,14 @@ Config.Autopilot = { AutopilotDesiredHeading = nil } -Throttles = { - fore = { - name = nil, - side = nil - }, - aft = { - name = nil, - side = nil - }, - down = { - name = nil, - side = nil - } -} - - +Config.Throttles = nil function ThrottleInit() + if Config.Throttles ~= nil then + return + end + local sides = { "top", "bottom", @@ -227,8 +216,8 @@ function ThrottleInit() local initialState = initialStates[pname] for k, v in pairs(currentState) do if initialState[k] ~= v then - Throttles[throttleType].name = pname - Throttles[throttleType].side = k + Config.Throttles[throttleType].name = pname + Config.Throttles[throttleType].side = k return end end @@ -443,7 +432,7 @@ function UpdateStabilization() end function PollThrottle() - for _, v in pairs(Throttles) do + for _, v in pairs(Config.Throttles) do if v.name and v.side then v.input = peripheral.wrap(v.name).getAnalogInput(v.side) end @@ -613,6 +602,15 @@ function UpdateGlobalThrust() end function Init() + + local configFile = io.open(Config.ConfigPath, "r") + if configFile then + local configContent = configFile:read("*all") + Config = stringToTable(configContent) + configFile:close() + else + print("Could not open the config file") + end -- Initialize monitors - identify which monitor is for which type if Config.Monitors.InstrumentPanelMonitor == nil then @@ -629,15 +627,6 @@ function Init() ThrottleInit() Thrusters = {} - - local configFile = io.open(Config.ConfigPath, "r") - if configFile then - local configContent = configFile:read("*all") - Config = stringToTable(configContent) - configFile:close() - else - print("Could not open the config file") - end if Config.thrusterConfigPath ~= nil then local thrusterConfigFile = io.open(Config.thrusterConfigPath, "r") @@ -750,6 +739,18 @@ function collectSensorData() return data end +function WriteConfigFiles() + print("Writing to thruster config file.") + local thrusterConfigFile = fs.open(Config.thrusterConfigPath, "w+") + thrusterConfigFile.write(tableToString(Thrusters)) + thrusterConfigFile.close() + + print("Writing to config file") + local configFile = fs.open(Config.ConfigPath, "w+") + configFile.write(tableToString(Config)) + configFile.close() +end + function Main() Init() @@ -778,24 +779,13 @@ function Main() local event = nil local function getTerminateEvent() event = os.pullEventRaw("terminate") + if event == "terminate" then print("Terminate Event Received") WriteConfigFiles() end end local function do_sleep() os.sleep(0.1) end parallel.waitForAny(do_sleep, getTerminateEvent) - if event == "terminate" then print("Terminate Event Received") break end - end - - print("Writing to thruster config file.") - local thrusterConfigFile = fs.open(Config.thrusterConfigPath, "w+") - thrusterConfigFile.write(tableToString(Thrusters)) - thrusterConfigFile.close() - - print("Writing to config file") - local configFile = fs.open(Config.ConfigPath, "w+") - configFile.write(tableToString(Config)) - configFile.close() end -- Run the main function when the script starts