Revision: 9604 http://pure-data.svn.sourceforge.net/pure-data/?rev=9604&view=rev Author: eighthave Date: 2008-03-17 13:52:14 -0700 (Mon, 17 Mar 2008)
Log Message: ----------- got a very rough version of the sudden motion sensor/accelerometer working
Modified Paths: -------------- trunk/externals/apple/sudden_motion_sensor.c
Modified: trunk/externals/apple/sudden_motion_sensor.c =================================================================== --- trunk/externals/apple/sudden_motion_sensor.c 2008-03-17 20:33:44 UTC (rev 9603) +++ trunk/externals/apple/sudden_motion_sensor.c 2008-03-17 20:52:14 UTC (rev 9604) @@ -63,42 +63,105 @@ static void sudden_motion_sensor_output(t_sudden_motion_sensor* x) { DEBUG(post("sudden_motion_sensor_output");); - kern_return_t kern_result; + t_atom output_atoms[3];
+ kern_return_t kern_return; + mach_port_t mach_port; + io_iterator_t io_iterator; + io_object_t io_object; + io_connect_t io_connect; + int kernel_function, sizeof_data; + IOItemCount structureInputSize; IOByteCount structureOutputSize;
struct data inputStructure; struct data outputStructure;
- t_atom output_atoms[3]; + kern_return = IOMasterPort(MACH_PORT_NULL, &mach_port); + if (kern_return != KERN_SUCCESS) + { + pd_error(x, "[sudden_motion_sensor]: cannot get mach_port"); + return; + }
+ //PowerBookG4, iBookG4 + kern_return = IOServiceGetMatchingServices(mach_port, IOServiceMatching("IOI2CMotionSensor"), &io_iterator); + if (kern_return == KERN_SUCCESS && io_iterator != 0) + { + x->sensor_name = gensym("IOI2CMotionSensor"); + sizeof_data = 60; + kernel_function = 21; + goto FOUND_SENSOR; + } + // + kern_return = IOServiceGetMatchingServices(mach_port, IOServiceMatching("PMUMotionSensor"), &io_iterator); + if(kern_return == KERN_SUCCESS && io_iterator != 0) + { + x->sensor_name = gensym("PMUMotionSensor"); + sizeof_data = 60; + kernel_function = 21; + goto FOUND_SENSOR; + } + // MacBook Pro + kern_return = IOServiceGetMatchingServices(mach_port, IOServiceMatching("SMCMotionSensor"), &io_iterator); + if(kern_return == KERN_SUCCESS && io_iterator != 0) + { + x->sensor_name = gensym("SMCMotionSensor"); + sizeof_data = 40; + kernel_function = 5; + goto FOUND_SENSOR; + } + + pd_error(x,"[sudden_motion_sensor]: cannot find motionsensor\n"); + return;
- structureInputSize = x->data_size; //sizeof(struct data); - structureOutputSize = x->data_size; //sizeof(struct data);
+FOUND_SENSOR: + io_object = IOIteratorNext(io_iterator); + IOObjectRelease(io_iterator); + + if(!io_object) + { + pd_error(x,"[sudden_motion_sensor]: No motion sensor available."); + return; + } + + kern_return = IOServiceOpen(io_object, mach_task_self(), 0, &io_connect); + IOObjectRelease(io_object); + + if(kern_return != KERN_SUCCESS) + { + pd_error(x,"[sudden_motion_sensor]: Could not open motion sensor device."); + return; + } + + structureInputSize = sizeof_data; //sizeof(struct data); + structureOutputSize = sizeof_data; //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) + + kern_return = IOConnectMethodStructureIStructureO( + io_connect, + kernel_function, /* index to kernel function */ + structureInputSize, + &structureOutputSize, + &inputStructure, + &outputStructure); + + if( kern_return == 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) + else if(kern_return == kIOReturnBusy) pd_error(x,"[sudden_motion_sensor]: device busy"); else pd_error(x,"[sudden_motion_sensor]: could not read device"); + IOServiceClose(io_connect); }
@@ -110,106 +173,11 @@ }
-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);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.