android
  #21  
Old 12-02-2009, 09:09 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

We'll here's some more sample work which is just basically a test haven for the functions. One odd, and something that I would consider bad, is the fact that after calling os.sleep and exiting the application(pressing the home button here) I am unable to go back to the application without restarting. I figured out that once it hits os.sleep() again at the next app run it just quits. To me it seems that I'm not cleaning up correctly upon exit. EDIT: When experiencing this the sudoku app seems to fix the issue and runs just fine...

While screwing around in trying to fix this I tried out os.exit();. Now this is supposed to exit the lua interpreter and return back to the main program. When I call it here, the whole thing just dies and I have to push the reset button. Does this mean the entire x-fi2 menu is coded in lua?!

Bouncing ball, backlight, circle, comment/block comment, and beep usage:
Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
x = 30;
y = 30;
speed = 3;
fps_delay = 2;
ball_size = 10;
direction = 1;
direction_change = 0;
--[[
1 = Right + Down
2 = Right + Up
3 = Left + Down
4 = Left + Up
--]]

--http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html
function circlePoints(cx,cy,x,y,pixColor)
  if (x == 0) then
    screen.drawline(cx,cy,cx,cy + y,pixColor);
    screen.drawline(cx,cy,cx,cy - y,pixColor);
    screen.drawline(cx,cy,cx + y,cy,pixColor);
    screen.drawline(cx,cy,cx - y,cy,pixColor);
  elseif (x == y) then
    screen.drawline(cx,cy,cx + x,cy + y,pixColor);
    screen.drawline(cx,cy,cx - x,cy + y,pixColor);
    screen.drawline(cx,cy,cx + x,cy - y,pixColor);
    screen.drawline(cx,cy,cx - x,cy - y,pixColor);
  elseif (x < y) then
    screen.drawline(cx,cy,cx + x,cy + y,pixColor);
    screen.drawline(cx,cy,cx - x,cy + y,pixColor);
    screen.drawline(cx,cy,cx + x,cy - y,pixColor);
    screen.drawline(cx,cy,cx - x,cy - y,pixColor);
    screen.drawline(cx,cy,cx + y,cy + x,pixColor);
    screen.drawline(cx,cy,cx - y,cy + x,pixColor);
    screen.drawline(cx,cy,cx + y,cy - x,pixColor);
    screen.drawline(cx,cy,cx - y,cy - x,pixColor); 
  end;
end;

function circleMidpoint(xCenter,yCenter,radius,circleColor)
  local x = 0;
  local y = radius;
  local p = (5 - (radius * 4)) / 4;
  circlePoints(xCenter,yCenter,x,y,circleColor);
  while (x < y) do
    x = x + 1;
    if (p < 0) then
      p = p + ((2 * x) + 1);
    else
      y = y - 1;
      p = p + ((2 * (x - y)) + 1);
    end;
    circlePoints(xCenter,yCenter,x,y,circleColor);
  end;
end;

screen.clear();
screen.fillrect(0,0,400,240,color_black);
screen.fillrect(0,0,20,240,color_white);
screen.fillrect(0,0,380,20,color_white);
screen.fillrect(380,0,20,240,color_white);
screen.fillrect(0,220,400,20,color_white);

while true do
  circleMidpoint(x,y,ball_size,color_black);
  if direction_change > 1 then
    direction_change = direction_change - 1;
  elseif direction_change == 1 then
    audio.beep(500,100);
    screen.backlight(50);
    direction_change = 0;
  end;
  if direction == 1 then
    if ((x + speed) > 368) then 
      direction = 3; 
      direction_change = 3;
    end;
    if ((y + speed) > 206) then
      direction = 2;
      direction_change = 3;
    end;
    x = x + speed;
    y = y + speed;
  elseif direction == 2 then
    if ((x + speed) > 368) then
      direction = 4;
      direction_change = 3;
    end;
    if ((y - speed) < 33) then
      direction = 1;
      direction_change = 3;
    end;
    x = x + speed;
    y = y - speed;
  elseif direction == 3 then
    if ((x - speed) < 33) then
      direction = 2;
      direction_change = 3;
    end;
    if ((y + speed) > 206) then
      direction = 4;
      direction_change = 3;
    end; 
    x = x - speed;
    y = y + speed;
  elseif direction == 4 then
    if ((x - speed) < 33) then
      direction = 2;
      direction_change = 2;
    end;
    if ((y - speed) < 33) then
      direction = 3;
      direction_change = 2;
    end;
    x = x - speed;
    y = y - speed;
  end;
  circleMidpoint(x,y,ball_size,color_white);
  screen.update();
  if (control.read() == 1) and (button.home() == 1) then
    break;
  end;
  os.sleep(fps_delay);
end;

Last edited by ThievingSix; 12-02-2009 at 10:56 PM.
Reply With Quote

Advertisement [Remove Advertisement]

  #22  
Old 12-03-2009, 11:28 AM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

ThievingSix, thanks, that's cool! I was thinking of writing something similar, as I've got a pong app in mind.

