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

[1.31] Small Monitor API v1.2

Started by ihaveamac, 13 March 2012 - 04:32 AM
ihaveamac #1
Posted 13 March 2012 - 05:32 AM
While the API isn't really big, I think it would save me time rather than doing a bunch of "term.redirect(peripheral.wrap(side)) mycode() mycodeagain() term.restore() write("Did something")".
Currently I added normal write/print functions, flashing text and marquee left and right. More is coming.
Current functions:
SpoilerwriteM(side, text) - Write text on the monitor.
printM(side, text) - Print text on the monitor (it adds an extra line).
writePos(side, text, xpos, ypos) - Write text at a specific point.
slowPrint(side, text) - Slowly print text on the monitor.
slowPrintPos(side, text, xpos, ypos) - Slowly print text at a specific point.
clear(side) - Clear the monitor's screen.
flashyText(side, text, sleep, times) - Flash text on the monitor.
flashyTextPos(side, text, xpos, ypos, sleep, times) - Flash text at a specific point.
marqueeLeft(side, text, ypos, sleep) - Slide text from right to left.
marqueeRight(side, text, ypos, sleep) - Slide text from left to right.
The marquee functions now work properly thanks to Lyqyd.
goto(side) - Redirect to a monitor quickly (term.redirect(peripheral.wrap("right")) vs monitorapi.goto("right")). Use term.restore() to go back.
runprgm(side, prgm, args) - Run a program on the monitor.
run(side, func) - Run some lua code. For func, either put a function name or do "function() yourcodehere() end".
Source:
Spoiler
--Monitor API by ihaveamac
function getVersion()
	return 1.2
end
function writeM(side, text)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		write(text)
		term.restore()
	end
end
function printM(side, text)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		print(text)
		term.restore()
	end
end
function writePos(side, text, xpos, ypos)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		term.setCursorPos(xpos, ypos)
		write(text)
		term.restore()
	end
end
function slowPrint(side, text)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		textutils.slowPrint(text)
		term.restore()
	end
end
function slowPrintPos(side, text, xpos, ypos)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		term.setCursorPos(xpos, ypos)
		textutils.slowPrint(text)
		term.restore()
	end
end
function clear(side)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		term.clear()
		term.setCursorPos(1, 1)
		term.restore()
	end
end
function flashyText(side, text, sleep, times)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		for i = 1, times do
			local cx, cy = term.getCursorPos()
			write(text)
			term.setCursorPos(cx, cy)
			os.sleep(sleep)
			term.clearLine()
			os.sleep(sleep)
		end
		term.restore()
	end
end
function flashyTextPos(side, text, xpos, ypos, sleep, times)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		for i = 1, times do
			term.setCursorPos(xpos, ypos)
			write(text)
			term.setCursorPos(xpos, ypos)
			os.sleep(sleep)
			term.clearLine()
			os.sleep(sleep)
		end
		term.restore()
	end
