-
Notifications
You must be signed in to change notification settings - Fork 3
/
MotionSystem.h
102 lines (85 loc) · 2.13 KB
/
MotionSystem.h
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
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
using namespace std;
class MotionSystem
{
public:
MotionSystem(char dest[18]);
~MotionSystem();
void forward();
void backward();
void left();
void right();
void stop();
private:
int client_sd;
bool enabled;
};
MotionSystem::MotionSystem(char dest[18])
{
enabled = false;
if(!enabled){return;}
struct sockaddr_rc addr = { 0 };
int status;
//char dest[18] = "00:1B:10:80:13:ED";
// allocate a socket
client_sd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );
// connect to server
status = connect(client_sd, (struct sockaddr *)&addr, sizeof(addr));
// send a message
if ( status < 0 ) {
perror("Failed to connect to Bluetooth device.");
}
}
MotionSystem::~MotionSystem()
{
if(!enabled){return;}
close(client_sd);
}
void MotionSystem::stop()
{
if(!enabled){return;}
int status = write(client_sd, " ", 1);
if(status < 0) {
perror("Failed to send 'stop' directive to MotionSystem via bluetooth.");
}
}
void MotionSystem::forward()
{
if(!enabled){return;}
int status = write(client_sd, "w", 1);
if(status < 0) {
perror("Failed to send 'forward' directive to MotionSystem via bluetooth.");
}
}
void MotionSystem::backward()
{
if(!enabled){return;}
int status = write(client_sd, "s", 1);
if(status < 0) {
perror("Failed to send 'backward' directive to MotionSystem via bluetooth.");
}
}
void MotionSystem::left()
{
if(!enabled){return;}
int status = write(client_sd, "a", 1);
if(status < 0) {
perror("Failed to send 'left' directive to MotionSystem via bluetooth.");
}
}
void MotionSystem::right()
{
if(!enabled){return;}
int status = write(client_sd, "d", 1);
if(status < 0) {
perror("Failed to send 'right' directive to MotionSystem via bluetooth.");
}
}