This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
firewolf31's profile picture

touchscreen help

Started by firewolf31, 07 May 2014 - 05:04 AM
firewolf31 #1
Posted 07 May 2014 - 07:04 AM
Hi i am trying to write a program that will in realtime give me stats of whatever machine i have it connected to. Now i got that part down and its working but what i want to do is to be able to click certain stats to get elsewhere in the program using os.pullEvent ("monitor_touch") or whatever, but the few ways i have tried it specially as a test with a character key to stop the program just disables my realtime feature(running loop that displays my information on screen) is it possible to do so and if so how would i add the code to my existing code in order for the stats loop to keep going and still have the program wait for a touch event.

thanks in advance for your help much appreciated

ps my code is ugly and long for nothing and dirty coding for i am learning but it still works :D/>
Edited on 07 May 2014 - 05:09 AM
CometWolf #2
Posted 07 May 2014 - 07:23 AM
Your gonna have to post your code first, but chances are you could do this with the parallel api.
firewolf31 #3
Posted 07 May 2014 - 08:34 AM
ROFL you want to see bad code :D/> but yeah parallelle api souns like it would run multiple bits of codes simutaneously i will look into it and if nothing can be done on my side i will post bad code :D/> for now bed time… me thinks tomorow is a new day and a fresher mind :D/>

thanks

edit: did a bt of reading and looks to me that using parallel api wants to have functions. as i understand i would have to put all of my code in one function! could be wrong though im not expert!
Edited on 07 May 2014 - 06:48 AM
CometWolf #4
Posted 07 May 2014 - 08:41 AM
We obviously can't suggest any other solution, unless we see how your code currently operates.
firewolf31 #5
Posted 07 May 2014 - 08:56 AM
Well here is my bad code :D/>
dont get me wrong it works.

the part inside the while loop where i specify fuelRods which really are control rods i want that to be click able to bring me to an other page where i can play with the controle rods setting from the touch screen but while it waits for me to click i want the rest of the number to keep refreshing every 2 seconds

thats the part i cant get
after i get this part i can get the clicking part done and i am prettey sure i can
i just want to be able to listen for an event while my monitor keeps refreshing every 2 seconds


thanks



--Peripheral Wrapping--
mon = peripheral.wrap("monitor_0")
r = peripheral.wrap("BigReactors-Reactor_0")
t = peripheral.wrap("BigReactors-Turbine_0")
page = 0
--Arrays--
fuelRods = {}
--Variables--
--Initializing Code--
mon.clear()
mon.setTextColor(colors.white)
mon.setBackgroundColor(colors.black)
--Functions--
function heading(text)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, 1)
  mon.write(text)
end
function label1(text, y)
  mon.setTextColor(colors.white)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, y)
  mon.write(text..": ")
end
function label(text,device, y)
  active = device.getActive()
  mon.setTextColor(colors.white)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, y)
  mon.write(text ..": ")
  if active == true then
	mon.setTextColor(colors.lime)
	mon.write("Active")
  elseif active == false then
	mon.setTextColor(colors.red)
	mon.write("Inactive")
  end
  mon.setTextColor(colors.white)
end
function casingTemp()
  temp = math.floor(r.getCasingTemperature())
  mon.write("Casing Temperature: ")
  mon.setTextColor(colors.purple)
  mon.write(temp.." C")
  mon.setTextColor(colors.white)
end
function fuelReactivity()
  reactivity = math.floor(r.getFuelReactivity())
  mon.write("Fuel Reactivity: ")
  mon.setTextColor(colors.purple)
  mon.write(reactivity.."%")
  mon.setTextColor(colors.white)
end
function fuelTemp()
  temp =math.floor(r.getFuelTemperature())
  mon.write("Fuel Temperature: ")
  mon.setTextColor(colors.purple)
  mon.write(temp.." C")
  mon.setTextColor(colors.white)
end
function fuelConsumed()
  consume = tostring(r.getFuelConsumedLastTick())
  mon.write("Fuel Consumed: ")
  mon.setTextColor(colors.purple)
  mon.write(consume)
  mon.setTextColor(colors.white)
end
function fuelAmount()
  amount = math.floor(r.getFuelAmount())
  mon.write("Fuel: ")
  mon.setTextColor(colors.purple)
  mon.write(amount.."mb")
  mon.setTextColor(colors.white)
end
function wasteAmount()
  waste = math.floor(r.getWasteAmount())
  mon.write("Waste: ")
  mon.setTextColor(colors.purple)
  mon.write(waste.."mb")
  mon.setTextColor(colors.white)
