Click here to Skip to main content
13,055,884 members (51,034 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

23.2K views
19 bookmarked
Posted 6 Jan 2009

Using the APS Accelerometer in Lenovo Laptops

, 6 Jan 2009
Use the accelerometer in Lenovo laptops to determine the computer's attitude and temperature.
AS3.swf
AxInterop.ShockwaveFlashObjects.dll
Flash.exe
Interop.ShockwaveFlashObjects.dll
Flash
AS3.swf
bin
Release
AS3.swf
AxInterop.ShockwaveFlashObjects.dll
Flash.exe
Flash.vshost.exe
Flash.vshost.exe.manifest
Interop.ShockwaveFlashObjects.dll
Properties
namespace MWudka
{
    using System.Runtime.InteropServices;

    /// <summary>
    /// Provides a moderately convenient interface to the Lenovo APS
    /// </summary>
    public class APS
    {
        /// <summary>
        /// The horizontal center
        /// </summary>
        private double horizontal = 419;

        /// <summary>
        /// The vertical center
        /// </summary>
        private double vertical = 474;

        [DllImport("sensor.dll")]
        private static extern void ShockproofGetAccelerometerData(ref APSReading accData);

        /// <summary>
        /// Takse a raw reading from the APS
        /// </summary>
        /// <returns>The current APS status</returns>
        public APSReading Read()
        {
            var a = new APSReading();
            ShockproofGetAccelerometerData(ref a);
            return a;
        }

        /// <summary>
        /// Stores the current pitch/roll as the center for future readings
        /// </summary>
        public void Calibrate()
        {
            var r = this.Read();
            this.vertical = r.y0;
            this.horizontal = r.x0;
        }

        /// <summary>
        /// Determines the current pitch/roll in degrees
        /// </summary>
        /// <returns>The current pitch/roll</returns>
        public Attitude ReadAttitude()
        {
            var r = this.Read();

            Attitude ret;

            ret.Pitch = (int) (90*(r.xx - vertical)/(horizontal - vertical));

            ret.Roll = (int) (90*(r.yy - horizontal)/(vertical - horizontal));

            return ret;
        }

        /// <summary>
        /// Reads the temperature
        /// </summary>
        /// <returns>The temperature in degrees centigrate</returns>
        public int ReadTempC()
        {
            return this.Read().temperature;
        }

        /// <summary>
        /// Reads the temperature
        /// </summary>
        /// <returns>The current temperature in fahrenheit</returns>
        public int ReadTempF()
        {
            return (int) (this.Read().temperature*9.0/5.0 + 32.0);
        }
    }

    /// <summary>
    /// Represents the attitude
    /// </summary>
    public struct Attitude
    {
        /// <summary>
        /// Pitch (forward/back) in degrees
        /// </summary>
        public int Pitch;

        /// <summary>
        /// Roll (left/right) in degrees
        /// </summary>
        public int Roll;
    }

    /// <summary>
    /// Represents the status of the APS
    /// </summary>
    public enum APSStatus
    {
        /// <summary>
        /// Not moved recently
        /// </summary>
        Stable = 0,
        UK01 = 1,

        /// <summary>
        /// Moved recently, but now stable
        /// </summary>
        Moved2 = 2,

        /// <summary>
        /// Moved recently, but now stable
        /// </summary>
        Moved3 = 3,

        /// <summary>
        /// Moved recently, but now stable
        /// </summary>
        Moved4 = 4,
        UK05 = 5,
        UK06 = 6,
        UK07 = 7,

        /// <summary>
        /// Paused drive because of movement
        /// </summary>
        Paused3 = 8,

        /// <summary>
        /// Paused drive because of movement
        /// </summary>
        Paused9 = 9,
        UK10 = 10,
        UK11 = 11,
        UK12 = 12,

        /// <summary>
        /// Restarted drive and ignoring further movement
        /// </summary>
        GaveUp = 13,

        /// <summary>
        /// User disabled APS
        /// </summary>
        Disabled = 14
    }

    /// <summary>
    /// Represents a raw reading from the APS
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct APSReading
    {
        /// <summary>
        /// Current status
        /// </summary>
        public APSStatus status;

        /// <summary>
        /// Raw roll
        /// </summary>
        public ushort x;

        /// <summary>
        /// Raw pitch
        /// </summary>
        public ushort y;

        /// <summary>
        /// Roll averaged over ~40 ms
        /// </summary>
        public ushort xx;

        /// <summary>
        /// Pitch averaged over ~40 ms
        /// </summary>
        public ushort yy;

        /// <summary>
        /// Temperature in centigrade
        /// </summary>
        public byte temperature;

        /// <summary>
        /// Current "resting" roll
        /// </summary>
        public ushort x0;

        /// <summary>
        /// Current "resting" pitch
        /// </summary>
        public ushort y0;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Sigfrid3141
United States United States
No Biography provided

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Jan 2009
Article Copyright 2009 by Sigfrid3141
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid