Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://ww1.microchip.com/downloads/en/DeviceDoc/bluetooth_cr_UG-v1.0r.pdf RN-42 reference
- // http://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/RN-HID-User-Guide-v1.0r.pdf keycodes
- // pushbutton attached to GND/D2
- // HC-06 pins are connected as RXD to TX0, TXD to RX1
- #define BT Serial1
- void keyCommand(uint8_t modifiers, uint8_t key1, uint8_t key2 = 0, uint8_t key3 = 0, uint8_t key4 = 0, uint8_t key5 = 0, uint8_t key6 = 0) {
- BT.write(0xFD); // our command
- BT.write(0x09); // size
- BT.write(0x01); // descriptor
- BT.write(modifiers); // modifier
- BT.write(0x00); // 0x00
- BT.write(key1); // key code 1
- BT.write(key2); // key code 2
- BT.write(key3); // key code 3
- BT.write(key4); // key code 4
- BT.write(key5); // key code 5
- BT.write(key6); // key code 6
- }
- void consumerReport(uint16_t code) {
- BT.write(0xFD);
- BT.write(0x03);
- BT.write(0x03);
- BT.write((uint8_t)(code&0xff));
- BT.write((uint8_t)(code>>8));
- }
- void cmd(const char * s, int timeout=100, bool bSendCR=true) {
- Serial.write("> ");
- Serial.write(s);
- Serial.write("\r\n");
- BT.write(s);
- if (bSendCR) {
- BT.write("\r");
- }
- // rn42 output is tricky, let's use read timeout for now
- long t = millis();
- while(millis() < t + timeout) {
- while (BT.available()) {
- Serial.write(BT.read());
- }
- delay(1);
- }
- }
- void rn42_command_mode() {
- // If the device sees any bytes before or after the $$$ characters in a 1 second window,
- // the device does not enter command mode and these bytes are passed through
- delay(1000);
- cmd("$$$", 100, false);
- }
- void rn42_init() {
- rn42_command_mode();
- //cmd("SF,1", 1000); // factory defaults (resets pairing info)
- cmd("SS,Keyboard/Mouse"); // service name (1-20 chars)
- cmd("SW,8000"); // Sniff disable
- cmd("SH,003C"); // combo device, out-report, 4-reconnect
- cmd("SY,FFF4"); // transmit power -12
- cmd("S~,6"); // Set HID device
- cmd("SM,5"); // Mode = 5, (any) Auto reconnect (can't use mode 4 because GPIO6 can be unavailable)
- cmd("SC,0000"); // Service COD = 00002540 (Periheral, Keyboard) - Service class (upper 2 bytes) = 0000
- cmd("SD,2540"); // Device Class (2 lower bytes) = 2540
- cmd("SA,2"); // Authentication Mode 2 (SSP, "just works" mode)
- cmd("SN,RN-42"); // Set BT Name (may be non-unique, use "S-,Name" to append MAC-address)
- cmd("R,1"); // Reboot!
- }
- void rn42_connect() {
- rn42_command_mode();
- cmd("V"); // show version
- //cmd("X"); Serial.println(); // show settings + line break
- cmd("CFI"); // connect
- cmd("---"); // exit command mode
- }
- void setup() {
- Serial.begin(9600); // This pipes to the serial monitor
- BT.begin(115200); // This is the UART, pipes to sensors attached to board (9600 won't work for RN42)
- pinMode(2, INPUT); // input mode for the button pin (D2)
- digitalWrite(2, HIGH); // set button pin to HIGH (software pullup) so it triggers when pulled to GND
- BT.write(0); // disconnect if connected to the host (HID report 0), in case if GPIO6 is unavaliable
- rn42_init(); // can be skipped to speed up connection
- rn42_connect();
- // tweak sound volume slider up and down to indicate connection
- delay(2500); consumerReport(0x10); delay(25); consumerReport(0); delay(25); consumerReport(0x20); delay(25); consumerReport(0);
- }
- bool wasPressed = false;
- void loop() {
- bool pressed = digitalRead(2)==LOW;
- if (pressed != wasPressed) {
- keyCommand(pressed ? 0x80 : 0, 0); // winkey
- wasPressed = pressed;
- delay(25);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement