Driver for the JY-MCU v1.06 HC-06 Bluetooth module.

Dependents:   DISCO-F746NG_rtos_test MbedTableControl

HC06Bluetooth.cpp

Committer:
Electrotiger
Date:
2016-06-16
Revision:
8:14bf9b541f9a
Parent:
6:5ba0038a7a9a
Child:
9:3e23f3f615f2

File content as of revision 8:14bf9b541f9a:

/*
 * HC06Bluetooth.cpp
 *
 *  Created on: Jun 4, 2016
 *      Author: Developer
 */

#include <HC06Bluetooth.hpp>
#include <algorithm>



HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString), void (*charCallbackFunc) (char readChar))
: btSerialObj(TX, RX), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc) {
    btSerialObj.baud(115200);

    // Set the interrupt to be called when a byte is received.
    if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL)) {
        btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
    }
}

void HC06Bluetooth::runSetup(std::string deviceName, std::string PIN) {
    // TODO: Sweep through a list of Baud rates until we find one that works!
    int numCharsReceived = 0;
    // The default baud rate is 9600. Overwrite it to 115200.
    btSerialObj.puts("AT+BAUD8");
    btSerialObj.baud(115200);
    // Wait for the 8 character reply "OK115200"
    for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) {
        while(!btSerialObj.readable());
        btSerialObj.getc();
    }

    // Set the name of the device.
    btSerialObj.puts(("AT+NAME" + deviceName.substr(0,20)).c_str());
    // Wait for the 6 character reply "OKname"
    for(numCharsReceived = 0 ; numCharsReceived < 6; numCharsReceived++) {
        while(!btSerialObj.readable());
        btSerialObj.getc();
    }

    //Set the password of the device.
    btSerialObj.puts(("AT+PIN" + PIN.substr(0, 4)).c_str());
    // Wait for the 8 character reply "OKsetpin"
    for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) {
        while(!btSerialObj.readable());
        btSerialObj.getc();
    }
}

HC06Bluetooth::~HC06Bluetooth() {
    // TODO Auto-generated destructor stub
}

void HC06Bluetooth::print(const char* buffer) {
    btSerialObj.puts(buffer);
}

void HC06Bluetooth::print(char c) {
    btSerialObj.putc(c);
}

void HC06Bluetooth::receiveByteISR() {
    while(btSerialObj.readable()) {
        // Get the character from the input.
        char receivedChar = btSerialObj.getc();

        // Call the character callback function if it is not null.
        if (charCallbackFunc != NULL) {
            charCallbackFunc(receivedChar);
        }

        // If the character is a newline or carriage return, then call the line callback function.
        if ((receivedChar == '\n') || (receivedChar == '\r')) {
            // Terminate the buffer with a null character, since that is what strings end with.
            receivedChar = '\0';
            dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
            // Copy data from the buffer to a copy.
            std::copy(dataReceivedBuffer, dataReceivedBuffer + dataReceivedBufferPos, dataReceivedBufferCopy);
            // Reset the buffer position.
            dataReceivedBufferPos = 0;
            // Call the callback function.
            if (lineCallbackFunc != NULL) {
                lineCallbackFunc((const char*)dataReceivedBuffer);
            }
        }
        // Otherwise, just place it in the buffer and move on.
        else {
            dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
            dataReceivedBufferPos++;
        }
    }
}