Understanding Accelerometer Development
by @ryancanullaWhat is an Accelerometer?
An accelerometer is a piece of hardware that measures the acceleration of forces. There are two types of forces that we care about; static and dynamic. You can think of static force as gravity which at all times is pulling us towards the center of the earth. We use static acceleration to figure out the angle an object is tilted. Dynamic acceleration is used for measuring the actual motion of an object. For this intro we’ll be talking mostly about tilt-based static acceleration.
Common uses
There are accelerometers all around us from our cars to our computers and phones. In cars they use accelerometer data to determine vibrations on the engine and in the airbag sensors. Companies also use accelerometers in hard-drives to protect data during collisions and general impact. Nintendo Wii shaped the future of motion gaming adding an accelerometer to their remote. Whenever you see a labyrinth style game or are playing a driving game by tilting your phone or tablet you can thank the accelerometer. As developers it’s our job to learn and interpret this information in order to improve the experience to our users.
How to handle local accelerometer on mobile device
When you are writing flash application that will handle local accelerometer updates from a phone or tablet the work flow is very simple. You check the to see if the device supports accelerometer using the flash.sensors.Accelerometer.isSupported() method. If it is supported you register a few event listeners, then handle the updates when they come in.
if (Accelerometer.isSupported) {
accelerometer = new Accelerometer();
accelerometer.addEventListener(AccelerometerEvent.UPDATE, accUpdateHandler);
}
private final function accUpdateHandler(event:AccelerometerEvent):void {
xSpeed -= event.accelerationX;
ySpeed += event.accelerationY;
}
How to handle accelerometer data from litl os remotes
When you are dealing with multiple remotes connected to a single application things get a bit more complicated because you need to pair remote updates with specific remotes, manage connections, and not mix it up in the meantime. This is an approach that works for us. It involves the creation of three classes by the developer.

LitlRemoteManager()
We will need to extend RemoteHandlerManager in order to inherit functionality. By doing this now we can override two important methods; onRemoteConnected and onRemoteDisconnected. You can do whatever you want inside these methods knowing that they’ll be called when a remote connects/disconnects.
You will need to instantiate after you are connected to the LitlService() as you will need to pass a reference to your service into it’s constructor. You will also need to instantiate a new RemoteFactory() in the constructor. This is what it looks like in our instantiation:
private function createRemoteManager():void {
remoteManager = new LitlRemoteManager(_service, new RemoteFactory());
}
RemoteFactory()
Implement IRemoteFactory(), return RemoteHandler() and you are all set. I usually set this up once and barely look at it again.
package service
{
import com.litl.helpers.richinput.remotehandler.IRemoteHandler;
import com.litl.helpers.richinput.remotehandler.IRemoteHandlerFactory;
public class RemoteFactory implements IRemoteHandlerFactory
{
public function RemoteFactory() {
}
public function createHandler():IRemoteHandler {
return new RemoteHandler();
}
public function get klass():Class {
return RemoteHandler;
}
}
}
RemoteHandler()
We have one job left to do and RemoteHandler() keeps it simple and to the point. This is where we define what to do when we get accelerometer updates. Start by extending AccelerometerEventHandler() and implement IRemoteHandler(). Next, override onAccelerometerEvent and handle your logic to handle accelerometer data. I usually loop through an array of remotes/players in my model and reference the remoteID property to match up remotes.
package service
{
import com.litl.helpers.richinput.remotehandler.AccelerometerRemoteHandler;
import com.litl.helpers.richinput.remotehandler.IRemoteHandler;
import com.litl.sdk.event.AccelerometerEvent;
public class RemoteHandler extends AccelerometerRemoteHandler implements IRemoteHandler
{
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public function RemoteHandler() {
super();
}
override protected function onAccelerometerEvent(event:AccelerometerEvent):void {
xSpeed -= event.accelerationX;
ySpeed += event.accelerationY;
}
}
}
Wrapping up
As you can see there are not too many differences between the two workflows. Check back soon as we'll be posting more advanced accelerometer development articles, tutorials, tips and tricks!
Join us at RIA Unleashed Workshop / Webinar this Thursday, Nov 11 at RIAUnleashed
by @ryancanullaBe prepared to get your hands dirty on Thursday, 11/11 at 1:00 during the RIAUnleashed workshop. Kathryn Rotondo and Ryan Canulla will teach and guide you through the latest litl technology, including our updated Channel SDK, new AIR simulator and exciting Android-remote controlled Accelerometer features. We’ll hand out USB sticks with the latest litl bits and Chuck Freedman will be serving as Teaching assistant to help you during the workshop.
We are happy to announce that our special developer incentive program, which awards each developer that submits a channel both $500 and a free litl TV Companion device, will be extended to those attending the workshop.
Lastly, while those in attendance at the RIAunleashed workshop will get to bask in the brilliance of our presenters, we are streaming our workshop to our developer community via a LIVE webinar.
So, whether attending in person, or looking to get a limited seat for the LIVE webinar, please sign up here:
More details on the workshop:This lab is a great opportunity for developers interested in deploying Flash/Flex Channels on the litl platform. We'll have a good portion of our team on hand to work directly with you, doing a live walk through of our devices, platform, FAQs and sample channel code. The sample channel code projects are ready to import into either Flash CS5 or FlashBuilder, so they're ready-to-run. After introducing you to the devices, SDK and platform, we'll help you code your own channels and set you up to deploy them in our Channel store!
