Changeset - eda19f9f41b5
[Not reviewed]
default
0 0 2
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-10-25 20:17:56
ethanzonca@CL-SEC241-08.cedarville.edu
Added boilerplate code for state machine
2 files changed with 108 insertions and 0 deletions:
0 comments (0 inline, 0 general)
master/master/lib/serparser.c
Show inline comments
 
new file 100644
 
/*
 
 * serparser.c
 
 *
 
 * Created: 10/25/2012 8:11:43 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
// ************* Macros ***************
 
#define SERIAL_RX_HASBYTES UCSRA & _BV(RXC)
 
#define MAX_CMD_LEN 8
 
#define BROADCAST_ADDR 0
 
 
//#define DEBUG
 
 
// ************* Command Definitions ***************
 
 
// Serial Commands
 
enum luma_cmd {  // CMD ID#
 
	BOARDTEMP = 0, // 0
 
	PRESSURE,      // 1
 
};
 
 
// Data length of each command, in bytes
 
uint8_t cmd_len[] = {
 
	2, // BOARDTEMP_LEN
 
	0, // PRESSURE_LEN
 
};
 
 
// Incoming command buffer
 
uint8_t buffer[MAX_CMD_LEN+2];
 
 
// Current buffer location
 
uint8_t buffercount = 0;
 
 
 
 
/* Get char from UART */
 
uint8_t uart_getchar(void)
 
{
 
	// Wait for chars
 
	while (!(UCSRA & (1<<RXC))){
 
		idle();
 
	}
 
	return UDR;
 
}
 
 
 
 
/* Process incoming serial data */
 
int uart_machine(void) {
 
 
	char byte;
 
 
	// Only process data if buffer has a byte
 
	if(SERIAL_RX_HASBYTES) {
 
		#ifdef DEBUG
 
		redflash();
 
		#endif
 
		
 
		// Pull byte off of the buffer
 
		byte = uart_getchar();
 
		buffer[buffercount] = byte;
 
 
		
 
		// Start byte
 
		if(buffercount == 0) {
 
			if(byte == '[') {
 
				// If this is a start byte, we're OK. Keep reading.
 
				buffercount++;
 
			}
 
			else {
 
				// Not a start byte that we're expecting. Reset to initial state.
 
				buffercount = 0;
 
			}
 
		}
 
		
 
		
 
	}
 
	else {
 
		return 0; // serial data not available
 
	}
 
}
 
\ No newline at end of file
master/master/lib/serparser.h
Show inline comments
 
new file 100644
 
/*
 
 * serparser.h
 
 *
 
 * Created: 10/25/2012 8:11:49 PM
 
 *  Author: ethanzonca
 
 */ 
 
 
 
#ifndef SERPARSER_H_
 
#define SERPARSER_H_
 
 
 
// Prototypes
 
uint8_t convertchar(uint8_t);
 
uint8_t uart_getchar(void);
 
void got_char(uint8_t);
 
int uart_machine(void);
 
void uart_putchar(uint8_t);
 
void exec_cmd(void);
 
void idle(void);
 
void uart_init(void);
 
 
 
 
#endif /* SERPARSER_H_ */
 
\ No newline at end of file
0 comments (0 inline, 0 general)