How to use three-axis sensor code in iPhone applications

Source: Internet
Author: User

IPhone applicationsHow to use iPhoneThree-axis sensorThe code case is the content you will introduce in this article, mainly to understand and learnIphone applications. In many applications, the built-in iPhoneThree-axis sensorAcceleration Sensor), hope the following code example can open your mind for using the iPhone three axis sensor. :

 

 
 
  1. #include <IOKit/IOKitLib.h> 
  2. #include <CoreFoundation/CoreFoundation.h> 
  3.  
  4. typedef struct {} *IOHIDEventSystemRef;  
  5. typedef struct {} *IOHIDEventRef;  
  6. float IOHIDEventGetFloatValue(IOHIDEventRef ref, int param);  
  7.  
  8. void handleHIDEvent(int a, int b, int c, IOHIDEventRef ptr) {  
  9. int type = IOHIDEventGetType(ptr);  
  10. if (type == 12) {  
  11. float x,y,z;  
  12. x = IOHIDEventGetFloatValue(ptr, 0xc0000);  
  13. y = IOHIDEventGetFloatValue(ptr, 0xc0001);  
  14. z = IOHIDEventGetFloatValue(ptr, 0xc0002);  
  15. // do whatever you need to do with the gravity  
  16. ballSetAccel(x, y);  
  17. }  
  18. }  
  19. #define expect(x) if(!x) { printf("failed: %sn", #x); return; }  
  20. void initialize(int hz) {  
  21. mach_port_t master;  
  22. expect(0 == IOMasterPort(MACH_PORT_NULL, &master));  
  23.  
  24. int page = 0xff00, usage = 3;  
  25.  
  26. CFNumberRef nums[2];  
  27. CFStringRef keys[2];  
  28. keys[0] = CFStringCreateWithCString(0, "PrimaryUsagePage", 0);  
  29. keys[1] = CFStringCreateWithCString(0, "PrimaryUsage", 0);  
  30. nums[0] = CFNumberCreate(0, kCFNumberSInt32Type, &page);  
  31. nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage);  
  32. CFDictionaryRef dict = CFDictionaryCreate(0, (const void**)keys,  
  33. (const void**)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);  
  34.  expect(dict);  
  35. IOHIDEventSystemRef sys = (IOHIDEventSystemRef) IOHIDEventSystemCreate(0);  
  36. expect(sys);  
  37.  CFArrayRef srvs = (CFArrayRef)IOHIDEventSystemCopyMatchingServices(sys, dict, 0, 0, 0);  
  38. expect(CFArrayGetCount(srvs)==1);  
  39.    
  40. io_registry_entry_t serv = (io_registry_entry_t)CFArrayGetValueAtIndex(srvs, 0);  
  41. expect(serv);  
  42.    
  43. CFStringRef cs = CFStringCreateWithCString(0, "ReportInterval", 0);  
  44. int rv = 1000000/hz;  
  45. CFNumberRef cn = CFNumberCreate(0, kCFNumberSInt32Type, &rv);  
  46.    
  47. int res = IOHIDServiceSetProperty(serv, cs, cn);  
  48. expect(res == 1);  
  49.    
  50. res = IOHIDEventSystemOpen(sys, handleHIDEvent, 0, 0);  
  51. expect(res != 0);  

Summary:IPhone applicationsHow to UseThree-axis sensorThe content of the code case has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.