陀螺仪

很多应用里都要用到苹果iPhone自带的三轴感应器(加速度传感器),希望下面这段代码例子能为您使用iPhone三轴感应器打开思路。


#include <IOKit/IOKitLib.h>

#include <CoreFoundation/CoreFoundation.h>

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;
= IOHIDEventGetFloatValue(ptr, 0xc0000);
y = IOHIDEventGetFloatValue(ptr, 0xc0001);
= 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, 000);
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, 00);
expect(res != 0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值