Ok, here is the whole code:
#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
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);
}
#ifndef BPLib_h
#define BPLib_h
#include "Arduino.h"
//Bluetooth Modes
#define BP_MODE_COMMAND "$$"
#define BP_MODE_EXITCOMMAND "---\r\n"
#define BP_MODE_SPP "S~,0\r\n"
#define BP_MODE_HID "S~,6\r\n"
#define BP_MODE_AUTOCONNECT "SM,6\r\n"
#define BP_MODE_MANUCONNECT "SM,4\r\n"
#define BP_MODE_STATUS "SO,/#\r\n"
//Bluetooth status messages
#define BP_STAT_CMD "CMD\r\n"
#define BP_STAT_END "END\r\n"
#define BP_STAT_ACK "AOK\r\n"
#define BP_STAT_REBOOT "Reboot!\r\n"
//Bluetooth GET commands
#define BP_GET_HID "GH\n"
//Bluetooth system commands
#define BP_REBOOT "R,1\r\n"
#define BP_RECONNECT "C\r\n"
#define BP_CHANGE_NAME "SN,"
//Bluetooth protocol types
#define BP_SPP_SPP "AW\r\n"
#define BP_HID_KEYBOARD "SH,0200\r\n"
#define BP_HID_MOUSE "SH,0220\r\n"
#define BP_HID_GAMEPAD "SH,0210\r\n"
#define BP_HID_JOYSTICK "SH,0240\r\n"
#define BP_HID_COMBO "SH,0230\r\n"
// KEYBOARD Scan Codes
#define BP_KEY_ENTER 0x28 //Enter
#define BP_KEY_LEFT_ARROW 0x50 //Left arrow
#define BP_KEY_DOWN_ARROW 0x51 //Down arrow
#define BP_KEY_ENTER 0x28 //Enter
#define BP_KEY_UP_ARROW 0x52 //Up arrow
#define BP_KEY_ESCAPE 0x29 //Escape
#define BP_KEY_CAPSLOCK 0x39 //CapsLock
#define BP_KEY_SCROLLLOCK 0x47 //ScrollLock
#define BP_KEY_BREAK_PAUSE 0x48 //Break-pause
#define BP_KEY_NUMLOCK 0x53 //NumLock
#define BP_KEY_TOGGLE_IPHONE_VIRTUAL_KEYBOARD 0x65 //Toggle iPhone Virtual Keyboard
#define BP_KEY_LEFT_CONTROL 0xE0 //Left Control
#define BP_KEY_LEFT_SHIFT 0xE1 //Left Shift
#define BP_KEY_LEFT_ALT 0xE2 //Left Alt
#define BP_KEY_LEFT_GUI 0xE3 //Left GUI
#define BP_KEY_RIGHT_CONTROL 0xE4 //Right Control
#define BP_KEY_RIGHT_SHIFT 0xE5 //Right Shift
#define BP_KEY_RIGHT_ALT 0xE6 //Right Alt
#define BP_KEY_RIGHT_GUI 0xE7 //Right GUI
#define BP_KEY_F1 0x3A //F1
#define BP_KEY_F2 0x3B //F2
#define BP_KEY_F3 0x3C //F3
#define BP_KEY_F4 0x3D //F4
#define BP_KEY_F5 0x3E //F5
#define BP_KEY_F6 0x3F //F6
#define BP_KEY_F7 0x40 //F7
#define BP_KEY_F8 0x41 //F8
#define BP_KEY_F9 0x42 //F9
#define BP_KEY_F10 0x43 //F10
#define BP_KEY_F11 0x44 //F11
#define BP_KEY_F12 0x45 //F12
//KEYBOARD MODEFIER CODES
#define BP_MOD_RIGHT_GUI (1<<7)
#define BP_MOD_RIGHT_ALT (1<<6)
#define BP_MOD_RIGHT_SHIFT (1<<5)
#define BP_MOD_RIGHT_CTRL (1<<4)
#define BP_MOD_LEFT_GUI (1<<3)
#define BP_MOD_LEFT_ALT (1<<2)
#define BP_MOD_LEFT_SHIFT (1<<1)
#define BP_MOD_LEFT_CTRL (1<<0)
#define BP_MOD_NOMOD 0x00
//Mouse Codes
#define BP_MOUSE_BTN_LEFT (1<<0)
#define BP_MOUSE_BTN_RIGHT (1<<1)
#define BP_MOUSE_BTN_MIDDLE (1<<2)
//GAMEPAD/JOYSTICK BUTTON CODES
//ST == First button combination (BTN0 to BTN7)
#define BP_GAMEJOY_ST_BTN0 (1<<0)
#define BP_GAMEJOY_ST_BTN1 (1<<1)
#define BP_GAMEJOY_ST_BTN2 (1<<2)
#define BP_GAMEJOY_ST_BTN3 (1<<3)
#define BP_GAMEJOY_ST_BTN4 (1<<4)
#define BP_GAMEJOY_ST_BTN5 (1<<5)
#define BP_GAMEJOY_ST_BTN6 (1<<6)
#define BP_GAMEJOY_ST_BTN7 (1<<7)
#define BP_GAMEJOY_ST_NOBTN 0x00
//ND = Second button combination (BTN0 to BTN7)
#define BP_GAMEJOY_ND_BTN0 (1<<0)
#define BP_GAMEJOY_ND_BTN1 (1<<1)
#define BP_GAMEJOY_ND_BTN2 (1<<2)
#define BP_GAMEJOY_ND_BTN3 (1<<3)
#define BP_GAMEJOY_ND_BTN4 (1<<4)
#define BP_GAMEJOY_ND_BTN5 (1<<5)
#define BP_GAMEJOY_ND_BTN6 (1<<6)
#define BP_GAMEJOY_ND_BTN7 (1<<7)
#define BP_GAMEJOY_ND_NOBTN 0x00
class BPLib
{
public:
BPLib();
byte begin(char BP_Mode[], char BP_Type[]);
byte sendCmd(char BP_CMD[]);
void sendByte(byte rawData);
void sendChar(char rawData);
void sendInt(int rawData);
void sendFloat(float rawData);
void sendLong(long rawData);
void sendString(char rawData[]);
byte readRaw();
int available();
void keyboardPrint(char BP_MSG[]);
void keyboardPress(byte BP_KEY,byte BP_MOD);
void keyboardReleaseAll();
void mouseClick(byte BP_BUTTON);
void mouseMove(signed int BP_X,signed int BP_Y);
void mouseWheel(signed int BP_WHEEL);
void mousePress(byte BP_BUTTON);
void mouseReleaseAll();
void gameJoyPress(byte BP_ST_BTN, byte BP_ND_BTN);
void gameJoyMove(signed int BP_X1,signed int BP_Y1,signed int BP_X2,signed int BP_Y2);
void gameJoyReleaseAll();
byte connected();
byte changeName(char BP_NAME[]);
private:
byte get(char BP_STAT[], byte strlen);
};
#endif