-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBPLib.cpp
More file actions
218 lines (195 loc) · 5.51 KB
/
BPLib.cpp
File metadata and controls
218 lines (195 loc) · 5.51 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
BPLib.cpp - Library for communication with RN-42 HID Bluetooth module
Created by Basel Al-Rudainy, 6 april 2013.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*/
#include "Arduino.h"
#include "BPLib.h"
BPLib::BPLib(){
pinMode(7,INPUT);
digitalWrite(7,LOW);
}
byte BPLib::begin(char BP_Mode[], char BP_Type[])
{
Serial1.begin(115200);
Serial1.print(BP_MODE_COMMAND);
if (get(BP_STAT_CMD, (byte)5)!=1) {
return (byte)0;
}//if
Serial1.print(BP_Mode);
if (get(BP_STAT_ACK, (byte)5)!=1) {
return (byte)0;
}//if
if(strcmp(BP_Type,BP_SPP_SPP)>0){
Serial1.print(BP_Type);
if (get(BP_STAT_ACK, (byte)5)!=1) {
return (byte)0;
}//if
}
Serial1.print(BP_REBOOT);
if (get(BP_STAT_REBOOT, (byte)9)!=1) {
return (byte)0;
}//if
delay(1000); //Delay (Bluetooth boot-up)
return (byte)1;
}
byte BPLib::sendCmd(char BP_CMD[])
{
Serial1.print(BP_MODE_COMMAND);
if (get(BP_STAT_CMD, (byte)5)!=1) {
return (byte)0;
}//if
Serial1.print(BP_CMD);
if (get(BP_STAT_ACK, (byte)5)!=1) {
return (byte)0;
}//if
Serial1.print(BP_MODE_EXITCOMMAND);
if (get(BP_STAT_END, (byte)5)!=1) {
return (byte)0;
}//if
return (byte)1;
}
byte BPLib::readRaw(){
return Serial1.read();
}
int BPLib::available(){
return Serial1.available();
}
byte BPLib::get(char BP_STAT[],byte strlen)
{
char buffer[strlen + 1];
while (Serial1.available() <= (strlen-1)) {};
int count = 0;
while (Serial1.available() > 0) {
buffer[count]=(char)Serial1.read();
count++;
}//while
buffer[strlen]=0;
//Serial.print(buffer);//DEBUG
if (strcmp(buffer,BP_STAT)==0) {
return (byte)1;
}//if
else {
return (byte)0;
}//else
}//get
void BPLib::keyboardPress(byte BP_KEY,byte BP_MOD){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x9); //Length byte
Serial1.write((byte)0x1); //Descriptor byte
Serial1.write(BP_MOD); //Modifier byte
Serial1.write((byte)0x00); //-
Serial1.write(BP_KEY); //Send KEY
for(byte i = 0;i<5;i++){ //Send five zero bytes
Serial1.write((byte)0x00);
}
}
void BPLib::keyboardReleaseAll(){
keyboardPress((byte)0x00,BP_MOD_NOMOD);
}
void BPLib::mouseClick(byte BP_BUTTON){
mousePress(BP_BUTTON);
mouseReleaseAll();
}
void BPLib::mouseMove(signed int BP_X,signed int BP_Y){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x5); //Length byte
Serial1.write((byte)0x2); //Descriptor byte
Serial1.write((byte)0x00); //Button byte
Serial1.write(BP_X); //(-127 to 127)
Serial1.write(BP_Y); //(-127 to 127)
Serial1.write((byte)0x00);
}
void BPLib::mousePress(byte BP_BUTTON){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x5); //Length byte
Serial1.write((byte)0x2); //Descriptor byte
Serial1.write(BP_BUTTON); //Button byte
for(byte i = 0;i<3;i++){ //Send three zero bytes
Serial1.write((byte)0x00);
}
}
void BPLib::mouseReleaseAll(){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x5); //Length byte
Serial1.write((byte)0x2); //Descriptor byte
for(byte i = 0;i<4;i++){ //Send four zero bytes
Serial1.write((byte)0x00);
}
}
void BPLib::mouseWheel(signed int BP_WHEEL){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x5); //Length byte
Serial1.write((byte)0x2); //Descriptor byte
for(byte i = 0;i<3;i++){ //Send three zero bytes
Serial1.write((byte)0x00);
}
Serial1.write(BP_WHEEL); //Wheel byte (-127 to 127)
}
byte BPLib::changeName(char BP_NAME[]){
Serial1.print(BP_MODE_COMMAND);
if (get(BP_STAT_CMD, (byte)5)!=1) {
return (byte)0;
}//if
Serial1.print(BP_CHANGE_NAME);
Serial1.print(BP_NAME);
Serial1.print(F("\r\n"));
if (get(BP_STAT_ACK, (byte)5)!=1) {
return (byte)0;
}//if
Serial1.print(BP_MODE_EXITCOMMAND);
if (get(BP_STAT_END, (byte)5)!=1) {
return (byte)0;
}//if
return (byte)1;
}
void BPLib::sendByte(byte rawData){
Serial1.print(rawData);
}
void BPLib::sendChar(char rawData){
Serial1.print(rawData);
}
void BPLib::sendInt(int rawData){
Serial1.print(rawData);
}
void BPLib::sendFloat(float rawData){
Serial1.print(rawData);
}
void BPLib::sendLong(long rawData){
Serial1.print(rawData);
}
void BPLib::sendString(char rawData[]){
Serial1.print(rawData);
}
void BPLib::gameJoyPress(byte BP_ST_BTN, byte BP_ND_BTN){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x6); //Length byte
Serial1.write((byte)BP_ST_BTN); //First Button byte
Serial1.write((byte)BP_ND_BTN); //Second Button byte
for(byte i = 0;i<4;i++){ //Send four zero bytes
Serial1.write((byte)0x00);
}
}
void BPLib::gameJoyMove(signed int BP_X1,signed int BP_Y1,signed int BP_X2,signed int BP_Y2){
Serial1.write((byte)0xFD); //Start HID Report
Serial1.write((byte)0x6); //Length byte
Serial1.write((byte)BP_GAMEJOY_ST_NOBTN); //First Button byte
Serial1.write((byte)BP_GAMEJOY_ND_NOBTN); //Second Button byte
Serial1.write(BP_X1 & 0xFF); //First X coordinate
Serial1.write(BP_Y1 & 0xFF); //First Y coordinate
Serial1.write(BP_X2 & 0xFF); //Second X coordinate
Serial1.write(BP_Y2 & 0xFF); //Second Y coordinate
}
void BPLib::gameJoyReleaseAll(){
gameJoyPress(BP_GAMEJOY_ST_NOBTN, BP_GAMEJOY_ND_NOBTN);
}
byte BPLib::connected(){
return digitalRead(7);
}