end
function coolantAmount()
  coolant = math.floor(r.getCoolantAmount())
  mon.write("Coolant: ")
  mon.setTextColor(colors.purple)
  mon.write(coolant.."mb")
  mon.setTextColor(colors.white)
end
function hotFluidAmount()
  hotFluid = math.floor(r.getHotFluidAmount())
  mon.write("Hot Fluid: ")
  mon.setTextColor(colors.purple)
  mon.write(hotFluid.."mb")
  mon.setTextColor(colors.white)
end
function controlRods()
  rodCount = (tostring(reactor.getNumberOfControlRods()))
  --print (rodCount)
  y = 5
  --rodName = (reactor.getControlRodName())
  for i = 0 , rodCount-1 do
	mon.setCursorPos(1,y)
	mon.write(reactor.getControlRodName(i)..": "..reactor.getControlRodLevel(i).."%")
	y = y+1
  end
  
end
--************************--
function rotorSpeed()
  speed = math.floor(t.getRotorSpeed())
  mon.write("Rotor Speed: ")
  mon.setTextColor(colors.purple)
  mon.write(speed)
  mon.setTextColor(colors.white)
end
function lastTickEnergy()
  energy = math.floor(t.getEnergyProducedLastTick())
  mon.write("Energy Produced Last Tick: ")
  mon.setTextColor(colors.purple)
  mon.write(energy)
  mon.setTextColor(colors.white)
end
function nBlades()
  blades = tostring(t.getNumberOfBlades())
  mon.write("Number Of Blades: ")
  mon.setTextColor(colors.purple)
  mon.write(blades)
  mon.setTextColor(colors.white)
end
function bladeEff()
  eff = tostring(t.getBladeEfficiency())
  mon.write("Blade Efficiency: ")
  mon.setTextColor(colors.purple)
  mon.write(eff.."%")
  mon.setTextColor(colors.white)
end
function fluidFlow()
  flow = t.getFluidFlowRate()
  max = t.getFluidFlowRateMax()

	mon.write("Flow Rate / Max: ")
	mon.setTextColor(colors.purple)
	mon.write(flow.."/"..max)
	mon.setTextColor(colors.white)
end
--Program Code
while true do
	if page == 0 then
	mon.clear()
	heading("Reactor - Turbine Control")
	label("Reactor", r, 3)
	mon.setCursorPos(1,5)
	casingTemp()
	mon.setCursorPos(1,6)
	fuelTemp()
	mon.setCursorPos(1,7)
	fuelReactivity()
	mon.setCursorPos(1,8)
	fuelConsumed()
	mon.setCursorPos(1,9)
	fuelAmount()
	mon.setCursorPos(1,10)
	wasteAmount()
	mon.setCursorPos(1,11)
	coolantAmount()
	mon.setCursorPos(1,12)
	hotFluidAmount()
	mon.setCursorPos(1,13)
	mon.setBackgroundColor(colors.gray)
	mon.write("Fuel Rods")
	mon.setBackgroundColor(colors.black)
	--********************--
	label("Turbine", t, 15)
	mon.setCursorPos(1,17)
	rotorSpeed()
	mon.setCursorPos(1,18)
	lastTickEnergy()
	mon.setCursorPos(1,19)
	nBlades()
	mon.setCursorPos(1,20)
	bladeEff()
	mon.setCursorPos(1,21)
	fluidFlow()
	elseif page == 1 then
	  heading("Reactor - Turbine Control")
	  label1("Control Rods", 3)
	  controlRods()
	--else
--local event, param1 = os.pullEvent ("char")
  --if param1 == "x" then
	--break
  --end
	end
	sleep(1)
end

obvioudly i commented out the pulleven part since i tried it in various spots in my code and couldnt get that to work
Edited on 07 May 2014 - 06:57 AM
CometWolf #6
Posted 07 May 2014 - 10:47 AM
pullEvent causes the program to yield until it gets an event of the type specified, or any event at all if no type is specified. However, note your use of sleep(1) to pause your refreshing. sleep actually uses pullEvent, specifically a timer event. So what you have to do essentially, is make your own sleep function which also processes other events while waiting for the timer event.
firewolf31 #7
Posted 07 May 2014 - 11:15 AM
interesting i can see what ur saying but would u be abe to provide me with a small example
Bomb Bloke #8
Posted 07 May 2014 - 01:00 PM
local running = true
local event, param1, param2, param3

