Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Ustoff

Two screens and two gpu's

Question

I have to write a little test program to understand How can I write text on the second screen, and I can't understand, what I do wrong.

That's the code
 

local component = require ("component")
local term = require("term")
local gpu2a = component.get("7ba")
local gpu2 = component.proxy(gpu2a)
local scr2a = component.get("e6e")
local scr2 = component.proxy(scr2a)

gpu2.bind(scr2a)
term.write("Hello")
--gpu2.setBackground(0xe9ebe4)
gpu2.setResolution(36,5)
gpu2.setForeground(0xd98116)
gpu2.fill(1,1,36,5, "X")
os.sleep(0.9)

http://pastebin.com/RxVTmdp4

 

 

 

gpu2a - address of the second gpu card

scr2a - address of the second screen

So, when text printed , it printed on the first screen, again and again

If I use print("Hello") instead term.write, so text will be written on the first screen.

 

On the picture - right screen - scr2

 

What command I need to write in my code , that computer can write text on the second screen with binding on the second gpu

2016-11-27_02.35.29.png

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

term.read, print, io.write print text on the first screen, as you said, because it is a primary component. Primary components are choosen randomly when the computer boots up. Proxies of such components are returned when they are accessed like that: component.screen (or component.redstone, component.gpu, etc.).

The primary GPU is automatically bound to the primary screen. So you have to bind GPU to the second screen. The following example prints "Hello, world!" on the second screen.

local component = require("component")
local gpu = component.gpu  -- get the proxy of the primary GPU

local addr1 = component.get("ad03")  -- the address of the first screen
local addr2 = component.get("235f")  -- the address of the second screen

gpu.bind(addr2, false)  -- switch GPU to the second screen (false means that it won't clear the screen)

io.write("Hello, ")
print("world!")

gpu.bind(addr1, false)  -- switch GPU back to the first screen
Link to post
Share on other sites
  • 0

Ah, sorry, I didn't notice that you have two GPUs. Well, as you said, print functions work by default with the primary GPU component. An obvious way to do what you want is to access the two GPU components directly.

local component = require("component")

local gpu1 = component.proxy(component.get("897c"))  -- the first GPU
local gpu2 = component.proxy(component.get("b2bd"))  -- the second GPU

local screen1 = component.get("10da")  -- the first screen
local screen2 = component.get("e52a")  -- the second screen

-- bind the screens to the GPUs
gpu1.bind(screen1)
gpu2.bind(screen2)

-- use the `set` method to write text
gpu1.set(10, 1, "Hello, world!")
gpu2.set(1, 2, "Hello.")

-- the arguments are: x, y, text
-- if x is 1, the text will be printed at the beginning of line
-- if y is 1, the text will be printed at the top line

-- [1, 1] [2, 1] [3, 1] [4, 1] [5, 1]
-- [1, 2] [2, 2] [3, 2] [4, 2] [5, 2]
-- [1, 3] [2, 3] [3, 3] [4, 3] [5, 3]
-- [1, 4] [2, 4] [3, 4] [4, 4] [5, 4]
-- [1, 5] [2, 5] [3, 5] [4, 5] [5, 5]

...Or you can use an undocumented term library feature.

local component = require("component")
local term = require("term")

local gpu2 = component.get("a562")  -- the non-primary GPU
local screen2 = component.get("cd64")  -- the non-primary screen

local window = term.internal.open()  -- create a new window
window.gpu = gpu2  -- set window GPU...
window.screen = screen2  -- ...and screen

term.setViewport(nil, nil, nil, nil, nil, nil, window)  -- assign viewport values (resolution, cursor position, etc.)

-- now you can use term.drawText to write text on the second screen
--            text value       , should wrap, window
term.drawText("Hello, world!\n", false,       window)
term.drawText("Hello", false, window)
term.drawText(" again", false, window)

Much easier than the first way.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.