From c912f0be1a8e28b4e9db168cb6cc555af2c45634 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Fri, 23 Oct 2009 20:14:38 +0000 Subject: [PATCH] started to integrate inquiry view - not working yet... --- example/WiiMoteOpenGLDemo/BTWiiMote.c | 98 ---------------- example/WiiMoteOpenGLDemo/BTWiiMote.h | 13 --- .../Classes/EAGLViewController.h | 11 ++ .../Classes/EAGLViewController.m | 25 ++++ .../Classes/WiiMoteOpenGLDemoAppDelegate.h | 13 ++- .../Classes/WiiMoteOpenGLDemoAppDelegate.m | 109 ++++++++++++++---- .../project.pbxproj | 12 +- 7 files changed, 138 insertions(+), 143 deletions(-) delete mode 100644 example/WiiMoteOpenGLDemo/BTWiiMote.c delete mode 100644 example/WiiMoteOpenGLDemo/BTWiiMote.h create mode 100644 example/WiiMoteOpenGLDemo/Classes/EAGLViewController.h create mode 100644 example/WiiMoteOpenGLDemo/Classes/EAGLViewController.m diff --git a/example/WiiMoteOpenGLDemo/BTWiiMote.c b/example/WiiMoteOpenGLDemo/BTWiiMote.c deleted file mode 100644 index d40e0bb72..000000000 --- a/example/WiiMoteOpenGLDemo/BTWiiMote.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * test.c - * - * Created by Matthias Ringwald on 7/14/09. - */ - -#include -#include -#include -#include - -#include -#include -#include - -// bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard -bd_addr_t addr = {0x00, 0x19, 0x1d, 0x90, 0x44, 0x68 }; // WiiMote -// bd_addr_t addr = {0x00, 0x19, 0x1d, 0x94, 0x7a, 0xff }; // WiiMote - iPhoneBlog.de - -static void (*data_cb)(uint8_t x, uint8_t y, uint8_t z); -static void (*state_cb)(char *text); - -void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ - bd_addr_t event_addr; - - switch (packet_type) { - - case L2CAP_DATA_PACKET: - if (packet[0] == 0xa1 && packet[1] == 0x31){ - (*data_cb)(packet[4], packet[5], packet[6]); - } - break; - - case HCI_EVENT_PACKET: - - switch (packet[0]){ - - case BTSTACK_EVENT_STATE: - // bt stack activated, get started - // use pairing yes/no - if (packet[2] == HCI_STATE_WORKING) { - bt_send_cmd(&hci_write_authentication_enable, 0); - (*state_cb)("BT running"); - } - break; - - case L2CAP_EVENT_CHANNEL_OPENED: - // inform about new l2cap connection - bt_flip_addr(event_addr, &packet[3]); - uint16_t psm = READ_BT_16(packet, 11); - uint16_t source_cid = READ_BT_16(packet, 13); - printf("Channel successfully opened: "); - print_bd_addr(event_addr); - printf(", handle 0x%02x, psm 0x%02x, source cid 0x%02x, dest cid 0x%02x\n", - READ_BT_16(packet, 9), psm, source_cid, READ_BT_16(packet, 15)); - if (psm == 0x13) { - // interupt channel openedn succesfully, now open control channel, too. - bt_send_cmd(&l2cap_create_channel, event_addr, 0x11); - } else { - // request acceleration data.. probably has to be sent to control channel 0x11 instead of 0x13 - uint8_t setMode31[] = { 0x52, 0x12, 0x00, 0x31 }; - bt_send_l2cap( source_cid, setMode31, sizeof(setMode31)); - uint8_t setLEDs[] = { 0x52, 0x11, 0x10 }; - bt_send_l2cap( source_cid, setLEDs, sizeof(setLEDs)); - (*state_cb)("WiiMote connected"); - } - - case HCI_EVENT_COMMAND_COMPLETE: - // connect to HID device (PSM 0x13) at addr - if ( COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable) ) { - (*state_cb)("Connecting to WiiMote"); - bt_send_cmd(&l2cap_create_channel, addr, 0x13); - } - break; - - default: - break; - } - - default: - break; - } -} - -void set_bt_state_cb(void (*cb)(char *text)){ - state_cb = cb; -} - -void set_data_cb(void (*handler)(uint8_t x, uint8_t y, uint8_t z)){ - data_cb = handler; -} - -void start_bt(){ - run_loop_init(RUN_LOOP_COCOA); - bt_open(); - bt_register_packet_handler(packet_handler); - (*state_cb)("BT started"); - bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); -} \ No newline at end of file diff --git a/example/WiiMoteOpenGLDemo/BTWiiMote.h b/example/WiiMoteOpenGLDemo/BTWiiMote.h deleted file mode 100644 index e474f621d..000000000 --- a/example/WiiMoteOpenGLDemo/BTWiiMote.h +++ /dev/null @@ -1,13 +0,0 @@ -/* - * BTWiiMote.h - * - * Created by Matthias Ringwald on 8/2/09. - */ - -#include - -void start_bt(); -void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)); - -void set_bt_state_cb(void (*cb)(char *text)); -void set_data_cb(void (*handler)(uint8_t x, uint8_t y, uint8_t z)); diff --git a/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.h b/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.h new file mode 100644 index 000000000..f0b68994b --- /dev/null +++ b/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.h @@ -0,0 +1,11 @@ +// +// EAGLViewController.h +// +// Created by Matthias Ringwald on 10/23/09. +// + +#import + + +@interface EAGLViewController : UIViewController +@end diff --git a/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.m b/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.m new file mode 100644 index 000000000..326a40a61 --- /dev/null +++ b/example/WiiMoteOpenGLDemo/Classes/EAGLViewController.m @@ -0,0 +1,25 @@ +// +// EAGLViewController.m +// +// Created by Matthias Ringwald on 10/23/09. +// + +#import "EAGLViewController.h" +#import "EAGLView.h" + +@implementation EAGLViewController +// The simplest UIViewController subclass just overrides -loadView. +- (void)loadView +{ + UIView *view = [[EAGLView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; + self.view = view; + [view release]; + + self.title = @"WiiMote"; +} +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + return YES; +} + +@end diff --git a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.h b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.h index f1e10e03b..11e563635 100644 --- a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.h +++ b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.h @@ -5,18 +5,23 @@ // #import +#import "BTInquiryViewController.h" @class EAGLView; -@interface WiiMoteOpenGLDemoAppDelegate : NSObject { +@interface WiiMoteOpenGLDemoAppDelegate : NSObject { UIWindow *window; + UIViewController *glViewControl; + BTInquiryViewController *inqViewControl; + UINavigationController *navControl; EAGLView *glView; UILabel *status; } -@property (nonatomic, retain) IBOutlet UIWindow *window; -@property (nonatomic, retain) IBOutlet EAGLView *glView; -@property (nonatomic, retain) IBOutlet UILabel *status; +@property (nonatomic, retain) UIWindow *window; +@property (nonatomic, retain) UINavigationController *navControl; +@property (nonatomic, retain) UIViewController *glViewControl; +@property (nonatomic, retain) EAGLView *glView; @end diff --git a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m index 577c28660..20fb86f2c 100644 --- a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m +++ b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m @@ -5,10 +5,18 @@ // #import "WiiMoteOpenGLDemoAppDelegate.h" +#import "BTDevice.h" #import "EAGLView.h" -#include "../BTWiiMote.h" +#import "EAGLViewController.h" +#import "BTInquiryViewController.h" -// #define USE_BLUETOOTH +#import +#import +#import + +#define USE_BLUETOOTH + +bd_addr_t addr = {0x00, 0x19, 0x1d, 0x90, 0x44, 0x68 }; // WiiMote WiiMoteOpenGLDemoAppDelegate * theMainApp; @@ -16,14 +24,10 @@ WiiMoteOpenGLDemoAppDelegate * theMainApp; @synthesize window; @synthesize glView; -@synthesize status; - -static void bt_state_cb(char *text){ - NSLog(@"BT state: %s", text); - NSString *stringFromUTFString = [[NSString alloc] initWithUTF8String:text]; - [[theMainApp status] setText:stringFromUTFString]; -} +@synthesize navControl; +@synthesize glViewControl; +#define SIZE 5 static void bt_data_cb(uint8_t x, uint8_t y, uint8_t z){ // NSLog(@"BT data: %u %u %u", x , y ,z); // [[theMainApp status] setText:[NSString stringWithFormat:@"X:%03u Y:%03u Z:%03u", x, y, z]]; @@ -33,10 +37,8 @@ static void bt_data_cb(uint8_t x, uint8_t y, uint8_t z){ int roll = atan2(ax, sqrt(ay*ay+az*az)) * 180 / M_PI; int pitch = atan2(ay, sqrt(ax*ax+az*az)) * 180 / M_PI; - #if 1 // moving average of size SIZE -#define SIZE 5 static int pos = 0; static int historyRoll[SIZE]; static int historyPitch[SIZE]; @@ -55,30 +57,87 @@ static void bt_data_cb(uint8_t x, uint8_t y, uint8_t z){ roll = roll / SIZE; pitch = pitch / SIZE; #endif - // pitch += 90; [[theMainApp glView] setRotationX:-pitch Y:roll Z:0]; } +void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ + bd_addr_t event_addr; + + switch (packet_type) { + + case L2CAP_DATA_PACKET: + if (packet[0] == 0xa1 && packet[1] == 0x31){ + bt_data_cb(packet[4], packet[5], packet[6]); + } + break; + + case HCI_EVENT_PACKET: + + switch (packet[0]){ + + case L2CAP_EVENT_CHANNEL_OPENED: + // inform about new l2cap connection + bt_flip_addr(event_addr, &packet[3]); + uint16_t psm = READ_BT_16(packet, 11); + uint16_t source_cid = READ_BT_16(packet, 13); + printf("Channel successfully opened: "); + print_bd_addr(event_addr); + printf(", handle 0x%02x, psm 0x%02x, source cid 0x%02x, dest cid 0x%02x\n", + READ_BT_16(packet, 9), psm, source_cid, READ_BT_16(packet, 15)); + if (psm == 0x13) { + // interupt channel openedn succesfully, now open control channel, too. + bt_send_cmd(&l2cap_create_channel, event_addr, 0x11); + } else { + // request acceleration data.. probably has to be sent to control channel 0x11 instead of 0x13 + uint8_t setMode31[] = { 0x52, 0x12, 0x00, 0x31 }; + bt_send_l2cap( source_cid, setMode31, sizeof(setMode31)); + uint8_t setLEDs[] = { 0x52, 0x11, 0x10 }; + bt_send_l2cap( source_cid, setLEDs, sizeof(setLEDs)); + // (*state_cb)("WiiMote connected"); + } + + default: + break; + } + + default: + break; + } +} + - (void)applicationDidFinishLaunching:(UIApplication *)application { NSLog(@"Started"); + +#ifdef USE_BLUETOOTH + run_loop_init(RUN_LOOP_COCOA); + bt_open(); + bt_register_packet_handler(packet_handler); +#endif + // create window window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setBackgroundColor:[UIColor blueColor]]; - // add view to window - glView = [[EAGLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - [window addSubview:glView]; - - // status = [[UILabel alloc] init]; - // [status setText:@"This is my text"]; - theMainApp = self; + // create view controller + glViewControl = [[EAGLViewController alloc] init]; + // create inq controller + inqViewControl = [[BTInquiryViewController alloc] init]; + [inqViewControl setTitle:@"BTstack Device Selector"]; + // create nav view controller + navControl = [[UINavigationController alloc] initWithRootViewController:inqViewControl]; + + // add view to window + [window addSubview:[navControl view]]; + + theMainApp = self; + #ifdef USE_BLUETOOTH - set_bt_state_cb(bt_state_cb); - set_data_cb(bt_data_cb); - start_bt(); + [inqViewControl setDelegate:self]; + [inqViewControl startInquiry]; #endif + glView = (EAGLView *) [glViewControl view]; glView.animationInterval = 1.0 / 60.0; [glView startAnimation]; @@ -95,6 +154,12 @@ static void bt_data_cb(uint8_t x, uint8_t y, uint8_t z){ glView.animationInterval = 1.0 / 60.0; } +-(void) deviceChoosen:(BTInquiryViewController *) inqView device:(BTDevice*) device{ + NSLog(@"deviceChoosen %@", [device toString]); + + [navControl pushViewController:glViewControl animated:YES]; +} + - (void)dealloc { [window release]; diff --git a/example/WiiMoteOpenGLDemo/WiiMoteOpenGLDemo.xcodeproj/project.pbxproj b/example/WiiMoteOpenGLDemo/WiiMoteOpenGLDemo.xcodeproj/project.pbxproj index dbdea1311..8785b8100 100755 --- a/example/WiiMoteOpenGLDemo/WiiMoteOpenGLDemo.xcodeproj/project.pbxproj +++ b/example/WiiMoteOpenGLDemo/WiiMoteOpenGLDemo.xcodeproj/project.pbxproj @@ -15,11 +15,11 @@ 28FD15080DC6FC5B0079059D /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28FD15070DC6FC5B0079059D /* QuartzCore.framework */; }; 9C0D06391091035200FC3BBA /* BTDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C0D06361091035200FC3BBA /* BTDevice.m */; }; 9C0D063A1091035200FC3BBA /* BTInquiryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C0D06381091035200FC3BBA /* BTInquiryViewController.m */; }; + 9C0D070D1092316D00FC3BBA /* EAGLViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C0D070C1092316D00FC3BBA /* EAGLViewController.m */; }; 9C180042108B95B000824BE7 /* libBTstack.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C180041108B95B000824BE7 /* libBTstack.dylib */; }; 9C6BB62E1027911E00A0BCB0 /* wiimote_texture.png in Resources */ = {isa = PBXBuildFile; fileRef = 9C6BB62D1027911E00A0BCB0 /* wiimote_texture.png */; }; 9CB96E9810278945002663D0 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9CB96E9710278945002663D0 /* CoreGraphics.framework */; }; 9CB96EEF10278D8D002663D0 /* EAGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = 28FD14FD0DC6FC130079059D /* EAGLView.m */; }; - 9CCE6DDA1025E18700FCE9F4 /* BTWiiMote.c in Sources */ = {isa = PBXBuildFile; fileRef = 9CCE6DD91025E18700FCE9F4 /* BTWiiMote.c */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -39,6 +39,8 @@ 9C0D06361091035200FC3BBA /* BTDevice.m */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.objc; name = BTDevice.m; path = ../../CocoaTouch/src/BTDevice.m; sourceTree = SOURCE_ROOT; }; 9C0D06371091035200FC3BBA /* BTInquiryViewController.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = BTInquiryViewController.h; path = ../../CocoaTouch/include/BTstack/BTInquiryViewController.h; sourceTree = SOURCE_ROOT; }; 9C0D06381091035200FC3BBA /* BTInquiryViewController.m */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.objc; name = BTInquiryViewController.m; path = ../../CocoaTouch/src/BTInquiryViewController.m; sourceTree = SOURCE_ROOT; }; + 9C0D070B1092316D00FC3BBA /* EAGLViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EAGLViewController.h; sourceTree = ""; }; + 9C0D070C1092316D00FC3BBA /* EAGLViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EAGLViewController.m; sourceTree = ""; }; 9C18001B108B94FB00824BE7 /* btstack.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; path = btstack.h; sourceTree = ""; }; 9C18001C108B94FB00824BE7 /* hci_cmds.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; path = hci_cmds.h; sourceTree = ""; }; 9C18001D108B94FB00824BE7 /* linked_list.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; path = linked_list.h; sourceTree = ""; }; @@ -47,8 +49,6 @@ 9C180041108B95B000824BE7 /* libBTstack.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libBTstack.dylib; path = ../../src/libBTstack.dylib; sourceTree = SOURCE_ROOT; }; 9C6BB62D1027911E00A0BCB0 /* wiimote_texture.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wiimote_texture.png; sourceTree = ""; }; 9CB96E9710278945002663D0 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; - 9CCE6DD91025E18700FCE9F4 /* BTWiiMote.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; path = BTWiiMote.c; sourceTree = ""; }; - 9CCE6DDB1025E19600FCE9F4 /* BTWiiMote.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BTWiiMote.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -73,6 +73,8 @@ children = ( 28FD14FC0DC6FC130079059D /* EAGLView.h */, 28FD14FD0DC6FC130079059D /* EAGLView.m */, + 9C0D070B1092316D00FC3BBA /* EAGLViewController.h */, + 9C0D070C1092316D00FC3BBA /* EAGLViewController.m */, 1D3623240D0F684500981E51 /* WiiMoteOpenGLDemoAppDelegate.h */, 1D3623250D0F684500981E51 /* WiiMoteOpenGLDemoAppDelegate.m */, ); @@ -152,8 +154,6 @@ 9C0D06361091035200FC3BBA /* BTDevice.m */, 9C0D06371091035200FC3BBA /* BTInquiryViewController.h */, 9C0D06381091035200FC3BBA /* BTInquiryViewController.m */, - 9CCE6DD91025E18700FCE9F4 /* BTWiiMote.c */, - 9CCE6DDB1025E19600FCE9F4 /* BTWiiMote.h */, ); name = BTstack; sourceTree = ""; @@ -213,10 +213,10 @@ files = ( 1D60589B0D05DD56006BFB54 /* main.m in Sources */, 1D3623260D0F684500981E51 /* WiiMoteOpenGLDemoAppDelegate.m in Sources */, - 9CCE6DDA1025E18700FCE9F4 /* BTWiiMote.c in Sources */, 9CB96EEF10278D8D002663D0 /* EAGLView.m in Sources */, 9C0D06391091035200FC3BBA /* BTDevice.m in Sources */, 9C0D063A1091035200FC3BBA /* BTInquiryViewController.m in Sources */, + 9C0D070D1092316D00FC3BBA /* EAGLViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };