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

[LUA] Problems with a boot loader...

Started by Oct125, 07 January 2013 - 09:34 AM
Oct125 #1
Posted 07 January 2013 - 10:34 AM
I'm creating a boot loader for my OS, but i'm having problems… He have a countdown, but when the count Reaches 0, the boot loader doesn't stop… If i press any key that i'm using in the boot loader (arrows), the boot loader restart and reboot the os… So i have to stop him! And i don't know how do this… And when i select another item, the boot loader start a new count, and don't stop the old…

Here's the code:
Spoiler

--Made by Oct125 for Util OS
selection = 1
term.clear()
term.setCursorPos(1,1)
print("				   Boot Loader				   ")
print("												 ")
print("	+---------------------------------------+ ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	|												  | ")
print("	+---------------------------------------+ ")
print("	|												  | ")
print("	|												  | ")
print("	+---------------------------------------+ ")
function timer()
term.setCursorPos(6,14)
print("Starting in: 4")
sleep(1)
term.setCursorPos(6,14)
print("Starting in: 3")
sleep(1)
term.setCursorPos(6,14)
print("Starting in: 2")
sleep(1)
term.setCursorPos(6,14)
print("Starting in: 1")
sleep(1)
term.setCursorPos(6,14)
term.clearLine(6,14)
print("BOOTING...")
if selection == 1 then
shell.run(".Util/os") -- boot the OS
return
else
term.clear()
term.setCursorPos(1,1)
shell.run("shell") -- boot default shell
return
end
end
function text()
if selection == 1 then -- Util OS
term.setCursorPos(6,4)
term.setBackgroundColor(colors.lightGray)
write("									   ")
term.setCursorPos(6,4)
write("Util OS 0.1 BETA")
term.setCursorPos(6,5)
term.setBackgroundColor(colors.black)
write(os.version())
else -- CraftOS
term.setCursorPos(6,4)
term.setBackgroundColor(colors.black)
write("									   ")
term.setCursorPos(6,4)
write("Util OS 0.1 BETA")
term.setCursorPos(6,5)
term.setBackgroundColor(colors.lightGray)
write("									   ")
term.setCursorPos(6,5)
write(os.version())
term.setBackgroundColor(colors.black)
end
parallel.waitForAny(
function()
local sEvent, param = os.pullEvent("key")
		if sEvent == "key" then
			if param == 208 then
	selection = 2
	text()
   elseif param == 200 then
	   selection = 1
				tempo = 4
	text()
	else
	text()
   end
		end
end,
function()
timer()
end
)
end
text()

Some one can help me?
Goof #2
Posted 07 January 2013 - 11:32 AM
Your term.cleaLine(6,14) should be
term.clearLine() – Without Any arguments.
Edit. Dont listen to this!

Your term.clearLine(6,14) is throwing an error, which shuts down the system, before you should know it.
and you should call your parallel.waitForAny() after the two functions :
Like :


function() – make a name for the function. Like:


function start1()
--- Do stuff

end
function time()
timer()
end
parallel.waitForAny(start1, time)

I think that would solve the problem :P/>/>


:D/>/>
Orwell #3
Posted 07 January 2013 - 11:40 AM
Your term.cleaLine(6,14) should be
term.clearLine() – Without Any arguments.

Your term.clearLine(6,14) is throwing an error, which shuts down the system, before you should know it.
and you should call your parallel.waitForAny() after the two functions :
Like :


function() – make a name for the function. Like:


function start1()
--- Do stuff

end
function time()
timer()
end
parallel.waitForAny(start1, time)

I think that would solve the problem :P/>


:D/>
You're wrong twice. You can perfectly call term.clearLine(6,14), it will just ignore the parameters. You can also perfectly define the functions within the call to parallel.waitForAny. Thus the problem is something different. If no one finds it soon, I will take a look myself.
Oct125 #4
Posted 07 January 2013 - 12:18 PM
Your term.cleaLine(6,14) should be
term.clearLine() – Without Any arguments.

Your term.clearLine(6,14) is throwing an error, which shuts down the system, before you should know it.
and you should call your parallel.waitForAny() after the two functions :
Like :


function() – make a name for the function. Like:


function start1()
--- Do stuff

end
function time()
timer()
end
parallel.waitForAny(start1, time)

I think that would solve the problem :P/>


:D/>
Well… Thanks anyway…
Your term.cleaLine(6,14) should be
term.clearLine() – Without Any arguments.

Your term.clearLine(6,14) is throwing an error, which shuts down the system, before you should know it.
and you should call your parallel.waitForAny() after the two functions :
Like :


function() – make a name for the function. Like:


function start1()
--- Do stuff

end
function time()
timer()
end
parallel.waitForAny(start1, time)

I think that would solve the problem :P/>


:D/>
You're wrong twice. You can perfectly call term.clearLine(6,14), it will just ignore the parameters. You can also perfectly define the functions within the call to parallel.waitForAny. Thus the problem is something different. If no one finds it soon, I will take a look myself.

Ok!
Goof #5
Posted 07 January 2013 - 07:35 PM
Oh then im really sorry, for posting wrong info…
i didnt really know that it would ignore the parametres.
And the parallel: i just think that it would be easier to just make two words in the paratheses.. ::-/
sorry.
remiX #6
Posted 07 January 2013 - 09:23 PM
And the parallel: i just think that it would be easier to just make two words in the paratheses.. ::-/
sorry.

It is but it's really up to the coder to do what he wants :)/>

I'm busy looking it at it now and I see you call text() within text() within the parallel.waitForAny(). Will that not give a stack overflow at times, or maybe the time will run out in time..

