Changeset - fe1ae61c1293
[Not reviewed]
default
0 2 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-01-17 21:47:55
ethanzonca@CL-ENS241-08.cedarville.edu
Destination address switching implemented. Needs testing to find additional bugs.
2 files changed with 97 insertions and 23 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -21,24 +21,27 @@
 
#define MODULE_ID '1'
 
#define BOARDTEMP_ADDR 0x90
 
 
// --------------------------------------------------------------------------
 
// Error Codes config (led.c, used throughout code)
 
// --------------------------------------------------------------------------
 
 
// SD Card
 
#define ERROR_SD_INIT 2
 
#define ERROR_SD_PARTITION 3
 
#define ERROR_SD_FILE 4
 
 
#define ERROR_XBEETIMEOUT 5
 
#define ERROR_NOXBEE 6
 
 
#define ERROR_CRAP 15
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
// NOT USED. Could integrate into slavesensors.c setup function for configurability eventually.
 
// Currently manual configuration of sensors is done in slavesensors.c
 
#define SLAVE0_SENSORS BOARDTEMP
 
#define SLAVE1_SENSORS BOARDTEMP | HUMIDITY | TEMPERATURE | PRESSURE | AMBIENTLIGHT
 
#define SLAVE2_SENSORS BOARDTEMP | GEIGER
 
#define SLAVE3_SENSORS BOARDTEMP | CAMERA
master/master/lib/slavesensors.c
Show inline comments
 
@@ -67,51 +67,48 @@ char* bufPtr = 0x00;
 
char debugBuf[64];
 
char slaveAddressLow[6][9];
 
char slaveAddressHigh[6][9];
 

	
 
void slavesensors_network_scan() {
 
	serial0_ioff();
 
	
 
	int atOK;
 
	
 
	serial0_sendString("Beginning network scan...\r\n\r\n");
 
	
 
	_delay_ms(500); // xbee warmup
 
	
 
	// Enter AT mode
 
	serial0_sendChar('+'); // Enter AT mode
 
	serial0_sendChar('+');
 
	serial0_sendChar('+');
 
	wdt_reset();
 
	
 
	// Wait 1ms until we get "OK"
 
	while(!serial0_hasChar());
 
	
 
	bufPtr = serial0_readLine();
 
	
 
	atOK = slavesensors_enterAT();
 
		
 
	char nameString[20] = "NONE";
 
	
 
	char slaveNames[6][16]; // Hold 16-char addresses of max 6 nodes, we only need them for debug so they are local 
 
	
 
	int nodeCount = 0;
 
	uint8_t nodeCount = 0;
 
	
 
	// wait for OK
 
	//todo
 
	if(strcmp(bufPtr, "OK") == 0)
 
	if(atOK == 0)
 
	{
 
		atOK = 1;
 
		
 
		serial0_sendString("ATND");
 
		serial0_sendChar(0x0D);
 
		
 
		// wait for scan to complete
 
		uint16_t scanStart = time_millis(); 		
 
		while(!serial0_hasChar()) {
 
			if(time_millis() - scanStart > 5000) {
 
				led_errorcode(ERROR_XBEETIMEOUT);
 
				return;
 
			}
 
			wdt_reset();
 
		}
 
		
 
		// Scan data end when newline by itself ("")	
 
		int lineCount = 0;	
 
	
 
		while(1) {
 
			bufPtr = serial0_readLine();
 
			
 
			// If we're starting a new block but got a newline instead, we're done!
 
			if(lineCount == 0 && strcmp(bufPtr, "") == 0) {
 
				break;			
 
@@ -129,59 +126,133 @@ void slavesensors_network_scan() {
 
			}
 
			
 
			// If we've finished one chunk (including the newline after it). Can't be else if because it controls increment.
 
			if(lineCount == 9) {
 
				// bufPtr should be null at this point, because we read in a newlinem after one chunk
 
				nodeCount++;
 
				lineCount = 0;
 
			}
 
			else {
 
				lineCount++;
 
			}
 

	
 
			
 
		}		
 
		// Exit AT
 
		serial0_sendString("ATCN");
 
		serial0_sendChar(0x0D);
 

	
 
		slavesensors_exitAT();
 

	
 
	}
 
	else {
 
		atOK = 0;
 
	}
 
	
 
	led_on(LED_SIDEBOARD);
 
	_delay_ms(200);
 
	led_off(LED_SIDEBOARD);
 

	
 
	#ifdef DEBUG_NETWORKSCAN
 
	serial0_sendString("Discovered: \r\n");
 
	for(int i=0; i<nodeCount; i++) {
 
		snprintf(debugBuf, 64, "  %s - %s%s\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i]);
 
		serial0_sendString(debugBuf);
 
	}
 
	
 
	if(atOK != 1) {
 
	serial0_sendString("\r\n");
 
	if(atOK != 0) {
 
		serial0_sendString("AT mode failed \r\n");
 
	}
 
	#endif
 
	
 
	
 
	// Wait for response
 
	// will be multiple values separated by <CR>
 
	// 9 data lines per node, <CR> terminated, followed by a new line with only <CR> at the end of all nodes.
 
	
 
	// <CR> followed by another <CR> signifies end of scan data
 
	
 
	slavesensors_selectnode(1);
 
	
 
	serial0_ion();
 
}
 
 
void slavesensors_exitAT() 
 
{
 
	// Exit AT
 
	serial0_sendString("ATCN");
 
	serial0_sendChar(0x0D);
 
	xbeeIsOk(); // wait for OK
 
}
 
 
// Enter AT mode. Leaves "OK" on the buffer.
 
int slavesensors_enterAT() 
 
{
 
	// Delay guard time
 
	_delay_ms(1);
 
	
 
	// Enter AT mode
 
	serial0_sendChar('+'); // Enter AT mode
 
	serial0_sendChar('+');
 
	serial0_sendChar('+');
 
	
 
	// Wait 1ms until we get "OK"
 
	int atStart = time_millis();
 
	while(!serial0_hasChar()) {
 
		if(time_millis() - atStart > 500) {
 
			led_errorcode(ERROR_XBEETIMEOUT);
 
			wdt_reset();
 
			return 1;
 
		}
 
	};	
 
	
 
	return xbeeIsOk();
 
 
}
 
 
int xbeeIsOk() 
 
{
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		return 1;
 
	}
 
}
 
 
void slavesensors_selectnode(uint8_t nodeIndex) 
 
{
 
	char tmpBuf[23];
 
	
 
	// If we can get into AT mode
 
	if(slavesensors_enterAT() == 0) {
 
		
 
		snprintf(tmpBuf, 23, "ATDH %s%c",slaveAddressHigh[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) {
 
			led_errorcode(ERROR_NOXBEE);
 
			return;
 
		}
 
		
 
		snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) {
 
			led_errorcode(ERROR_NOXBEE);
 
			return;
 
		}
 
		
 
		slavesensors_exitAT();
 
	}
 
	return;
 
}
 
 
bool slavesensors_isrequesting() 
 
{
 
	return requesting;	
 
}
 
 
void slavesensors_startprocess() 
 
{
 
	requesting = true;
 
	slavesensors_request();		
 
}
 
 
// TODO: inline. static.
0 comments (0 inline, 0 general)