All of lore.kernel.org
 help / color / mirror / Atom feed
From: Niel Lambrechts <antispam@telkomsa.net>
To: Linux Kernel ML <linux-kernel@vger.kernel.org>
Subject: Re: [WISHLIST] IBM HD Shock detection in Linux
Date: Mon, 13 Dec 2004 00:01:22 +0200	[thread overview]
Message-ID: <1102888882.15558.2.camel@ksyrium.local> (raw)

[-- Attachment #1: Type: text/plain, Size: 194 bytes --]

I picked this up from somewhere on the net, pity that it is not c
code...

The code apparently can display the horizon, but cannot prevent
shocks :(

-- 
Niel Lambrechts <antispam@telkomsa.net>

[-- Attachment #2: horizon.cs --]
[-- Type: text/x-csharp, Size: 5167 bytes --]

using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

public sealed class Accelerometer : IDisposable
{
	private const uint GENERIC_READ = 0x80000000;
	private const uint FILE_SHARE_READ = 1;
	private const uint FILE_SHARE_WRITE = 2;
	private const uint OPEN_EXISTING = 3;
	private readonly static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
	private const uint IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA = 0x733fc;
	private const int FACILITY_WIN32 = unchecked((int)0x80070000);
	private IntPtr handle = INVALID_HANDLE_VALUE;
	private AccelerometerData sample;

	private static int HRESULT_FROM_WIN32(int x)
	{
		return x <= 0 ? x : ((x & 0x0000FFFF) | FACILITY_WIN32);
	}

	private struct AccelerometerData
	{
		internal int status;
		internal short x0;
		internal short y0;
		short x1;
		short y1;
		short x2;
		short y2;
		short x3;
		short y3;
		short x4;
		short y4;
		short x5;
		short y5;
		short x6;
		short y6;
		short x7;
		short y7;
		short x8;
		short y8;
		short x9;
		short y9;
		short x10;
		short y10;
		short x11;
		short y11;
		short x12;
		short y12;
		short x13;
		short y13;
		short unknown0;
		short unknown1;
	}

	[DllImport("kernel32.dll", SetLastError = true)]
	private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

	[DllImport("kernel32.dll")]
	private static extern void CloseHandle(IntPtr handle);

	[DllImport("kernel32.dll", SetLastError = true)]
	private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref AccelerometerData lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);

	public Accelerometer()
	{
		handle = CreateFile(@"\\.\ShockMgr", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
		if(handle == INVALID_HANDLE_VALUE)
		{
			GC.SuppressFinalize(this);
			Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Marshal.GetLastWin32Error()));
		}
	}

	~Accelerometer()
	{
		CloseImpl();
	}

	private void CloseImpl()
	{
		IntPtr h = handle;
		if(h != INVALID_HANDLE_VALUE)
		{
			handle = INVALID_HANDLE_VALUE;
			CloseHandle(h);
		}
	}

	public void ReadSample()
	{
		uint dwRead = 0;
		if(!DeviceIoControl(handle, IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA, IntPtr.Zero, 0, ref sample, 0x24, ref dwRead, IntPtr.Zero))
		{
			Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Marshal.GetLastWin32Error()));
		}
	}

	public int Status
	{
		get
		{
			return sample.status;
		}
	}

	public int X
	{
		get
		{
			return sample.x0;
		}
	}

	public int Y
	{
		get
		{
			return sample.y0;
		}
	}

	public void Dispose()
	{
		CloseImpl();
		GC.SuppressFinalize(this);
	}
}

namespace Horizon
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Timer timer1;
		private Accelerometer sensor = new Accelerometer();

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			sensor.ReadSample();
			Invalidate();
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			float horizontal = 640;
			float vertical = 800;
			float y = sensor.Y;
			float angle = 90 * (y - horizontal) / (vertical - horizontal);
			e.Graphics.DrawString(angle.ToString(), Font, Brushes.Black, 0, 0);
			e.Graphics.TranslateTransform(ClientRectangle.Width / 2, ClientRectangle.Height / 2);
			e.Graphics.RotateTransform(- angle);
			e.Graphics.DrawLine(Pens.Black, - ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, 0);
		}
	}
}

             reply	other threads:[~2004-12-12 22:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-12 22:01 Niel Lambrechts [this message]
2004-12-12 22:06 ` [WISHLIST] IBM HD Shock detection in Linux Jan Engelhardt
2004-12-12 22:11   ` Niel Lambrechts
2004-12-12 22:15     ` Jan Engelhardt
2004-12-12 22:48       ` Jesper Juhl
2004-12-12 22:41         ` Jan Engelhardt
2004-12-14  0:41           ` Kevin Puetz
2004-12-14  3:22         ` [OT] IBM Active Protection System (Re: [WISHLIST] IBM HD Shock detection in Linux) YOSHIFUJI Hideaki / 吉藤英明
2004-12-13  8:05       ` [WISHLIST] IBM HD Shock detection in Linux Manu Abraham
2004-12-13  9:10     ` Sander
2004-12-12 22:59 ` Bernd Eckenfels
  -- strict thread matches above, loose matches on Subject: below --
2004-12-10  8:39 Shawn Starr
2004-12-10 19:59 ` Lee Revell
2004-12-10 20:03 ` Kay Sievers
2004-12-10 20:17   ` Lee Revell
2004-12-10 21:08     ` Alan Cox
2004-12-01 18:31 Shawn Starr
2004-12-01 18:42 ` Lee Revell
2004-12-01 18:57   ` Robert Love
2004-12-02 13:21     ` Ian Soboroff
2004-12-01 20:51   ` Kay Sievers
2004-12-01 20:13 ` Joseph Pingenot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1102888882.15558.2.camel@ksyrium.local \
    --to=antispam@telkomsa.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.