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>
// variable declarations
uint8_t i;
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}};
@ -111,7 +113,7 @@ ISR(ANALOG_COMP_vect) {
// enable ADC and start a first ADC conversion
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
@ -119,7 +121,7 @@ ISR(WDT_vect) {
for (i=0; i<4; i++)
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
@ -210,22 +212,20 @@ void setup()
void send(const struct sensor *measurement)
{
uint8_t i, length;
char buffer[49];
uint8_t i = 46;
uint32_t value = measurement->value;
char pulse[49];
// determine the length of value
ltoa(measurement->value, buffer, 10);
length = strlen(buffer);
// generate pulse message structure
strcpy(pulse, "pls ");
strcpy(&pulse[4], measurement->id);
strcpy(&pulse[36], ":0000000000\n");
strcpy(buffer, "pls ");
strcpy(&buffer[4], measurement->id);
strcpy(&buffer[36], ":");
// 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");
do { // generate digits in reverse order
pulse[i--] = '0' + value % 10; // get next digit
} while ((value /= 10) > 0); // delete it
printString(buffer);
printString(pulse);
// blink the green LED
PORTB |= (1<<PB5);
@ -257,6 +257,7 @@ int main(void)
// so the pulses are counted but not sent to the deamon
for (i=0; i<4; i++) _delay_ms(5000);
serialFlush();
WDT_on();
for (;;) loop();

View File

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

View File

@ -28,7 +28,7 @@
// 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
// 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];
@ -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)
{
serialWrite(c);
}
void printNewline()
{
printByte('\n');
}
void printString(const char *s)
{
while (*s)
@ -194,19 +184,3 @@ void printBinary(unsigned long n)
{
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 serialRead(void);
void serialFlush(void);
void printMode(int);
void printByte(unsigned char c);
void printNewline(void);
void printString(const char *s);
void printInteger(long n);
void printHex(unsigned long n);