Monday, May 17, 2010

Creative ZEN Xi-Fi2 acceleratorometer visualisation

I made a small soft for my wife's mp3 player.
Just drop it into a lua file in the mp3 player application folder (into a new folder,and the file name is main,and you need a png icon from an other folder:-)).
Here is the source:

--acc_met_visual

height = screen.height()

width= screen.width()

black = color.new(0,0,0)

red = color.new(255,0,0)



screen.fillrect(0,0,width,height,black)


text.color(red)



text.size(30)
text.draw(0,80,"fsdf")

accelerometer.open()
os.sleep(10)
accelerometer.set_samplerate(100)
os.sleep(10)
accelerometer.set_datatype("all")
os.sleep(10)
screen.update()
screen.fillrect(0,0,width,height,red)

text.draw(0,height -30,"fsdf")
text.draw(0,height -60,"adas")
text.draw(0,height -90,"mnb")
screen.update()






za=0
ya=0
xa=0
cyc_x=1
while true do

if (control.read() == 1)and (control.isSensor() == 1) then
text.draw(0,height-120,"XXXXXX")



z_cord=accelerometer.getdata("z")
os.sleep(10)
x_cord=accelerometer.getdata("x")
os.sleep(10)
y_cord=accelerometer.getdata("y")
os.sleep(10)
za=za*0.1+z_cord*0.9
ya=ya*0.1+x_cord*0.9
xa=xa*0.1+y_cord*0.9
screen.fillrect(0,120,width-150,height,black)
text.draw(0,height-30,math.floor(z_cord))
text.draw(0,height-60,math.floor(x_cord))
text.draw(0,height-90,math.floor(y_cord))
col_a = color.new((za+15)*8,(xa+15)*8,(ya+15)*8)
screen.fillrect(cyc_x,0,cyc_x,120,black)
screen.fillrect(230,0,280,120,col_a)
screen.update()
screen.drawpixel(cyc_x,za+30,color.new(220,120,120))
screen.drawpixel(cyc_x,xa+60,color.new(120,220,120))
screen.drawpixel(cyc_x,ya+90,color.new(120,120,220))
screen.drawpixel(width-100+ya*4,height-4*xa-64,col_a)
screen.update()
cyc_x=cyc_x+1
if cyc_x>220 then
screen.fillrect(0,120,width,height,black)
cyc_x=1
end
screen.update()



end
if (control.read() == 1) and (control.isButton() == 1)
then
screen.fillrect(0,0,width,height,black)

text.draw(0,height-90,"sdfsf")
screen.update()
break



end
os.sleep(10)


end
text.draw(0,height-90,"endgame")
screen.update()
accelerometer.close()

0 comments: