uc: serial.c clean-up + send() rewrite

This commit is contained in:
Bart Van Der Meerssche 2009-11-03 14:09:41 +00:00
parent 7c9bb607b5
commit 52f670577a
4 changed files with 17 additions and 46 deletions

View File

@ -36,6 +36,8 @@
#include <avr/wdt.h> #include <avr/wdt.h>
// variable declarations // variable declarations
uint8_t i;
volatile struct state aux[4] = {{false, false, START}, {false, false, START}, {false, false, START}, {false, false, START}}; volatile struct state aux[4] = {{false, false, START}, {false, false, START}, {false, false, START}, {false, false, START}};
volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}}; volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}};
@ -111,7 +113,7 @@ ISR(ANALOG_COMP_vect) {
// enable ADC and start a first ADC conversion // enable ADC and start a first ADC conversion
ADCSRA |= (1<<ADEN) | (1<<ADSC); ADCSRA |= (1<<ADEN) | (1<<ADSC);
printString("msg metervalues written to EEPROM (BROWN-OUT)\n"); printString("msg BROWN-OUT\n");
} }
// interrupt service routine for watchdog timeout // interrupt service routine for watchdog timeout
@ -119,7 +121,7 @@ ISR(WDT_vect) {
for (i=0; i<4; i++) for (i=0; i<4; i++)
eeprom_write_block((const void*)&measurements[i].value, (void*)&EEPROM_measurements[i].value, 4); eeprom_write_block((const void*)&measurements[i].value, (void*)&EEPROM_measurements[i].value, 4);
printString("msg metervalues written to EEPROM (WDT)\n"); printString("msg WDT\n");
} }
// disable WDT // disable WDT
@ -210,22 +212,20 @@ void setup()
void send(const struct sensor *measurement) void send(const struct sensor *measurement)
{ {
uint8_t i, length; uint8_t i = 46;
char buffer[49]; uint32_t value = measurement->value;
char pulse[49];
// determine the length of value // generate pulse message structure
ltoa(measurement->value, buffer, 10); strcpy(pulse, "pls ");
length = strlen(buffer); strcpy(&pulse[4], measurement->id);
strcpy(&pulse[36], ":0000000000\n");
strcpy(buffer, "pls "); do { // generate digits in reverse order
strcpy(&buffer[4], measurement->id); pulse[i--] = '0' + value % 10; // get next digit
strcpy(&buffer[36], ":"); } while ((value /= 10) > 0); // delete it
// insert leading 0's
for (i=0; i<10-length; i++) strcpy(&buffer[37+i], "0");
ltoa(measurement->value, &buffer[47-length], 10);
strcpy(&buffer[47], "\n");
printString(buffer); printString(pulse);
// blink the green LED // blink the green LED
PORTB |= (1<<PB5); PORTB |= (1<<PB5);
@ -257,6 +257,7 @@ int main(void)
// so the pulses are counted but not sent to the deamon // so the pulses are counted but not sent to the deamon
for (i=0; i<4; i++) _delay_ms(5000); for (i=0; i<4; i++) _delay_ms(5000);
serialFlush();
WDT_on(); WDT_on();
for (;;) loop(); for (;;) loop();

View File

@ -50,8 +50,6 @@
#define END0 0xcccccccc #define END0 0xcccccccc
// datastructures // datastructures
uint8_t i;
struct state { struct state {
boolean pulse; boolean pulse;
boolean toggle; boolean toggle;

View File

@ -28,7 +28,7 @@
// using a ring buffer (I think), in which rx_buffer_head is the index of the // using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail // location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read. // is the index of the location from which to read.
#define RX_BUFFER_SIZE 128 #define RX_BUFFER_SIZE 64
unsigned char rx_buffer[RX_BUFFER_SIZE]; unsigned char rx_buffer[RX_BUFFER_SIZE];
@ -128,21 +128,11 @@ SIGNAL(SIG_UART_RECV)
} }
} }
void printMode(int mode)
{
// do nothing, we only support serial printing, not lcd.
}
void printByte(unsigned char c) void printByte(unsigned char c)
{ {
serialWrite(c); serialWrite(c);
} }
void printNewline()
{
printByte('\n');
}
void printString(const char *s) void printString(const char *s)
{ {
while (*s) while (*s)
@ -194,19 +184,3 @@ void printBinary(unsigned long n)
{ {
printIntegerInBase(n, 2); printIntegerInBase(n, 2);
} }
/* Including print() adds approximately 1500 bytes to the binary size,
* so we replace it with the smaller and less-confusing printString(),
* printInteger(), etc.
void print(const char *format, ...)
{
char buf[256];
va_list ap;
va_start(ap, format);
vsnprintf(buf, 256, format, ap);
va_end(ap);
printString(buf);
}
*/

View File

@ -99,9 +99,7 @@ void serialWrite(unsigned char);
int serialAvailable(void); int serialAvailable(void);
int serialRead(void); int serialRead(void);
void serialFlush(void); void serialFlush(void);
void printMode(int);
void printByte(unsigned char c); void printByte(unsigned char c);
void printNewline(void);
void printString(const char *s); void printString(const char *s);
void printInteger(long n); void printInteger(long n);
void printHex(unsigned long n); void printHex(unsigned long n);