214 posts
Posted 27 September 2012 - 08:05 PM
ok, so i got this clock code i thought up. not sure if it works but thats not what i need help with. i need some code that could auto resize something, so it fits a montior and prints it center. if this is not possible i would like something that could change the size of the output text on a monitor.
here is the code for the clock
mon = perpheral.wrap("left")
while true do
local time = os.time()
mon.write(time)
term.clear()
term.setCursorPos(1,1)
end
i surpose i could change the setcursor pos to something different, but thats just annoying to figure out.
could someone maybe tell me how much i would have to change it by to center it on a 1x1 monitor? then i could just work out how many it would be for a different size.
all help appreciated. thanks
-Cheeky
3790 posts
Location
Lincoln, Nebraska
Posted 27 September 2012 - 08:12 PM
If you want auto resizing, just use this: x,y = mon.getSize() to get the size of the monitor. YOu can then use if/then statements to use mon.setTextScale() to change the scale of the text.
If you want to auto center your text, do this:
local x,y = mon.getSize()
local mY = y/2 --divides y in half
local time = os.time()
local b = string.len(textutils.format(time,false))/2
local mX = x-b
mon.setCursorPos(mX,mY) --using calibrated x,y positions
mon.write(textutils.format(time,false)) --now prints time on monitor, centered
That code above assumes that the textScale is at default 1. You would have to adjust accordingly for larger text.
214 posts
Posted 27 September 2012 - 09:26 PM
If you want auto resizing, just use this: x,y = mon.getSize() to get the size of the monitor. YOu can then use if/then statements to use mon.setTextScale() to change the scale of the text. If you want to auto center your text, do this:
local x,y = mon.getSize() local mY = y/2 --divides y in half local time = os.time() local b = string.len(textutils.format(time,false))/2 local mX = x-b mon.setCursorPos(mX,mY) --using calibrated x,y positions mon.write(textutils.format(time,false)) --now prints time on monitor, centered
That code above assumes that the textScale is at default 1. You would have to adjust accordingly for larger text.
i get clock2:1: attempt to index ? (a nil value)
im not really sure what this means ?
3790 posts
Location
Lincoln, Nebraska
Posted 27 September 2012 - 09:51 PM
I deliberately did not post a fully completed code, Cheeky. The error you are getting is simply saying that there is an error on line one, where it is trying to call back to an index that does not exist yet. Since line one contains
local x,y = mon.getSize()
You can see that it's trying to find where mon is referenced. Any time you use peripheral.wrap to add a peripheral, you need to define it before the code you are using it in. So for example, you would just add
local mon = peripheral.wrap("side")
to the top of your code, to "index" mon to the peripheral specified. You can then call back to that index by using mon.functionName(), and it would use mon specifically.
Take a look at the Peripheral API in the wiki for more information about using peripherals. There are some greate examples.
214 posts
Posted 27 September 2012 - 10:44 PM
I deliberately did not post a fully completed code, Cheeky. The error you are getting is simply saying that there is an error on line one, where it is trying to call back to an index that does not exist yet. Since line one contains
local x,y = mon.getSize()
You can see that it's trying to find where mon is referenced. Any time you use peripheral.wrap to add a peripheral, you need to define it before the code you are using it in. So for example, you would just add
local mon = peripheral.wrap("side")
to the top of your code, to "index" mon to the peripheral specified. You can then call back to that index by using mon.functionName(), and it would use mon specifically.
Take a look at the Peripheral API in the wiki for more information about using peripherals. There are some greate examples.
ok so using the wiki and messing around a bit i have come up with this. it works but there is just a couple of problems.
first, it doesent update. ive tried term.clear() but that doesent work. second its not centerd its on the far right of the monitor.
please can you help me out with those?
heres my code
local mon = peripheral.wrap("right")
local x,y = mon.getSize()
mon.setTextScale(3)
local mY = y/2 --divides y in half
local time = os.time()
local b = string.len(textutils.formatTime(time, false))/2
local mX = x-b
mon.setCursorPos(mX,mY) --using calibrated x,y positions
mon.write(textutils.formatTime(time, false))
thanks -Cheeky
3790 posts
Location
Lincoln, Nebraska
Posted 27 September 2012 - 10:48 PM
Whoops. The centered on the right was my mistake in math. This line:
local mX = x-b
needs to be:
local mX = (x/2)-b
For updating, you should put the code in a while loop. Just do:
while true do
--code here
sleep(.5)
end
And you would want to use mon.clear() instead of term.clear(), since we are using the monitor now.
214 posts
Posted 27 September 2012 - 11:15 PM
Whoops. The centered on the right was my mistake in math. This line:
local mX = x-b
needs to be:
local mX = (x/2)-b
For updating, you should put the code in a while loop. Just do:
while true do
--code here
sleep(.5)
end
And you would want to use mon.clear() instead of term.clear(), since we are using the monitor now.
ok, so now i got this code. it displays the time in the right place. just doesent update. i put it in a while loop but still wont. could you take a look for me. note i tried this with a 1 second delay and 0.5
local mon = peripheral.wrap("right")
local x,y = mon.getSize()
mon.setTextScale(3)
local mY = y/2 --divides y in half
local time = os.time()
local b = string.len(textutils.formatTime(time, false))/2
local mX = (x/2)-b
while true do
mon.setCursorPos(mX,mY) --using calibrated x,y positions
mon.write(textutils.formatTime(time, false))
sleep(0.5)
mon.clear()
end
3790 posts
Location
Lincoln, Nebraska
Posted 27 September 2012 - 11:27 PM
It actually is updating, but since time=os.time() is not within the loop, it sets time, but never changes that value. Put the while loop three lines higher to include that line.
214 posts
Posted 27 September 2012 - 11:30 PM
It actually is updating, but since time=os.time() is not within the loop, it sets time, but never changes that value. Put the while loop three lines higher to include that line.
ok thanks for your help. i just thought it was returning false as the loop was while true. anyway thanks :P/>/>
-Cheeky