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

Refreshing Monitor With Changing Colours

Started by Kezaraux, 21 September 2013 - 09:02 AM
Kezaraux #1
Posted 21 September 2013 - 11:02 AM
Hi, I'm trying to make a program that prints out to a monitor the percentage of power that I have in my MFSU that is also right next to the computer. I've managed to get the code to where it will print the first part of what I want it to print in white, the change the colour based on what the percentage is and then print the number with a percent sign. The only issue I'm running into is the refresh part. I have this doing it in a loop, but after it prints and works for the first time it just stops working. If someone could help me fix this and make it work that would be great!

Code:
Spoiler

MFSU = peripheral.wrap("right")
m = peripheral.wrap("left")
power = MFSU.getStored()
cap = MFSU.getCapacity()

function round(num)
  local mult = 10 ^ 0
  return math.floor(num*mult)/mult
end

percent = round((power/cap)*100)

function chooseColor()
  if percent < 25 then
	col = 16384
	return col
  elseif percent < 50 then
	col = 2
	return col
  elseif percent < 101 then
	col = 8192
	return col
  end
  return col
end

function writeText(col,text)
  term.setTextColor(col)
  write(text)
end

while true do
  term.redirect(m)
  term.clear()
  m.setTextScale(2)
  term.setCursorPos(1,1)
  term.setTextColor(1)
  write ("Current Power Stored: ")
  writeText(chooseColor(),percent)
  print "%"
  term.restore()
  sleep(0.1)
end

Thanks to anyone who can help me out with this!
theoriginalbit #2
Posted 21 September 2013 - 11:26 AM
To refresh just put percent = round((power/cap)*100) inside of the loop (making sure to also put power and cap in there too) EDIT: Also you could make a function to do this too

However that being said there are some other things I've noticed you doing that you don't always need to do. Such as redirecting and restoring the terminal each loop (you can redirect once, then redirect back when a terminate event comes through), setting the text scale (only needs to be done once), and clearing the screen (just clear the line).
Kezaraux #3
Posted 21 September 2013 - 11:43 AM
Okay, I'll try this out, and thanks for letting me know about those others things. I just like to make sure that everything does what it's supposed to do every time and that's really the only way that I know how to do it.
Kezaraux #4
Posted 21 September 2013 - 11:46 AM
Well, that fixed it for me! Thanks! I also had to do the exact same thing in my Open Peripheral Terminal Glasses program… Guess I should remember to do this… XD
BigTwisty #5
Posted 22 September 2013 - 12:33 AM
The round function also seems a bit redundant. 10^0 is just 1. You are multiplying a number by 1, rounding it down, and the dividing it by 1. Why not just:

function round(num)
  return math.floor(num)
end

Although it would probably be more appropriate to simply use math.floor() inline.

Also, use Computercraft's constants to make your code more readable.



function chooseColor()
  if percent < 25 then
	return colors.red
  elseif percent < 50 then
	return colors.yellow
  end
  return colors.green
end
theoriginalbit #6
Posted 22 September 2013 - 05:26 AM
The round function also seems a bit redundant. 10^0 is just 1. You are multiplying a number by 1, rounding it down, and the dividing it by 1. Why not just:

function round(num)
  return math.floor(num)
end
Although it would probably be more appropriate to simply use math.floor() inline.
Because that's not rounding :P/> That's flooring.

local function round(num)
  return math.floor(num+0.5)
end
That's rounding

Although, this is a fully functional rounding, that supports preserving decimal places (as much as Lua can, it always removes trailing 0's)

local function round(num, idp)
  idp = 10^(idp or 0)
  if num >= 0 then
    return math.floor(num*idp+0.5)/idp
  end
  return math.ceil(num*idp-0.5)/idp
end