And yes, I've run into the same issue with using the home button and then the app always doing a quick dump after starting. That's why I added a close tap to my last script. But yeah, it looks like a cleanup issue.

And that os.exit() thing? Scary.
Reply With Quote

  #23  
Old 12-03-2009, 04:25 PM
k1llua k1llua is offline
Junior Member
 
Join Date: Nov 2009
Posts: 51
Default

pong!! that would be great.... space invaders would be awesome if the firmware could support it...
nice work
Reply With Quote

  #24  
Old 12-04-2009, 12:15 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

The only problem I see with something like pong is that the touch screen isn't that good for delicate tracking like that.
Reply With Quote

  #25  
Old 12-04-2009, 12:30 AM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

OK this dofile test works OK:

MAIN.LUA
Code:
dofile("x.lua");
init();

X.LUA
Code:
function init()
 blk = color.new(0,0,0);
 screen.clear();
 screen.fillrect(0,0,400,240,blk);
 screen.update();
 os.sleep(100);
end;
Reply With Quote

  #26  
Old 12-04-2009, 02:02 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

If I'm correct, I believe dofile() will actually execute X.LUA. In your case I believe you want to use loadfile() instead.
Reply With Quote

  #27  
Old 12-04-2009, 10:32 AM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Thumb Down

Quote:
Originally Posted by ThievingSix View Post
If I'm correct, I believe dofile() will actually execute X.LUA. In your case I believe you want to use loadfile() instead.
It looks like dofile might be more convenient, depending on what I need to do. The Lua.org description of loadfile is a little vague, but I may give it a try.

Also, a general note: it looks like functions MUST be defined in the code text before being called, as in:

Code:
function x()
blah
end;

x();
but not:

Code:
x();

function x()
blah
end;

Last edited by rphunt2002; 12-04-2009 at 11:11 AM.
Reply With Quote

  #28  
Old 12-04-2009, 02:15 PM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Quote:
Originally Posted by ThievingSix View Post
If I'm correct, I believe dofile() will actually execute X.LUA. In your case I believe you want to use loadfile() instead.
Oh wait, sorry, did you mean like as if the function was called? so that the follwing init(); line is redundant? That didn't occur to me at all. Hmm, I'll have to check. I'll report back how these things behave.Thanks.
Reply With Quote

  #29  
Old 12-04-2009, 02:55 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Yeh, LUA is brand new to me but I think I have grasp(This reminds me of mIRC scriping lol). Basically when we have a plain text lua file that hasn't been compiled dofile()/loadfile() will compile it and assign it to a function variable.

As such this:
Code:
myfunc() = loadfile("x.lua");
is valid.

dofile() will execute any non-function encased code. If I'm correct the entire shell that we use to browse music, videos, etc on our player is made in lua. If this is the case then when we click on our application it's calling dofile("MAIN.LUA").
Reply With Quote

  #30  
Old 12-04-2009, 05:06 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Hi

I found this thread over at Creative forum, thank you once again :-)
for cracking this nut.

I just found out that when using:
(control.isButton() == 1) and (button.home() == 1) then
break;

to exit an application, then it can be run again and again.

Or at least that's the behaviour i get from this little circle program.

Click the screen to draw a random size and color expanding circle.
Code:
function putpixel(x,y,pixelcolor)
    i=1;
    if ( x > 399 ) then i=0; end;
    if ( x <   0 ) then i=0; end;
    if ( y > 239 ) then i=0; end;
    if ( y <   0 ) then i=0; end;
    if ( i == 1 )  then
    screen.drawpixel(x,y,pixelcolor);
    end;
end;

-- modified source from http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html
function circle(xCenter,yCenter,radius,pxcolor)
        r2 = radius * radius;
        putpixel(xCenter, yCenter + radius,pxcolor );
        putpixel(xCenter, yCenter - radius,pxcolor );
        putpixel(xCenter + radius, yCenter,pxcolor );
        putpixel(xCenter - radius, yCenter,pxcolor );
     y = radius;
        x = 1;
        y = math.sqrt(r2 - 1) + 0.5;
          while (x < y) do
            putpixel(xCenter + x, yCenter + y,pxcolor );
            putpixel(xCenter + x, yCenter - y,pxcolor );
            putpixel(xCenter - x, yCenter + y,pxcolor );
            putpixel(xCenter - x, yCenter - y,pxcolor );
            putpixel(xCenter + y, yCenter + x,pxcolor );
            putpixel(xCenter + y, yCenter - x,pxcolor );
            putpixel(xCenter - y, yCenter + x,pxcolor );
            putpixel(xCenter - y, yCenter - x,pxcolor );
            x=x+1;
            y = math.sqrt(r2 - x*x) + 0.5;
          end;
            if (x == y) then
            putpixel(xCenter + x, yCenter + y,pxcolor );
            putpixel(xCenter + x, yCenter - y,pxcolor );
            putpixel(xCenter - x, yCenter + y,pxcolor );
            putpixel(xCenter - x, yCenter - y,pxcolor );
            end;

end;


