Here's some code to initialize the accelerometer to run at full speed. Pass the desired sample rate (in Hz) to the initialize function. Go wild!
[updated 9/3: fixed typo]
#include
typedef struct {} *IOHIDEventSystemRef;
typedef struct {} *IOHIDEventRef;
float IOHIDEventGetFloatValue(IOHIDEventRef ref, int param);
void handleHIDEvent(int a, int b, int c, IOHIDEventRef ptr) {
int type = IOHIDEventGetType(ptr);
if (type == 12) {
float x,y,z;
x = IOHIDEventGetFloatValue(ptr, 0xc0000);
y = IOHIDEventGetFloatValue(ptr, 0xc0001);
z = IOHIDEventGetFloatValue(ptr, 0xc0002);
// do whatever you need to do with the gravity
ballSetAccel(x, y);
}
}
#define expect(x) if(!x) { printf("failed: %s\n", #x); return; }
void initialize(int hz) {
mach_port_t master;
expect(0 == IOMasterPort(MACH_PORT_NULL, &master));
int page = 0xff00, usage = 3;
CFNumberRef nums[2];
CFStringRef keys[2];
keys[0] = CFStringCreateWithCString(0, "PrimaryUsagePage", 0);
keys[1] = CFStringCreateWithCString(0, "PrimaryUsage", 0);
nums[0] = CFNumberCreate(0, kCFNumberSInt32Type, &page);
nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage);
CFDictionaryRef dict = CFDictionaryCreate(0, (const void**)keys, (const void**)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
expect(dict);
IOHIDEventSystemRef sys = (IOHIDEventSystemRef) IOHIDEventSystemCreate(0);
expect(sys);
CFArrayRef srvs = (CFArrayRef)IOHIDEventSystemCopyMatchingServices(sys, dict, 0, 0, 0);
expect(CFArrayGetCount(srvs)==1);
io_registry_entry_t serv = (io_registry_entry_t)CFArrayGetValueAtIndex(srvs, 0);
expect(serv);
CFStringRef cs = CFStringCreateWithCString(0, "ReportInterval", 0);
int rv = 1000000/hz;
CFNumberRef cn = CFNumberCreate(0, kCFNumberSInt32Type, &rv);
int res = IOHIDServiceSetProperty(serv, cs, cn);
expect(res == 1);
res = IOHIDEventSystemOpen(sys, handleHIDEvent, 0, 0);
expect(res != 0);
}
Previous Ramblings
-
▼
2011
(127)
-
▼
May
(14)
- Rumor: Camera woes holding up white iPhone, may be...
- iPhone accelerometer source code
- The PlayStation Phone is still real
- iPhoneWorld.ca launches revamped direct downloads ...
- iOS devices top survey of UK children's most-wante...
- iSlate, iPad or iTablet would be based on iPhone OS
- Apple starts selling iPhone 4 in China, launches C...
- Trick or Treat with TUAW: iMainGo 2 mobile speakers
- White iPhone 4 delayed until Spring 2011
- iPhone 3.1 Out, Don’t Let it Re-Lock your Unlocked...
- White iPhone 4 canceled?
- German Electric Car Drives a Record-Breaking 372 M...
- Apple introduces online Chinese store, App Store n...
- News: Apple begins unlocked iPhone sales in Germany
-
▼
May
(14)
Hi
- Dan Knottingham
- My Dad used to make up an area outside complete with backyard baseball batting cages, basketball hoop and everything else that could fit. When I was young I dreamed of going to the NBA. Now, I am happy to coach Little League and Steve Nash Minor Basketball!
0 comments:
Post a Comment