├── examples ├── HID_MOUSE_WHEEL │ └── HID_MOUSE_WHEEL.ino ├── HID_MOUSE │ └── HID_MOUSE.ino ├── SPP_SEND_REC_MSG │ └── SPP_SEND_REC_MSG.ino └── HID_KEYBOARD │ └── HID_KEYBOARD.ino ├── library.properties ├── README.md └── src ├── BPLib.h └── BPLib.cpp /examples/HID_MOUSE_WHEEL/HID_MOUSE_WHEEL.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Bluetooth Mouse Wheel - Example 3 | 4 | This example illustrated the use of the Bluetooth Mouse Wheel (Scrolling). 5 | 6 | */ 7 | 8 | #include 9 | 10 | BPLib *BPMod; 11 | void setup(){ 12 | Serial.begin(115200); 13 | BPMod = new BPLib(swSer); 14 | BPMod->begin(BP_MODE_HID,BP_HID_MOUSE); 15 | } 16 | 17 | void loop(){ 18 | delay(5000); 19 | for(signed int i=-5;i<5;i++){ 20 | BPMod->mouseWheel(i); 21 | delay(100); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=BPLib 2 | version=1.0.0 3 | author=Brian Lough 4 | maintainer=Brian Lough 5 | sentence=This library simplifies using the RN-42 Bluetooth Module 6 | paragraph=This library simplifies using the RN-42 Bluetooth Module, the main advantage of the RN-42 module is it allows you to emulate HID devices, which allows your Arduino project to connect to your devices as a Bluetooth keyboard, mouse or gamepad. 7 | category=Communication 8 | url=https://github.com/witnessmenow/BPLib 9 | architectures=* 10 | -------------------------------------------------------------------------------- /examples/HID_MOUSE/HID_MOUSE.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Bluetooth HID Mouse - Example 3 | 4 | This example illustrated how the library can be used to make mouse movements and clicks. 5 | 6 | */ 7 | 8 | #include 9 | 10 | BPLib *BPMod; 11 | void setup(){ 12 | Serial.begin(115200); 13 | BPMod = new BPLib(swSer); 14 | BPMod->begin(BP_MODE_HID,BP_HID_MOUSE); 15 | 16 | } 17 | 18 | void loop(){ 19 | delay(1000); 20 | for(signed int i=-127;i<127;i++){ 21 | BPMod->mouseMove(i,i); 22 | delay(100); 23 | } 24 | BPMod->mouseClick(BP_MOUSE_BTN_LEFT); 25 | } 26 | -------------------------------------------------------------------------------- /examples/SPP_SEND_REC_MSG/SPP_SEND_REC_MSG.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Bluetooth SPP - example 4 | This example illustrates the use of the bluetooth serial protocol. 5 | 6 | */ 7 | 8 | 9 | #include 10 | 11 | BPLib *BPMod; 12 | int count = 0; 13 | void setup(){ 14 | Serial.begin(115200); 15 | 16 | // The 7 pin was something that was previously hard coded into the library, its state gets read by the connected method 17 | // I'm not really sure what it does :) 18 | // I guess it has something to do with "BlueNar One only" comments 19 | pinMode(7, INPUT); 20 | BPMod = new BPLib(swSer, 7); 21 | BPMod->begin(BP_MODE_SPP,BP_SPP_SPP); //Bluetooth Serial Mode 22 | BPMod->changeName("BlueNar_One"); //Change bluetooth name (appears on devices searching for BT) 23 | while(!BPMod->connected()){}; //BlueNar One only! Check if a connection is made 24 | } 25 | 26 | void loop(){ 27 | if(BPMod->connected()){ //BlueNar One only! if connected then proceed 28 | if(BPMod->available()>0){ //Check if data is available 29 | BPMod->readRaw(); //Read the data (just to empty the buffer) 30 | count++; 31 | BPMod->sendString("Count: "); //Send a string 32 | BPMod->sendInt(count); //Send a integer 33 | BPMod->sendChar('\n'); //Send character 34 | } 35 | } 36 | else{ 37 | while(!BPMod->connected()){}; //BlueNar One only! - If disconnected wait for a new connection 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /examples/HID_KEYBOARD/HID_KEYBOARD.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Bluetooth HID Keyboard - example 3 | This example illustrates the different ways you can send keys/keystrokes with the HID keyboard protocol. 4 | This sketch will print the following on the recieving device: lesab (mirrored basel). 5 | 6 | This example is using the ESP8266 software serial 7 | */ 8 | 9 | 10 | #include 11 | #include 12 | 13 | #define RX_PIN D2 // connect to TXD of module 14 | #define TX_PIN D1 // connect to RXD of module (logic level 3.3v!) 15 | 16 | SoftwareSerial swSer(RX_PIN, TX_PIN, false, 128); 17 | 18 | BPLib *BPMod; 19 | void setup(){ 20 | Serial.begin(115200); 21 | swSer.begin(115200); 22 | 23 | BPMod = new BPLib(swSer); 24 | 25 | BPMod->begin(BP_MODE_HID,BP_HID_KEYBOARD); //Begin HID Mode with HID KEYBOARD AS TYPE 26 | 27 | } 28 | 29 | void loop(){ 30 | Serial.println("loop"); 31 | delay(5000); //Delay 5 seconds between each loop (Not nec.) 32 | BPMod->sendString("b"); //Send a string (will be recieved as keystroke) 33 | BPMod->keyboardPress(BP_KEY_LEFT_ARROW,BP_MOD_NOMOD); //Send Scan code (KEY, MODEFIER key) 34 | BPMod->keyboardReleaseAll(); //Release all keys 35 | BPMod->sendString("a"); 36 | BPMod->keyboardPress(BP_KEY_LEFT_ARROW,BP_MOD_NOMOD); 37 | BPMod->keyboardReleaseAll(); 38 | BPMod->sendString("s"); 39 | BPMod->keyboardPress(BP_KEY_LEFT_ARROW,BP_MOD_NOMOD); 40 | BPMod->keyboardReleaseAll(); 41 | BPMod->sendString("e"); 42 | BPMod->keyboardPress(BP_KEY_LEFT_ARROW,BP_MOD_NOMOD); 43 | BPMod->keyboardReleaseAll(); 44 | BPMod->sendString("l"); 45 | BPMod->keyboardPress(BP_KEY_LEFT_ARROW,BP_MOD_NOMOD); 46 | BPMod->keyboardReleaseAll(); 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BPLib - Bluetooth SPP/HID Library 2 | ===== 3 | 4 | This library simplifies using the RN-42 Bluetooth Module ([or the $3 HC-05/6 flashed with RN-42 firmware](https://youtu.be/y8PcNbAA6AQ)). 5 | 6 | The main advantage of the RN-42 module is it allows you to emulate HID devices, which allows your Arduino project to connect to your devices as a Bluetooth keyboard, mouse or gamepad. 7 | 8 | This library is forked from this one [here](https://github.com/baselsw/BPLib), the main differences between this one and the original is that hardcoded references to a serial interface have been removed so it can be used with basically any Arduino device. 9 | 10 | ## Usage 11 | 12 | Basic usage of the library is as shown, this sketch when connected to your device will send the key commands "H" and "i" every 5 seconds. 13 | 14 | ``` 15 | #include 16 | BPLib *BPMod; 17 | 18 | void setup(){ 19 | Serial.begin(115200); // 115200 is the default baud of the RN-42 20 | BPMod = new BPLib(Serial); 21 | BPMod->begin(BP_MODE_HID,BP_HID_KEYBOARD); //Begin HID Mode with HID KEYBOARD AS TYPE 22 | 23 | } 24 | 25 | void loop(){ 26 | delay(5000); 27 | BPMod->sendString("Hi"); 28 | } 29 | ``` 30 | 31 | The library also supports connecting using software serial interfaces. 32 | 33 | ``` 34 | #include 35 | SoftwareSerial swSer(1, 2, false, 128); 36 | 37 | .... 38 | 39 | void setup(){ 40 | swSer.begin(115200); 41 | BPMod = new BPLib(swSer); 42 | BPMod->begin(BP_MODE_HID,BP_HID_KEYBOARD); 43 | } 44 | 45 | ... 46 | ``` 47 | 48 | It is also worth noting that the **BPMod->begin** method is a once off configuration, if you do it for your module once it does not need to be run again, even after reboots, so it does not need to be part of your final sketch. 49 | 50 | ## Examples 51 | 52 | - [Keyboard - Using software serial](https://github.com/witnessmenow/BPLib/blob/master/examples/HID_KEYBOARD/HID_KEYBOARD.ino) 53 | - [Mouse](https://github.com/witnessmenow/BPLib/blob/master/examples/HID_MOUSE/HID_MOUSE.ino) 54 | - [Mouse Wheel](https://github.com/witnessmenow/BPLib/blob/master/examples/HID_MOUSE_WHEEL/HID_MOUSE_WHEEL.ino) 55 | - [Basic serial connection](https://github.com/witnessmenow/BPLib/blob/master/examples/SPP_SEND_REC_MSG/SPP_SEND_REC_MSG.ino) 56 | 57 | 58 | ## Details 59 | 60 | Check out the [header file of the library](https://github.com/witnessmenow/BPLib/blob/master/src/BPLib.h) for a full list of functions and parameters available. If you have suggests on how to present this better please feel free to submit a PR! 61 | -------------------------------------------------------------------------------- /src/BPLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | BPLib.h - Library for communication with RN-42 HID Bluetooth module 3 | Created by Basel Al-Rudainy, 6 april 2013. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | */ 15 | #ifndef BPLib_h 16 | #define BPLib_h 17 | 18 | #include "Arduino.h" 19 | 20 | //Bluetooth Modes 21 | #define BP_MODE_COMMAND "$$$" 22 | #define BP_MODE_EXITCOMMAND "---\r\n" 23 | #define BP_MODE_SPP "S~,0\r\n" 24 | #define BP_MODE_HID "S~,6\r\n" 25 | #define BP_MODE_AUTOCONNECT "SM,6\r\n" 26 | #define BP_MODE_MANUCONNECT "SM,4\r\n" 27 | #define BP_MODE_STATUS "SO,/#\r\n" 28 | 29 | //Bluetooth status messages 30 | #define BP_STAT_CMD "CMD\r\n" 31 | #define BP_STAT_END "END\r\n" 32 | #define BP_STAT_ACK "AOK\r\n" 33 | #define BP_STAT_REBOOT "Reboot!\r\n" 34 | //Bluetooth GET commands 35 | #define BP_GET_HID "GH\n" 36 | 37 | //Bluetooth system commands 38 | #define BP_REBOOT "R,1\r\n" 39 | #define BP_RECONNECT "C\r\n" 40 | #define BP_CHANGE_NAME "SN," 41 | //Bluetooth protocol types 42 | #define BP_SPP_SPP "AW\r\n" 43 | #define BP_HID_KEYBOARD "SH,0200\r\n" 44 | #define BP_HID_MOUSE "SH,0220\r\n" 45 | #define BP_HID_GAMEPAD "SH,0210\r\n" 46 | #define BP_HID_JOYSTICK "SH,0240\r\n" 47 | #define BP_HID_COMBO "SH,0230\r\n" 48 | 49 | // KEYBOARD Scan Codes 50 | #define BP_KEY_HOME 0x4a // Keyboard Home 51 | #define BP_KEY_PAGEUP 0x4b // Keyboard Page Up 52 | #define BP_KEY_DELETE 0x4c // Keyboard Delete Forward 53 | #define BP_KEY_END 0x4d // Keyboard End 54 | #define BP_KEY_PAGEDOWN 0x4e // Keyboard Page Down 55 | #define BP_KEY_RIGHT_ARROW 0x4f //Right arrow 56 | #define BP_KEY_LEFT_ARROW 0x50 //Left arrow 57 | #define BP_KEY_DOWN_ARROW 0x51 //Down arrow 58 | #define BP_KEY_ENTER 0x28 //Enter 59 | #define BP_KEY_UP_ARROW 0x52 //Up arrow 60 | #define BP_KEY_ESCAPE 0x29 //Escape 61 | #define BP_KEY_BACKSPACE 0x2a //Backspace 62 | #define BP_KEY_TAB 0x2b //Tab 63 | #define BP_KEY_CAPSLOCK 0x39 //CapsLock 64 | #define BP_KEY_SCROLLLOCK 0x47 //ScrollLock 65 | #define BP_KEY_BREAK_PAUSE 0x48 //Break-pause 66 | #define BP_KEY_NUMLOCK 0x53 //NumLock 67 | #define BP_KEY_TOGGLE_IPHONE_VIRTUAL_KEYBOARD 0x65 //Toggle iPhone Virtual Keyboard 68 | #define BP_KEY_LEFT_CONTROL 0xE0 //Left Control 69 | #define BP_KEY_LEFT_SHIFT 0xE1 //Left Shift 70 | #define BP_KEY_LEFT_ALT 0xE2 //Left Alt 71 | #define BP_KEY_LEFT_GUI 0xE3 //Left GUI 72 | #define BP_KEY_RIGHT_CONTROL 0xE4 //Right Control 73 | #define BP_KEY_RIGHT_SHIFT 0xE5 //Right Shift 74 | #define BP_KEY_RIGHT_ALT 0xE6 //Right Alt 75 | #define BP_KEY_RIGHT_GUI 0xE7 //Right GUI 76 | #define BP_KEY_F1 0x3A //F1 77 | #define BP_KEY_F2 0x3B //F2 78 | #define BP_KEY_F3 0x3C //F3 79 | #define BP_KEY_F4 0x3D //F4 80 | #define BP_KEY_F5 0x3E //F5 81 | #define BP_KEY_F6 0x3F //F6 82 | #define BP_KEY_F7 0x40 //F7 83 | #define BP_KEY_F8 0x41 //F8 84 | #define BP_KEY_F9 0x42 //F9 85 | #define BP_KEY_F10 0x43 //F10 86 | #define BP_KEY_F11 0x44 //F11 87 | #define BP_KEY_F12 0x45 //F12 88 | 89 | 90 | //KEYBOARD MODEFIER CODES 91 | #define BP_MOD_RIGHT_GUI (1<<7) 92 | #define BP_MOD_RIGHT_ALT (1<<6) 93 | #define BP_MOD_RIGHT_SHIFT (1<<5) 94 | #define BP_MOD_RIGHT_CTRL (1<<4) 95 | #define BP_MOD_LEFT_GUI (1<<3) 96 | #define BP_MOD_LEFT_ALT (1<<2) 97 | #define BP_MOD_LEFT_SHIFT (1<<1) 98 | #define BP_MOD_LEFT_CTRL (1<<0) 99 | #define BP_MOD_NOMOD 0x00 100 | 101 | //Mouse Codes 102 | #define BP_MOUSE_BTN_LEFT (1<<0) 103 | #define BP_MOUSE_BTN_RIGHT (1<<1) 104 | #define BP_MOUSE_BTN_MIDDLE (1<<2) 105 | 106 | //GAMEPAD/JOYSTICK BUTTON CODES 107 | //ST == First button combination (BTN0 to BTN7) 108 | #define BP_GAMEJOY_ST_BTN0 (1<<0) 109 | #define BP_GAMEJOY_ST_BTN1 (1<<1) 110 | #define BP_GAMEJOY_ST_BTN2 (1<<2) 111 | #define BP_GAMEJOY_ST_BTN3 (1<<3) 112 | #define BP_GAMEJOY_ST_BTN4 (1<<4) 113 | #define BP_GAMEJOY_ST_BTN5 (1<<5) 114 | #define BP_GAMEJOY_ST_BTN6 (1<<6) 115 | #define BP_GAMEJOY_ST_BTN7 (1<<7) 116 | #define BP_GAMEJOY_ST_NOBTN 0x00 117 | 118 | //ND = Second button combination (BTN0 to BTN7) 119 | #define BP_GAMEJOY_ND_BTN0 (1<<0) 120 | #define BP_GAMEJOY_ND_BTN1 (1<<1) 121 | #define BP_GAMEJOY_ND_BTN2 (1<<2) 122 | #define BP_GAMEJOY_ND_BTN3 (1<<3) 123 | #define BP_GAMEJOY_ND_BTN4 (1<<4) 124 | #define BP_GAMEJOY_ND_BTN5 (1<<5) 125 | #define BP_GAMEJOY_ND_BTN6 (1<<6) 126 | #define BP_GAMEJOY_ND_BTN7 (1<<7) 127 | #define BP_GAMEJOY_ND_NOBTN 0x00 128 | 129 | 130 | class BPLib 131 | { 132 | public: 133 | BPLib(Stream &port, int pin = -1); 134 | byte begin(char BP_Mode[], char BP_Type[]); 135 | byte sendCmd(char BP_CMD[]); 136 | void sendByte(byte rawData); 137 | void sendChar(char rawData); 138 | void sendInt(int rawData); 139 | void sendFloat(float rawData); 140 | void sendLong(long rawData); 141 | void sendString(char rawData[]); 142 | byte readRaw(); 143 | int available(); 144 | //Keyboard 145 | void keyboardPrint(char BP_MSG[]); 146 | void keyboardPress(byte BP_KEY,byte BP_MOD); 147 | void keyboardReleaseAll(); 148 | // Mouse 149 | void mouseClick(byte BP_BUTTON); 150 | void mouseMove(signed int BP_X,signed int BP_Y); 151 | void mouseWheel(signed int BP_WHEEL); 152 | void mousePress(byte BP_BUTTON); 153 | void mouseReleaseAll(); 154 | // Consumer 155 | void volumeUp(); 156 | void volumeDown(); 157 | void muteAudio(); 158 | void playPause(); 159 | void nextTrack(); 160 | void prevTrack(); 161 | void stopAudio(); 162 | void fastForwardAudio(); 163 | void rewindAudio(); 164 | void keyRelease(); 165 | void sendConsumerCommand(byte lowByte, byte highByte); 166 | // GamePad 167 | void gameJoyPress(byte BP_ST_BTN, byte BP_ND_BTN); 168 | void gameJoyMove(signed int BP_X1,signed int BP_Y1,signed int BP_X2,signed int BP_Y2); 169 | void gameJoyReleaseAll(); 170 | int connected(); 171 | byte changeName(char BP_NAME[]); 172 | private: 173 | Stream *serialInterface; 174 | int statusPin; 175 | byte get(char BP_STAT[], byte strlen); 176 | }; 177 | #endif 178 | -------------------------------------------------------------------------------- /src/BPLib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | BPLib.cpp - Library for communication with RN-42 HID Bluetooth module 3 | Created by Basel Al-Rudainy, 6 april 2013. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | */ 15 | #include "Arduino.h" 16 | #include "BPLib.h" 17 | 18 | BPLib::BPLib(Stream &port, int pin) { 19 | this->serialInterface = &port; 20 | if (pin > -1) { 21 | statusPin = pin; 22 | } 23 | } 24 | 25 | byte BPLib::begin(char BP_Mode[], char BP_Type[]) 26 | { 27 | serialInterface->print(BP_MODE_COMMAND); 28 | if (get(BP_STAT_CMD, (byte)5) != 1) { 29 | return (byte)0; 30 | }//if 31 | 32 | serialInterface->print(BP_Mode); 33 | if (get(BP_STAT_ACK, (byte)5) != 1) { 34 | return (byte)0; 35 | }//if 36 | if (strcmp(BP_Type, BP_SPP_SPP) > 0) { 37 | serialInterface->print(BP_Type); 38 | if (get(BP_STAT_ACK, (byte)5) != 1) { 39 | return (byte)0; 40 | }//if 41 | } 42 | serialInterface->print(BP_REBOOT); 43 | if (get(BP_STAT_REBOOT, (byte)9) != 1) { 44 | return (byte)0; 45 | }//if 46 | delay(1000); //Delay (Bluetooth boot-up) 47 | 48 | return (byte)1; 49 | } 50 | 51 | byte BPLib::sendCmd(char BP_CMD[]) 52 | { 53 | serialInterface->print(BP_MODE_COMMAND); 54 | if (get(BP_STAT_CMD, (byte)5) != 1) { 55 | return (byte)0; 56 | }//if 57 | serialInterface->print(BP_CMD); 58 | if (get(BP_STAT_ACK, (byte)5) != 1) { 59 | return (byte)0; 60 | }//if 61 | serialInterface->print(BP_MODE_EXITCOMMAND); 62 | if (get(BP_STAT_END, (byte)5) != 1) { 63 | return (byte)0; 64 | }//if 65 | return (byte)1; 66 | } 67 | 68 | byte BPLib::readRaw() { 69 | return serialInterface->read(); 70 | } 71 | int BPLib::available() { 72 | return serialInterface->available(); 73 | } 74 | 75 | byte BPLib::get(char BP_STAT[], byte strlen) 76 | { 77 | char buffer[strlen + 1]; 78 | while (serialInterface->available() <= (strlen - 1)) {}; 79 | int count = 0; 80 | while (serialInterface->available() > 0) { 81 | buffer[count] = (char)serialInterface->read(); 82 | count++; 83 | }//while 84 | buffer[strlen] = 0; 85 | //serialInterface->print(buffer);//DEBUG 86 | if (strcmp(buffer, BP_STAT) == 0) { 87 | return (byte)1; 88 | }//if 89 | else { 90 | return (byte)0; 91 | }//else 92 | }//get 93 | 94 | 95 | void BPLib::keyboardPress(byte BP_KEY, byte BP_MOD) { 96 | serialInterface->write((byte)0xFD); //Start HID Report 97 | serialInterface->write((byte)0x9); //Length byte 98 | serialInterface->write((byte)0x1); //Descriptor byte 99 | serialInterface->write(BP_MOD); //Modifier byte 100 | serialInterface->write((byte)0x00); //- 101 | serialInterface->write(BP_KEY); //Send KEY 102 | for (byte i = 0; i < 5; i++) { //Send five zero bytes 103 | serialInterface->write((byte)0x00); 104 | } 105 | 106 | } 107 | 108 | void BPLib::keyboardReleaseAll() { 109 | keyboardPress((byte)0x00, BP_MOD_NOMOD); 110 | } 111 | 112 | void BPLib::mouseClick(byte BP_BUTTON) { 113 | mousePress(BP_BUTTON); 114 | mouseReleaseAll(); 115 | } 116 | void BPLib::mouseMove(signed int BP_X, signed int BP_Y) { 117 | serialInterface->write((byte)0xFD); //Start HID Report 118 | serialInterface->write((byte)0x5); //Length byte 119 | serialInterface->write((byte)0x2); //Descriptor byte 120 | serialInterface->write((byte)0x00); //Button byte 121 | serialInterface->write(BP_X); //(-127 to 127) 122 | serialInterface->write(BP_Y); //(-127 to 127) 123 | serialInterface->write((byte)0x00); 124 | 125 | } 126 | void BPLib::mousePress(byte BP_BUTTON) { 127 | serialInterface->write((byte)0xFD); //Start HID Report 128 | serialInterface->write((byte)0x5); //Length byte 129 | serialInterface->write((byte)0x2); //Descriptor byte 130 | serialInterface->write(BP_BUTTON); //Button byte 131 | for (byte i = 0; i < 3; i++) { //Send three zero bytes 132 | serialInterface->write((byte)0x00); 133 | } 134 | } 135 | 136 | void BPLib::mouseReleaseAll() { 137 | serialInterface->write((byte)0xFD); //Start HID Report 138 | serialInterface->write((byte)0x5); //Length byte 139 | serialInterface->write((byte)0x2); //Descriptor byte 140 | for (byte i = 0; i < 4; i++) { //Send four zero bytes 141 | serialInterface->write((byte)0x00); 142 | } 143 | } 144 | 145 | void BPLib::mouseWheel(signed int BP_WHEEL) { 146 | serialInterface->write((byte)0xFD); //Start HID Report 147 | serialInterface->write((byte)0x5); //Length byte 148 | serialInterface->write((byte)0x2); //Descriptor byte 149 | for (byte i = 0; i < 3; i++) { //Send three zero bytes 150 | serialInterface->write((byte)0x00); 151 | } 152 | serialInterface->write(BP_WHEEL); //Wheel byte (-127 to 127) 153 | } 154 | 155 | void BPLib::volumeUp() { 156 | sendConsumerCommand((byte)0x10, (byte)0x00); 157 | } 158 | 159 | void BPLib::volumeDown() { 160 | sendConsumerCommand((byte)0x20, (byte)0x00); 161 | } 162 | 163 | void BPLib::muteAudio() { 164 | sendConsumerCommand((byte)0x40, (byte)0x00); 165 | } 166 | 167 | void BPLib::playPause() { 168 | sendConsumerCommand((byte)0x80, (byte)0x00); 169 | } 170 | 171 | void BPLib::nextTrack() { 172 | sendConsumerCommand((byte)0x00, (byte)0x01); 173 | } 174 | 175 | void BPLib::prevTrack() { 176 | sendConsumerCommand((byte)0x00, (byte)0x02); 177 | } 178 | 179 | void BPLib::stopAudio() { 180 | sendConsumerCommand((byte)0x00, (byte)0x04); 181 | } 182 | 183 | void BPLib::fastForwardAudio() { 184 | sendConsumerCommand((byte)0x00, (byte)0x10); 185 | } 186 | 187 | void BPLib::rewindAudio() { 188 | sendConsumerCommand((byte)0x00, (byte)0x20); 189 | } 190 | 191 | void BPLib::keyRelease() { 192 | sendConsumerCommand((byte)0x00, (byte)0x00); 193 | } 194 | 195 | void BPLib::sendConsumerCommand(byte lowByte, byte highByte) { 196 | serialInterface->write((byte)0xFD); //Start HID Report 197 | serialInterface->write((byte)0x3); //Length byte 198 | serialInterface->write((byte)0x3); //Descriptor byte 199 | serialInterface->write(lowByte); //Key Release LowByte 200 | serialInterface->write(highByte); //Key Release HighByte 201 | } 202 | 203 | byte BPLib::changeName(char BP_NAME[]) { 204 | serialInterface->print(BP_MODE_COMMAND); 205 | if (get(BP_STAT_CMD, (byte)5) != 1) { 206 | return (byte)0; 207 | }//if 208 | serialInterface->print(BP_CHANGE_NAME); 209 | serialInterface->print(BP_NAME); 210 | serialInterface->print(F("\r\n")); 211 | if (get(BP_STAT_ACK, (byte)5) != 1) { 212 | return (byte)0; 213 | }//if 214 | serialInterface->print(BP_MODE_EXITCOMMAND); 215 | if (get(BP_STAT_END, (byte)5) != 1) { 216 | return (byte)0; 217 | }//if 218 | return (byte)1; 219 | 220 | } 221 | 222 | 223 | void BPLib::sendByte(byte rawData) { 224 | serialInterface->print(rawData); 225 | } 226 | void BPLib::sendChar(char rawData) { 227 | serialInterface->print(rawData); 228 | } 229 | void BPLib::sendInt(int rawData) { 230 | serialInterface->print(rawData); 231 | } 232 | void BPLib::sendFloat(float rawData) { 233 | serialInterface->print(rawData); 234 | } 235 | void BPLib::sendLong(long rawData) { 236 | serialInterface->print(rawData); 237 | } 238 | void BPLib::sendString(char rawData[]) { 239 | serialInterface->print(rawData); 240 | } 241 | 242 | void BPLib::gameJoyPress(byte BP_ST_BTN, byte BP_ND_BTN) { 243 | serialInterface->write((byte)0xFD); //Start HID Report 244 | serialInterface->write((byte)0x6); //Length byte 245 | serialInterface->write((byte)BP_ST_BTN); //First Button byte 246 | serialInterface->write((byte)BP_ND_BTN); //Second Button byte 247 | for (byte i = 0; i < 4; i++) { //Send four zero bytes 248 | serialInterface->write((byte)0x00); 249 | } 250 | } 251 | void BPLib::gameJoyMove(signed int BP_X1, signed int BP_Y1, signed int BP_X2, signed int BP_Y2) { 252 | serialInterface->write((byte)0xFD); //Start HID Report 253 | serialInterface->write((byte)0x6); //Length byte 254 | serialInterface->write((byte)BP_GAMEJOY_ST_NOBTN); //First Button byte 255 | serialInterface->write((byte)BP_GAMEJOY_ND_NOBTN); //Second Button byte 256 | serialInterface->write(BP_X1 & 0xFF); //First X coordinate 257 | serialInterface->write(BP_Y1 & 0xFF); //First Y coordinate 258 | serialInterface->write(BP_X2 & 0xFF); //Second X coordinate 259 | serialInterface->write(BP_Y2 & 0xFF); //Second Y coordinate 260 | } 261 | void BPLib::gameJoyReleaseAll() { 262 | gameJoyPress(BP_GAMEJOY_ST_NOBTN, BP_GAMEJOY_ND_NOBTN); 263 | } 264 | 265 | int BPLib::connected() { 266 | if (statusPin > -1) { 267 | return digitalRead(statusPin); 268 | } else { 269 | return -1; 270 | } 271 | } 272 | --------------------------------------------------------------------------------