Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

Using the APS Accelerometer in Lenovo Laptops

Rate me:
Please Sign up or sign in to vote.
4.14/5 (5 votes)
6 Jan 2009Public Domain3 min read 29.6K   924   19  
Use the accelerometer in Lenovo laptops to determine the computer's attitude and temperature.
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions