android
  #41  
Old 12-06-2009, 10:13 PM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Created a tiny library, and a test app of some buttons.

I place this file, LIB.LUA in the /applications/ folder
Code:
-- set some color vars
function setColors()
 clrBlack = color.new(0,0,0);
 clrWhite = color.new(255,255,255);
 clrRed = color.new(255,0,0);
 clrBlue = color.new(0,0,255);
 clrYellow = color.new(255,255,0);
 clrGreen = color.new(0,255,0);
 clrOrange = color.new(255,128,0);
 clrGray = color.new(128,128,128);
 clrViolet = color.new(128,0,255);
 clrCyan = color.new(0,128,255);
 clrBrown = color.new(128,80,64);
end;
-- screen reset
function resetScreen(color, update)
 screen.clear();
 screen.fillrect(0,0,400,240, color);
 if update==1 then 
  screen.update();
 end;
end;
-- consolidated text operations
function textWrite(x,y,color,size,string)
 text.color(color);
 text.size(size);
 text.draw(x,y,string);
end;
-- check home button
function btnHome()
 --if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.up() == 1) then
 if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.click() == 1) then 
  return true;
 else
  return false;
 end;
end;
--create button
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor, string)
 screen.fillrect(x,y,width,height,bgcolor);
 screen.drawrect(x,y,x+width,y+height,bordercolor);
 text.color(textcolor);
 text.size(textsize);
 text.draw(x,y,string);
end;
-- check screen click
function scrnClick()
 if (control.read() == 1) and (control.isTouch() == 1) and ((touch.click() == 1) or (touch.up() == 1)) then 
  x,y = touch.pos();
  return true;
 else
  return false;
 end;
end;
-- check button
function btnClick(tx,ty,twidth,theight)
 if ((x>tx) and (x<(tx+twidth)) and (y>ty) and (y<(ty+theight))) then
  return true;
 else
  return flase;
 end;
end;
The create an app folder for the MAIN.LUA

Code:
dofile("../LIB.LUA");
setColors();
a=0;
function refresh()
 resetScreen(clrGray, 0);
 textWrite(10,10,clrBlack,20,"a = "..a);
 makeButton(350,0,50,50,clrWhite,clrViolet,20,clrWhite,"Up");
 makeButton(350,140,50,50,clrWhite,clrViolet,20,clrWhite,"Dn");
 makeButton(350,190,50,50,clrWhite,clrViolet,20,clrWhite,"Cls");
 screen.update();
end;
refresh();
while true do
 if btnHome() then break end;
 if scrnClick() then 
  -- up button
  if btnClick(350,0,50,50) then
   a = a+1;
   refresh();
  end;
  
  --down button
  if btnClick(350,140,50,50) then
   a = a-1;
   refresh();
  end;
  --close button
  if btnClick(350,190,50,50) then
   break;
  end;
 end;
  
 os.sleep(3);
end;
The app simply increments or decrements a number using the up and down buttons, and has a close button. Very simple, but it taught me several things about X-Fi2 LUA.
Reply With Quote

Advertisement [Remove Advertisement]

  #42  
Old 12-06-2009, 11:52 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

May I recommend some changes?

Code:
function textWrite(x,y,color,size,string)
 text.color(color);
 text.size(size);
 text.draw(x,y,string);
end;
to

Code:
function textWrite(x,y,color,size,string)
 text.color(color);
 local osize = text.size(size);
 text.draw(x,y,string);
text.size(osize)
end;
Code:
function scrnClick()
 if (control.read() == 1) and (control.isTouch() == 1) and ((touch.click() == 1) or (touch.up() == 1)) then 
  x,y = touch.pos();
  return true;
 else
  return false;
 end;
end;
to

Code:
function scrnClick()
 if (control.read() == 1) and (control.isTouch() == 1) and ((touch.click() == 1) or (touch.up() == 1)) then 
  --Not even sure if we have to call the below to remove it from the queue
  local x,y = touch.pos();
  return true;
 else
  return false;
 end;
end;
Code:
function setColors()
 clrBlack = color.new(0,0,0);
 clrWhite = color.new(255,255,255);
 clrRed = color.new(255,0,0);
 clrBlue = color.new(0,0,255);
 clrYellow = color.new(255,255,0);
 clrGreen = color.new(0,255,0);
 clrOrange = color.new(255,128,0);
 clrGray = color.new(128,128,128);
 clrViolet = color.new(128,0,255);
 clrCyan = color.new(0,128,255);
 clrBrown = color.new(128,80,64);
end;
to

Code:
clrBlack = color.new(0,0,0);
clrWhite = color.new(255,255,255);
clrRed = color.new(255,0,0);
clrBlue = color.new(0,0,255);
clrYellow = color.new(255,255,0);
clrGreen = color.new(0,255,0);
clrOrange = color.new(255,128,0);
clrGray = color.new(128,128,128);
clrViolet = color.new(128,0,255);
clrCyan = color.new(0,128,255);
clrBrown = color.new(128,80,64);
If I understand dofile() correctly when you call it and load your library you won't have to call setColors()
Reply With Quote

  #43  
