[chuck-dev] Patch TiltSensor on linux

Cédric Krier ced at ced.homedns.org
Sat May 19 11:11:43 EDT 2007


Hi, here is a patch that allow to use TiltSensor on a macbook with
linux.

Cédric Krier
-------------- next part --------------
--- chuck-1.2.0.8/src/util_hid.cpp	2007-03-22 07:36:00.000000000 +0100
+++ chuck-1.2.0.8-smc/src/util_hid.cpp	2007-05-19 16:16:25.000000000 +0200
@@ -6712,14 +6712,139 @@
 int WiiRemote_send( const HidMsg * msg ){ return -1; }
 const char * WiiRemote_name( int wr ){ return NULL; }
 
+#define SYSFS_TILTSENSOR_FILE "/sys/devices/platform/applesmc/position"
+#define TILTSENSOR_BUF_LEN 32
+
+static struct t_TiltSensor_data
+{
+	union
+	{
+		struct t_macbook
+		{
+			int x;
+			int y;
+			int z;
+		} macbook;
+	} data;
+	int dataType;
+	int detected;
+	int refcount;
+
+	t_TiltSensor_data()
+	{
+		refcount = 0;
+		dataType = -1;
+		detected = 0;
+	}
+
+} TiltSensor_data;
+enum
+{
+	linuxAppleSMCMacBookDataType
+};
+static int TiltSensor_detect()
+{
+	int fd;
+
+	fd = open(SYSFS_TILTSENSOR_FILE, O_RDONLY);
+
+	if (fd > 0)
+	{
+		TiltSensor_data.dataType = linuxAppleSMCMacBookDataType;
+		TiltSensor_data.detected = 1;
+		close(fd);
+		return 1;
+	}
+
+	TiltSensor_data.detected = -1;
+
+	return 0;
+}
+
+static int TiltSensor_do_read()
+{
+
+	switch(TiltSensor_data.dataType)
+	{
+		case linuxAppleSMCMacBookDataType:
+			char buf[TILTSENSOR_BUF_LEN];
+			int ret, fd;
+			fd = open(SYSFS_TILTSENSOR_FILE, O_RDONLY);
+
+			if (fd < 0) {
+				return -1;
+			}
+			ret = read(fd, buf, TILTSENSOR_BUF_LEN);
+			if (ret < 0) {
+				close(fd);
+				return -1;
+			}
+			if (sscanf(buf, "(%d,%d,%d)\n", &TiltSensor_data.data.macbook.x, &TiltSensor_data.data.macbook.y, &TiltSensor_data.data.macbook.z) != 3) {
+				close(fd);
+				return -1;
+			}
+			close(fd);
+			break;
+		default:
+			return 0;
+	}
+	return 1;
+}
 void TiltSensor_init(){}
 void TiltSensor_quit(){}
 void TiltSensor_probe(){}
-int TiltSensor_count(){ return 0; }
-int TiltSensor_open( int ts ){ return -1; }
-int TiltSensor_close( int ts ){ return -1; }
-int TiltSensor_read( int ts, int type, int num, HidMsg * msg ){ return -1; }
-const char * TiltSensor_name( int ts ){ return NULL; }
+int TiltSensor_count()
+{
+	if(TiltSensor_data.detected == 0)
+		TiltSensor_detect();
+
+	if(TiltSensor_data.detected == -1)
+		return 0;
+	else if(TiltSensor_data.detected == 1)
+		return 1;
+
+	return 0;
+}
+int TiltSensor_open( int ts )
+{
+	if(TiltSensor_data.detected == 0)
+		TiltSensor_detect();
+
+	if(TiltSensor_data.detected == -1)
+		return -1;
+
+	TiltSensor_data.refcount++;
+
+	return 0;
+}
+int TiltSensor_close( int ts )
+{
+	TiltSensor_data.refcount--;
+
+	return 0;
+}
+int TiltSensor_read( int ts, int type, int num, HidMsg * msg )
+{
+
+	if(TiltSensor_data.detected == -1)
+		return -1;
+
+	if(!TiltSensor_do_read())
+		return -1;
+
+	if(TiltSensor_data.dataType == linuxAppleSMCMacBookDataType)
+	{
+		msg->idata[0] = TiltSensor_data.data.macbook.x;
+		msg->idata[1] = TiltSensor_data.data.macbook.y;
+		msg->idata[2] = TiltSensor_data.data.macbook.z;
+	}
+
+	return 0;
+}
+const char * TiltSensor_name( int ts )
+{
+	return "Apple Sudden Motion Sensor";
+}
 
 
 #endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cs.princeton.edu/pipermail/chuck-dev/attachments/20070519/fc2ba37a/attachment.bin 


More information about the chuck-dev mailing list