gyronorm.js

gyronorm.js

I had two previous posts on accessing the accelerometer and gyroscope data on mobile devices via JavaScript. Part I mentions how to access them, and Part II is about the differences between devices.

With these learnings I put together a JavaScript file. By default it returns (almost) the same values for different devices. So you can program your logic on top of it and do not worry about these differences.

It can be found on GitHub https://github.com/dorukeker/gyronorm.js 

Here is a basic usage:

<script src="/js/gyronorm.js"></script>

<script>
    var gn = new GyroNorm();
    gn.start(function(data){
        // Process:
        // data.do.alpha    ( deviceorientation event alpha value )
        // data.do.beta     ( deviceorientation event beta value )
        // data.do.gamma    ( deviceorientation event gamma value )
        // data.do.absolute ( deviceorientation event absolute value )

        // data.dm.x        ( devicemotion event acceleration x value )
        // data.dm.y        ( devicemotion event acceleration y value )
        // data.dm.z        ( devicemotion event acceleration z value )

        // data.dm.gx       ( devicemotion event accelerationIncludingGravity x value )
        // data.dm.gy       ( devicemotion event accelerationIncludingGravity y value )
        // data.dm.gz       ( devicemotion event accelerationIncludingGravity z value )

        // data.dm.alpha    ( devicemotion event rotationRate alpha value )
        // data.dm.beta     ( devicemotion event rotationRate beta value )
        // data.dm.gamma    ( devicemotion event rotationRate gamma value )
    });
</script>

You can learn more about the methods and options on the Readme of the GitHub page.

Next Steps

I plan to add module loader support and refactor to avoid the need for globals. I will update this post and the GitHub page as the project evolves.

UPDATE:

I have updated the GitHub project as mentioned in the next steps. You can visit the GitHub page and get the latest code. I have also updated the code sample in this post.