Jump to content




Monitor & control program with touch button overrides


15 replies to this topic

#1 xBlizzDevious

  • Members
  • 56 posts

Posted 06 October 2014 - 02:35 PM

Hello!

I'm writing a little program that I want to use to control some engines based on the level of the attached energy cell. I've got that code sorted just perfectly as I've done this many times before.

However, I'd like to have some buttons on my monitor that allow me to override the automatic function as and when needed.

I'm pretty sure I know all the code I need to do the buttons, however, I'm having some trouble figuring out how to get the os.pullEvent and yet have the program running in the background still.
- Will I have to use the parallel API or is there a simpler way for just having a couple of buttons controlling that application?


I basically want a toggle button in the bottom left of the monitor that will switch the engines on if they're off and off if they're on and then another button on the bottom right which will be a "reset to auto" button. The program runs just fine at the moment without the buttons, monitoring the Energy cell and applying a redstone signal once the cell reaches a certain threshold and then removing the redstone signal when reaching another threshold.

For reference: I'm using FTB monster V1.1.2 on MC 1.6.4.

Thanks for help in advance.

Edited by xBlizzDevious, 06 October 2014 - 02:35 PM.


#2 Doyle3694

  • Members
  • 815 posts

Posted 06 October 2014 - 03:28 PM

While I am not experienced with parallel API and that might be the best thing for this, you could have an os.pullEvent() with a timer so that every 0.25 seconds it will check, then run your engine code, then check, the run your engine code and so on. Then again, you should be able to just use

-- Code

parallel.waitForAny(enginecode, buttoncode)

And then have it in the button code so that it returns if the button is pressed.

#3 xBlizzDevious

  • Members
  • 56 posts

Posted 06 October 2014 - 03:31 PM

View PostDoyle3694, on 06 October 2014 - 03:28 PM, said:

While I am not experienced with parallel API and that might be the best thing for this, you could have an os.pullEvent() with a timer so that every 0.25 seconds it will check, then run your engine code, then check, the run your engine code and so on. Then again, you should be able to just use

-- Code

parallel.waitForAny(enginecode, buttoncode)

And then have it in the button code so that it returns if the button is pressed.

Hmm. That's what I thought. But checking for the os.pullEvent in my loop caused it to hang until I clicked. I need the rest of the program to constantly run.

#4 Doyle3694

  • Members
  • 815 posts

Posted 06 October 2014 - 03:36 PM

Thats why you can use a timer with os.pullEvent: http://computercraft...mer_%28event%29

But yes, the parallel API should be just fine for your needs

#5 xBlizzDevious

  • Members
  • 56 posts

Posted 06 October 2014 - 03:47 PM

View PostDoyle3694, on 06 October 2014 - 03:36 PM, said:

Thats why you can use a timer with os.pullEvent: http://computercraft...mer_%28event%29

But yes, the parallel API should be just fine for your needs

Well I've never used the parallel API or used that sort of thing in any other language so it's a little daunting. I'll see if I can figure out how to do something with the timer thing. I didn't realise that I could just pull any event and then check what it was - in which case, I think I know what I need to do. Otherwise, I might have to look into the parallel API.

Thanks!

#6 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 06 October 2014 - 08:46 PM

I used a os.pullevent for my multitasking

Check this out it might help you

http://www.computerc...se-the-program/

Edit: mine is to work on a pocket pc to work stuff as I walk around but can easily put on a monitor. If you want I could convert mine to work how you want ?

Edited by VSXGsonic, 06 October 2014 - 08:49 PM.


#7 xBlizzDevious

  • Members
  • 56 posts

Posted 06 October 2014 - 08:55 PM

View PostVSXGsonic, on 06 October 2014 - 08:46 PM, said:

I used a os.pullevent for my multitasking

Check this out it might help you

http://www.computerc...se-the-program/

Edit: mine is to work on a pocket pc to work stuff as I walk around but can easily put on a monitor. If you want I could convert mine to work how you want ?

Thanks. I've managed to get it working how I want now though - I think...

I'm sure there's a much more elegant way of writing that and there's probably a bug or three in there that I've not found yet, but from some quick testing, it works! And I think it's bad practice to run a function from one function that leads back to the original function. But as I said, it works!

Here's the code:

Spoiler



EDIT: found a bug...
I'm getting:

startup:21: vm error:
java.lang.ArrayIndexOutOfBoundsException