while running do
	if page == 0 then
		...
	elseif page == 1 then
		...
	end
	
	local myTimer = os.startTimer(1) -- Start a timer that will queue an event in one second. Record the timer ID in myTimer.

	repeat
		event, param1, param2, param3 = os.pullEvent ()
		
		if event == "char" and param1 == "x" then
			running = false
			break
		elseif event == "monitor_touch" then
			do something else
		elseif ...
			etc
		end		
	until event == "timer" and param1 == myTimer
end
firewolf31 #9
Posted 07 May 2014 - 02:26 PM
local running = true
local event, param1, param2, param3

while running do
	if page == 0 then
		...
	elseif page == 1 then
		...
	end
	
	local myTimer = os.startTimer(1) -- Start a timer that will queue an event in one second. Record the timer ID in myTimer.

	repeat
		event, param1, param2, param3 = os.pullEvent ()
		
		if event == "char" and param1 == "x" then
			running = false
			break
		elseif event == "monitor_touch" then
			do something else
		elseif ...
			etc
		end		
	until event == "timer" and param1 == myTimer
end

with this code anything i have under the pages will refresh every second until i cause an event to either break the program or change the page ?

Alright i have added this in my code but while the code does work in giving me my stats in realtime i cannot break the program using x



--Peripheral Wrapping--
mon = peripheral.wrap("monitor_4")
r = peripheral.wrap("BigReactors-Reactor_0")
t = peripheral.wrap("BigReactors-Turbine_0")
page = 0
--Arrays--
fuelRods = {}
--Variables--
local running = true
local event, param1, param2, param3
--Initializing Code--
mon.clear()
mon.setTextColor(colors.white)
mon.setBackgroundColor(colors.black)
--Functions--
function heading(text)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, 1)
  mon.write(text)
end
function label1(text, y)
  mon.setTextColor(colors.white)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, y)
  mon.write(text..": ")
end
function label(text,device, y)
  active = device.getActive()
  mon.setTextColor(colors.white)
  w, h = mon.getSize()
  mon.setCursorPos((w-string.len(text))/2+1, y)
  mon.write(text ..": ")
  if active == true then
	mon.setTextColor(colors.lime)
	mon.write("Active")
  elseif active == false then
	mon.setTextColor(colors.red)
	mon.write("Inactive")
  end
  mon.setTextColor(colors.white)
end
function casingTemp()
  temp = math.floor(r.getCasingTemperature())
  mon.write("Casing Temperature: ")
  mon.setTextColor(colors.purple)
  mon.write(temp.." C")
  mon.setTextColor(colors.white)
end
function fuelReactivity()
  reactivity = math.floor(r.getFuelReactivity())
  mon.write("Fuel Reactivity: ")
  mon.setTextColor(colors.purple)
  mon.write(reactivity.."%")
  mon.setTextColor(colors.white)
end
function fuelTemp()
  temp =math.floor(r.getFuelTemperature())
  mon.write("Fuel Temperature: ")
  mon.setTextColor(colors.purple)
  mon.write(temp.." C")
  mon.setTextColor(colors.white)
end
function fuelConsumed()
  consume = tostring(r.getFuelConsumedLastTick())
  mon.write("Fuel Consumed: ")
  mon.setTextColor(colors.purple)
  mon.write(consume)
  mon.setTextColor(colors.white)
end
function fuelAmount()
  amount = math.floor(r.getFuelAmount())
  mon.write("Fuel: ")
  mon.setTextColor(colors.purple)
  mon.write(amount.."mb")
  mon.setTextColor(colors.white)
end
function wasteAmount()
  waste = math.floor(r.getWasteAmount())
  mon.write("Waste: ")
  mon.setTextColor(colors.purple)
  mon.write(waste.."mb")
  mon.setTextColor(colors.white)
end
function coolantAmount()
  coolant = math.floor(r.getCoolantAmount())
  mon.write("Coolant: ")
  mon.setTextColor(colors.purple)
  mon.write(coolant.."mb")
  mon.setTextColor(colors.white)
end
function hotFluidAmount()
  hotFluid = math.floor(r.getHotFluidAmount())
  mon.write("Hot Fluid: ")
  mon.setTextColor(colors.purple)
  mon.write(hotFluid.."mb")
  mon.setTextColor(colors.white)
end
function controlRods()
  rodCount = (tostring(reactor.getNumberOfControlRods()))
  --print (rodCount)
  y = 5
  --rodName = (reactor.getControlRodName())
  for i = 0 , rodCount-1 do
	mon.setCursorPos(1,y)
	mon.write(reactor.getControlRodName(i)..": "..reactor.getControlRodLevel(i).."%")
	y = y+1
  end
  
