refactored printIntegerInBase to work without a buffer + minor cleanup

pull/1/head
Simen Svale Skogsrud 2011-06-03 21:50:02 +02:00
rodzic 69be1240be
commit f0843db46e
4 zmienionych plików z 15 dodań i 13 usunięć

21
print.c
Wyświetl plik

@ -40,25 +40,24 @@ void printPgmString(const char *s)
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)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
serial_write('0');
printDigit(0);
return;
}
while (n > 0) {
buf[i++] = n % base;
printDigit(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)

Wyświetl plik

@ -72,7 +72,7 @@ uint8_t protocol_execute_line(char *line) {
void protocol_process()
{
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!
line[char_counter] = 0; // treminate string

Wyświetl plik

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

Wyświetl plik

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