end
function marqueeLeft(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		local sx, sy = term.getSize()
		text = string.rep(" ", sx - 1)..text
		for i=1,string.len(text) do
			term.clearLine()
			displayString = string.sub(text, i, math.min(string.len(text), (i + sx - 2)))
			term.setCursorPos(1, ypos)
			write(displayString)
			os.sleep(sleep)
		end
		term.clearLine()
		term.restore()
	end
end
function marqueeRight(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		local sx, sy = term.getSize()
		text = string.rep(" ", sx - 1)..text..string.rep(" ", sx - 1)
		for i = 1,string.len(text) - (sx - 1) do
			term.clearLine()
			displayString = string.sub(text, -math.min(string.len(text), i + sx - 2), -i)
			term.setCursorPos(1, ypos)
			write(displayString)
			os.sleep(sleep)
		end
		term.clearLine()
		term.restore()
	end
end
--thanks to Lyqyd from the ComputerCraft fourms for fixing the marquee code. :mellow:/>/>
function goto(side)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
	end
end
function runprgm(side, prgm, args)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		shell.run(prgm, args)
		term.restore()
	end
end
function run(side, func)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		func()
		term.restore()
	end
end
Download: here (it may save as a .txt)
Tron #2
Posted 13 March 2012 - 06:10 PM
I keep getting
lua:43: attempt to call nil
when I try to Use this?
ihaveamac #3
Posted 13 March 2012 - 10:43 PM
I keep getting
lua:43: attempt to call nil
when I try to Use this?
You're calling a function that doesn't exist. It would be monitorapi.writeM(side, text), not write since it was calling itself and not the OS function write(). Unless someone can think of a workaround, some of them will have an M after that for Monitor.
Tron #4
Posted 14 March 2012 - 12:14 AM
Ok thanks, I forgot the monitorapi part :/
mrgreaper #5
Posted 15 March 2012 - 03:31 AM
this looks majorly useful, thank you
bobster71 #6
Posted 15 March 2012 - 10:09 PM
I agree with mrgreaper. Very nice. Very useful. Thanks. Saves me the trouble of building something like it.
mrgreaper #7
Posted 16 March 2012 - 04:33 PM
bit of an error


--parallel.waitForAny(one,two,three,four,five)
function one()
monitorapi.marqueeLeft("top", "We have a facebook group and a webpage http://max-xtreme-server.enjin.com/ the facebook group will get you a faster response to any quaries", 5, 0.1)
end
function two()
monitorapi.marqueeLeft("top", "We run multiple plugins : Ask8ball Chestshop Citizens CommandBook dynmap dynmap-Herochat ecoCreature Falsebook(all modules but cart) Herochat iChat iConomy Lockette mcbans mcMMO MobCatcher Mobloot Myworlds PermissionsBukkit Residence ServerEvents Vault WorldEdit Worldguard", 4, 0.1)
end
function three()
monitorapi.marqueeLeft("top", "we also run multiple mods : IC2 Buildcraft animalbikes forestry morepistons computercraft", 3, 0.1)
end
function four()
monitorapi.marqueeLeft("top", "testing2..........", 2, 0.1)
end
function five()
monitorapi.marqueeLeft("top", "testing1..........", 1, 0.1)
end
one()
two()
three()
four()
five()

the long sentences dont appear it simply skips them (ignore the remed out parallel line i wanted to have multiple lines scrolling when i first made it but that wont work due to limitation of lua)

also when the testing2…… and 1 line scroll they start on the right as the whole word and disapear when the first letter hits the left side, they dont scroll like a marque should if that makes sense

im guessing its an error with the way i called the api?
ihaveamac #8
Posted 16 March 2012 - 06:30 PM
bit of an error


--parallel.waitForAny(one,two,three,four,five)
function one()
monitorapi.marqueeLeft("top", "We have a facebook group and a webpage http://max-xtreme-server.enjin.com/ the facebook group will get you a faster response to any quaries", 5, 0.1)
end
function two()
monitorapi.marqueeLeft("top", "We run multiple plugins : Ask8ball Chestshop Citizens CommandBook dynmap dynmap-Herochat ecoCreature Falsebook(all modules but cart) Herochat iChat iConomy Lockette mcbans mcMMO MobCatcher Mobloot Myworlds PermissionsBukkit Residence ServerEvents Vault WorldEdit Worldguard", 4, 0.1)
end
function three()
monitorapi.marqueeLeft("top", "we also run multiple mods : IC2 Buildcraft animalbikes forestry morepistons computercraft", 3, 0.1)
end
function four()
monitorapi.marqueeLeft("top", "testing2..........", 2, 0.1)
end
function five()
monitorapi.marqueeLeft("top", "testing1..........", 1, 0.1)
end
one()
two()
three()
four()
five()

the long sentences dont appear it simply skips them (ignore the remed out parallel line i wanted to have multiple lines scrolling when i first made it but that wont work due to limitation of lua)

also when the testing2…… and 1 line scroll they start on the right as the whole word and disapear when the first letter hits the left side, they dont scroll like a marque should if that makes sense

im guessing its an error with the way i called the api?
I had trouble trying to make it like a real one, where it would be "testing" then "esting" then "sting" and so on. I tried to make it and I didn't get it working. For now, you can make the monitor longer and cover the sides with blocks like RedPower cover plates (when the mod is updated). If anyone knows how to make it like a real marquee please tell me. I'll keep trying to make it work properly. Sorry, that's how it is right now. :D/>/>

The basic functionality of the text moving from one side to another works though.

Edit: You could also separate the long text into several lines.
ihaveamac #9
Posted 17 March 2012 - 01:52 AM
Version 1.1 now here, added monitorapi.goto(side) (replacement for term.redirect(peripheral.wrap("side")) and monitorapi.runprgm(side, prgm, args). :D/>/>
mrgreaper #10
Posted 17 March 2012 - 10:15 PM
bit of an error



the long sentences dont appear it simply skips them (ignore the remed out parallel line i wanted to have multiple lines scrolling when i first made it but that wont work due to limitation of lua)

also when the testing2…… and 1 line scroll they start on the right as the whole word and disapear when the first letter hits the left side, they dont scroll like a marque should if that makes sense

im guessing its an error with the way i called the api?
I had trouble trying to make it like a real one, where it would be "testing" then "esting" then "sting" and so on. I tried to make it and I didn't get it working. For now, you can make the monitor longer and cover the sides with blocks like RedPower cover plates (when the mod is updated). If anyone knows how to make it like a real marquee please tell me. I'll keep trying to make it work properly. Sorry, that's how it is right now. :D/>/>

The basic functionality of the text moving from one side to another works though.

Edit: You could also separate the long text into several lines.

how did they do it with the space ship animation on the hidden starwars film ? that used text scrolling
(if you dont know what im talking about build a big monitier but a computer under it then type cd rom cd programs cd secret and run monitor top alongtimeago)

mind you i guess they knew exactly what lines they were having so it could be just a load of write commands

shame we cant convert a line of text into seperate 1 char per variable and then write the variables one by one and scroll that way
ihaveamac #11
Posted 18 March 2012 - 12:51 AM
bit of an error



the long sentences dont appear it simply skips them (ignore the remed out parallel line i wanted to have multiple lines scrolling when i first made it but that wont work due to limitation of lua)

also when the testing2…… and 1 line scroll they start on the right as the whole word and disapear when the first letter hits the left side, they dont scroll like a marque should if that makes sense

im guessing its an error with the way i called the api?
I had trouble trying to make it like a real one, where it would be "testing" then "esting" then "sting" and so on. I tried to make it and I didn't get it working. For now, you can make the monitor longer and cover the sides with blocks like RedPower cover plates (when the mod is updated). If anyone knows how to make it like a real marquee please tell me. I'll keep trying to make it work properly. Sorry, that's how it is right now. :D/>/>

The basic functionality of the text moving from one side to another works though.

Edit: You could also separate the long text into several lines.

how did they do it with the space ship animation on the hidden starwars film ? that used text scrolling
(if you dont know what im talking about build a big monitier but a computer under it then type cd rom cd programs cd secret and run monitor top alongtimeago)

mind you i guess they knew exactly what lines they were having so it could be just a load of write commands

shame we cant convert a line of text into seperate 1 char per variable and then write the variables one by one and scroll that way
In that program, it does use a bunch of print lines, so it does something like print("test") then print("est") then print("st") and so on. The code for the marquee function in this doesn't do that, it does the simple function of moving text from one side to another and doesn't use a bunch of print lines.
Popinman322 #12
Posted 28 March 2012 - 04:11 PM

function loopedMarqueeRight(side, text, ypos, sleep)
	if not peripheral.isPresent(side) then return end
	local control = true
	local function halt() control = false; end
	coroutine.wrap(function()
		while control do
			local screen = peripheral.wrap(side)
			for i = 1, text:len() do
				screen.setCursorPos(1,ypos)
				screen.clearLine()
				
				screen.write(text:sub(i) .. text:sub(1,i-1))
				
				os.sleep(sleep)
				screen.clearLine()
			end
		end
	end)()
	return halt
end

local haltLoop = loopedMarqueeRight("right", "Moocowsaysmoo! ", 1, 0.1)
sleep(10)
haltLoop()

Could anyone test this code out for me? I don't have access to a ComputerCraft MC client atm.
ihaveamac #13
Posted 08 April 2012 - 01:35 AM

function loopedMarqueeRight(side, text, ypos, sleep)
	if not peripheral.isPresent(side) then return end
	local control = true
	local function halt() control = false; end
	coroutine.wrap(function()
		while control do
			local screen = peripheral.wrap(side)
			for i = 1, text:len() do
				screen.setCursorPos(1,ypos)
				screen.clearLine()
				
				screen.write(text:sub(i) .. text:sub(1,i-1))
				
				os.sleep(sleep)
				screen.clearLine()
			end
		end
	end)()
	return halt
end

local haltLoop = loopedMarqueeRight("right", "Moocowsaysmoo! ", 1, 0.1)
sleep(10)
haltLoop()

Could anyone test this code out for me? I don't have access to a ComputerCraft MC client atm.
I never got CC 1.32 since I never paid attention. I didn't really use mods for 1.2.4 other than TMI and a few others. I'll try to get CC for 1.2.5 when it comes out and try it.
ihaveamac #14
Posted 17 April 2012 - 03:16 AM

function loopedMarqueeRight(side, text, ypos, sleep)
	if not peripheral.isPresent(side) then return end
	local control = true
	local function halt() control = false; end
	coroutine.wrap(function()
		while control do
			local screen = peripheral.wrap(side)
			for i = 1, text:len() do
				screen.setCursorPos(1,ypos)
				screen.clearLine()
				
				screen.write(text:sub(i) .. text:sub(1,i-1))
				
				os.sleep(sleep)
				screen.clearLine()
			end
		end
	end)()
	return halt
end

local haltLoop = loopedMarqueeRight("right", "Moocowsaysmoo! ", 1, 0.1)
sleep(10)
haltLoop()

Could anyone test this code out for me? I don't have access to a ComputerCraft MC client atm.
Doesn't work. It just printed the text but didn't scroll.

Yes, CC1.32 works with 1.2.5.
iRiky #15
Posted 20 April 2012 - 04:04 PM
i don't reach to use runprgm this is a part of code
:
runprgm("Top", "time")

how i can run the program ?
scuse me i'm new about computercraft and non english xD
ihaveamac #16
Posted 23 April 2012 - 11:45 PM
i don't reach to use runprgm this is a part of code
:
runprgm("Top", "time")

how i can run the program ?
scuse me i'm new about computercraft and non english xD
No, it's fine. I made this to make it easy for new lua programmers to do stuff with monitors.

Did you save the file in the apis directory? If you did, you need to do monitorapi.runprgm("top", "time") or else it doesn't know what API to use.
MathManiac #17
Posted 19 May 2012 - 04:37 AM
:3 Oh, monitors! I should make monitor programs!
Lyqyd #18
Posted 19 May 2012 - 06:07 AM
These may marquee properly:


function marqueeLeft(side, text, ypos, sleep)
    if peripheral.isPresent(side) then
	    term.redirect(peripheral.wrap(side))
	    local sx, sy = term.getSize()
	    text = string.rep(" ", sx - 1)..text
	    for i=1,string.len(text) do
		    term.clearLine()
		    displayString = string.sub(text, i, math.min(string.len(text), i + sx - 1))
		    write(displayString)
		    os.sleep(sleep)
	    end
	    term.clearLine()
	    term.restore()
    end
end
function marqueeRight(side, text, ypos, sleep)
    if peripheral.isPresent(side) then
	    term.redirect(peripheral.wrap(side))
	    local sx, sy = term.getSize()
	    text = string.rep(" ", sx - 1)..text
	    for i = 1,string.len(text) do
		    term.clearLine()
		    displayString = string.sub(text, -i, math.min(string.len(text), i + sx - 1))
		    write(displayString)
		    os.sleep(sleep)
	    end
	    term.clearLine()
	    term.restore()
    end
end
ComputerCraftFan11 #19
Posted 19 May 2012 - 09:14 AM
You should add lua.

function mFunction(side, functionname)
  term.redirect(peripheral.wrap(side))
  functionname()
  term.restore()
end

function test()
  print "Hello world!"
end

mFunction("left", test)
ihaveamac #20
Posted 15 June 2012 - 05:27 AM
Hey, sorry for my inactivity! I was busy on school and we were on a week-long road trip, but I'm back home and ready to work.
These may marquee properly:


function marqueeLeft(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		local sx, sy = term.getSize()
		text = string.rep(" ", sx - 1)..text
		for i=1,string.len(text) do
			term.clearLine()
			displayString = string.sub(text, i, math.min(string.len(text), i + sx - 1))
			write(displayString)
			os.sleep(sleep)
		end
		term.clearLine()
		term.restore()
	end
end
function marqueeRight(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
		term.redirect(peripheral.wrap(side))
		local sx, sy = term.getSize()
		text = string.rep(" ", sx - 1)..text
		for i = 1,string.len(text) do
			term.clearLine()
			displayString = string.sub(text, -i, math.min(string.len(text), i + sx - 1))
			write(displayString)
			os.sleep(sleep)
		end
		term.clearLine()
		term.restore()
	end
end
Thanks, I'll try this out when I can. :(/>/> I've never heard of math.min() though.
You should add lua.

function mFunction(side, functionname)
  term.redirect(peripheral.wrap(side))
  functionname()
  term.restore()
end

function test()
  print "Hello world!"
end

mFunction("left", test)
I'll try this out. It could be "monitorapi.lua("left", function() print("HELLO WORLD") end)". (yes, that should work)
Lyqyd #21
Posted 15 June 2012 - 06:24 PM
Give me a shout if either of those don't work and I'll see if I can fix them up for you.
Xemiru #22
Posted 23 June 2012 - 10:29 AM
For the marquee, try

x,y = term.getSize()
function marqueeRight(text,ypos)
  val = 1
  while true do
    term.clear()
    term.setCursorPos(val,ypos)
    write(text)
    if val == x then
	  val = 1
    else
	  val = val + 1
    end
    sleep(0.5)
  end
end

It failed when I tested it, but maybe you can build something off of it.
ihaveamac #23
Posted 25 June 2012 - 09:51 PM
Give me a shout if either of those don't work and I'll see if I can fix them up for you.
It didn't work properly, the text was going up. It was moving to the side I believe though. Try it yourself.
Lyqyd #24
Posted 26 June 2012 - 01:57 AM

function marqueeLeft(side, text, ypos, sleep)
    if peripheral.isPresent(side) then
		    term.redirect(peripheral.wrap(side))
		    local sx, sy = term.getSize()
		    text = string.rep(" ", sx - 1)..text
		    for i=1,string.len(text) do
				    term.clearLine()
				    displayString = string.sub(text, i, math.min(string.len(text), (i + sx - 2)))
				    term.setCursorPos(1, ypos)
				    write(displayString)
				    os.sleep(sleep)
		    end
		    term.clearLine()
		    term.restore()
    end
end
function marqueeRight(side, text, ypos, sleep)
    if peripheral.isPresent(side) then
		    term.redirect(peripheral.wrap(side))
		    local sx, sy = term.getSize()
		    text = string.rep(" ", sx - 1)..text..string.rep(" ", sx - 1)
		    for i = 1,string.len(text) - (sx - 1) do
				    term.clearLine()
				    displayString = string.sub(text, -math.min(string.len(text), i + sx - 2), -i)
				    term.setCursorPos(1, ypos)
				    write(displayString)
				    os.sleep(sleep)
		    end
		    term.clearLine()
		    term.restore()
    end
end

Fixed versions. Tested them and they seem to work fine here on a variety of monitor sizes. Give these guys a whirl.
ihaveamac #25
Posted 30 June 2012 - 11:03 PM

function marqueeLeft(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
			term.redirect(peripheral.wrap(side))
			local sx, sy = term.getSize()
			text = string.rep(" ", sx - 1)..text
			for i=1,string.len(text) do
					term.clearLine()
					displayString = string.sub(text, i, math.min(string.len(text), (i + sx - 2)))
					term.setCursorPos(1, ypos)
					write(displayString)
					os.sleep(sleep)
			end
			term.clearLine()
			term.restore()
	end
end
function marqueeRight(side, text, ypos, sleep)
	if peripheral.isPresent(side) then
			term.redirect(peripheral.wrap(side))
			local sx, sy = term.getSize()
			text = string.rep(" ", sx - 1)..text..string.rep(" ", sx - 1)
			for i = 1,string.len(text) - (sx - 1) do
					term.clearLine()
					displayString = string.sub(text, -math.min(string.len(text), i + sx - 2), -i)
					term.setCursorPos(1, ypos)
					write(displayString)
					os.sleep(sleep)
			end
			term.clearLine()
			term.restore()
	end
end

Fixed versions. Tested them and they seem to work fine here on a variety of monitor sizes. Give these guys a whirl.
Thanks, it worked! I'll add it for the next version. :P/>/>
ihaveamac #26
Posted 16 July 2012 - 12:22 AM
Updated to v1.2, fixed marquee functions and added monitorapi.run(side, func).
FuzzyPurp #27
Posted 11 August 2012 - 11:55 AM
Changed title for you, next time just edit your topic, then choose Full Editor.
Jamboozlez #28
Posted 01 May 2013 - 03:33 PM
Could you please create a function for:

m.setTextScale(size) -- size can be any number from 1 to 5
--example:
monitorapi.setTextScale(side, size) -- make it return an error when you use a size not within 1-5
Thanks ;)/>
Lyqyd #29
Posted 01 May 2013 - 05:08 PM
That function is a pretty trivial addition.


function setTextScale(side, size)
  return peripheral.call(side, "setTextScale", size)
end

Might as well just use the peripheral call. Either way, this is pretty old. Locked.