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

how to use two monitors at the same time?

Started by DTM450, 18 May 2016 - 05:45 AM
DTM450 #1
Posted 18 May 2016 - 07:45 AM
Ok I'm writing up a reactor/turbine controller program and I want to have two monitors one in the reactor room and one in my main room for controlling settings and showing information.

How would I go about this?

I thought maybe I could have both monitors in an array but I don't know if this is possible.
Bomb Bloke #2
Posted 18 May 2016 - 09:02 AM
There are quite a few ways, but this here's a fairly short one.
DTM450 #3
Posted 18 May 2016 - 09:14 AM
*snip*

Thanks I'll try this
Edited on 18 May 2016 - 07:55 AM
DTM450 #4
Posted 18 May 2016 - 09:50 AM
Is there a way to take touchpoint button presses from the monitors as well?
Edited on 18 May 2016 - 07:50 AM
Dragon53535 #5
Posted 18 May 2016 - 11:55 AM
From both monitors? It's possible yeah. Difficult given that Lyqyd didn't create an in built way to do so.

If you just want to use the in built button function, the easiest way would be to create an array of touchpoint pointers, one for each monitor, and loop through this array and create an array of function pointers that are structured like:

function() return otherFunction(touch[i]) end
And then use unpack on this array inside a parallel.waitForAny() call.



os.loadAPI("touchpoint")
local function runButtons(t)
  while true do

   local event = {t:handleEvents()}

   if event == "button_click" then
	  
	 if t.buttonList[event[2]].func then
	
	   t.buttonList[event[2]].func()

	 end
   end
  end
end

--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_666"

local touch = {}

for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end

--#Define your buttons here. As well as any functions they should run

local runTable = {}

for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end

parallel.waitForAny(unpack(runTable))
Coded it up on a whim because it was an interesting problem, mind you anything you want your buttons to do you have to code, but this 'should' allow for you to set up touchpoints on each monitor, as well as automatically handle buttons and running functions per buttons.
Edited on 18 May 2016 - 10:00 AM
DTM450 #6
Posted 19 May 2016 - 05:19 AM
From both monitors? It's possible yeah. Difficult given that Lyqyd didn't create an in built way to do so.

yes from both

heres what I have so far but I'm having trouble wrapping my head around it

*snip*

--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_666"
monSides = {"monitor_0","monitor_1"}
local touch = {}

for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end

--#Define your buttons here. As well as any functions they should run

local runTable ={add("b1", t:flash(event[2]),1,1,4,1,colours.red,colours.lime)
				}

for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end

parallel.waitForAny(unpack(runTable))

would I be able to populate the monSides table with peripheral.find("monitor")
and I'm not sure where I put the t:draw function and would I still need to use the t = touchpoint.new() declaration to be able to use the 't:'
Dragon53535 #7
Posted 19 May 2016 - 05:47 AM
No you wouldn't be able to populate it with peripheral.find as that automatically wraps the peripherals for you, you need the side/name because touchpoint wraps it as well.

Make monSides a local table.

Nono, you're not going to use t = touchpoint.new at all, that's what the first loop does, it create a new table and puts a touchpoint object (what touchpoint new gives) into that table for each monitor you tell it to. So touch[1] is going to be the touchpoint for "monitor_0"

Don't touch the runTable table. Moreso because you never actually closed it, and because it's going to be filled with funciton pointers.

As for adding buttons, you need to use touch[number]:add to add them. For your t:flash()


function() touch[number]:flash("b1") end

So the entire add for b1 would be.


touch[1]:add("b1",function() touch[1]:flash("b1") end,1,1,4,1,colors.red,colors.llime)
Edited on 19 May 2016 - 03:48 AM
DTM450 #8
Posted 19 May 2016 - 06:50 AM
Ok I sort of understand it better but I cant get the button to flash when I press it on either monitor

also changing :flash("b1") to :flash(event[2]) should do the same thing. using event[2] simplifies the code so you don't have to edit multiple parts of the code

revised code

--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_666"

local monSides = {"monitor_0","monitor_1"}
local touch = {}

for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end

--#Define your buttons here. As well as any functions they should run

--#This loop adds and draws buttons to all screens
for a,v in ipairs(monSides) do
  touch[a]:add("b1",function() touch[a]:flash("b1") end,1,1,4,3,colors.red,colors.lime)
  touch[a]:draw()