Old 12-07-2009, 09:11 AM
rphunt2002 rphunt2002 is offline
Junior Member
 
Join Date: Nov 2009
Posts: 52
Default

Quote:
you won't have to call setColors()
That's exactly right, and I was on the fence as to which way to go, but I elected to go with functions for everything. Strictly a presonal taste thing.

And thanks for the help on var scope stuff, I need to play wth that some more.
Reply With Quote

  #44  
Old 12-07-2009, 02:08 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Yeh I was trying to push the zen to it's limits and noticed that local scope is MUCH faster in terms of execution speed. Mostly because local variables are kept on a "stack" in the VM.
Reply With Quote

  #45  
Old 12-07-2009, 03:16 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

"and (touch.up() == 1 or touch.click == 1)"

Hi, It works great to avoid double clicks, but it makes clicking slow, and unresponsive.

Regarding the function 'textwrite', what about using the alignment commands, the center command makes it easy to place text, uneffected of text size and number of char's.


Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
color_text  = color.new(255,255,200);
screen.fillrect(0,0,400,240,color_black);
text.color(color_white);
text1="Anything But "; text2="Ipod";
text.size(16);
text.draw(0,10, text1 .. text2, "left",  400);
text.draw(0,30, text1 .. text2, "center",400);
text.draw(0,50, text1 .. text2, "right", 400);
text.size(40);
text.color(color_text);
text.draw(0,100, "CREATIVE X-Fi2", "center", 400);
screen.update();
screen output:

Quote:
Anything but ipod
Anything but ipod
Anything but ipod

CREATIVE X-Fi2
Reply With Quote

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

Code:
if control.read() == 1 and control.isTouch() == 1 and (touch.up() == 1 or touch.click() == 1) then
        x, y = touch.pos()
Hasn't been unresponsive at all for me.
Reply With Quote

  #47  
Old 12-07-2009, 05:48 PM
marcel-o marcel-o is offline
Junior Member
 
Join Date: Dec 2009
Location: Rotterdam, Holland
Posts: 4
Default accelerometer

Nice topic !!
I always like programming on devices other dan a pc

Quote:
Originally Posted by ThievingSix View Post
Well I can't actually get anything beyond 380x235 when I'm touching.

Wanted to get the accelerometer working..but I didn't. I did however get some data:

Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
color_blue = color.new(0,0,255);
color_red = color.new(255,0,0);
color_green = color.new(0,255,0);

screen.clear();
text.size(20);
text.color(color_white);

while true do
  x = accelerometer.getdata();
  y = accelerometer.get_senddatatype();
  z = accelerometer.get_samplerate();
  screen.fillrect(0,0,400,240,color_black);
  text.draw(10,10,"Accelerometer Test");
  text.draw(10,30,x);
  text.draw(10,50,y);
  text.draw(10,70,z);
  screen.update();
  if (control.read() == 1) and (button.home() == 1) then
    break;
  end;
  os.sleep(10);
