Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Orientation and accelerometer

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2010, 02:50 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Orientation and accelerometer

The attached program displays the values of the different sensors:



It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.
Attached Files
File Type: zip SensorsExample.zip (5.5 KB, 1090 views)
Reply With Quote
  #2 (permalink)  
Old 11-24-2010, 07:47 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

Hi Erel, I have an issue....
I force my main Activitie to be portrait, and all the other 4 activities I have in the current project, but only the main one stays in portrait mode when I rotate my device....
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!
Reply With Quote
  #3 (permalink)  
Old 11-25-2010, 05:35 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Fixed for the next update.
Reply With Quote
  #4 (permalink)  
Old 01-09-2011, 08:59 AM
Senior Member
 
Join Date: Jan 2011
Posts: 157
Default

Hello Erel

The orientation sensor seems to have an impact on the overall timing of an app. It seems to steal quite a bit of operation time
Could you look at adding options to set the polling rate?

Have a look at this example to see what I mean
Here, a ball is set to roll when the phone is tilted (NOTE: operate in landcape mode). On my Samsung Galaxy S there are notable delays as the ball travels
I have set the timer to 10ms interval in order to allow some leeway for the orientation sensor but there is still a significant impact when polling the sensor

Code:
'AccelBall
Sub Process_Globals
    
Dim Orientation As PhoneOrientation
    
Dim time As Timer
End Sub

Sub Globals
    
Dim can As Canvas
    
Dim ballx,bally,ballsize As Float
    
Dim targety As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ballx=
80 : bally=Activity.Height-Activity.Height/2
    ballsize=
42 : targety=bally
    can.Initialize(Activity)
    time.Initialize(
"Timer1",10)
End Sub

Sub Activity_Resume
    Orientation.StartListening(
"Orientation"
    time.Enabled=
True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Orientation.StopListening
    time.Enabled=
False
End Sub

Sub Timer1_Tick
    can.DrawColor(
Colors.Black)
    bally=bally+(targety-bally)/
2.0
    can.DrawLine(ballx,
0,ballx,Activity.Height,Colors.Red,1.0)
    can.DrawCircle(ballx+ballsize,bally,ballsize,
Colors.White,True,0.0)
    Activity.Invalidate
End Sub

Sub Orientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)
    targety=targety-Pitch
    
If targety<ballsize Then targety=ballsize
    
If targety>(Activity.Height-ballsize) Then targety=(Activity.Height-ballsize)
End Sub
Reply With Quote
  #5 (permalink)  
Old 01-09-2011, 12:39 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should not redraw the whole activity each time.
The Accelerometer is more useful for this task.

This code works smoothly: Before running it go to Project - Orientation Supported and choose Landscape only.
Code:
'AccelBall
Sub Process_Globals
    
Dim Accelerometer As PhoneAccelerometer
    
Dim timer1 As Timer
    
Dim speed As Float
    
Dim position As Int
    
Dim positionY As Int
End Sub

Sub Globals
    
Dim can As Canvas
    
Dim rect1 As Rect
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
If firstTime Then
        timer1.Initialize(
"Timer1",30)
    
End If
    positionY = 
100%y - 100dip
    can.Initialize(Activity)
    can.DrawLine(
0,positionY + 21dip100%x, positionY + 21dip,Colors.Red,1.0)
    position = 
50%x
    rect1.Initialize(
0, positionY - 20dip40dip, positionY + 20dip)
End Sub

Sub Activity_Resume
    Accelerometer.StartListening(
"Accelerometer")
    timer1.Enabled = 
True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Accelerometer.StopListening
    timer1.Enabled = 
False
End Sub

Sub Timer1_Tick
    can.DrawCircle(position, positionY, 
20dipColors.Black, True0)
    activity.Invalidate2(rect1)
    position = position + speed
    
If position > 100%x + 20dip Then position = -18dip
    
If position < -20dip Then position = 100%x + 18dip
    rect1.Left = position - 
20dip
    rect1.Right = position + 
40dip
    can.DrawCircle(position, positionY, 
20dipColors.White, True0)
    activity.Invalidate2(rect1)
End Sub

Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)
    speed = speed + Y * 
0.5
End Sub
Reply With Quote
  #6 (permalink)  
Old 01-11-2011, 11:32 AM
Senior Member
 
Join Date: Jan 2011
Posts: 157
Default

Thank you as usual Erel

I take it 'dirty rects' is the way forward for updating the screen whilst maintaining relatively smooth performance?
Is this because the draw routines need to be system-friendly (not stealing too much processing power)?


In general, what would be the best solution if you needed to redraw the whole screen, say in a scrolling game environment?
I am guessing this would have to be done with a 2D OpenGL library ?
Reply With Quote
  #7 (permalink)  
Old 01-11-2011, 11:49 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should try to only refresh the parts required.

Refreshing the whole screen each time can be done. In your code you also drew the whole screen each tick.
Reply With Quote
  #8 (permalink)  
Old 02-06-2011, 02:33 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The first post was updated with code that uses the new PhoneSensors object.
Reply With Quote
  #9 (permalink)  
Old 02-12-2011, 02:34 AM
Junior Member
 
Join Date: Feb 2011
Posts: 21
Default

I'm having trouble running this example. I get this error:

Compiling code. Error
Error parsing program.
Error description: Unknown type: phonesensors
Are you missing a library reference?
Occurred on line: 16
Dim ps As PhoneSensors 'This object is only used to access the type constants.

In the IDE, I have Core, GPS, and Phone(vers 1.31) selected.
Am I missing a reference?

Be nice if there was a "project" option in the IDE to have the references already setup.
Reply With Quote
  #10 (permalink)  
Old 02-12-2011, 03:25 AM
alfcen's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
Default

The latest Phone library version is 1.32.
Please make sure to copy the new library files into the Libraries folder underneath C:\Program Files\Anywhere Software\Basic4android
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Force Orientation merli Questions (Windows Mobile) 1 01-05-2010 04:50 PM
Orientation problem HARRY Questions (Windows Mobile) 5 11-17-2009 06:06 PM
Using Diamond's Accelerometer Mr_Gee Questions (Windows Mobile) 17 01-18-2009 03:00 PM
Portrait-landscape-accelerometer on HTC Phone. dan kabestan Questions (Windows Mobile) 4 01-09-2009 06:34 PM
sceen orientation Filec Questions (Windows Mobile) 3 07-13-2007 12:34 PM


All times are GMT. The time now is 10:19 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0