I was playing with AppGameKit V2 Alpha 7.1 and its sensor commands. Much like the excellent tutorial that has now been published in the newsletter. I decided to make a compass.
AGK V2 makes it so easy. Most of the code is to create the graphics. There is no media needed - just copy and paste the code.
// Project: Compass
// Created: 2014-09-26
// set window properties
SetWindowTitle( "Compass" )
// set display properties
SetOrientationAllowed( 1, 0, 0,0 ) ` For simplicity dont allow screen rotation
SetDisplayAspect( 0.625 ) `the aspect ratio of my nexus 7
`set variables
Dim AveAngleY# [100]
Dim AveAngleX# [100]
count as integer
tune#=-90.0 ` I think my calculations were 90 degrees out - this sorts it
scale=60
`create Exit Button
` draw powerbutton
DrawEllipse (50,50,10,10*0.625,yellow,255,1)
DrawEllipse (50,50,8,8*0.625,0,0,1)
drawBox(47,50,53,60,0,0,0,0,1)
drawBox(49,52,51,58,255,0,0,255,1)
Render()
GetImage( 4, 33, 35,30 ,30)
clearScreen()
AddVirtualButton( 1, 89, 11*0.625, 20 )
SetVirtualButtonColor( 1, 255, 0, 0 )
SetPrintColor( 255, 255, 125 )
setvirtualbuttonimageup(1,4)
SetVirtualButtonVisible(1,0)
`Check for magnetic sensor
SensorAvailable=GetMagneticSensorExists()
if SensorAvailable=1
`create compass sprite
yellow=MakeColor( 255, 255, 125 )
DrawEllipse (50,50,40,40*0.625,yellow,125,1)
DrawEllipse (50,50,38,38*0.625,0,0,1)
for temp=1 to 5
if temp=1 then x#=10
if temp=2 then x#=90
if temp=3 then x#=50
if temp=4 then x#=20.3125
if temp=5 then x#=79.6875
y# = sqrt(1600 - (x#-50)*(x#-50))
Y#=Y#*0.625
DrawEllipse( x#, 50-y#, 2, 2*0.625, yellow, 125, 1)
if temp=3 or temp=4 or temp=5 then DrawEllipse( x#, 50+y#, 2, 2*0.625, yellow, 125, 1)
if temp=4 then l1y#=50-y#
if temp=5 then l2y#=50+y#
next temp
DrawLine( 50, 25, 50, 75, 255, 255, 155 )
DrawLine( 10, 50, 90, 50, 255, 255, 155 )
DrawLine( 20.3125, l1y#, 79.6875, l2y#, 255, 255, 155 )
DrawLine( 20.3125,l2y#,79.6875,l1y#, 255, 255, 155 )
CreateText( 1,"N" )
CreateText( 2,"E" )
CreateText( 3,"W" )
CreateText( 4,"S" )
for temp=1 to 4
SetTextAlignment( temp, 1 )
setTextColor(temp,255, 255, 155,255)
next temp
settextSize(1,6)
SetTextPosition(1,50,19)
SetTextPosition(2,95,48)
SetTextPosition(3,5,48)
SetTextPosition(4,50,76)
Render()
GetImage( 1, 0, 0,100 ,100 )
clearScreen()
for temp=1 to 4
DeleteText (temp)
next temp
`draw middle of compass for spirit level
DrawEllipse (50,50,13,13*0.625,yellow,125,1)
DrawEllipse (50,50,11,11*0.625,0,0,1)
Render()
GetImage( 2, 0, 0,100 ,100 )
clearScreen()
`draw ball for spirit level
DrawEllipse (50,50,10,10*0.625,yellow,255,1)
Render()
GetImage( 3, 0, 0,100 ,100 )
clearScreen()
`create sprites
CreateSprite(1,1)
setspritesize(1,100,100)
SetSpriteOffset(1,50,50)
SetSpritePositionByOffset(1,50,50)
CreateSprite(2,2)
setspritesize(2,100,100)
SetSpriteOffset(2,50,50)
SetSpritePositionByOffset(2,50,50)
CreateSprite(3,3)
setspritesize(3,100,100)
SetSpriteOffset(3,50,50)
SetSpritePositionByOffset(3,50,50)
SetVirtualButtonVisible(1,1)
`text
CreateText(1,"Compass")
settextsize(1,10)
SetTextColor(1,255,0,0,255)
settextposition(1,5,1)
CreateText(2,"By JammySoft")
SetTextColor(2,255,255,155,255)
settextposition(2,12,12)
CreateText(3,"For compass to be accurate,")
settextsize(3,3.5)
SetTextColor(3,255,255,155,255)
settextposition(3,2,80)
CreateText(4,"your device has to be flat.")
settextsize(4,3.5)
SetTextColor(4,255,255,155,255)
settextposition(4,2,85)
CreateText(5,"Keep the ball in the centre")
settextsize(5,3.5)
SetTextColor(5,255,255,155,255)
settextposition(5,2,90)
CreateText(6,"to keep your device flat.")
settextsize(6,3.5)
SetTextColor(6,255,255,155,255)
settextposition(6,2,95)
`main Loop
do
` Get Magnetic Sensor readings
MagX# = GetRawMagneticX()
MagY# = GetRawMagneticY()
`MagZ# = GetRawMagneticZ()
` Get Accelerometer Sensor readings
DevRotX# = GetRawAccelX()*Scale
DevRotY# = (GetRawAccely() *0.625)*scale
DevRotZ# = GetRawAccelz() *scale
` Work out angle and turn Compass
angle#= ATanFull( MagY#, MagX# )
resultangle#=angle#+tune#
SetSpriteAngle(1,resultangle#)
`Work out an Average of Accelerometer readings to smooth movement
count=count+1
if count=61 then count=1
AveAngleY#[count]=DevRotY#
AveAngleX#[count]=DevRotX#
totangleY#=0
totangleX#=0
for temp=1 to 60
totangleY#=totangleY#+ AveAngleY#[temp]
totangleX#=totangleX#+ AveAngleX#[temp]
next temp
resultangleY#=totangleY#/60
resultangleX#=totangleX#/60
`Position ball spirit level
SetSpritePositionByOffset(3,50+resultangleX#,50+resultangleY#*0.625)
if GetVirtualButtonReleased( 1 ) =1 then exit
Sync()
loop
else
`if no Sensor found then say so.
SetVirtualButtonVisible(1,1)
do
Print("")
Print("")
Print("")
Print("")
Print("")
Print("")
Print("")
Print ("No magnetic sensor available.")
if GetVirtualButtonReleased( 1 ) =1 then exit
sync()
loop
endif
I tried to make it rotation independent, but the trigonometry got too complicated for me. If you can work it out it would be nice if you can post the solution.