end;
x: Outputs 0 always. It's probably a pointer which is telling me I'm getting NULL for a result.
y: Outputs...nothing.
z: Outputs(from the values I've noticed) -1, 8, 16, 1000, 500, 250, 64, 125. I get different values depending on how I hold it. When it's flat I get 8, 16, 1000. When it's vertical 500 gets thrown around. 90* angle? 125 and 64 as well. Way to random and way to general for use. Which leads me to think, can this thing only detect angles? If so that's a huge disappointment.
nice work.
indeed a lot of random numbers, but there is a change when you move the device.
i've added some sound with the changing var's
Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
color_blue = color.new(0,0,255);
color_red = color.new(255,0,0);
color_green = color.new(0,255,0);

screen.clear();
text.size(20);
text.color(color_white);

while true do
  x = accelerometer.getdata();
  y = accelerometer.get_senddatatype();
  z = accelerometer.get_samplerate();
  screen.fillrect(0,0,400,240,color_black);
  text.draw(10,10,"Accelerometer Test");
  text.draw(10,30,x);
  text.draw(10,50,y);
  text.draw(10,70,z);
  audio.beep(z+500,20);
  screen.update();
  if (control.read() == 1) and (button.home() == 1) then
    break;
  end;
  os.sleep(4);
end;
The strange thing is that the changing var is the samplerate.
it does not only "feels" the angle but also raising up and down, you can hear the sound changing.

I think that the different data are different types of data.
One range is the angle, 1 range is raise up and down.
it's just a tought.

sorry for my bad english, we call it Nederspeak :P (spenglish for dutch people)
Reply With Quote

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

X-Clock


A clock in size 140 fonts, using the "center" alignment.

Amazing what you can do with few lines, this clock on the O2
would have been 1000's of lines and bitmap font loading and so on. :-)
Here we have fully anti aliasing scalable fonts in three lines.

Code:
-- X-Fi Clock
color_black = color.new(0,0,0);
color_date  = color.new(120,200,255);
color_time  = color.new(0,144,255);

while true do

    text.color(color_date);
    text.size(20);
    text.draw(0,10, os.date(), "center", 400);

    text.color(color_time);
    text.size(140);
    text.draw(0,40, string.sub (os.date(),12,16), "center", 400);

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

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

 os.sleep(10);

end;
To get seconds to display in the big clock edit this:
text.size(140);
text.draw(0,40, string.sub (os.date(),12,16), "center", 400);

to this
text.size(100);
text.draw(0,60, string.sub (os.date(),12,19), "center", 400);

Last edited by Jan_DK; 12-08-2009 at 09:50 AM. Reason: add seconds
Reply With Quote

  #49  
Old 12-07-2009, 08:15 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Absolutely wonderful idea on the accelerometer with the beeps. Still trying to find out to make it practical.
Reply With Quote

  #50  
Old 12-08-2009, 07:11 AM
Mike15 Mike15 is offline
Junior Member
 
Join Date: Dec 2009
Posts: 2
Default

Hi guys. I just bought my fi2 and am waiting for it.
Glad to see it can be programmed!
Reply With Quote

  #51  
Old 12-08-2009, 05:10 PM
skybluedream skybluedream is offline
Member
 
Join Date: Oct 2009
Posts: 132
Default

Yup for sure its nice to see this player can be programmed. If this thing gets more popular we might get some homebrew scene going if we're in luck!
Reply With Quote

  #52  
Old 12-08-2009, 05:33 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

It's still seriously limited in my view. In terms of the API we are given in terms of images, screen, and drawing are all very set. Nor can we counteract by redrawing a lot because the thing just isn't very powerful especially when reduced to a scripting language.

The touch screen isn't responsive enough for things passed tic-tac-toe(I would loved to be proved wrong here) and the accelerometer can only detect the orientation you are holding the player.

Homebrew are going to have to be either simplistic or creative to be actually usable.

Edit: Was messing around with image.create() today. Was hoping it created a copy of the screen at selected coordinates, because we have no way of getting data from the screen. Well, course it didn't to what I wanted. It basically created a new image with non-initialized memory. Completely useless if you ask me. I don't see how I can draw to the image, nor can we save it.

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

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

The only LUA/X-Fi2 thing I've fund out today is to play audio wave files

The wave file was a 44100hz stereo 1.2Mb
the range in the volume I'm not sure of?

Code:
audio.volume(25);
background_sound = wav.load("sound1.wav");
wav.play(background_sound);
Anyone knows how the image.create function works ?
I'm looking for a way to take a screen shot and save to disk.

Jan.
Reply With Quote

  #54  
Old 12-09-2009, 04:31 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

lol Jan, read my last post, it's what I've been trying to screw around with as well.

And I want to correct my last post. I can write to newly created images with image.fill(color), but not entirely useful.
Reply With Quote

  #55  
Old 12-10-2009, 01:13 AM
marcel-o marcel-o is offline
Junior Member
 
Join Date: Dec 2009
Location: Rotterdam, Holland
Posts: 4
Default

Quote:
Originally Posted by Jan_DK View Post
The only LUA/X-Fi2 thing I've fund out today is to play audio wave files

The wave file was a 44100hz stereo 1.2Mb
the range in the volume I'm not sure of?

Code:
audio.volume(25);
background_sound = wav.load("sound1.wav");
wav.play(background_sound);
Anyone knows how the image.create function works ?
I'm looking for a way to take a screen shot and save to disk.

Jan.
ah.. Nice!
maybe its possible to make something like Axe for the Nintendo DS .
But in that app, the sound is generated. Should also be possible i think.
Reply With Quote

  #56  
Old 12-10-2009, 03:42 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Not possible. We don't have sufficient access to the hardware directly for an application like that.
Reply With Quote

  #57  
Old 12-14-2009, 01:43 PM
badazzmofo badazzmofo is offline
Junior Member
 
Join Date: Dec 2009
Posts: 75
Thumb Up so cool

nice job all.

i have one question that there seems to be some ambiguity on...

can i save straight text with a .lua extension, or do i need a program to change it to that bytecode? if so, which program should i use?

also, if anyone is looking to build a game for this, might i suggest atari games?

the old atari had crap resolution for its controllers and crap resolution for its screen, yet the games were badass...not to mention the code for them in various languages is available as public domain.

asteroids would be nice...joust, missile command maybe?
Reply With Quote

  #58  
Old 12-14-2009, 02:19 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

You can use notepad in windows,
no converting or compiling is necessary.

1. make a folder in the application folder on the X-fi2.
2. save the text file as MAIN.LUA
Reply With Quote

  #59  
Old 12-14-2009, 08:22 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

You can do either, compile or no compile. One would have less load time.
Reply With Quote

  #60  
Old 12-15-2009, 04:26 AM
badazzmofo badazzmofo is offline
Junior Member
 
Join Date: Dec 2009
Posts: 75
Default

thanks.

what about reading the soduku.lua...what program would i use?

i'd like to use lua to change a few little things

-thicker volume control always showing

-turn off screen while docked

-move scroller position to touch point without having to drag the little button
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 10:04 PM.