function drawcircle(x, y)
   RNDcolor_R = math.random(255);
   RNDcolor_G = math.random(255);
   RNDcolor_B = math.random(255);
   RNDSize = math.random(300);
   rndcolor = color.new(RNDcolor_R,RNDcolor_G,RNDcolor_B);
   for i=2,RNDSize do
   circle(x,y,i,rndcolor);
   screen.update();
   end;
end;

color_black = color.new(0,0,0);
screen.fillrect(0,0,400,240,color_black);
screen.update();

while true do
   if (control.read() == 1) and (control.isTouch() == 1) then
      x, y = touch.pos();
      drawcircle(x, y);
   end;

    if (control.isButton() == 1) and (button.home() == 1) then
         break;
    end;

 os.sleep(2);
end;
What a can't find out is how to avoid the doubble circle drawing in one click, any suggestions ?

Last edited by Jan_DK; 12-04-2009 at 05:21 PM.
Reply With Quote

  #31  
Old 12-04-2009, 05:20 PM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

Try adding os.sleep(100) after the drawing process. I guess that should be okay.
Reply With Quote

  #32  
Old 12-04-2009, 05:29 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Quote:
Originally Posted by ZaPx64 View Post
Try adding os.sleep(100) after the drawing process. I guess that should be okay.
Thank you for the suggestion, but even os.sleep(500) don't work, it just increase the pause between the two circles.

Last edited by Jan_DK; 12-04-2009 at 06:05 PM.
Reply With Quote

  #33  
Old 12-05-2009, 04:39 AM
utubo_sk8er utubo_sk8er is offline
Junior Member
 
Join Date: Dec 2009
Posts: 1
Default

Add this code in the code of waiting inputs, then worked well.

Quote:
... (control.isTouch() == 1) and (touch.up() == 1 or touch.click == 1) then
umm... is my english right?
I'm Japanese and still learning english...

Last edited by utubo_sk8er; 12-05-2009 at 04:54 AM.
Reply With Quote

  #34  
Old 12-05-2009, 05:12 AM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

Yep, utubo_sk8er is right. You have to test if touch.click() it true, otherwise it will draw circles even if you slide the finger over the display.
Reply With Quote

  #35  
Old 12-05-2009, 11:30 PM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default loadfile vs dofile

OK...
dofile() will load and execute a file as lua code.
loadfile() will allow loading a file as a function.

To use dofile():
Code:
dofile("x.lua");
init();
where X.LUA is:
Code:
function init();
text.draw(0,0,"hi");
end;
An equivalent using loadfile():
Code:
init = loadfile("x.lua");
init();
where X.LUA is:
Code:
text.draw(0,0,"hi");
The benefit of dofile() is that the code just runs, like a PHP include(), good if you want to just run a snippet of code, or add a collection of predefined functions. The benefit of loadfile() is that you get a function that can be named whatever.

Last edited by rphunt2002; 12-06-2009 at 09:26 AM.
Reply With Quote

  #36  
Old 12-06-2009, 01:06 AM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Slight touch up of home button code:
Code:
 if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.up() == 1) then 
     break;
end;
this way it doesn't need a control.read() done elsewhere, and doesn't make 2 jumps back to the main menu.

EDIT: It's not perfect though, something is still missimg. Run your app, close with Home, run your app again, it's fine. But run your app, close with Home, run Sudoku, and Sudoku closes right off, but runs a second time.

Last edited by rphunt2002; 12-06-2009 at 09:27 AM.
Reply With Quote

  #37  
Old 12-06-2009, 11:36 AM
skybluedream skybluedream is offline
Member
 
Join Date: Oct 2009
Posts: 132
Default

I just hope in the future firmware they switch to vertical view (like iTouch, S9) instead of horizontal...you need to hold the player with both your hands to make this horizontal angle work by the looks of it...so vertical would be heaps cool.
Reply With Quote

  #38  
Old 12-06-2009, 02:16 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Straight(almost) from the sudoku app itself:

Code:
  if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.click() == 1) then
    break;
  end;
Works fine now.

Edit: Really disappointed that os.execute() is non-existent seeing as it's supposed to be a lua standard.

Edit2: My guess as to why this fixes it is this: The control table accesses a queue. When pressing the home button we activate a down and up sent to the queue. First we see if there is anything to be read with control.read(). Next we see if the thing we read is a button. Then if it's the home button. Finally we remove the entire thing from the queue with button.click(). When we didn't add button.click() nothing got removed from the queue. When we added button.up() we only removed the first part.

Last edited by ThievingSix; 12-06-2009 at 04:49 PM.
Reply With Quote

  #39  
Old 12-06-2009, 04:20 PM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Quote:
Works fine now.
Yep, that does seem better, thanks!
Reply With Quote

  #40  
Old 12-06-2009, 09:52 PM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Quote:
When we added button.up() we only removed the first part.
yes this control queue is something to sort out. I found that if I did the same test twice, only the first code ran. But if I used the test once to set a boolean flag, I could use that to run both sets of code no problem. I also noticed that if the read() was run once, it was good for subsequent tests. Bizarre.
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:43 AM.