【转载】iphone 使用iPhone三轴感应器的代码例子
2011-11-23 18:26
316 查看
本文摘自:http://www.cocoachina.com/iphonedev/sdk/2010/0624/1749.html
多应用里都要用到苹果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;
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);
}
多应用里都要用到苹果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;
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);
}
相关文章推荐
- 使用iPhone三轴感应器的代码例子
- [转]使用iPhone三轴感应器的代码例子
- 这批代码介绍如何使用iPhone的三轴感应器
- VS2012_MVC4入门例子、代码视图分离办法、需要注意的坑爹问题等_被坑后不断更新此贴,要转载的话,请使用链接,不要转载内容
- dom例子三(禁粘贴-转载文章添加信息-使用外站代码)
- (转载)如何将多种设计模式结合使用(有原代码)
- 【转载】使用Json比用string返回数据更友好,也更面向对象一些 |Asp.net MVC 2.0 + Unity 2.0(IoC) + EF4.0 实例:RoRoWoBlog 开源项目框架代码
- Objective-C语法之代码块(block)的使用(转载)
- 黄聪:C#代码生成工具:文本模板初体验 使用T4批量修改实体框架(转载)
- 使用控件,代码小例子
- 使用 CXF 做 webservice 简单例子(转载)
- 转载-使用 Feed4JUnit 进行数据与代码分离的 Java 单元测试
- iPhone 例子代码
- 【转载】新浪云Git代码仓库使用入门
- 【转载】如何使用Git上传项目代码到github
- Stm32使用Usart代码例子
- 【转载】使用javassist动态注入代码
- uboot 使用 以及 启动代码分析(转载网络)
- Iphone开发(3) 一个使用组合的简单例子
- android iphone手机服务端接口(php实现,使用框架fat-free,解说例子blog)