这批代码介绍如何使用iPhone的三轴感应器
2011-10-19 14:23
267 查看
#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);
}
#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 使用iPhone三轴感应器的代码例子
- iOS代码注释方法大全,图文介绍如何使用快速注释
- iPhone 实用技巧 之 快速使用iTools安装ipa软件。本节简单介绍如何使用iTools安装在iPhone上安装ipa软件,具体如下
- 如何使用代码动态设置水晶报表中的文本,字段等对象呢?
- 对于如何在编码过程中,我们如何管理代码的问题,下面是针对代码管理工具git的安装和使用
- [转]如何在 Visual Studio 中使用 Git 同步代码到 CodePlex
- 百度统计使用教程:如何安装统计代码详细教程
- 如何使用GitHub管理代码
- IPHONE应用开发 - Tab Bar Control(标签栏)使用的简单介绍
- 如何使用iTunes同步的方法将软件安装到越狱的iPhone中
- 如何使用SmartGit快速上传下载新建分支修改代码
- C#如何在项目的代码中使用图片文件资源
- WPF中如何使用代码操作数据模板生成的控件
- 如何在kdevelop中让QT使用代码补全功能(转)
- 关于如何在Android源码目录中建立自己的C工程,并用mm命令单独进行该C工程模块的编译,以及Android.mk的使用介绍(2)
- 如何使用sourcetree 或 IDEA 自带的git合并代码?
- 如何让 Drupal 使用 Wordpress 形式的编辑代码?
- Xcode升级到5之后,iphone的iOS7模拟器的大小和mini一样大 又如何使用原来的模拟器