edit: I think the problem does lie within the parallel function, where you call text() all the time. Weird that the time keeps changing seconds, maybe you should have used a for loop for the timer function.

Also, if you don't know about Tables, you should learn about them and use them a lot, they are really useful.

Check this out:

Spoiler

-- Made by remiX for Util OS
selection = 1

t_Options = {
    { text = "Util OS 0.1 BETA", handler = function() shell.run(".Util/os") end },
    { text = os.version(), handler = function() term.setBackgroundColour(colours.black) term.setTextColour(colours.white) term.setCursorPos(1, 1) term.clear() end }
}

function printMain()
    term.setBackgroundColour(colours.black)
    term.setTextColour(colours.white)
    term.clear()
    term.setCursorPos(1, 2)
    print(" +------------------Boot Loader------------------+ ")
    print(" |											   | ")
    print(" +-----------------------------------------------+ ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" +-----------------------------------------------+ ")
    print(" |											   | ")
    print(" |											   | ")
    print(" |											   | ")
    print(" +-----------------------------------------------+ ")
end

function timer()
    for i = 5, 1, -1 do
        term.setBackgroundColour(colours.black)
        term.setCursorPos(6, 16)
        write("Booting in " .. i)
        sleep(1)
    end
    return
end

function text()
    while true do
        for op = 1, #t_Options do
            term.setCursorPos(7, op + 5)
            if op == selection then term.setBackgroundColour(colours.lightGrey) else term.setBackgroundColour(colours.black) end
            write(t_Options[op].text)
        end
        local _, key = os.pullEvent("key")
        if key == keys.down and selection < #t_Options then
            -- Down
            selection = selection + 1
        elseif key == keys.up and selection > 1 then
            -- Up
            selection = selection - 1
        elseif key == keys.enter then
            t_Options[selection].handler()
            return
        end
    end
end

printMain()
parallel.waitForAny(timer, text)

-- This is here and will only occur if the timer runs out
-- Because of the sleep(2) (so you can read what it is booting)
-- I had to put it here or else you could still use up and down keys
-- while it's booting
printMain()
term.setCursorPos(7, 5)
print("BOOTING...")
term.setCursorPos(7, 6)
write(t_Options[selection].text)
sleep(2)
t_Options[selection].handler()
Oct125 #7
Posted 08 January 2013 - 05:52 AM
Oh then im really sorry, for posting wrong info…
i didnt really know that it would ignore the parametres.
And the parallel: i just think that it would be easier to just make two words in the paratheses.. ::-/
sorry.
It's ok! You just trying to help ;)/>
And the parallel: i just think that it would be easier to just make two words in the paratheses.. ::-/
sorry.

It is but it's really up to the coder to do what he wants :)/>

I'm busy looking it at it now and I see you call text() within text() within the parallel.waitForAny(). Will that not give a stack overflow at times, or maybe the time will run out in time..

edit: I think the problem does lie within the parallel function, where you call text() all the time. Weird that the time keeps changing seconds, maybe you should have used a for loop for the timer function.

Also, if you don't know about Tables, you should learn about them and use them a lot, they are really useful.

Check this out:

Spoiler

-- Made by remiX for Util OS
selection = 1

t_Options = {
	{ text = "Util OS 0.1 BETA", handler = function() shell.run(".Util/os") end },
	{ text = os.version(), handler = function() term.setBackgroundColour(colours.black) term.setTextColour(colours.white) term.setCursorPos(1, 1) term.clear() end }
}

function printMain()
	term.setBackgroundColour(colours.black)
	term.setTextColour(colours.white)
	term.clear()
	term.setCursorPos(1, 2)
	print(" +------------------Boot Loader------------------+ ")
	print(" |											   | ")
	print(" +-----------------------------------------------+ ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" +-----------------------------------------------+ ")
	print(" |											   | ")
	print(" |											   | ")
	print(" |											   | ")
	print(" +-----------------------------------------------+ ")
end

function timer()
	for i = 5, 1, -1 do
		term.setBackgroundColour(colours.black)
		term.setCursorPos(6, 16)
		write("Booting in " .. i)
		sleep(1)
	end
	return
end

function text()
	while true do
		for op = 1, #t_Options do
			term.setCursorPos(7, op + 5)
			if op == selection then term.setBackgroundColour(colours.lightGrey) else term.setBackgroundColour(colours.black) end
			write(t_Options[op].text)
		end
		local _, key = os.pullEvent("key")
		if key == keys.down and selection < #t_Options then
			-- Down
			selection = selection + 1
		elseif key == keys.up and selection > 1 then
			-- Up
			selection = selection - 1
		elseif key == keys.enter then
			t_Options[selection].handler()
			return
		end
	end
end

printMain()
parallel.waitForAny(timer, text)

-- This is here and will only occur if the timer runs out
-- Because of the sleep(2) (so you can read what it is booting)
-- I had to put it here or else you could still use up and down keys
-- while it's booting
printMain()
term.setCursorPos(7, 5)
print("BOOTING...")
term.setCursorPos(7, 6)
write(t_Options[selection].text)
sleep(2)
t_Options[selection].handler()

Thanks! This will help a lot! I'll put your name on the credits session ;)/>

And i modified the code a little to, for example, bypass the boot loader if a function in my os it is activated…
remiX #8
Posted 08 January 2013 - 06:37 AM
–snip

Thanks! This will help a lot! I'll put your name on the credits session ;)/>

And i modified the code a little to, for example, bypass the boot loader if a function in my os it is activated…

No prob :)/> Cool :)/> And once again, forum screws up the spacing within the prints -_-/>