Accelerometer

From WebOS Internals
Revision as of 00:37, 4 April 2010 by Zinge (talk | contribs) (added note about lightweight stages)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The accelerometer appears to be a "KXSD9 SERIES Tri-Axis, 2g, 4g, 6g, 8g, User Selectable, Ultra Low Power Digital" http://www.kionix.com/accelerometers/accelerometer-KXSD9.html

<source lang="text"> root@castle:/sys/class/input/input5# cat name kxsd9_accelerometer </source>

more info: <source lang="text"> root@castle:/sys/class/input/input5# cat uevent PHYSDEVPATH=/class/i2c-adapter/i2c-3/3-0018 PHYSDEVBUS=i2c PHYSDEVDRIVER=kxsd9_accelerometer PRODUCT=18/1/1/100 NAME="kxsd9_accelerometer" EV==9 ABS==7 MODALIAS=input:b0018v0001p0001e0100-e0,3,kra0,1,2,mlsfw </source>

and at the very least you could adjust the low pass filter used, or the threshold for waking up:

<source lang="text"> root@castle:/sys/class/input/input5# ls -al drwxr-xr-x 6 root root 0 Jun 11 17:47 . drwxr-xr-x 8 root root 0 Jun 11 17:47 .. -rw-r--r-- 1 root root 4096 Jun 11 23:58 accelerometer_filter_frequency -rw-r--r-- 1 root root 4096 Jun 11 23:58 accelerometer_motion_wake_up_threshold drwxr-xr-x 2 root root 0 Jun 11 23:58 capabilities lrwxrwxrwx 1 root root 0 Jun 11 23:58 device -> ../../../class/i2c-adapter/i2c-3/3-0018 drwxr-xr-x 3 root root 0 Jun 11 23:49 event5 drwxr-xr-x 2 root root 0 Jun 11 23:58 id lrwxrwxrwx 1 root root 0 Jun 11 23:58 input:event5 -> ../../../class/input/input5/event5 -r--r--r-- 1 root root 4096 Jun 11 23:58 modalias -rw-r--r-- 1 root root 0 Jun 11 23:50 mode -r--r--r-- 1 root root 4096 Jun 11 23:53 name -r--r--r-- 1 root root 4096 Jun 11 23:58 phys -rw-r--r-- 1 root root 4096 Jun 11 23:58 poll_interval drwxr-xr-x 2 root root 0 Jun 11 23:58 power lrwxrwxrwx 1 root root 0 Jun 11 23:58 subsystem -> ../../../class/input -rw-r--r-- 1 root root 4096 Jun 11 23:58 uevent -r--r--r-- 1 root root 4096 Jun 11 23:58 uniq </source>


More accelerometer info

There's a library called libhidaccelerometer.so -- so it's treated as an HID class of devices. There is no dbus activity when you move the device, so this probably a much lower level service. Makes sense: you don't want zillions of dbus messages firing off whenever someone moves.

Looking in running processes, I notice /usr/bin/hidd, which references a configuration file /etc/hidd/HidPlugins.xml

In this file, two sockets are mentioned: /var/tmp/hidd/AccelerometerCmdSocket /var/tmp/hidd/AccelerometerEventSocket

These are UNIX DGRAM sockets, which are open on the device.

Read Accelerometer from hidd

  1. Implement the sample code in hidd
  2. Change the main function to below

<source lang="c"> char code_names[] = {'x','y','z'};

int main() {

 void *pHandle;
 struct input_event myevents[20];
 int ret = 0;
 int i = 0;
 pHandle = HidEventOpen(1);
 while (1) {
   ret = HidEventRead(pHandle, myevents, 20);
   for (i = 0; i < ret; i++)
   {
       float* value = (float*)(&myevents[i].value);
       if (myevents[i].type == 3 &&
         myevents[i].code < 3)
       {
         printf("%c: %f ",
           code_names[myevents[i].code],
           *value);
       }
   }
   printf("\n");
 }
 HidEventDeInit();
 HidEventClose(pHandle);
 return 0;

}

</source>


Sample code

Taken from boydell's Magic 8 Ball app:

In his first-assistant.js (... = code skips) <source lang="javascript"> ...

       this.controller.listen(this.controller.sceneElement, Mojo.Event.tap, this.handleTap.bind(this));
       this.controller.listen(document, 'shakestart', this.handleShakeStart.bind(this));
       this.controller.listen(document, 'shaking', this.handleShaking.bind(this));
       this.controller.listen(document, 'shakeend', this.handleShakeEnd.bind(this));

... FirstAssistant.prototype.handleShakeStart = function(event) {

       this.setBall();
       this.hideMessage();
       Event.stop(event);

}

FirstAssistant.prototype.handleShaking = function(event) {

       this.setBall();
       this.hideMessage();
       Event.stop(event);

}

FirstAssistant.prototype.handleShakeEnd = function(event) {

       this.showRandomMessage();
       Event.stop(event);

</source>


To increase the resolution to 30Hz, use this snippet (requires WebOS 1.3.5 or above): <source lang="javascript"> this.controller.stageController.setWindowProperties("fastAccelerometer"); </source>

Note: After hours of trying to get the accelerometer working in my app, I figured out that if you are using createStageWithCallback to create your stages (such as in handleLaunch in the App Assistant), in the stage arguments, the "lightweight" property MUST be set to false. If it is set to true, the accelerometer will never return data (free window orientation will still work, however).