Cardboard Robot SDK
 All Classes Functions Properties
CBDeviceConnection.h
1 /*=========================================================================
2  This file is part of the Cardboard Robot SDK.
3 
4  Copyright (C) 2012 Ken Ihara.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 =========================================================================*/
19 
20 #import <Foundation/Foundation.h>
21 #import "hidapi/hidapi.h"
22 #import "CBRobot.h"
23 #import "CBDeviceDelegate.h"
24 
25 #define CBR_VENDOR_ID 0x04d8 /* Vendor ID of the USB device */
26 #define CBR_PRODUCT_ID 0xfc8b /* Product ID of the USB device */
27 
28 enum dataConditions { CB_NO_DATA, CB_HAS_DATA };
29 enum connectionConditions { CB_NOT_SENT, CB_SENT };
30 
31 /* Data sent to the device */
32 typedef struct {
33  short m1Target, m2Target, m3Target, m4Target;
34  short m1Speed, m2Speed, m3Speed, m4Speed;
35  BOOL paused;
36 } CBRDataOut;
37 
38 /* Data read from the device */
39 typedef struct {
40  short m1Current, m2Current, m3Current, m4Current;
41 } CBRDataIn;
42 
43 @class CBRobot;
44 
45 /* A low-level connection to the USB device. */
46 @interface CBDeviceConnection : NSObject {
47 
48  hid_device *deviceHandle; /* Device handle, NULL if not connected */
49 
50  NSThread *thread; /* Read / write thread */
51 
52  CBRDataOut dataOut; /* Data to write / last written */
53  CBRDataIn dataIn; /* Data last read */
54  BOOL resetHomePositionFlag; /* Should the home position be set on the next write? */
55 
56  NSConditionLock *dataMutex; /* Data mutex with NO_DATA / HAS_DATA flag */
57  NSConditionLock *connectionMutex; /* Connection state mutex with NOT_WRITTEN / WRITTEN flag */
58 }
59 
61 @property (readonly, nonatomic) BOOL connected;
62 
63 - (id)init;
64 - (void)cancel;
65 
66 - (void)tryConnect;
67 - (void)disconnect;
68 - (void)testConnection;
69 
70 - (void)updateRobot:(CBRobot *)robot;
71 
72 @end