Line 21 is:
function createButtons()
	mon.setTextColor(colors.white) --THIS ONE
	if overrideText == "ON " then

Any ideas as to why that's causing what I assume is an external Java error rather than a Lua error?

Edited by xBlizzDevious, 06 October 2014 - 09:07 PM.


#8 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 06 October 2014 - 09:07 PM

Well aslong as your happy with it and it works . Mine is going to run everything in my place from one screen so the funception was the easyer way around ^-^.

#9 xBlizzDevious

  • Members
  • 56 posts

Posted 06 October 2014 - 09:07 PM

View PostVSXGsonic, on 06 October 2014 - 09:07 PM, said:

Well aslong as your happy with it and it works . Mine is going to run everything in my place from one screen so the funception was the easyer way around ^-^.

Please see edits above.

#10 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 07 October 2014 - 01:07 AM

View PostxBlizzDevious, on 06 October 2014 - 08:55 PM, said:

View PostVSXGsonic, on 06 October 2014 - 08:46 PM, said:

I used a os.pullevent for my multitasking

Check this out it might help you

http://www.computerc...se-the-program/

Edit: mine is to work on a pocket pc to work stuff as I walk around but can easily put on a monitor. If you want I could convert mine to work how you want ?

Thanks. I've managed to get it working how I want now though - I think...

I'm sure there's a much more elegant way of writing that and there's probably a bug or three in there that I've not found yet, but from some quick testing, it works! And I think it's bad practice to run a function from one function that leads back to the original function. But as I said, it works!

Here's the code:

Spoiler



EDIT: found a bug...
I'm getting:

startup:21: vm error:
java.lang.ArrayIndexOutOfBoundsException

Line 21 is:
function createButtons()
	mon.setTextColor(colors.white) --THIS ONE
	if overrideText == "ON " then

Any ideas as to why that's causing what I assume is an external Java error rather than a Lua error?

ArrayIndexOutOfBoundsException indicates that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. but i have tested it and i have not been given this error. when did it occur?
EDIT: nevermind got the error i will play with it and your energy reading stops updating and that give error

Edited by VSXGsonic, 07 October 2014 - 01:34 AM.


#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 October 2014 - 01:39 AM

It's happening because of the recursive function calls.

Let's say you've got a script that looks like this:

function moo()
  print("Moo")
  sleep(0)
  moo2()
end

function moo2()
  print("Moo2")
  sleep(0)
  moo()
end

moo()

When the moo() function calls moo2(), it doesn't end. It stays loaded in memory, waiting for the moo2() function to end so that it can carry on from where it left off.

But moo2() doesn't end. Instead, it calls moo() again, and therefore also stays loaded in memory, waiting for that fresh new instance of moo() to complete so it can carry on.

As the script continues, you get more and more copies of these functions pouring into memory, until eventually the space reserved for them - which we'll call the "function stack" - runs out, and the whole thing crashes (a "stack overflow").

Calling any function adds to the stack, and that includes your monitor's "setTextColor" function. Usually this doesn't matter, because when those functions complete they're removed from the stack (which can hold something like 256 functions at a time). But your "auto()" and "override()" functions never finish - they just keep on starting up fresh instances of mainLoop(), which in turn starts up fresh new instances of auto()/override().

You could get around this with a process called "tail calling", but personally I think you'd be better off re-organising the script from this sort of structure:

function auto()
  -- do stuff
  mainLoop()
end

function override()
  -- do stuff
  mainLoop()
end

function mainLoop()
  while true do
    if stuff then
      auto()
    else
      override()
    end
  end
end

To this sort of structure:

function auto()
  -- do stuff
end

function override()
  -- do stuff
end

function doMenu()
  if stuff then
    return "auto"
  else
    return "override"
  end
end

while true do
  local menuResult = doMenu()
  if menuResult == "auto" then auto() elseif menuResult == "override" then override() end
end


#12 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 07 October 2014 - 03:56 AM

its like 5 am here and i made this for you to try it will update and i haven't changed how it looks much .(i dont think ) :wacko: and it shouldn't give you the error

what you think ?

Spoiler

hope it works for you :unsure:

Edited by VSXGsonic, 07 October 2014 - 03:59 AM.


#13 xBlizzDevious

  • Members
  • 56 posts

Posted 07 October 2014 - 07:20 AM

