Added ability to print negative numbers

This commit is contained in:
Kristian Sloth Lauszus 2012-10-23 22:49:19 +02:00
parent 4e10fd3046
commit 8f7d93896e
2 changed files with 39 additions and 12 deletions

47
SPP.cpp
View file

@ -783,7 +783,12 @@ void SPP::println(void) {
} }
/* These must be used to print numbers */ /* These must be used to print numbers */
void SPP::printNumber(uint16_t n) { void SPP::printNumber(int16_t n) {
bool negative = false;
if(n < 0) {
negative = true;
n = -n;
}
uint16_t temp = n; uint16_t temp = n;
uint8_t digits = 0; uint8_t digits = 0;
while (temp) { while (temp) {
@ -793,15 +798,27 @@ void SPP::printNumber(uint16_t n) {
if(digits == 0) if(digits == 0)
print("0"); print("0");
else { else {
uint8_t buf[digits]; uint8_t buf[digits+1]; // Add one extra in case it is negative
for(uint8_t i = 1; i < digits+1; i++) { for(uint8_t i = 1; i < digits+1; i++) {
buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character if(negative)
buf[digits-i+1] = n%10 + '0'; // Get number and convert to ASCII Character
else
buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character
n /= 10; n /= 10;
} }
print(buf,digits); if(negative) {
buf[0] = '-';
print(buf,digits+1);
} else
print(buf,digits);
} }
} }
void SPP::printNumberln(uint16_t n) { void SPP::printNumberln(int16_t n) {
bool negative = false;
if(n < 0) {
negative = true;
n = -n;
}
uint16_t temp = n; uint16_t temp = n;
uint8_t digits = 0; uint8_t digits = 0;
while (temp) { while (temp) {
@ -811,14 +828,24 @@ void SPP::printNumberln(uint16_t n) {
if(digits == 0) if(digits == 0)
print("0\r\n"); print("0\r\n");
else { else {
uint8_t buf[digits+2]; uint8_t buf[digits+3]; // Add one extra in case it is negative
for(uint8_t i = 1; i < digits+1; i++) { for(uint8_t i = 1; i < digits+1; i++) {
buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character if(negative)
buf[digits-i+1] = n%10 + '0'; // Get number and convert to ASCII Character
else
buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character
n /= 10; n /= 10;
}
if(negative) {
buf[0] = '-';
buf[digits+1] = '\r';
buf[digits+2] = '\n';
print(buf,digits+3);
} else {
buf[digits] = '\r';
buf[digits+1] = '\n';
print(buf,digits+2);
} }
buf[digits] = '\r';
buf[digits+1] = '\n';
print(buf,digits+2);
} }
} }

4
SPP.h
View file

@ -115,8 +115,8 @@ 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(uint16_t n); // These must be used to print numbers void printNumber(int16_t n); // These must be used to print numbers
void printNumberln(uint16_t n); // This will include newline and carriage return void printNumberln(int16_t n); // This will include newline and carriage return
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