end
--************************--
function rotorSpeed()
  speed = math.floor(t.getRotorSpeed())
  mon.write("Rotor Speed: ")
  mon.setTextColor(colors.purple)
  mon.write(speed)
  mon.setTextColor(colors.white)
end
function lastTickEnergy()
  energy = math.floor(t.getEnergyProducedLastTick())
  mon.write("Energy Produced Last Tick: ")
  mon.setTextColor(colors.purple)
  mon.write(energy)
  mon.setTextColor(colors.white)
end
function nBlades()
  blades = tostring(t.getNumberOfBlades())
  mon.write("Number Of Blades: ")
  mon.setTextColor(colors.purple)
  mon.write(blades)
  mon.setTextColor(colors.white)
end
function bladeEff()
  eff = tostring(t.getBladeEfficiency())
  mon.write("Blade Efficiency: ")
  mon.setTextColor(colors.purple)
  mon.write(eff.."%")
  mon.setTextColor(colors.white)
end
function fluidFlow()
  flow = t.getFluidFlowRate()
  max = t.getFluidFlowRateMax()

	mon.write("Flow Rate / Max: ")
	mon.setTextColor(colors.purple)
	mon.write(flow.."/"..max)
	mon.setTextColor(colors.white)
end
--Program Code
while running do
	if page == 0 then
	mon.clear()
	heading("Reactor - Turbine Control")
	label("Reactor", r, 3)

	mon.setCursorPos(1,5)
	casingTemp()
	mon.setCursorPos(1,6)
	fuelTemp()
	mon.setCursorPos(1,7)
	fuelReactivity()
	mon.setCursorPos(1,8)
	fuelConsumed()
	mon.setCursorPos(1,9)
	fuelAmount()
	mon.setCursorPos(1,10)
	wasteAmount()
	mon.setCursorPos(1,11)
	coolantAmount()
	mon.setCursorPos(1,12)
	hotFluidAmount()
	mon.setCursorPos(1,13)
	mon.setBackgroundColor(colors.gray)
	mon.write("Fuel Rods")
	mon.setBackgroundColor(colors.black)
	--********************--
	label("Turbine", t, 15)
	mon.setCursorPos(1,17)
	rotorSpeed()
	mon.setCursorPos(1,18)
	lastTickEnergy()
	mon.setCursorPos(1,19)
	nBlades()
	mon.setCursorPos(1,20)
	bladeEff()
	mon.setCursorPos(1,21)
	fluidFlow()
	elseif page == 1 then
	  heading("Reactor - Turbine Control")
	  label1("Control Rods", 3)
	  controlRods()
  
	local myTimer = os.startTimer(1)
  
	repeat
	  event, param1, param2, param3 = os.pullEvent()
		if event == "char" and  param1 == "x" then
		  running = false
		  break
		end
	until event == "timer" and param1 == myTimer  
	  
	end
	sleep(1)
end

if i remove my sleep(1) line it then returns an error (too long without yeilding)

basically this whole block does not work:

    local myTimer = os.startTimer(1)
  
    repeat
		  event, param1, param2, param3 = os.pullEvent()
		    if event == "char" and  param1 == "x" then
				  running = false
				  break
		    end
    until event == "timer" and param1 == myTimer
Edited on 07 May 2014 - 05:18 PM
Bomb Bloke #10
Posted 07 May 2014 - 11:02 PM
Because you've rigged it to only occur if page == 1.
firewolf31 #11
Posted 07 May 2014 - 11:11 PM
yes but i should still be able to break the program no ?
Bomb Bloke #12
Posted 07 May 2014 - 11:33 PM
Nope, because page aways == 0.

SpoilerChange this:

        elseif page == 1 then
          heading("Reactor - Turbine Control")
          label1("Control Rods", 3)
          controlRods()

        local myTimer = os.startTimer(1)

        repeat
          event, param1, param2, param3 = os.pullEvent()
                if event == "char" and  param1 == "x" then
                  running = false
                  break
                end
        until event == "timer" and param1 == myTimer  

        end
        sleep(1)
end

… to this:

        elseif page == 1 then
          heading("Reactor - Turbine Control")
          label1("Control Rods", 3)
          controlRods()
        end

        local myTimer = os.startTimer(1)

        repeat
          event, param1, param2, param3 = os.pullEvent()
                if event == "char" and  param1 == "x" then
                  running = false
                  break
                end
        until event == "timer" and param1 == myTimer 
end
firewolf31 #13
Posted 08 May 2014 - 12:17 AM
yes that worked.. i am able to exit the program by pressing x and my stats still change realtime

thanks

not to get the clicking aspect in :D/>