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

Unexpected Shutdown

Started by CrazyTech13, 16 November 2014 - 05:32 AM
CrazyTech13 #1
Posted 16 November 2014 - 06:32 AM
NOTE: I am running MC version 1.5.2 and CraftOS 1.5, which may be a factor to my problem.

I created an API for easily forming a slideshow. I have a control computer and a display computer (hooked up to a monitor) that are linked via wireless rednet connection.
Whenever my slideshow ends, the display computer always shuts down, even though I haven't put any os.shutdown() in the code.

Here is the code of the API:

local slide -- variable to hold which slide is activated
local totalSlides -- total slides in the whole slide show

local rnSide -- modem side
local ctrlCom -- computer to take messages from

local com, msg -- event holders, com is id and stands for computer

function setRNSide(side) -- gets the modem side from the program
  rnSide = side
end

function setCtrlCom(id) -- gets the computer id to listen to
  ctrlCom = id
end

function setTotalSlides(amount) -- gets total slides from program
  totalSlides = amount
end

function getSlide() -- used in program to detect which slide should be activated and displays it
  return slide
end

function wait() -- for lack of a better name to get msgs from ctrlCom

  com, msg = rednet.receive()

  if com == ctrlCom then

	if msg == "forward slide" then -- next slide msg
	
	  if slide == totalSlides then -- if on the last slide return one more than total slides so the program detects if the slideshow is over
	    rednet.close(rnSide)
	    return slide + 1
	  end
	
	  slide = slide + 1
	
	elseif msg == "back slide" and slide > 1 then -- back slide msg
	  slide = slide - 1
	
	else
	  wait()
	
	end
  
	return slide
  
  else
	wait()
  
  end

end

function slideShowStartup() -- sets up necessary things and waits for controller to get online

  rednet.open(rnSide)

  repeat
	com, msg = rednet.receive()
  until com == ctrlCom and msg == "start slide"

  rednet.send(ctrlCom, "started") -- tells controller that the show started
  rednet.send(ctrlCom, "ts: "..totalSlides) -- gives the controller the amount of slides

  slide = 1 - initializes the slide to 1

end

The display computer's program:

local totalSlides = 5

os.loadAPI("slideshows/ss")
ss.setRNSide("top")
ss.setCtrlCom(6)
ss.setTotalSlides(totalSlides) -- giving the API info to work on

local slide = 1
local mon = peripheral.wrap("back")

local function slide1()

  -- slide 1 display

end

local function slide2()

  -- slide 2 display

end

local function slide3()

  -- slide 3 display

end

local function slide4()

  -- slide 4 display

end

local function slide5()

  -- slide 5 display

end

local function display() -- checks which slide to display

  mon.setBackgroundColor(colors.cyan)
  mon.clear()
  mon.setTextScale(3)
  mon.setTextColor(colors.white)

  if slide == 1 then
	slide1()
  
  elseif slide == 2 then
	slide2()
  
  elseif slide == 3 then
	slide3()
  
  elseif slide == 4 then
	slide4()
  
  elseif slide == 5 then
	slide5()
  
  end

end

mon.setBackgroundColor(colors.black) -- clears the monitor before the slideshow starts
mon.clear()

ss.slideShowStartup()

while slide <= totalSlides do -- if current slide is not over the total slides

  slide = ss.getSlide() -- gets current slide from API
  display()

  slide = ss.wait()

end

mon.setBackgroundColor(colors.black) -- clears the monitor after the slideshow ends
mon.clear() -- where the program ends but then shuts down the computer

And the controller program (on pastebin for your intrerest, I don't believe this program causes the problem)

So basically the API takes the info you give it and does all the work. All you do in the actual program is give that info to the API and set up the slide displays.

I have absolutely no clue as to why the computer shuts down. Maybe a bug due to the version? Dunno. Any help is appreciated!

Edit: I managed to catch the message before the shutdown. It read:
Error resuming bios.lua
Check you have installed ComputerCraft correctly.
Edited on 16 November 2014 - 06:21 AM
Bomb Bloke #2
Posted 16 November 2014 - 07:33 AM
Are you sure those last two lines are running? What happens if you put this at the very end of the script?:

print("Press any key to end...")
os.pullEvent("char")

How are you running the problematic script? Via startup, or…?
CrazyTech13 #3
Posted 16 November 2014 - 07:49 AM
Ok so I ran the code with that snippet you provided, but it didn't print or take an event.

I thought those last two lines were running, but it may have been from the computer shutting down. Although, right when the program was ideally supposed to end the loop the screen went black, I checked the computer and it had not shut down yet.

The process I take to start the slide show is by running that program on the display computer, and then going to the controller and running its control program.

What is also weird is that I made a test slide show on the display computer, (the control program was the same) and it did not have this problem.
Bomb Bloke #4
Posted 16 November 2014 - 08:04 AM
In that case, I suggest sticking that snippet into other places in your program - make it print something different each time, and you should be able to work out exactly where the shutdown occurs.
CrazyTech13 #5
Posted 16 November 2014 - 09:51 AM
So I did some extreme debugging and came to the conclusion that the redirection in slide1() is crashing the computer at the end. Without them, the computer doesn't crash. Do you have any idea why?
CrazyTech13 #6
Posted 16 November 2014 - 10:06 AM
Omg never mind I figured it out.
I was supposed to use term.restore(), but I just don't understand because in a different program of mine I used term.redirect(term) and it worked fine.

Well thanks Bomb Bloke, and I'm sorry for wasting your time where I just needed one different function. <_</>