This commit is contained in:
Oleg Mazurov 2013-04-03 13:20:50 -06:00
commit 2d3b73f9a7
3 changed files with 72 additions and 32 deletions

90
SPP.cpp
View file

@ -729,51 +729,87 @@ uint8_t SPP::calcFcs(uint8_t *data) {
void SPP::print(const String &str) { void SPP::print(const String &str) {
if (!connected) if (!connected)
return; return;
uint8_t length = str.length(); int16_t stringLength = str.length(); // This will be used to store the characters that still needs to be sent
if (length > (sizeof (l2capoutbuf) - 4)) uint8_t length; // This is the length of the string we are sending
length = sizeof (l2capoutbuf) - 4; uint8_t offset = 0; // This is used to keep track of where we are in the string
l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address
l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = str[i];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf);
RFCOMM_Command(l2capoutbuf, length + 4); do {
if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer
length = sizeof (l2capoutbuf) - 4;
else
length = stringLength;
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = str[i + offset];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum
RFCOMM_Command(l2capoutbuf, length + 4);
stringLength -= length;
offset += length; // Increment the offset
} while (stringLength); // We will run this loop until this variable is less than 0
} }
void SPP::print(const char* str) { void SPP::print(const char* str) {
if (!connected) if (!connected)
return; return;
uint8_t length = strlen(str); int16_t stringLength = strlen(str); // This will be used to store the characters that still needs to be sent
if (length > (sizeof (l2capoutbuf) - 4)) uint8_t length; // This is the length of the string we are sending
length = sizeof (l2capoutbuf) - 4; uint8_t offset = 0; // This is used to keep track of where we are in the string
l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address
l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = str[i];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf);
RFCOMM_Command(l2capoutbuf, length + 4); do {
if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer
length = sizeof (l2capoutbuf) - 4;
else
length = stringLength;
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = str[i + offset];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum
RFCOMM_Command(l2capoutbuf, length + 4);
stringLength -= length;
offset += length; // Increment the offset
} while (stringLength); // We will run this loop until this variable is less than 0
} }
void SPP::print(uint8_t* array, uint8_t length) { void SPP::print(uint8_t* array, int16_t stringLength) {
if (!connected) if (!connected)
return; return;
if (length > (sizeof (l2capoutbuf) - 4)) uint8_t length; // This is the length of the string we are sending
length = sizeof (l2capoutbuf) - 4; uint8_t offset = 0; // This is used to keep track of where we are in the string
l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address
l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = array[i];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf);
RFCOMM_Command(l2capoutbuf, length + 4); do {
if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer
length = sizeof (l2capoutbuf) - 4;
else
length = stringLength;
l2capoutbuf[2] = length << 1 | 1; // Length
uint8_t i = 0;
for (; i < length; i++)
l2capoutbuf[i + 3] = array[i + offset];
l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum
RFCOMM_Command(l2capoutbuf, length + 4);
stringLength -= length;
offset += length; // Increment the offset
} while (stringLength); // We will run this loop until this variable is less than 0
} }
void SPP::println(const String &str) { void SPP::println(const String &str) {

2
SPP.h
View file

@ -158,7 +158,7 @@ public:
* @param array Array to send. * @param array Array to send.
* @param length Number of bytes to send. * @param length Number of bytes to send.
*/ */
void print(uint8_t* array, uint8_t length); void print(uint8_t* array, int16_t length);
/** /**
* Same as print(uint8_t* array, uint8_t length), but will include newline and carriage return. * Same as print(uint8_t* array, uint8_t length), but will include newline and carriage return.
* @param array Array to send. * @param array Array to send.

View file

@ -21,7 +21,7 @@ PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch //PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
boolean firstMessage = true; boolean firstMessage = true;
String output; // We will store the data in these string so we doesn't overflow the dongle String output = ""; // We will store the data in this string so we doesn't overflow the dongle
void setup() { void setup() {
Serial.begin(115200); // This wil lprint the debugging from the libraries Serial.begin(115200); // This wil lprint the debugging from the libraries
@ -30,6 +30,7 @@ void setup() {
while(1); //halt while(1); //halt
} }
Serial.print(F("\r\nBluetooth Library Started")); Serial.print(F("\r\nBluetooth Library Started"));
output.reserve(200); // Reserve 200 bytes for the output string
} }
void loop() { void loop() {
Usb.Task(); Usb.Task();
@ -130,8 +131,10 @@ void loop() {
if(PS3.getButtonClick(R3)) if(PS3.getButtonClick(R3))
output += " - R3"; output += " - R3";
if(PS3.getButtonClick(SELECT)) if(PS3.getButtonClick(SELECT)) {
output += " - Select"; output += " - Select - ";
output += PS3.getStatusString();
}
if(PS3.getButtonClick(START)) if(PS3.getButtonClick(START))
output += " - Start"; output += " - Start";
@ -142,5 +145,6 @@ void loop() {
SerialBT.println(string); SerialBT.println(string);
} }
} }
delay(10);
} }
} }