aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sudden_motion_sensor-help.pd84
-rw-r--r--sudden_motion_sensor.c231
-rw-r--r--sudden_motion_sensor.libs1
3 files changed, 316 insertions, 0 deletions
diff --git a/sudden_motion_sensor-help.pd b/sudden_motion_sensor-help.pd
new file mode 100644
index 0000000..00b529f
--- /dev/null
+++ b/sudden_motion_sensor-help.pd
@@ -0,0 +1,84 @@
+#N canvas 198 136 565 450 10;
+#X obj 4 398 cnv 15 550 20 empty \$0-pddp.cnv.footer empty 20 12 0
+14 -228856 -66577 0;
+#X obj 4 -17 cnv 15 550 40 empty \$0-pddp.cnv.header sudden_motion_sensor
+3 12 0 18 -204280 -1 0;
+#X obj 3 189 cnv 3 550 3 empty \$0-pddp.cnv.inlets inlet 15 12 0 13
+-228856 -1 0;
+#N canvas 779 22 494 344 META 0;
+#X text 12 25 KEYWORDS control number float store;
+#X text 12 45 LICENSE GPL;
+#X text 12 65 TEMPLATE template-help.pd v0.1;
+#X text 12 85 PLATFORM windows macosx gnulinux;
+#X text 12 105 DATATYPE float list;
+#X text 12 5 GENRE storage;
+#X text 12 125 LIBRARY internal;
+#X text 12 145 WEBSITE http://crca.ucsd.edu/~msp/;
+#X text 12 165 RELEASE_DATE 2007-08-24;
+#X text 12 185 RELEASE_VERSION 0.40-test05;
+#X restore 504 400 pd META;
+#X obj 3 258 cnv 3 550 3 empty \$0-pddp.cnv.outlets outlets 15 12 0
+13 -228856 -1 0;
+#X obj 3 338 cnv 3 550 3 empty \$0-pddp.cnv.more_info more_info 15
+12 0 13 -228856 -1 0;
+#N canvas 309 454 609 478 guts 0;
+#X obj 117 95 textfile;
+#X msg 116 24 symbol template-HCS.pd;
+#X msg 116 54 read \$1 \, rewind \, bang;
+#X obj 117 141 trigger bang anything;
+#X obj 58 117 bang;
+#X obj 240 162 route #X;
+#X obj 240 183 route text;
+#X obj 240 250 route DESCRIPTION;
+#X obj 239 276 print;
+#X obj 140 277 print TEXT;
+#X connect 0 0 3 0;
+#X connect 1 0 2 0;
+#X connect 2 0 0 0;
+#X connect 3 0 4 0;
+#X connect 3 1 5 0;
+#X connect 4 0 0 0;
+#X connect 5 0 6 0;
+#X connect 7 0 8 0;
+#X restore 453 400 pd guts;
+#X obj 374 6 pddp/pddplink http://wiki.puredata.info/en/apple/sudden_motion_sensor
+-text pdpedia: sudden_motion_sensor;
+#X text 102 208 BANG - output current screen brightness;
+#X msg 146 48 bang;
+#X msg 179 73 info;
+#X floatatom 146 155 5 0 0 3 z - -;
+#X floatatom 182 155 5 0 0 3 y - -;
+#X text 216 73 output info on right outlet;
+#X obj 73 73 metro 250;
+#X obj 73 54 tgl 15 0 empty empty empty 0 -6 0 10 -204786 -1 -1 0 1
+;
+#X text 183 46 output sensor values;
+#X text 102 227 "info" - outputs info about the sensor on the right
+outlet;
+#X obj 72 305 cnv 17 3 20 empty \$0-pddp.cnv.let.1 1 5 9 0 16 -228856
+-162280 0;
+#X obj 72 279 cnv 17 3 20 empty \$0-pddp.cnv.let.0 0 5 9 0 16 -228856
+-162280 0;
+#X text 103 276 LIST - a list with the values of the left and right
+sensors;
+#X text 103 306 selector series with info about the sensor;
+#X text 15 6 description: query the sudden motion sensor in Apples
+;
+#X obj 389 -14 apple/sudden_motion_sensor;
+#X obj 146 101 apple/sudden_motion_sensor;
+#X obj 299 153 pddp/print;
+#X obj 299 129 route sensor;
+#X obj 104 364 pddp/pddplink http://www.osxbook.com/book/bonus/chapter10/sms/
+;
+#X obj 146 129 unpack 0 0 0;
+#X floatatom 219 155 5 0 0 3 z - -;
+#X connect 9 0 24 0;
+#X connect 10 0 24 0;
+#X connect 14 0 24 0;
+#X connect 15 0 14 0;
+#X connect 24 0 28 0;
+#X connect 24 1 26 0;
+#X connect 26 0 25 0;
+#X connect 28 0 11 0;
+#X connect 28 1 12 0;
+#X connect 28 2 29 0;
diff --git a/sudden_motion_sensor.c b/sudden_motion_sensor.c
new file mode 100644
index 0000000..0073484
--- /dev/null
+++ b/sudden_motion_sensor.c
@@ -0,0 +1,231 @@
+/* --------------------------------------------------------------------------*/
+/* */
+/* read the sudden motion sensor on Apple Mac OS X */
+/* Written by Hans-Christoph Steiner <hans@at.or.at> */
+/* */
+/* Copyright (c) 2008 Free Software Foundation */
+/* */
+/* This program is free software; you can redistribute it and/or */
+/* modify it under the terms of the GNU General Public License */
+/* as published by the Free Software Foundation; either version 2 */
+/* of the License, or (at your option) any later version. */
+/* */
+/* See file LICENSE for further informations on licensing terms. */
+/* */
+/* This program is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
+/* GNU General Public License for more details. */
+/* */
+/* You should have received a copy of the GNU General Public License */
+/* along with this program; if not, write to the Free Software Foundation, */
+/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
+/* */
+/* --------------------------------------------------------------------------*/
+
+#include <mach/mach.h>
+#include <IOKit/IOKitLib.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <m_pd.h>
+
+#define DEBUG(x)
+//#define DEBUG(x) x
+
+/*------------------------------------------------------------------------------
+ * CLASS DEF
+ */
+
+static t_class *sudden_motion_sensor_class;
+
+typedef struct _sudden_motion_sensor {
+ t_object x_obj;
+ t_symbol* sensor_name;
+
+ io_connect_t io_connect;
+ int kernel_function;
+ int data_size;
+
+ t_outlet* data_outlet;
+ t_outlet* status_outlet;
+} t_sudden_motion_sensor;
+
+struct data {
+ char x;
+ char y;
+ char z;
+ char pad[57];
+};
+
+/*------------------------------------------------------------------------------
+ * IMPLEMENTATION
+ */
+
+static void sudden_motion_sensor_output(t_sudden_motion_sensor* x)
+{
+ DEBUG(post("sudden_motion_sensor_output"););
+ kern_return_t kern_result;
+
+ IOItemCount structureInputSize;
+ IOByteCount structureOutputSize;
+
+ struct data inputStructure;
+ struct data outputStructure;
+
+ t_atom output_atoms[3];
+
+
+ structureInputSize = x->data_size; //sizeof(struct data);
+ structureOutputSize = x->data_size; //sizeof(struct data);
+
+ memset(&inputStructure, 0, sizeof(inputStructure));
+ memset(&outputStructure, 0, sizeof(outputStructure));
+
+ kern_result = IOConnectMethodStructureIStructureO(x->io_connect,
+ x->kernel_function, /* index to kernel function */
+ structureInputSize,
+ &structureOutputSize,
+ &inputStructure,
+ &outputStructure);
+
+ IOServiceClose(x->io_connect);
+ if( kern_result == KERN_SUCCESS)
+ {
+ SETFLOAT(output_atoms, outputStructure.x);
+ SETFLOAT(output_atoms + 1, outputStructure.y);
+ SETFLOAT(output_atoms + 2, outputStructure.z);
+ outlet_list(x->data_outlet, &s_list, 3, output_atoms);
+ }
+ else if(kern_result == kIOReturnBusy)
+ pd_error(x,"[sudden_motion_sensor]: device busy");
+ else
+ pd_error(x,"[sudden_motion_sensor]: could not read device");
+}
+
+
+static void sudden_motion_sensor_info(t_sudden_motion_sensor* x)
+{
+ t_atom output_atom;
+ SETSYMBOL(&output_atom, x->sensor_name);
+ outlet_anything(x->status_outlet, gensym("sensor"), 1, &output_atom);
+}
+
+
+static int open_sensor(t_sudden_motion_sensor* x,
+ mach_port_t mach_port, io_iterator_t *io_iterator_ptr,
+ const char *sensor_string, int data_size, int kernel_function)
+{
+ kern_return_t kern_result;
+ post("trying %s", sensor_string);
+ kern_result = IOServiceGetMatchingServices(mach_port, IOServiceMatching("IOI2CMotionSensor"), io_iterator_ptr);
+ if (kern_result == KERN_SUCCESS && *io_iterator_ptr != 0)
+ {
+ post("found %s", sensor_string);
+ x->sensor_name = gensym(sensor_string);
+ x->data_size = data_size;
+ x->kernel_function = kernel_function;
+ return 1;
+ }
+ else
+ return 0;
+}
+
+static void *sudden_motion_sensor_new(void)
+{
+ DEBUG(post("sudden_motion_sensor_new"););
+ t_sudden_motion_sensor *x = (t_sudden_motion_sensor *)pd_new(sudden_motion_sensor_class);
+ io_iterator_t io_iterator;
+ io_object_t io_object;
+ kern_return_t kern_result;
+ mach_port_t mach_port;
+
+/*
+ kern_result = IOMasterPort(MACH_PORT_NULL, &mach_port);
+ if (kern_result != KERN_SUCCESS)
+ pd_error(x, "[sudden_motion_sensor]: IOMasterPort(MACH_PORT_NULL, &mach_port)");
+ else if(!open_sensor(x, mach_port, &io_iterator, "IOI2CMotionSensor", 60, 21))
+ if(!open_sensor(x, mach_port, &io_iterator, "PMUMotionSensor", 60, 21))
+ if(!open_sensor(x, mach_port, &io_iterator, "SMCMotionSensor", 40, 5))
+ pd_error(x,"[sudden_motion_sensor]: cannot find motion sensor");
+
+ io_object = IOIteratorNext(io_iterator);
+ IOObjectRelease(io_iterator);
+ if (io_object)
+ {
+ kern_result = IOServiceOpen(io_object, mach_task_self(), 0, &x->io_connect);
+ IOObjectRelease(io_object);
+ if (kern_result != KERN_SUCCESS)
+ pd_error(x,"[sudden_motion_sensor]: could not open motion sensor");
+ }
+ else
+ pd_error(x,"[sudden_motion_sensor]: motion sensor not available");
+*/
+
+
+
+ kern_result = IOMasterPort(MACH_PORT_NULL, &mach_port);
+ if (kern_result != KERN_SUCCESS) {
+ error("[sudden_motion_sensor]: IOMasterPort(MACH_PORT_NULL, &mach_port)");
+ }
+
+ //PowerBookG4, iBookG4
+ kern_result = IOServiceGetMatchingServices(mach_port, IOServiceMatching("IOI2CMotionSensor"), &io_iterator);
+ if (kern_result == KERN_SUCCESS && io_iterator != 0) {
+ x->sensor_name = gensym("IOI2CMotionSensor");
+ x->data_size = 60;
+ x->kernel_function = 21;
+ goto FOUND_SENSOR;
+ }
+ //
+ kern_result = IOServiceGetMatchingServices(mach_port, IOServiceMatching("PMUMotionSensor"), &io_iterator);
+ if (kern_result == KERN_SUCCESS && io_iterator != 0) {
+ x->sensor_name = gensym("PMUMotionSensor");
+ x->data_size = 60;
+ x->kernel_function = 21;
+ goto FOUND_SENSOR;
+ }
+ //
+ kern_result = IOServiceGetMatchingServices(mach_port, IOServiceMatching("SMCMotionSensor"), &io_iterator);
+ if (kern_result == KERN_SUCCESS && io_iterator != 0) {
+ x->sensor_name = gensym("SMCMotionSensor");
+ x->data_size = 40;
+ x->kernel_function = 5;
+ goto FOUND_SENSOR;
+ }
+
+ error("can't find motionsensor\n");
+
+
+FOUND_SENSOR:
+ io_object = IOIteratorNext(io_iterator);
+ IOObjectRelease(io_iterator);
+
+ if (io_object == 0) {
+ error("[sudden_motion_sensor]: No motion sensor available.");
+ }
+
+ kern_result = IOServiceOpen(io_object, mach_task_self(), 0, &x->io_connect);
+ IOObjectRelease(io_object);
+
+ if (kern_result != KERN_SUCCESS) {
+ error("[sudden_motion_sensor]: Could not open motion sensor device.");
+ }
+
+ x->data_outlet = outlet_new(&x->x_obj, &s_list);
+ x->status_outlet = outlet_new(&x->x_obj, &s_anything);
+
+ return (x);
+}
+
+void sudden_motion_sensor_setup(void)
+{
+ sudden_motion_sensor_class = class_new(gensym("sudden_motion_sensor"),
+ (t_newmethod)sudden_motion_sensor_new,
+ NULL,
+ sizeof(t_sudden_motion_sensor),
+ CLASS_DEFAULT,
+ 0);
+ /* add inlet datatype methods */
+ class_addbang(sudden_motion_sensor_class,(t_method) sudden_motion_sensor_output);
+ class_addmethod(sudden_motion_sensor_class,(t_method) sudden_motion_sensor_info,
+ gensym("info"), 0);
+}
diff --git a/sudden_motion_sensor.libs b/sudden_motion_sensor.libs
new file mode 100644
index 0000000..334d5d6
--- /dev/null
+++ b/sudden_motion_sensor.libs
@@ -0,0 +1 @@
+-framework IOKit