Author Topic: Lua Routines  (Read 13540 times)

0 Members and 1 Guest are viewing this topic.

Ashbad

  • Guest
Re: Lua Routines
« Reply #15 on: June 22, 2011, 08:57:36 am »
True.  Another way to do it would be this:

Code: [Select]
global_flags = {is_paused = nil}

setupBeginNoBreakPause = function(milliseconds)
   global_flags.is_paused = timer.start(milliseconds/1000)
end
   
-- all events are surrounded with a limited that doesn't allow anything to happen while the flag is set

function on.timer
   if global_flags.is_paused
      global_flags.is_paused = nil
   end

end


A bit more messy, but preserves all processing.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Routines
« Reply #16 on: June 22, 2011, 09:54:02 am »
Quote
If that's really an equilateral triangle, shouldn't the square root of 3 be involved somehow?

I don't have to, I basically use trigonometry to find out the height of the triangle and remove that height to the Y position and I get the Y position of the upper vertex and then I can use the drawLine functions to effectively draw the triangle.

I rechecked my routine and I happen to think the following one is better, I was using cos( instead of sin( in the wrong place.

Code: [Select]
function drawTriangle(gc,x,y,l)
gc:drawLine(x,y,x+l,y)
gc:drawLine(x, y, x+(l/2), y+(l*math.sin(60)))
gc:drawLine(x+(l/2), y+(l*math.sin(60)), x+l, y)
end

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #17 on: June 22, 2011, 01:41:02 pm »
Ashbad, thats quite a good way :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Routines
« Reply #18 on: June 22, 2011, 05:21:06 pm »
Ashbad, I don't really see how that works, can you give me an example please? Thanks

Ashbad

  • Guest
Re: Lua Routines
« Reply #19 on: June 22, 2011, 08:51:43 pm »
Well, I don't code in Lua anymore and I don't even own an Nspire, so I'm going blind here -- however, I can explain how it works.

You start by setting up a global table of non-linear flags that can accessed throughout the program, and you set the first one, which corresponds to saying if the timer is paused or not, to nil.  You then make a function that restarts the main timer an make it tick at a frequency defined by the milliseconds parameter.  All events cannot run while the flag is set (I simply set the flag by having it hold the current timer's key or whatever it uses to access the ticking, which normally is a no-no for flags but it saves space) and when the the timer ticks once, it resets the flag and allows for normal event execution.

Events a cool things in Lua, but in cases of timers and such, they can be really annoying and get in the way x.x

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Lua Routines
« Reply #20 on: June 25, 2011, 10:08:38 pm »
Pause for an exact amount of time, in milliseconds

Code: [Select]
WaitMS = function(milliseconds)
   while (stime = math.abs(timer.GetMilliSecCounter()+milliseconds))<
            math.abs(timer.getMilliSecCounter()) do end
   return true
end

This gives:
')' expected near '='

Ashbad

  • Guest
Re: Lua Routines
« Reply #21 on: June 25, 2011, 10:11:38 pm »
Don't use that one.  Use a custom-made implementation of this that fits your application-specific needs instead:


Code: [Select]
global_flags = {is_paused = nil}

setupBeginNoBreakPause = function(milliseconds)
   global_flags.is_paused = timer.start(milliseconds/1000)
end
   
-- all events are surrounded with a limited that doesn't allow anything to happen while the flag is set

function on.timer
   if global_flags.is_paused
      global_flags.is_paused = nil
   end

end


Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Lua Routines
« Reply #22 on: June 25, 2011, 10:31:10 pm »
Don't use that one.  Use a custom-made implementation of this that fits your application-specific needs instead:


Code: [Select]
global_flags = {is_paused = nil}

setupBeginNoBreakPause = function(milliseconds)
   global_flags.is_paused = timer.start(milliseconds/1000)
end
   
-- all events are surrounded with a limited that doesn't allow anything to happen while the flag is set

function on.timer
   if global_flags.is_paused
      global_flags.is_paused = nil
   end

end



'(' expected near 'if'
« Last Edit: June 25, 2011, 10:41:17 pm by BrownyTCat »

Ashbad

  • Guest
Re: Lua Routines
« Reply #23 on: June 25, 2011, 10:40:23 pm »
Simply inside of all your events, make it so once the global flag is set to a non-nil value, the event execution ends and don't allow them to be triggered by starting all code within the blocks with a "if global_flags.is_paused ~= nil" checker.  Simple as that ;)

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Lua Routines
« Reply #24 on: June 25, 2011, 10:42:34 pm »
Simply inside of all your events, make it so once the global flag is set to a non-nil value, the event execution ends and don't allow them to be triggered by starting all code within the blocks with a "if global_flags.is_paused ~= nil" checker.  Simple as that ;)
* BrownyTCat 's head exploded.

Grayscale calc owners, place this in your on.paint(gc) loop:
Code: [Select]
function setGrey(shade)
gc:setColorRGB((shade*255)/16,(shade*255)/16,(shade*255)/16)
end
That makes it so you can set the shade of grey like so: 0=black, 8=middle, 16=white.
« Last Edit: June 25, 2011, 10:48:12 pm by BrownyTCat »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #25 on: June 26, 2011, 04:05:24 am »
I would not place it in on.paint, as it will redefine it every time it is run.
I would do this:
Code: [Select]
function setGrey(shade)
platform.gc():setColorRGB((shade*255)/16,(shade*255)/16,(shade*255)/16)
end
or
Code: [Select]
function setGrey(gc, shade)
gc:setColorRGB((shade*255)/16,(shade*255)/16,(shade*255)/16)
end

And put both of those functions OUTSIDE of on.paint.
The first one platform.gc() to get the graphical context (valid if called from on.paint), and the second one ask's gc as a parameter (might be a bit faster).

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Routines
« Reply #26 on: June 26, 2011, 06:21:29 am »
Yeah Jim Bauwens's seems better because it would use lots of cycles and we want our programs to run fast, so defining it once is better.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Routines
« Reply #27 on: July 01, 2011, 12:09:06 pm »
BrownyTCat and Jim Bauwens, I love you!

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Routines
« Reply #28 on: July 09, 2011, 12:30:26 pm »
Hey, I just saw this topic and wanted to share with you guys what I had done on my side, without knowledge of what existed here :

https://github.com/adriweb/BetterLuaAPI-for-TI-Nspire/blob/master/BetterLuaAPI.lua

If that's ok with you, I'd want to add some of the routines from this topic in BetterLuaAPI. Of course, credits will be written.
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #29 on: July 09, 2011, 03:53:12 pm »
I haven't posted many routines here, but its ok for me :)