-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBluetoothRN42.cpp
198 lines (160 loc) · 2.93 KB
/
BluetoothRN42.cpp
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
193
194
195
196
197
198
/**
* @file BluetoothRN42.cpp
* @brief
* @author Travis Lane
* @version
* @date 2015-03-05
*/
#include "BluetoothRN42.h"
#include "Arduino.h"
const char *const BluetoothRN42::power_level_strs[] = {
RN42_POWER_N12_STR,
RN42_POWER_N8_STR ,
RN42_POWER_N4_STR,
RN42_POWER_0_STR,
RN42_POWER_4_STR,
RN42_POWER_8_STR,
RN42_POWER_12_STR,
RN42_POWER_16_STR
};
void BluetoothRN42::setup()
{
io->begin(115500);
delay(500);
enterCommand();
io->println("U,9600,N");
delay(500);
io->begin(9600);
enterCommand();
// Set Required Options
setMode(mode);
// Set Optional Options
if (pin != NULL)
setPin(pin);
if (name != NULL)
setName(name);
if (cod != NULL)
setCod(cod);
pinMode(status_pin, INPUT);
exitCommand();
}
void BluetoothRN42::enterCommand()
{
if(inCommand) {
return;
}
// Start by printing w/o CR.
io->print("$$$");
// Wait for chip
delay(100);
inCommand = true;
}
void BluetoothRN42::exitCommand()
{
if(!inCommand) {
return;
}
inCommand = false;
io->println("---");
delay(50);
}
int BluetoothRN42::setMode(int new_mode)
{
if(!inCommand) {
return -1;
}
// Set the mode
io->print("SM,");
io->println(new_mode);
delay(50);
return Bluetooth::setMode(new_mode);
}
int BluetoothRN42::setPin(const char *new_pin)
{
if(!inCommand) {
return -1;
}
io->print("SP,");
io->println(new_pin);
delay(50);
return Bluetooth::setPin(new_pin);
}
int BluetoothRN42::setName(const char *new_name)
{
if(!inCommand) {
return -1;
}
io->print("SN,");
io->println(new_name);
delay(50);
return Bluetooth::setName(new_name);
}
int BluetoothRN42::setCod(const char *new_cod)
{
if(!inCommand) {
return -1;
}
// Print the first two characters.
io->print("SC,00");
io->print(new_cod[5]);
io->println(new_cod[4]);
delay(50);
// Print the last four characters.
io->print("SD,");
io->print(new_cod[3]);
io->print(new_cod[2]);
io->print(new_cod[1]);
io->println(new_cod[0]);
delay(50);
return Bluetooth::setCod(new_cod);
}
int BluetoothRN42::setInquryScanWindow(const char *scan_window)
{
if(!inCommand) {
return -1;
}
io->print("SI,");
io->println(scan_window);
delay(50);
return 0;
}
int BluetoothRN42::setPageScanWindow(const char *scan_window)
{
if(!inCommand) {
return -1;
}
io->print("SJ,");
io->println(scan_window);
delay(50);
return 0;
}
int BluetoothRN42::setBonding(bool bonding_enabled)
{
if(!inCommand) {
return -1;
}
if(bonding_enabled) {
io->println("SX,1");
} else {
io->println("SX,0");
}
delay(50);
return 0;
}
int BluetoothRN42::setPower(enum rn42_power_e power_level)
{
if(!inCommand) {
return -1;
} else if(power_level > RN42_POWER_MAX || power_level << RN42_POWER_MIN) {
//ERANGE
return -2;
}
io->print("SC,");
io->println(power_level_strs[power_level]);
delay(50);
return 0;
}
bool BluetoothRN42::connected()
{
return digitalRead(status_pin) == HIGH;
}