Fixed doubleToString function

This commit is contained in:
Kristian Sloth Lauszus 2013-02-02 00:42:19 +01:00
parent 9365f034b9
commit f8beb5cfa7
2 changed files with 23 additions and 15 deletions

32
SPP.cpp
View file

@ -680,6 +680,8 @@ uint8_t SPP::calcFcs(uint8_t *data) {
/* Serial commands */ /* Serial commands */
void SPP::print(const String &str) { void SPP::print(const String &str) {
if(!connected)
return;
uint8_t length = str.length(); uint8_t length = str.length();
if(length > (sizeof(l2capoutbuf)-4)) if(length > (sizeof(l2capoutbuf)-4))
length = sizeof(l2capoutbuf)-4; length = sizeof(l2capoutbuf)-4;
@ -694,6 +696,8 @@ void SPP::print(const String &str) {
RFCOMM_Command(l2capoutbuf,length+4); RFCOMM_Command(l2capoutbuf,length+4);
} }
void SPP::print(const char* data) { void SPP::print(const char* data) {
if(!connected)
return;
uint8_t length = strlen(data); uint8_t length = strlen(data);
if(length > (sizeof(l2capoutbuf)-4)) if(length > (sizeof(l2capoutbuf)-4))
length = sizeof(l2capoutbuf)-4; length = sizeof(l2capoutbuf)-4;
@ -711,6 +715,8 @@ void SPP::print(uint8_t data) {
print(&data,1); print(&data,1);
} }
void SPP::print(uint8_t* array, uint8_t length) { void SPP::print(uint8_t* array, uint8_t length) {
if(!connected)
return;
if(length > (sizeof(l2capoutbuf)-4)) if(length > (sizeof(l2capoutbuf)-4))
length = sizeof(l2capoutbuf)-4; length = sizeof(l2capoutbuf)-4;
l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress;; // RFCOMM Address l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress;; // RFCOMM Address
@ -784,13 +790,13 @@ void SPP::println(void) {
} }
/* These must be used to print numbers */ /* These must be used to print numbers */
void SPP::printNumber(int16_t n) { void SPP::printNumber(int32_t n) {
bool negative = false; bool negative = false;
if(n < 0) { if(n < 0) {
negative = true; negative = true;
n = -n; n = -n;
} }
uint16_t temp = n; uint32_t temp = n;
uint8_t digits = 0; uint8_t digits = 0;
while (temp) { while (temp) {
temp /= 10; temp /= 10;
@ -814,13 +820,13 @@ void SPP::printNumber(int16_t n) {
print(buf,digits); print(buf,digits);
} }
} }
void SPP::printNumberln(int16_t n) { void SPP::printNumberln(int32_t n) {
bool negative = false; bool negative = false;
if(n < 0) { if(n < 0) {
negative = true; negative = true;
n = -n; n = -n;
} }
uint16_t temp = n; uint32_t temp = n;
uint8_t digits = 0; uint8_t digits = 0;
while (temp) { while (temp) {
temp /= 10; temp /= 10;
@ -850,16 +856,19 @@ void SPP::printNumberln(int16_t n) {
} }
} }
void SPP::printNumber(double n, uint8_t digits) { void SPP::printNumber(double n, uint8_t digits) {
print(doubleToString(n,digits)); char output[10];
doubleToString(n,output,digits);
print(output);
} }
void SPP::printNumberln(double n, uint8_t digits) { void SPP::printNumberln(double n, uint8_t digits) {
char buf[12]; char buf[10];
strcpy(buf,doubleToString(n,digits)); char output[12];
strcat(buf,"\r\n"); doubleToString(n,buf,digits);
print(buf); strcpy(output,buf);
strcat(output,"\r\n");
print(output);
} }
const char* SPP::doubleToString(double input, uint8_t digits) { void SPP::doubleToString(double input, char* output, uint8_t digits) {
char output[10];
char buffer[10]; char buffer[10];
if(input < 0) { if(input < 0) {
strcpy(output,"-"); strcpy(output,"-");
@ -887,7 +896,6 @@ const char* SPP::doubleToString(double input, uint8_t digits) {
} }
itoa((unsigned long)fractpart,buffer,10); // Convert to string itoa((unsigned long)fractpart,buffer,10); // Convert to string
strcat(output,buffer); strcat(output,buffer);
return output;
} }
uint8_t SPP::read() { uint8_t SPP::read() {

6
SPP.h
View file

@ -115,12 +115,12 @@ public:
void println(const __FlashStringHelper *); // Include newline and carriage return void println(const __FlashStringHelper *); // Include newline and carriage return
void println(void); // Use this to print newline and carriage return void println(void); // Use this to print newline and carriage return
void printNumber(int16_t n); // These must be used to print numbers void printNumber(int32_t n); // These must be used to print numbers
void printNumberln(int16_t n); // This will include newline and carriage return void printNumberln(int32_t n); // This will include newline and carriage return
void printNumber(double n, uint8_t digits = 2); // These must be used to print floating-point numbers void printNumber(double n, uint8_t digits = 2); // These must be used to print floating-point numbers
void printNumberln(double n, uint8_t digits = 2); // This will include newline and carriage return void printNumberln(double n, uint8_t digits = 2); // This will include newline and carriage return
const char* doubleToString(double input, uint8_t digits); // Helper function to convert from double to string void doubleToString(double input, char* output, uint8_t digits = 2); // Helper function to convert from double to string
uint8_t available() { return rfcommAvailable; }; // Get the bytes waiting to be read uint8_t available() { return rfcommAvailable; }; // Get the bytes waiting to be read
uint8_t read(); // Used to read the buffer uint8_t read(); // Used to read the buffer