Commits

Anonymous committed 66ade41

Added command code.

Comments (0)

Files changed (2)

+#include "command.h"
+
+int command_flags;
+
+void commands_init(void) {
+	command_flags = 0;
+}
+
+/** function: process_command
+  * parameters:
+  *           data (unsigned int 8 bits) - The character that is to be processed
+  *           uart (unsigned int 8 bits) - The USART number from which the data
+  *                                        was received.
+  * description:
+  *           Processes the command character and if it is not recognised 
+  *           passes it on to opposing uart.
+  * returns: 1 - if command character
+  *          0 - otherwise
+  */
+uint8_t process_or_pass_command(uint8_t data, uint8_t uart) {
+	uint8_t result;
+	
+	result = process_command(data, uart);
+	
+	if (!result) {
+		if (uart == 1) {
+			usart0_putchar(data);
+		} else {
+			usart1_putchar(data);
+		}
+	}
+	
+	return result;
+}
+
+/** function: process_command
+  * parameters:
+  *           data (unsigned int 8 bits) - The character that is to be processed
+  *           uart (unsigned int 8 bits) - The USART number from which the data
+  *                                        was received.
+  * description:
+  *           Parses the character and applys it's effect if it is a command 
+  *           character.
+  * returns: 1 - if command character
+  *          0 - otherwise
+  */
+uint8_t process_command(uint8_t data, uint8_t uart) {
+	uint8_t result = 1;
+	
+	switch(data) {
+		case LED_ON_CHAR:
+			debug_led(ON);
+			break;
+		case LED_OFF_CHAR:
+			debug_led(OFF);
+			break;
+		case LED_TOGGLE_CHAR:
+			debug_led(TOGGLE);
+			break;
+		case LED_BLINK_CHAR:
+			debug_led(BLINK);
+			break;
+		case 'P':
+		case 'p':
+			debug_led(TOGGLE);
+			break;
+
+		case PASS_COMMAND_CHAR_ON:
+			if (uart == 1) {
+				command_flags |= PASS_U1_2_U0_FLAG;
+			} else {
+				command_flags |= PASS_U0_2_U1_FLAG;
+			}
+			break;
+
+		case PASS_COMMAND_CHAR_OFF:
+			if (uart ==1) {
+				command_flags &= ~PASS_U1_2_U0_FLAG;
+			} else {
+				command_flags &= ~PASS_U0_2_U1_FLAG;
+			}
+			break;
+
+		case ECHO_CHAR_ON:
+			if (uart == 1) {
+				command_flags |= ECHO_U1;
+			} else {
+				command_flags |= ECHO_U0;
+			}
+			break;
+
+		case ECHO_CHAR_OFF:
+			if (uart == 1) {
+				command_flags &= ~ECHO_U1;
+			} else {
+				command_flags &= ~ECHO_U0;
+			}
+			break;
+
+		default:
+			result = 0;
+			break;
+	}
+	
+	return result;
+}
+
+/** function: process_usart0 (Motor Board)
+  * params:   data (unsigned int 8 bits)
+  * description:
+  *           Processes the character received on the UART0 communication line 
+  *           which is connected to the Motor Board.
+  */
+void process_usart0(uint8_t data) {
+	// Process "pass the parcel"
+	if ((command_flags & PASS_U0_2_U1_FLAG) && 
+		data != PASS_COMMAND_CHAR_ON && 
+		data != PASS_COMMAND_CHAR_OFF) {
+		
+		usart1_putchar(data); // Pass the data onto UART0
+		process_command(data, 0);
+	} else {
+		process_or_pass_command(data, 0);
+	}
+	
+	// Process "echo"
+	if (command_flags & ECHO_U0) {
+		usart0_putchar(data);
+	}
+}
+
+/** function: process_usart1 (WiFi)
+  * params:   data (unsigned int 8 bits)
+  * description:
+  *           Processes the character received on the UART1 communication line 
+  *           which is connected to the WiFi module.
+  */
+void process_usart1(uint8_t data) {
+	// Process "pass the parcel"
+	if ((command_flags & PASS_U1_2_U0_FLAG) && 
+	    data != PASS_COMMAND_CHAR_ON && 
+		data != PASS_COMMAND_CHAR_OFF) {
+		
+		usart0_putchar(data); // Pass the data onto UART0
+		process_command(data, 1);
+	} else {
+		process_or_pass_command(data, 1);
+	}
+	
+	// Process "echo"
+	if (command_flags & ECHO_U1) {
+		usart1_putchar(data);
+	}
+}
+/**  Project:    Wacky Racers
+  *  Subproject: Image sensor and WiFi board firmware
+  *  Authors:    Lazar Sumar, 
+  *              Simon Gow
+  *              Tracy Jackson
+  *
+  *  Company:    University of Canterbury
+  *  Department: Electrical and Computer Engineering
+  *  Course:     Computer Hardware II (ENEL429)
+  *
+  *  Group:         3
+  *  Group members: Lazar Sumar, Simon Gow, Tracy Jackson,
+  *                 Sean Thomas, Regan Gunther, Jared Hall
+  *
+  *  File:             wifi.h
+  *  File description: RN131G WiFly module code. It is connected to UART1 on 
+  *                    pins PD2 and PD3. User manual available at 
+  *                    http://www.rovingnetworks.com/Docs/WiFly-RN-UM.pdf
+  *
+  */
+#ifndef COMMAND_H
+#define COMMAND_H
+
+#include "util.h"
+#include "usart0.h"
+#include "usart1.h"
+
+#define PASS_COMMAND_CHAR_ON  '{'
+#define PASS_COMMAND_CHAR_OFF '}'
+#define ECHO_CHAR_ON          '('
+#define ECHO_CHAR_OFF         ')'
+#define LED_ON_CHAR           '+'
+#define LED_OFF_CHAR          '-'
+#define LED_TOGGLE_CHAR       '~'
+#define LED_BLINK_CHAR        '*'
+
+#define PASS_U0_2_U1_FLAG 1
+#define PASS_U1_2_U0_FLAG 2
+#define ECHO_U0 4
+#define ECHO_U1 8
+
+
+#include "command.h"
+
+/** function: command_init
+  * description:
+  *           Initialised the command settings to their default values. 
+  */
+void commands_init();
+
+/** function: process_command
+  * parameters:
+  *           data (unsigned int 8 bits) - The character that is to be processed
+  *           uart (unsigned int 8 bits) - The USART number from which the data
+  *                                        was received.
+  * description:
+  *           Processes the command character and if it is not recognised 
+  *           passes it on to opposing uart.
+  * returns: 1 - if command character
+  *          0 - otherwise
+  */
+uint8_t process_or_pass_command(uint8_t data, uint8_t uart);
+
+/** function: process_command
+  * parameters:
+  *           data (unsigned int 8 bits) - The character that is to be processed
+  *           uart (unsigned int 8 bits) - The USART number from which the data
+  *                                        was received.
+  * description:
+  *           Parses the character and applys it's effect if it is a command 
+  *           character.
+  * returns: 1 - if command character
+  *          0 - otherwise
+  */
+uint8_t process_command(uint8_t data, uint8_t uart);
+
+/** function: process_usart0 (Motor Board)
+  * params:   data (unsigned int 8 bits)
+  * description:
+  *           Processes the character received on the UART0 communication line 
+  *           which is connected to the Motor Board.
+  */
+void process_usart0(uint8_t data);
+/** function: process_usart1 (WiFi)
+  * params:   data (unsigned int 8 bits)
+  * description:
+  *           Processes the character received on the UART1 communication line 
+  *           which is connected to the WiFi module.
+  */
+void process_usart1(uint8_t data);
+
+#endif