Posted 20 June 2015 - 04:32 PM
Hey Guys,
So I am making this program which lets me control and see different things using the touch screen monitor.
The problem which I am having is that the screen flashes (I think its because of the loop updating the writing of the buttons or what not) so I tried adding a timer event within the loop but had no luck as it seems to somewhat ignore the timer i.e. doesn't stop everything keeps flashing the display.
Here is my code (Reactor is connected via wired modem):
So I am making this program which lets me control and see different things using the touch screen monitor.
The problem which I am having is that the screen flashes (I think its because of the loop updating the writing of the buttons or what not) so I tried adding a timer event within the loop but had no luck as it seems to somewhat ignore the timer i.e. doesn't stop everything keeps flashing the display.
Here is my code (Reactor is connected via wired modem):
Spoiler
-- Reactor controller program
-- Variables
m = peripheral.wrap("right")
r = peripheral.wrap("BigReactors-Reactor_1") -- r = Reactor
e = peripheral.wrap("tile_blockcapacitorbank_name_0") -- e = Capacitor Bank
-- c = Capacitor, r = reactor
cCurrentEnergy = 0
cEnergyLastTick = 0
cEnergyUseage = 0
rRFProduced = 0
rRFTarget = 0
rControlRodAmount = 0
test = true
w, h = m.getSize()
textButton = {
{text="Reactor: ", xPos=1 , yPos=1, other=r.getActive(), txtColor=colors.yellow, otherColor=colors.green, func=1},
{text="Energy Stored: ", xPos=1, yPos=3, other=0, txtColor=colors.yellow, otherColor=colors.green, func=2},
{text="Energy Producing: ", xPos=1, yPos=5, other=math.ceil(r.getEnergyProducedLastTick()), txtColor=colors.yellow, otherColor=colors.green, func=3},
--{text="Energy Usage: ", xPos=1, yPos=7, other=cEnergyUseage, txtColor=colors.yellow, otherColor=colors.green, func=4},
}
-- Functions
function writeInfo(_table)
for i, v in pairs(_table) do -- For loop for a table
m.setCursorPos(v.xPos, v.yPos) -- Within table takes the x and y pos to set the pos
if v.func == 1 then -- checks for func as that is what the func is
v.other = r.getActive() --gets if reactor is active to print out later
elseif v.func == 2 then
if isPercentage then -- Checks isPercentage if so returns a percentage using rPercentage()
v.other = math.ceil(rPercentageAmount) .."%"
else
v.other = math.ceil(r.getEnergyStored()) .." RF" -- If not just returns energy stored
end
elseif v.func == 3 then
v.other = math.ceil(r.getEnergyProducedLastTick()) .. " RF/T"
end
m.setTextColor(v.txtColor)
m.write(v.text) -- writes text
m.setTextColor(v.otherColor)
m.write(v.other) -- writes info that changes
end
end
function checkClick(_table, touchX, touchY)
for i,v in pairs(_table) do
if touchY == v.yPos then
return true, v.func, v.other
end
end
return false, nil
end
--[[function setVars() -- Sets some baribles for logic
-- Calculations for energy usage of base.
cEnergyLastTick = e.getEnergyStored()
cCurrentEnergy = e.getEnergyStored()
cEnergyUseage = cEnergyLastTick - cCurrentEnergy
end]]
function rPercentage() -- Works out current percentage in reactor
maxEnergy = 10000000
currEnergy = r.getEnergyStored()
rPercentageAmount = currEnergy / maxEnergy * 100
end
function changeActive()
if r.getActive() then
r.setActive(false)
else
r.setActive(true)
end
end
function changeEnergy(text)
if isPercentage then
isPercentage = false
else
isPercentage = true
end
end
m.clear()
myTimer=1
while true do
--display()
--getClickPos()
rPercentage() --Works out percentage for display
sleep(1)
writeInfo(textButton) -- Writes textButton on monitor
myTimer = os.startTimer(1) -- Starts timer so screen can update
event, but, x, y = os.pullEvent() -- Waits for a event to Pull
if event == "timer" and but == myTimer then
elseif event == "monitor_touch" then -- See if event monitor_touch was called
clicked, option, other2 = checkClick(textButton, x, y) -- Checks if the monitor were the monitor was touched only if the event was called
if option == 1 then -- sees if option is 1
changeActive() -- Calls functions
elseif option == 2 then
changeEnergy(other2)
end
-- Checks if event is timer and is equal to myTimer
end
m.clear()
end