mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Added more printNumber functions
This commit is contained in:
parent
1fbb7390a0
commit
09c5b6c5a3
2 changed files with 111 additions and 66 deletions
108
SPP.cpp
108
SPP.cpp
|
@ -795,86 +795,72 @@ void SPP::println(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* These must be used to print numbers */
|
/* These must be used to print numbers */
|
||||||
|
void SPP::printNumber(uint32_t n) {
|
||||||
|
char output[11];
|
||||||
|
intToString(n,output);
|
||||||
|
print(output);
|
||||||
|
}
|
||||||
|
void SPP::printNumberln(uint32_t n) {
|
||||||
|
char output[13];
|
||||||
|
intToString(n,output);
|
||||||
|
strcat(output,"\r\n");
|
||||||
|
print(output);
|
||||||
|
}
|
||||||
|
|
||||||
void SPP::printNumber(int32_t n) {
|
void SPP::printNumber(int32_t n) {
|
||||||
bool negative = false;
|
char output[12];
|
||||||
if(n < 0) {
|
if(n < 0) {
|
||||||
negative = true;
|
char buf[11];
|
||||||
n = -n;
|
intToString(n*-1,buf);
|
||||||
}
|
strcpy(output,"-");
|
||||||
uint32_t temp = n;
|
strcat(output,buf);
|
||||||
uint8_t digits = 0;
|
} else
|
||||||
while (temp) {
|
intToString(n,output);
|
||||||
temp /= 10;
|
print(output);
|
||||||
digits++;
|
|
||||||
}
|
|
||||||
if(digits == 0)
|
|
||||||
print("0");
|
|
||||||
else {
|
|
||||||
uint8_t buf[digits+1]; // Add one extra in case it is negative
|
|
||||||
for(uint8_t i = 1; i < digits+1; i++) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
if(negative) {
|
|
||||||
buf[0] = '-';
|
|
||||||
print(buf,digits+1);
|
|
||||||
} else
|
|
||||||
print(buf,digits);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void SPP::printNumberln(int32_t n) {
|
void SPP::printNumberln(int32_t n) {
|
||||||
bool negative = false;
|
char output[14];
|
||||||
if(n < 0) {
|
if(n < 0) {
|
||||||
negative = true;
|
char buf[11];
|
||||||
n = -n;
|
intToString(n*-1,buf);
|
||||||
}
|
strcpy(output,"-");
|
||||||
uint32_t temp = n;
|
strcat(output,buf);
|
||||||
|
} else
|
||||||
|
intToString(n,output);
|
||||||
|
strcat(output,"\r\n");
|
||||||
|
print(output);
|
||||||
|
}
|
||||||
|
void SPP::intToString(uint32_t input, char* output) {
|
||||||
|
uint32_t temp = input;
|
||||||
uint8_t digits = 0;
|
uint8_t digits = 0;
|
||||||
while (temp) {
|
while(temp) {
|
||||||
temp /= 10;
|
temp /= 10;
|
||||||
digits++;
|
digits++;
|
||||||
}
|
}
|
||||||
if(digits == 0)
|
if(digits == 0)
|
||||||
print("0\r\n");
|
strcpy(output,"0");
|
||||||
else {
|
else {
|
||||||
uint8_t buf[digits+3]; // Add one extra in case it is negative
|
for(uint8_t i = 1; i <= digits; i++) {
|
||||||
for(uint8_t i = 1; i < digits+1; i++) {
|
output[digits-i] = input%10 + '0'; // Get number and convert to ASCII Character
|
||||||
if(negative)
|
input /= 10;
|
||||||
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;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
output[digits] = '\0'; // Add null character
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPP::printNumber(double n, uint8_t digits) {
|
void SPP::printNumber(double n, uint8_t digits) {
|
||||||
char output[10];
|
char output[13+digits];
|
||||||
doubleToString(n,output,digits);
|
doubleToString(n,output,digits);
|
||||||
print(output);
|
print(output);
|
||||||
}
|
}
|
||||||
void SPP::printNumberln(double n, uint8_t digits) {
|
void SPP::printNumberln(double n, uint8_t digits) {
|
||||||
char buf[10];
|
char output[15+digits];
|
||||||
char output[12];
|
doubleToString(n,output,digits);
|
||||||
doubleToString(n,buf,digits);
|
|
||||||
strcpy(output,buf);
|
|
||||||
strcat(output,"\r\n");
|
strcat(output,"\r\n");
|
||||||
print(output);
|
print(output);
|
||||||
}
|
}
|
||||||
void SPP::doubleToString(double input, char* output, uint8_t digits) {
|
void SPP::doubleToString(double input, char* output, uint8_t digits) {
|
||||||
char buffer[10];
|
char buffer[13+digits];
|
||||||
if(input < 0) {
|
if(input < 0) {
|
||||||
strcpy(output,"-");
|
strcpy(output,"-");
|
||||||
input = -input;
|
input = -input;
|
||||||
|
@ -888,18 +874,18 @@ void SPP::doubleToString(double input, char* output, uint8_t digits) {
|
||||||
rounding /= 10.0;
|
rounding /= 10.0;
|
||||||
input += rounding;
|
input += rounding;
|
||||||
|
|
||||||
unsigned long intpart = (unsigned long)input;
|
uint32_t intpart = (uint32_t)input;
|
||||||
itoa(intpart,buffer,10); // Convert to string
|
intToString(intpart,buffer); // Convert to string
|
||||||
strcat(output,buffer);
|
strcat(output,buffer);
|
||||||
strcat(output,".");
|
strcat(output,".");
|
||||||
double fractpart = (input-(double)intpart);
|
double fractpart = (input-(double)intpart);
|
||||||
fractpart *= pow(10,digits);
|
fractpart *= pow(10,digits);
|
||||||
for(uint8_t i=1;i<digits;i++) { // Put zeroes in front of number
|
for(uint8_t i=1;i<digits;i++) { // Put zeros in front of number
|
||||||
if(fractpart < pow(10,digits-i)) {
|
if(fractpart < pow(10,digits-i)) {
|
||||||
strcat(output,"0");
|
strcat(output,"0");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
itoa((unsigned long)fractpart,buffer,10); // Convert to string
|
intToString((uint32_t)fractpart,buffer); // Convert to string
|
||||||
strcat(output,buffer);
|
strcat(output,buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
67
SPP.h
67
SPP.h
|
@ -173,16 +173,76 @@ public:
|
||||||
/** Use this to print newline and carriage return. */
|
/** Use this to print newline and carriage return. */
|
||||||
void println(void);
|
void println(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to print unsigned integers.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumber(uint8_t n) { printNumber((uint32_t)n); };
|
||||||
|
/**
|
||||||
|
* Same as printNumber(uint8_t n), but will include newline and carriage return.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumberln(uint8_t n) { printNumberln((uint32_t)n); };
|
||||||
/**
|
/**
|
||||||
* Used to print signed integers.
|
* Used to print signed integers.
|
||||||
* @param n Integers to send.
|
* @param n Signed integer to send.
|
||||||
|
*/
|
||||||
|
void printNumber(int8_t n) { printNumber((int32_t)n); };
|
||||||
|
/**
|
||||||
|
* Same as printNumber(int8_t n), but will include newline and carriage return.
|
||||||
|
* @param n Signed integer to send.
|
||||||
|
*/
|
||||||
|
void printNumberln(int8_t n) { printNumberln((int32_t)n); };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to print unsigned integers.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumber(uint16_t n) { printNumber((uint32_t)n); };
|
||||||
|
/**
|
||||||
|
* Same as printNumber(uint16_t n), but will include newline and carriage return.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumberln(uint16_t n) { printNumberln((uint32_t)n); };
|
||||||
|
/**
|
||||||
|
* Used to print signed integers.
|
||||||
|
* @param n Signed integer to send.
|
||||||
|
*/
|
||||||
|
void printNumber(int16_t n) { printNumber((int32_t)n); };
|
||||||
|
/**
|
||||||
|
* Same as printNumber(int16_t n), but will include newline and carriage return.
|
||||||
|
* @param n Signed integer to send.
|
||||||
|
*/
|
||||||
|
void printNumberln(int16_t n) { printNumberln((int32_t)n); };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to print unsigned integers.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumber(uint32_t n);
|
||||||
|
/**
|
||||||
|
* Same as printNumber(uint32_t n), but will include newline and carriage return.
|
||||||
|
* @param n Unsigned integer to send.
|
||||||
|
*/
|
||||||
|
void printNumberln(uint32_t n);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to print signed integers.
|
||||||
|
* @param n Signed integer to send.
|
||||||
*/
|
*/
|
||||||
void printNumber(int32_t n);
|
void printNumber(int32_t n);
|
||||||
/**
|
/**
|
||||||
* Same as printNumber(int32_t n), but will include newline and carriage return.
|
* Same as printNumber(int32_t n), but will include newline and carriage return.
|
||||||
* @param n Integers to send.
|
* @param n Signed integer to send.
|
||||||
*/
|
*/
|
||||||
void printNumberln(int32_t n);
|
void printNumberln(int32_t n);
|
||||||
|
/**
|
||||||
|
* Helper function to convert from a signed integer to a string.
|
||||||
|
* @param input Signed integer to convert.
|
||||||
|
* @param output Output buffer.
|
||||||
|
*/
|
||||||
|
void intToString(uint32_t input, char* output);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to print floating-point numbers.
|
* Used to print floating-point numbers.
|
||||||
* @param n Floating-point number to print.
|
* @param n Floating-point number to print.
|
||||||
|
@ -195,9 +255,8 @@ public:
|
||||||
* @param digits Number of digits to send. If argument is omitted, then 2 digits will be used.
|
* @param digits Number of digits to send. If argument is omitted, then 2 digits will be used.
|
||||||
*/
|
*/
|
||||||
void printNumberln(double n, uint8_t digits = 2);
|
void printNumberln(double n, uint8_t digits = 2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to convert from double to string.
|
* Helper function to convert from a double to a string.
|
||||||
* @param input Floating-point number to convert.
|
* @param input Floating-point number to convert.
|
||||||
* @param output Output buffer.
|
* @param output Output buffer.
|
||||||
* @param digits Number of digits to convert. If argument is omitted, then 2 digits will be used.
|
* @param digits Number of digits to convert. If argument is omitted, then 2 digits will be used.
|
||||||
|
|
Loading…
Reference in a new issue