-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCRemoteAccelerometer.m
192 lines (153 loc) · 4.67 KB
/
SCRemoteAccelerometer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// SCRemoteAccelerometer.m
// Alarm-Clock
//
// Created by Christian Stropp on 03.02.10.
// Copyright 2010 Christian Stropp. All rights reserved.
//
#import "SCRemoteAccelerometer.h"
#import <stdio.h> /* Standard input/output definitions */
#import <stdlib.h>
#import <stdint.h> /* Standard types */
#import <string.h> /* String function definitions */
#import <unistd.h> /* UNIX standard function definitions */
#import <fcntl.h> /* File control definitions */
#import <errno.h> /* Error number definitions */
#import <termios.h> /* POSIX terminal control definitions */
#import <sys/ioctl.h>
#import <getopt.h>
#define kSerialportName @"/dev/tty.usbserial-A600ahGl"
#define kBaudRate 9600
@implementation SCRemoteAccelerometer
@synthesize delegate;
- (id)init
{
if (self = [super init])
{
if (![self initSerialportWithName:kSerialportName andBaudRate:kBaudRate])
return nil;
isReading = NO;
}
return self;
}
- (void)startReading
{
isReading = YES;
[NSThread detachNewThreadSelector:@selector(readLoop) toTarget:self withObject:nil];
}
- (void)stopReading
{
isReading = NO;
}
- (void)readLoop
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (isReading)
{
NSString *message = [self readUntilCharacter:'\n'];
//NSLog(@"%@", message);
if ([message floatValue] == -1)
{
if ([delegate respondsToSelector:@selector(remoteAccelerometerDidReceiveButtonDown:)])
[delegate remoteAccelerometerDidReceiveButtonDown:self];
}
else if ([delegate respondsToSelector:@selector(remoteAccelerometer:didReceiveAcceleration:)])
[delegate remoteAccelerometer:self didReceiveAcceleration:[message floatValue]];
}
[pool drain];
}
- (Boolean)sendByte:(uint8_t)b
{
int n = write(serialport, &b, 1);
if (n != 1)
return NO;
return YES;
}
#pragma mark -
#pragma mark Serial Communication
// based on: http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
- (Boolean)sendString:(NSString *)str
{
int len = strlen([str UTF8String]);
int n = write(serialport, [str UTF8String], len);
if (n != len)
return NO;
return YES;
}
- (NSString *)readUntilCharacter:(char)until
{
char buf[128];
char b[1];
int i = 0;
do
{
int n = read(serialport, b, 1); // read a char at a time
if (n == -1)
return nil; // couldn't read
if (n == 0)
{
usleep(10 * 1000); // wait 10 msec try again
continue;
}
buf[i] = b[0];
i++;
} while (b[0] != until);
buf[i] = 0; // null terminate the string
return [NSString stringWithUTF8String:buf];
}
// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
// and a baud rate (bps) and connects to that port at that speed and 8N1.
// opens the port in fully raw mode so you can send binary data.
// returns valid fd, or -1 on error
- (Boolean)initSerialportWithName:(NSString *)serialportName andBaudRate:(int)baud
{
struct termios toptions;
//fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
// serialport,baud);
serialport = open([serialportName UTF8String], O_RDWR | O_NOCTTY | O_NDELAY);
if (serialport == -1) {
perror("init_serialport: Unable to open port ");
return NO;
}
if (tcgetattr(serialport, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return NO;
}
speed_t brate = baud; // let you override switch below if needed
switch(baud) {
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
#ifdef B14400
case 14400: brate=B14400; break;
#endif
case 19200: brate=B19200; break;
#ifdef B28800
case 28800: brate=B28800; break;
#endif
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
}
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
if (tcsetattr(serialport, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return NO;
}
return YES;
}
@end