{"id":324,"date":"2013-04-20T14:02:03","date_gmt":"2013-04-20T21:02:03","guid":{"rendered":"http:\/\/ainslies.net\/?p=324"},"modified":"2013-04-20T14:10:46","modified_gmt":"2013-04-20T21:10:46","slug":"imwatch-code-to-read-the-accelerometer","status":"publish","type":"post","link":"https:\/\/ainslies.net\/?p=324","title":{"rendered":"i&#8217;m Watch code to read the accelerometer"},"content":{"rendered":"<p>I figured out how to read and write the i2c bus on the imWatch so I can now sample at whatever rate I&#8217;d like. This <a href=\"http:\/\/akkea.ca\/files\/IMWatchSensors.tgz\" target=\"_blank\">tarball<\/a> includes code that should work upto 200Hz without overloading the processor.<\/p>\n<p>Faster rates should be possible but it would mean getting the FIFO in the accelerometer working and then backdating the samples.<\/p>\n<p><strong>Building the JNI<\/strong><\/p>\n<p>I&#8217;m not going to detail how to use a JNI within an Android project. there are plenty of other tutorials out there.<\/p>\n<p>From the tarball put &#8220;akkea-jni.c&#8221; and &#8220;Android.mk&#8221; into the jni directory of your android\u00a0&gt;project. Then run<\/p>\n<pre>ndk-build<\/pre>\n<p>You should get output similar to below.<\/p>\n<pre>Compile thumb : akkea-jni &lt;= akkea-jni.c\r\nSharedLibrary : libakkea-jni.so\r\nInstall : libakkea-jni.so =&gt; libs\/armeabi\/libakkea-jni.so<\/pre>\n<p><strong>Using the Accelerometer JNI code<\/strong><\/p>\n<p>Add these native prototypes and constants to your android code.<\/p>\n<pre>public native int readAccel( SeismoVector vector, int fd, long startTime );\r\npublic native int openI2c( String path );\r\npublic native void closeI2c( int fileHandle );\r\npublic native int i2cReadReg( int file, int addr, int reg);\r\npublic native int i2cWriteReg( int file, int addr, int reg, int val);\r\n\r\nint i2cHandle;\r\nfinal byte ACCEL_I2C_ADDR = 25;\r\nfinal int ACCEL_RATE_OFF = 0;\r\nfinal int ACCEL_RATE_1HZ = 1 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_10HZ = 2 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_25HZ = 3 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_50HZ = 4 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_100HZ = 5 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_200HZ = 6 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_400HZ = 7 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_1_6KHZ = 8 &lt;&lt; 4;\r\nfinal int ACCEL_RATE_5KHZ = 9 &lt;&lt; 4;<\/pre>\n<p>The I2C bus and accelerometer need to be setup<\/p>\n<pre>public boolean openI2C() {\r\n\r\n  try\r\n  {\r\n    Runtime.getRuntime().exec(\"chmod 777 \/dev\/i2c-0\");\r\n  } catch (IOException e) {\r\n    e.printStackTrace();\r\n  }\r\n\r\n  i2cHandle = openI2c( \"\/dev\/i2c-0\" );\r\n\r\n  if( i2cHandle &lt;= 0 )\r\n    return false;\r\n\r\n  int[] arr = new int[4];\r\n  for( int i=0; i&lt;128; i++ ) {\r\n    arr[0] = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, i );\r\n    if( arr[0] != 0 )\r\n      Log.d(TAG, \"Register \" + String.valueOf( i ) + \" = \" + String.valueOf( arr[0] ));\r\n  }\r\n\r\n  int val;\r\n  val = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, 0x20 );\r\n  val = ( val &amp; 0x0F );\r\n\r\n  switch( HZ ) {\r\n    case 1:\r\n      val |= ACCEL_RATE_1HZ;\r\n      break;\r\n    case 10:\r\n      val |= ACCEL_RATE_10HZ;\r\n      break;\r\n    case 25:\r\n      val |= ACCEL_RATE_25HZ;\r\n      break;\r\n    case 100:\r\n      val |= ACCEL_RATE_100HZ;\r\n      break;\r\n    case 200:\r\n      val |= ACCEL_RATE_200HZ;\r\n      break;\r\n    case 400:\r\n      val |= ACCEL_RATE_400HZ;\r\n      break;\r\n    case 1600:\r\n      val |= ACCEL_RATE_1_6KHZ;\r\n      break;\r\n  }\r\n\r\n  i2cWriteReg( i2cHandle, ACCEL_I2C_ADDR, 0x20, val );\r\n\r\n  val = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, 0x20 );\r\n  Log.d(TAG, \"Register Rate \" + String.valueOf( val ));\r\n\r\n  return true;\r\n}<\/pre>\n<p>I then use a TimerTask to read the accelerometer at whatever rate I want.<\/p>\n<pre>class sensorTask extends TimerTask {\r\n  @Override\r\n    public void run() {\r\n      long time;\r\n\r\n      if( index &gt;= totalSamples ) {\r\n        Log.d(TAG, \"index past end of array\");\r\n        return;\r\n      }\r\n\r\n      SeismoVector vector = new SeismoVector();\r\n\r\n      time = readAccel( vector, i2cHandle, startTime );\r\n\r\n      if( startTime == 0 )\r\n        startTime = time;\r\n\r\n      vectors[index++] = vector;\r\n\r\n    }\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I figured out how to read and write the i2c bus on the imWatch so I can now sample at whatever rate I&#8217;d like. This tarball includes code that should work upto 200Hz without overloading the processor. Faster rates should &hellip; <a href=\"https:\/\/ainslies.net\/?p=324\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-android-devices"],"_links":{"self":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324"}],"version-history":[{"count":10,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":334,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions\/334"}],"wp:attachment":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}