end

local runTable ={}

for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end

parallel.waitForAny(unpack(runTable))
Edited on 19 May 2016 - 04:53 AM
Dragon53535 #9
Posted 19 May 2016 - 09:32 AM
event[2] isn't defined at the time the function is written.

Revised2:


os.loadAPI("touchpoint")
local function runButtons(t)
  while true do

   local event = {t:handleEvents()}

   if event == "button_click" then

		 if t.buttonList[event[2]].func then

		   t.buttonList[event[2]].func(t,event[2])

		 end
   end
  end
end
--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_666"
local monSides = {"monitor_0","monitor_1"}
local touch = {}
for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end
--#Define your buttons here. As well as any functions they should run
--#This loop adds and draws buttons to all screens
for a,v in ipairs(monSides) do
  touch[a]:add("b1",function(self,param1) self:flash(param1) end,1,1,4,3,colors.red,colors.lime)
  touch[a]:draw()
end
local runTable ={}
for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end
parallel.waitForAny(unpack(runTable))

Changed the function call to pass the touchpoint table to your button function, as well as the button name. Changed the function declaration of your flash to accept the arguments and use them for flash.
Edited on 19 May 2016 - 07:33 AM
DTM450 #10
Posted 19 May 2016 - 10:57 AM
ok buttons aren't working they appear on the monitors and I've tried it using the term method but I cant press them for some reason
Spoiler

os.loadAPI("touchpoint")
local function runButtons(t)
  while true do

	local event = {t:handleEvents()}

	if event == "button_click" then

	  if t.buttonList[event[2]].func then

		t.buttonList[event[2]].func(t,event[2])

	  end
	end
  end
end

--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_666"
local monSides = {"monitor_0","monitor_1","term"}
local touch = {}
for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end

--#Define your buttons here. As well as any functions they should run
--#This loop adds and draws buttons to all screens

for a,v in ipairs(monSides) do
  touch[a]:add("b1",function(self,param1) self:flash(param1) end,1,1,4,3,colors.red,colors.lime)
  touch[a]:draw()
end

local runTable = {}

for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end

parallel.waitForAny(unpack(runTable))
Edited on 19 May 2016 - 11:39 PM
DTM450 #11
Posted 20 May 2016 - 01:41 AM
I updated the runButtons function to Revised2 because I forgot to do that last night but I still cant press the buttons and I have no idea why

also I'm running CC 1.75 but I dont think that should matter to much
Dragon53535 #12
Posted 20 May 2016 - 06:42 AM
Lol, I screwed up in the runButtons function.

On the if statement inside the runButtons function, add [1] after event.


if event[1] == "button_click" then
DTM450 #13
Posted 20 May 2016 - 06:47 AM
Lol, I screwed up in the runButtons function.

On the if statement inside the runButtons function, add [1] after event.


if event[1] == "button_click" then

XD okey dokey

*edit*

WOOH its working thanks a ton for your help


Heres the full functioning code in the spoiler for anyone who wants it. Also on pastebin

Spoiler

os.loadAPI("touchpoint")
local function runButtons(t)
  while true do

	local event = {t:handleEvents()}

	if event[1] == "button_click" then

	  if t.buttonList[event[2]].func then

		t.buttonList[event[2]].func(t,event[2])

	  end
	end
  end
end

--#Declare table monSides to contain either the side next to the computer, or the name of the monitor like "monitor_0"
local monSides = {"monitor_0","monitor_1","term"}
local touch = {}
for a,v in ipairs(monSides) do
  touch[a] = touchpoint.new(v)
end

--#Define your buttons here. As well as any functions they should run
--#This loop adds and draws buttons to all screens

for a,v in ipairs(monSides) do
  touch[a]:add("b1",function(self,param1) self:flash(param1) end,1,1,4,3,colors.red,colors.lime)
  touch[a]:add("b2",function(self,param1) self:flash(param1) end,5,1,9,3,colors.red,colors.lime)
  touch[a]:draw()
end

local runTable = {}

for a,v in ipairs(touch) do
  runTable[a] = function() return runButtons(touch[a]) end
end

parallel.waitForAny(unpack(runTable))
Edited on 20 May 2016 - 04:58 AM