refactored printIntegerInBase to work without a buffer + minor cleanup

This commit is contained in:
Simen Svale Skogsrud 2011-06-03 21:50:02 +02:00
parent 69be1240be
commit f0843db46e
4 changed files with 15 additions and 13 deletions

21
print.c
View File

@ -40,25 +40,24 @@ void printPgmString(const char *s)
serial_write(c); serial_write(c);
} }
// Prints a single digit of any base up to 36. 0..9 prints as
// '0'..'9' while 10..35 prints as 'a'..'z'
void printDigit(uint8_t value) {
serial_write(value < 10 ?
'0' + value :
'a' + value - 10);
}
void printIntegerInBase(unsigned long n, unsigned long base) void printIntegerInBase(unsigned long n, unsigned long base)
{ {
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) { if (n == 0) {
serial_write('0'); printDigit(0);
return; return;
} }
while (n > 0) { while (n > 0) {
buf[i++] = n % base; printDigit(n % base);
n /= base; n /= base;
} }
for (; i > 0; i--)
serial_write(buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10);
} }
void printInteger(long n) void printInteger(long n)

View File

@ -72,7 +72,7 @@ uint8_t protocol_execute_line(char *line) {
void protocol_process() void protocol_process()
{ {
char c; char c;
while((c = serial_read()) != 0xff) while((c = serial_read()) != SERIAL_NO_DATA)
{ {
if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute! if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute!
line[char_counter] = 0; // treminate string line[char_counter] = 0; // treminate string

View File

@ -23,6 +23,7 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/sleep.h> #include <avr/sleep.h>
#include "serial.h"
#ifdef __AVR_ATmega328P__ #ifdef __AVR_ATmega328P__
@ -97,7 +98,7 @@ uint8_t serial_read()
{ {
if (rx_buffer_head != rx_buffer_tail) { if (rx_buffer_head != rx_buffer_tail) {
// Return magic number if no data pending // Return magic number if no data pending
return 0xff; return SERIAL_NO_DATA;
} else { } else {
uint8_t data = rx_buffer[rx_buffer_tail]; uint8_t data = rx_buffer[rx_buffer_tail];
rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE; rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE;

View File

@ -24,6 +24,8 @@
#ifndef serial_h #ifndef serial_h
#define serial_h #define serial_h
#define SERIAL_NO_DATA 0xff
void serial_init(long baud); void serial_init(long baud);
void serial_write(uint8_t data); void serial_write(uint8_t data);