Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Sign in to follow this  

How do I get a print function working for a drone BIOS?


I've been writing up a bios for my drone with one major snag: text output will be a nightmare. I can't think of a "clean" way to implement a way of searching for a new line, since every space on the monitor technically has the observed text value of "" (when I added an if function to see if gpu.get returned "", it pulled through and outputted gpu.set(1,2). the previously non-occupied line had a value of "" rather than nil. also, type() is saying the type of the unoccupied space was a string).


First I took a peek at the term API, since I couldn't figure out where print was defined in openOS.


function term.write(value,wrap)
  local stdout = io.output()
  local stream = stdout and stdout.stream
  local previous_wrap = stream.wrap
  stream.wrap = wrap == nil and true or wrap
  stream.wrap = previous_wrap


Surprise surprise, it relies on the IO library, which is a part of the filesystem library.  Naturally, I took a look at the IO library's output function.

function io.input(file)
  return io.stream(0, file, 'r')

function io.output(file)
  return io.stream(1, file,'w')

function io.error(file)
  return io.stream(2, file,'w')

It simply returns io.stream, so that's where I look next.


function io.stream(fd,file,mode)
  assert(fd>=0,'fd must be >= 0. 0 is input, 1 is stdout, 2 is stderr')
  if file then
    if type(file) == "string" then
      local result, reason = io.open(file, mode)
      if not result then
        error(reason, 2)
      file = result
    elseif not io.type(file) then
      error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
    require("process").info().data.io[fd] = file
  return require("process").info().data.io[fd]

This is what confuses me.  io.output is passing file. Not a string, not a file path, not anything. Just file. What is this 'file' and what does it have to do with text output? Why are so many basic lua functionalities missing without openOS?

Share this post

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

"file" is a buffer, just like a buffered input file but not actually a file. Also BIOS is designed to be absolute minimum. A lot of things have been taken out as a result. How would the bios know what screen to print to?

Share this post

Link to post
Share on other sites
  • 0

FYI, dunno if you already know this but, the print function is just a wrapper for io.stdout:write with a concatenated newline char. i.e. 

function print( ... )
  local out = ""
  for i = 1, #arg do
    out = out .. type(arg[i]) == 'string' and arg[i] or tostring(arg[i])
  io.stdout:write( out .. '\n' )

... Seems the latest definition of print in OpenOS uses  a buffer like what @TYKUHN2 said. Here is the real def in the latest version of OpenOS.

-- Other code above in file.
-- found @ "/boot/00_base.lua"
function print(...)
  local args = table.pack(...)
  local stdout = io.stdout
  for i = 1, args.n do
    local arg = tostring(args[i])
    if i > 1 then
      arg = "\t" .. arg


Share this post

Link to post
Share on other sites
  • 0

the real "how to print to screen" work is done in a kernel level library called tty. These are NOT public methods because the method names and parameters will change without warning as I see fit to optimize or refactor code. But, if you want to know where "print" EVENTUALLY gets to actually rendering text on the screen, look here: https://github.com/payonel/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua#L319

The boot code has a far simpler, dumbed down version (for example, it doesn't wrap, it doesn't have a cursor position): https://github.com/payonel/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua#L45

Share this post

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.

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.

Sign in to follow this  

  • Create New...

Important Information

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