View PostBomb Bloke, on 07 October 2014 - 01:39 AM, said:

It's happening because of the recursive function calls.

Let's say you've got a script that looks like this:

function moo()
  print("Moo")
  sleep(0)
  moo2()
end

function moo2()
  print("Moo2")
  sleep(0)
  moo()
end

moo()

When the moo() function calls moo2(), it doesn't end. It stays loaded in memory, waiting for the moo2() function to end so that it can carry on from where it left off.

But moo2() doesn't end. Instead, it calls moo() again, and therefore also stays loaded in memory, waiting for that fresh new instance of moo() to complete so it can carry on.

As the script continues, you get more and more copies of these functions pouring into memory, until eventually the space reserved for them - which we'll call the "function stack" - runs out, and the whole thing crashes (a "stack overflow").

Calling any function adds to the stack, and that includes your monitor's "setTextColor" function. Usually this doesn't matter, because when those functions complete they're removed from the stack (which can hold something like 256 functions at a time). But your "auto()" and "override()" functions never finish - they just keep on starting up fresh instances of mainLoop(), which in turn starts up fresh new instances of auto()/override().

You could get around this with a process called "tail calling", but personally I think you'd be better off re-organising the script from this sort of structure:

function auto()
  -- do stuff
  mainLoop()
end

function override()
  -- do stuff
  mainLoop()
end

function mainLoop()
  while true do
	if stuff then
	  auto()
	else
	  override()
	end
  end
end

To this sort of structure:

function auto()
  -- do stuff
end

function override()
  -- do stuff
end

function doMenu()
  if stuff then
	return "auto"
  else
	return "override"
  end
end

while true do
  local menuResult = doMenu()
  if menuResult == "auto" then auto() elseif menuResult == "override" then override() end
end

I'll have a look at this later on, just off to work shortly. Thanks for the info, and that also explains why it's bad to call a function that leads back to the original one - I've witnessed the problems now! Haha

View PostVSXGsonic, on 07 October 2014 - 03:56 AM, said:

its like 5 am here and i made this for you to try it will update and i haven't changed how it looks much .(i dont think ) :wacko: and it shouldn't give you the error

what you think ?

-snip-
hope it works for you :unsure:

Haha, thanks a lot! I'll give it a try and see if it works. (I've got to neaten up that code first, though).

#14 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 07 October 2014 - 07:44 AM

All I changed was one part of the screen it now shows
Override auto run
ON OFF

It was neat ish before I pasted but it did its own thing haha. But u can just save it in game as test and try it I tryed to make it bug and lag free as possible well as much as lots of early coffee would allow I used notepad to write it didnt check but should work aslong as u havent moved the objects placement ^-^

#15 xBlizzDevious

  • Members
  • 56 posts

Posted 07 October 2014 - 03:21 PM

View PostVSXGsonic, on 07 October 2014 - 07:44 AM, said:

All I changed was one part of the screen it now shows
Override auto run
ON OFF

It was neat ish before I pasted but it did its own thing haha. But u can just save it in game as test and try it I tryed to make it bug and lag free as possible well as much as lots of early coffee would allow I used notepad to write it didnt check but should work aslong as u havent moved the objects placement ^-^

Thanks for the code, but two bugs I have found:

One: "Engines are: " on/off doesn't show. I've checked the code through and I see where it happens, but it doesn't seem to work - not sure why.

Two: Every time you click, something happens with the OS timer and it runs faster. So when you click enough times, eventually, it bugs out. I kept having this same issue too, but I don't know how to fix it - or what's causing it - though I guess it's to do with the timer duplicating or restarting or something.

#16 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 07 October 2014 - 07:00 PM


View PostxBlizzDevious, on 07 October 2014 - 03:21 PM, said:


One: "Engines are: " on/off doesn't show. I've checked the code through and I see where it happens, but it doesn't seem to work - not sure why.

Two: Every time you click, something happens with the OS timer and it runs faster. So when you click enough times, eventually, it bugs out. I kept having this same issue too, but I don't know how to fix it - or what's causing it - though I guess it's to do with the timer duplicating or restarting or something.


engines i see what you mean with the engine oops . i carnt make it do it i have been sat here for 5 mins clicking buttons and it still works. how did you make it bug ? silly question but is all up to date? i just updated to all new open peripherals and it works or it just doesn't wanna do it for me






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users