Click here to Skip to main content
15,893,564 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 Flash
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using MWudka;

    /// <summary>
    /// A fairly lame demo of the Lenovo APS. Wiggle the computer and the bar wiggles. I can get the ball to stay on the bar for about 8 seconds.
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// An APS instance for talking to the APS
        /// </summary>
        private APS aps;

        /// <summary>
        /// Initializes a new instance of the Form1 class
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Called when the form loads. It sets up the APS, calibrates it, and loads the sample SWF
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="e">Event args</param>
        private void Form1_Load(object sender, EventArgs e)
        {
            // Try loading the demo swf
            var demoPath = Path.GetFullPath("AS3.swf");
            if (!File.Exists(demoPath))
            {
                MessageBox.Show(
                    this,
                    "Can't load the demo. There should be a file called AS3.swf in this directory",
                    "Demo Missing",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                this.Close();
                return;
            }

            // Setup and calibrate the APS
            this.aps = new APS();
            this.aps.Calibrate();

            // Load the flash file
            var url = demoPath;
            axShockwaveFlash1.ScaleMode = 0;
            axShockwaveFlash1.LoadMovie(0, url);
        }

        /// <summary>
        /// Called when the flash file uses the external interface. It returns the current attitude.
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="e">Event args</param>
        private void AxShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
        {
            var att = this.aps.ReadAttitude();

            // Convert the attitude structure into a flash object and pass it to the flash file
            var ret = "<object><property id=\"x\"><number>{0}</number></property><property id=\"y\"><number>{1}</number></property></object>";
            ret = string.Format(ret, att.Roll, att.Pitch);
            axShockwaveFlash1.SetReturnValue(ret);
        }      
    }
}

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