Changeset - 6b1e328f8883
[Not reviewed]
default
0 7 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-02-05 16:41:38
ethanzonca@CL-ENS241-08.cedarville.edu
Trimmed significant amount of memory usage, fixed scope issues and downsized fixed arrays.
7 files changed with 37 insertions and 44 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -2,143 +2,138 @@
 
 * Master Firmware: Configuration
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#ifndef CONFIG_H_
 
#define CONFIG_H_
 
 
// --------------------------------------------------------------------------
 
// Module config (master.c)
 
// --------------------------------------------------------------------------
 
 
//#define DEBUG_OUTPUT
 
 
#define F_CPU 11059200
 
#define MODULE_ID '1'
 
#define BOARDTEMP_ADDR 0x90
 
 
#define HEATER_THRESHOLD 25
 
 
// --------------------------------------------------------------------------
 
// 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
 
 
#define ERROR_ATFAIL 3
 
#define ERROR_EXITAT 8
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
#define MAX_NUM_SLAVES 5  // Maximum number of nodes in the system
 
#define MAX_NUM_SENSORS 20 // Maximum number of unique types of sensors in the system
 
#define MAX_NUM_SENSORS 10 // Maximum number of unique types of sensors in the system
 
 
// Node identifier of log destination xbee
 
#define XBEE_LOGDEST_NAME "HAB-LOGGER"
 
 
#define DATAREQUEST_RATE 3000
 
 
// --------------------------------------------------------------------------
 
// Command Parser config (serparser.c)
 
// --------------------------------------------------------------------------
 
 
// Maximum payload size of command
 
#define MAX_PAYLOAD_LEN 16
 
 
// Circular serial buffer size. Must be at least MAX_CMD_LEN + 5
 
#define BUFFER_SIZE 128 
 
 
// Public broadcast address
 
#define BROADCAST_ADDR 0 
 
 
 
// --------------------------------------------------------------------------
 
// GPS config (xxx.c)
 
// --------------------------------------------------------------------------
 
#define NMEABUFFER_SIZE 150
 
 
// --------------------------------------------------------------------------
 
// USART config (serial.c)
 
// --------------------------------------------------------------------------
 
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200
 
 
 
// --------------------------------------------------------------------------
 
// AX.25 config (ax25.c)
 
// --------------------------------------------------------------------------
 

	
 
// TX delay in milliseconds
 
#define TX_DELAY      500
 

	
 
// Maximum packet delay
 
#define MAX_PACKET_LEN 512  // bytes
 
 

	
 
// --------------------------------------------------------------------------
 
// APRS config (aprs.c)
 
// --------------------------------------------------------------------------
 

	
 
// Set your callsign and SSID here. Common values for the SSID are
 
// (from http://zlhams.wikidot.com/aprs-ssidguide):
 
//
 
// - Balloons:  11
 
// - Cars:       9
 
// - Home:       0
 
// - IGate:      5
 
#define S_CALLSIGN      "KD8TDF"
 
#define S_CALLSIGN_ID   9 // 11
 

	
 
// Destination callsign: APRS (with SSID=0) is usually okay.
 
#define D_CALLSIGN      "APRS"
 
#define D_CALLSIGN_ID   0
 

	
 
// Digipeating paths:
 
// (read more about digipeating paths here: http://wa8lmf.net/DigiPaths/ )
 
// The recommended digi path for a balloon is WIDE2-1 or pathless. The default
 
// is pathless. Uncomment the following two lines for WIDE2-1 path:
 
#define DIGI_PATH1      "WIDE2"
 
#define DIGI_PATH1_TTL  1
 

	
 
// APRS comment: this goes in the comment portion of the APRS message. You
 
// might want to keep this short. The longer the packet, the more vulnerable
 
// it is to noise.
 
#define APRS_COMMENT    "[A-30.5 B45.64 C99542]"
 
 
// Transmit the APRS sentence every X milliseconds
 
#define APRS_TRANSMIT_PERIOD 20000
 

	
 

	
 
// --------------------------------------------------------------------------
 
// Logger config (logger.c)
 
// --------------------------------------------------------------------------
 
 
#define LOGGER_ID_EEPROM_ADDR 0x10
 
 
// Written to the beginning of every log file
 
#define LOGGER_HEADERTEXT "HAB Control Master - 1.0\n"
 
 
// Log to SD card every X milliseconds
 
#define LOGGER_RATE 1000 
 
 
// LED cycle indicator speed
 
#define LEDCYCLE_RATE 100 
 
 
#endif /* CONFIG_H_ */
 
\ No newline at end of file
master/master/lib/aprs.c
Show inline comments
 
/*
 
 * Master Firmware: APRS
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include "aprs.h"
 
#include "ax25.h"
 
#include "gps.h"
 
#include "sensordata.h"
 
#include <stdio.h>
 

	
 
const char *gps_aprs_lat = "39.74744N";
 
const char *gps_aprs_lon = "-83.81249W";
 
const char *gps_time = "081533/";
 
float gps_altitude = 123.5;
 
int gps_course = 5;
 
int gps_speed = 13;
 

	
 
float meters_to_feet(float m)
 
{
 
  // 10000 ft = 3048 m
 
  return m / 0.3048;
 
}
 

	
 
void aprs_send()
 
{
 
  char temp[12];                   // Temperature (int/ext)
 
  const struct s_address addresses[] = { 
 
    {D_CALLSIGN, D_CALLSIGN_ID},  // Destination callsign
 
    {S_CALLSIGN, S_CALLSIGN_ID},  // Source callsign (-11 = balloon, -9 = car)
 
#ifdef DIGI_PATH1
 
    {DIGI_PATH1, DIGI_PATH1_TTL}, // Digi1 (first digi in the chain)
 
#endif
 
#ifdef DIGI_PATH2
 
    {DIGI_PATH2, DIGI_PATH2_TTL}, // Digi2 (second digi in the chain)
 
#endif
 
  };
 

	
 
	// emz: modified this to get the size of the first address rather than the size of the struct itself, which fails
 
  ax25_send_header(addresses, sizeof(addresses)/sizeof(addresses[0]));
 
  ax25_send_byte('/');                // Report w/ timestamp, no APRS messaging. $ = NMEA raw data
 
  // ax25_send_string("021709z");     // 021709z = 2nd day of the month, 17:09 zulu (UTC/GMT)
 
  ax25_send_string(get_dayofmonth()); ///! Needs to be day hour minute        // 170915 = 17h:09m:15s zulu (not allowed in Status Reports)
 
  ax25_send_string(get_timestamp()); 
 
  ax25_send_byte('z'); // zulu time. h for nonzulu
 
  ax25_send_string(get_latitude());     // Lat: 38deg and 22.20 min (.20 are NOT seconds, but 1/100th of minutes)
 
  ax25_send_byte('N');
 
  ax25_send_byte('/');                // Symbol table
 
  ax25_send_string(get_longitude());     // Lon: 000deg and 25.80 min
 
  ax25_send_byte('W');
 
  ax25_send_byte('O');                // Symbol: O=balloon, -=QTH
 
  snprintf(temp, 4, "%03d", (int)(gps_course + 0.5)); 
 
  ax25_send_string(temp);             // Course (degrees)
 
  
 
  //snprintf(temp, 4, "%03d", (int)(get_course() + 0.5));  
 
  // !!!TODO: ENSURE THAT THE COURSE IS FORMATTED CORRECTLY!
 
  ax25_send_string(get_course());             // Course (degrees)
 
  
 
  ax25_send_byte('/');                // and
 
  
 
  // !!!TODO: Check the speed!
 
  //snprintf(temp, 4, "%03d", (int)(gps_speed + 0.5));
 
  ax25_send_string(get_speedKnots());             // speed (knots)
 
  
 
  /*
 
  ax25_send_string("/A=");            // Altitude (feet). Goes anywhere in the comment area
 
  snprintf(temp, 7, "%06ld", (long)(meters_to_feet(gps_altitude) + 0.5));
 
  ax25_send_string(temp);
 
  ax25_send_string("/Ti=");
 
  snprintf(temp, 6, "%d", 122);//sensors_int_lm60()); -- PUT SENSOR DATA HERE
 
  ax25_send_string(temp);
 
  ax25_send_string("/Te=");
 
  snprintf(temp, 6, "%d", 123);//sensors_ext_lm60());
 
  ax25_send_string(temp);
 
  ax25_send_string("/V=");
 
  snprintf(temp, 6, "%d", 123);//sensors_vin());
 
  ax25_send_string(temp);
 
  */
 
  
 
  ax25_send_byte(' ');
 
  //ax25_send_string(APRS_COMMENT);     // Comment
 
  ax25_send_string(slavesensors_getAPRScomment());
 
  ax25_send_footer();
 

	
 
  ax25_flush_frame();                 // Tell the modem to go
 
}
master/master/lib/i2c.c
Show inline comments
 
/*
 
 * i2c.c
 
 *
 
 * Created: 11/7/2012 7:18:23 PM
 
 *  Author: kripperger
 
 */ 
 
/*
 
 * Master Firmware: I2C
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include <inttypes.h>
 
#include <compat/twi.h>
 
#include "../config.h"
 
#include "i2c.h"
 
 
 
/* I2C clock in Hz */
 
#define SCL_CLOCK  100000L
 
 
 
/*************************************************************************
 
 Initialization of the I2C bus interface. Need to be called only once
 
*************************************************************************/
 
void i2c_init(void)
 
{
 
  /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
 
  
 
  TWSR = 0;                         /* no prescaler */
 
  TWBR = ((F_CPU/SCL_CLOCK)-16)/2;  /* must be > 10 for stable operation */
 
 
}/* i2c_init */
 
 
 
 
/*************************************************************************	
 
  Issues a start condition and sends address and transfer direction.
 
  return 0 = device accessible, 1= failed to access device
 
*************************************************************************/
 
unsigned char i2c_start(unsigned char address)
 
{
 
    uint8_t   twst;
 
 
	// send START condition
 
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	while(!(TWCR & (1<<TWINT)));
 
 
	// check value of TWI Status Register. Mask prescaler bits.
 
	twst = TW_STATUS & 0xF8;
 
	if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
 
 
	// send device address
 
	TWDR = address;
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wail until transmission completed and ACK/NACK has been received
master/master/lib/i2c.h
Show inline comments
 
/*
 
 * i2c.h
 
 *
 
 * Created: 11/7/2012 7:18:33 PM
 
 *  Author: kripperger
 
 */ 
 
 
/*
 
 * Master Firmware: I2C
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#ifndef I2C_H_
 
#define I2C_H_
 
 
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
 
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
 
#endif
 
 
#include <avr/io.h>
 
 
 
/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
 
#define I2C_READ    1
 
/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
 
#define I2C_WRITE   0
 
 
 
void i2c_init(void);	//initialize the I2C master interace. Need to be called only once
 
void i2c_stop(void);	//Terminates the data transfer and releases the I2C bus
 
 
unsigned char i2c_start(unsigned char addr);	//Issues a start condition and sends address and transfer direction 
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible 
 
	//Returns   1   failed to access device 
 
 
unsigned char i2c_rep_start(unsigned char addr);	//Issues a repeated start condition and sends address and transfer direction
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible
 
	//Returns   1   failed to access device
 
	
 
void i2c_start_wait(unsigned char addr);	//Issues a start condition and sends address and transfer direction. If device is busy, use ack polling to wait until device ready 
 
	//addr is the address and transfer direction of I2C device
 
 
unsigned char i2c_writeX(unsigned char data);	//Send one byte to I2C device
 
	//data is the byte to be transfered
 
	//Returns   0   write successful
 
	//Returns   1   write failed
 
 
unsigned char i2c_readAck(void);	//read one byte from the I2C device, request more data from device
 
	//Returns byte read from I2C device
 
 
unsigned char i2c_readNak(void);	//read one byte from the I2C device, read is followed by a stop condition
 
	//Returns byte read from I2C device
 
 
unsigned char i2c_readX(unsigned char ack);	//read one byte from the I2C device. Implemented as a macro, which calls either i2c_readAck or i2c_readNak
 
#define i2c_readX(ack)  (ack) ? i2c_readAck() : i2c_readNak(); 
 
	//When ack is 1: send ack, request more data from device
 
	//When ack is 0: send nak, read is followed by a stop condition
master/master/lib/led.h
Show inline comments
 
/*
 
 * Master Firmware: Status and Error LED Handler
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
 
#ifndef LED_H_
 
#define LED_H_
 
 
#include <avr/io.h>
 
 
enum leds {
 
	LED_ACT0 = 0,
 
	LED_ACT1,
 
	LED_ACT2,
 
	LED_ACT3,
 
	
 
	LED_POWER,
 
	LED_STATUS,
 
	LED_ERROR,
 
	LED_SIDEBOARD,
 
	LED_ACTIVITY,
 
	LED_CYCLE,
 
	LED_HEAT,
 
	LED_BUZZ,
 
};
 
 
typedef struct {uint8_t* direction; uint8_t* port; uint8_t pin;} led_t;
 
 
// Match order of leds enum
 
static led_t ledList[] = {
 
	{&DDRA, &PORTA, PA1}, // ACT0
 
	{&DDRA, &PORTA, PA2}, // ACT1
 
	{&DDRA, &PORTA, PA3}, // ACT2
 
	{&DDRA, &PORTA, PA4}, // ACT3
 
 
//pcb:
 
	{&DDRB, &PORTB, PB4}, // POWER
 
	{&DDRB, &PORTB, PB3}, // STATUS
 
	{&DDRB, &PORTB, PB2}, // ERROR
 
 
//breadboard:
 
//	{&DDRA, &PORTA, PA2}, // POWER
 
//	{&DDRA, &PORTA, PA0}, // STATUS
 
//	{&DDRA, &PORTA, PA1}, // ERROR
 
 
	{&DDRD, &PORTD, PD6}, // SIDEBOARD
 
	{&DDRD, &PORTD, PD5}, // ACTIVITY
 
	{&DDRD, &PORTD, PD4}, // CYCLE
 
	
 
	{&DDRA, &PORTA, PA6}, // HEAT
 
	{&DDRA, &PORTA, PA7}, // BUZZER
 
};
 
 
#define NUM_LEDS 12
 
 
void led_setup();
 
void led_on(uint8_t led);
 
void led_off(uint8_t led);
 
void led_toggle(uint8_t led);
 
void led_errorcode(uint8_t code);
 
void led_spin();
 
void led_alert();
 
 
#endif /* LED_H_ */
master/master/lib/sensordata.c
Show inline comments
 
@@ -16,97 +16,98 @@
 
#include <string.h>
 
#include "sensordata.h"
 
#include "slavesensors.h"
 
#include "boardtemp.h"
 
#include "looptime.h"
 
#include "gps.h"
 
#include "logger.h"
 

	
 
int32_t slaves[MAX_NUM_SLAVES][MAX_NUM_SENSORS];
 

	
 
void sensordata_setup() 
 
{
 
	for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
		for(int j=0; j<MAX_NUM_SENSORS; j++) {
 
			slaves[i][j] = -2111111111; // minimum value of 16 bit integer
 
		}
 
	}
 
}
 
 
void sensordata_set(uint8_t nodeID, uint8_t type, int32_t value)
 
{
 
	if(nodeID < MAX_NUM_SLAVES) {
 
		slaves[nodeID][type] = value;
 
	}	
 
}
 
 
int32_t sensordata_get(uint8_t nodeID, uint8_t type) 
 
{
 
	// Avoid reading out of bad places!
 
	if(nodeID < MAX_NUM_SLAVES) {
 
		return slaves[nodeID][type];
 
	}
 
	else {
 
		return 0;
 
	}
 
}
 
 
 
char commentBuffer[128];
 
 
 
// [A-30.5 B45.64 C99542]"
 
char* slavesensors_getAPRScomment() {
 
	snprintf(commentBuffer,128, "T%d S%s V%s H%s", sensors_getBoardTemp(), get_sv(), get_speedKnots(), get_hdop());
 
	return commentBuffer;
 
}
 
 

	
 
char logbuf[256];
 
 
bool dataWasReady = false;
 
 
void sensordata_logvalues() {
 
	// Generate CSV header after we have queried all slaves once
 
	if(slavesensors_dataReady()) {
 
	
 
		// Only generate/write header the first time data is ready
 
		if(!dataWasReady) {
 
			#define CSV_HEADER_SIZE 512
 
			char csvHeader[CSV_HEADER_SIZE];
 
			csvHeader[0] = 0x00;
 
		
 
			// Add master data headers
 
			snprintf(csvHeader, CSV_HEADER_SIZE, "Time,BoardTemp,GPSTime,GPSLat,GPSLon,GPSSpeed,GPSHDOP,GPSCourse,GPSSV,");
 
		
 
			// Add slave data headers
 
			for(uint8_t i=0; i<MAX_NUM_SLAVES; i++) {
 
				for(uint8_t j=0; j<MAX_NUM_SENSORS; j++) {
 
					int32_t tmp = sensordata_get(i, j);
 
					if(tmp != -2111111111) {
 
						// FIXME: will the 128 here really provide safety? might want to subtract the strlen
 
						snprintf(csvHeader + strlen(csvHeader), CSV_HEADER_SIZE-strlen(csvHeader),"%s-%s,", slavesensors_slavename(i), slavesensors_getLabel(j));
 
					}
 
				}
 
			}
 
		
 
			// Terminate header string and write to SD card
 
			snprintf(csvHeader + strlen(csvHeader), CSV_HEADER_SIZE-strlen(csvHeader),"\r\n");
 
			logger_log(csvHeader);
 
			dataWasReady = true;
 
		}
 
	
 
		// Write CSV sensor values to SD card
 
		char logbuf[256];
 
		logbuf[0] = 0x00;
 
		snprintf(logbuf, 256, "%lu,%d,%s,%s,%s,%s,%s,%s,%s,", time_millis(), sensors_getBoardTemp(),get_timestamp(),get_latitude(),get_longitude(),get_speedKnots(),get_hdop(), get_course(), get_sv());
 
		for(int i=0; i<MAX_NUM_SLAVES; i++) {
 
			for(int j=0; j<MAX_NUM_SENSORS; j++) {
 
				int32_t tmp = sensordata_get(i, j);
 
				if(tmp != -2111111111) {
 
					snprintf(logbuf + strlen(logbuf),256-strlen(logbuf)," %ld,", tmp);
 
				}
 
			
 
			}
 
		}
 
		snprintf(logbuf + strlen(logbuf),256-strlen(logbuf),"\r\n");
 
		logger_log(logbuf);
 
	}
 
}
 
\ No newline at end of file
master/master/lib/slavesensors.c
Show inline comments
 
/*
 
 * Master Firmware: Slave Sensor Data Acquisition
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
#include "../config.h"
 
#include <avr/io.h>
 
#include <stdbool.h>
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <util/delay.h>
 
#include <avr/wdt.h>
 
#include <avr/pgmspace.h>
 
#include "serial.h"
 
#include "serparser.h"
 
#include "slavesensors.h"
 
#include "sensordata.h"
 
#include "led.h"
 
#include "looptime.h"
 

	
 
// Label lookup table
 
// Make sure there are never more labels than there are MAX_NUM_SENSORS! 
 
const char label_0[] PROGMEM = "BoardTemp";
 
const char label_1[] PROGMEM = "HeaterStatus";
 
const char label_2[] PROGMEM = "BatteryLevel";
 
const char label_3[] PROGMEM = "AirTemp";
 
const char label_4[] PROGMEM = "AmbientLight";
 
const char label_5[] PROGMEM = "Humidity";
 
const char label_6[] PROGMEM = "Pressure";
 
const char label_7[] PROGMEM = "Altitude";
 
const char label_8[] PROGMEM = "CPM-Radiation";
 

	
 
const char *const labelLookup[] PROGMEM =
 
{
 
	label_0,
 
	label_1,
 
	label_2,
 
	label_3,
 
	label_4,
 
	label_5,
 
	label_6,
 
	label_7,
 
	label_8,
 
};
 

	
 
char labelBuffer[25]; // Size to length of label
 
char labelBuffer[15]; // Size to length of label
 
char* slavesensors_getLabel(uint8_t sensorID) {
 
	//snprintf(labelBuffer, 25, "[%u]", sensorID);
 
	//return labelBuffer;
 
	if(sensorID < 9)
 
	{
 
		strncpy_P(labelBuffer,(char*)pgm_read_word(&(labelLookup[sensorID])),25);
 
		strncpy_P(labelBuffer,(char*)pgm_read_word(&(labelLookup[sensorID])),15);
 
		
 
		return labelBuffer;
 
	}
 
	else {
 
		return NULL;
 
	}
 
}
 

	
 
uint8_t currentSlave = 0;
 
uint8_t currentSlaveSensor = 0;
 
 
bool requesting = false;
 
 
void slavesensors_setup() 
 
{
 
	
 
}
 

	
 
//#define DEBUG_NETWORKSCAN
 
//#define DEBUG_GETSLAVEDATA
 
 
char* bufPtr = 0x00;
 

	
 
static char slaveAddressLow[MAX_NUM_SLAVES][9];
 
static char slaveAddressHigh[MAX_NUM_SLAVES][9];
 
static char slaveNames[MAX_NUM_SLAVES][20];
 
static char slaveNames[MAX_NUM_SLAVES][15];
 

	
 
uint8_t loggerIndex = 255;
 
uint8_t nodeCount = 0;
 
bool dataReady = false;
 

	
 
char* slavesensors_slavename(uint8_t id) {
 
	return slaveNames[id];
 
}
 

	
 
void slavesensors_network_scan() {
 
	serial0_ioff();
 
	
 
	int atOK;
 
	
 
	#ifdef DEBUG_OUTPUT
 
	serial0_sendString("Beginning network scan...\r\n\r\n");
 
	#endif
 
	
 
	_delay_ms(500); // xbee warmup
 
	_delay_ms(200); // xbee warmup
 
	wdt_reset();
 
	
 
	led_on(LED_ACTIVITY);
 
	atOK = slavesensors_enterAT();
 
	
 
	// wait for OK
 
	if(atOK == 0)
 
	{
 
		led_on(LED_CYCLE);
 
		serial0_sendString("ATND");
 
		serial0_sendChar(0x0D);
 
		
 
		// wait for scan to complete
 
		uint16_t scanStart = time_millis(); 		
 
		while(!serial0_hasChar()) {
 
			if(time_millis() - scanStart > 7000) {
 
				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;			
 
			}
 
			
 
			if(lineCount == 1) {
 
				strncpy(slaveAddressHigh[nodeCount],bufPtr, 9);
 
			}
 
			else if(lineCount == 2) {
 
				strncpy(slaveAddressLow[nodeCount],bufPtr, 9);
 
			}
 
			else if(lineCount == 3) {
 
				strncpy(slaveNames[nodeCount], bufPtr, 20);
 
				strncpy(slaveNames[nodeCount], bufPtr, 15);
 
			}
 
			
 
			// 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 newline after one chunk
 
				nodeCount++;
 
				lineCount = 0;
 
			}
 
			else {
 
				lineCount++;
 
			}
 

	
 
		}		
 

	
 
		slavesensors_exitAT();
 

	
 
	}
 
	
 
	// Display number of found nodes on spinning indicator
 
	switch(nodeCount) {
 
		case 0:
 
			break;
 
		case 3:
 
			led_on(LED_ACT2);
 
			_delay_ms(100);
 
		case 2:
 
			led_on(LED_ACT1);
 
			_delay_ms(100);	
 
		case 1:
 
			led_on(LED_ACT0);
 
			_delay_ms(100);
 
	}
 
	
 
	led_on(LED_SIDEBOARD);
 
	_delay_ms(200);
 
	led_off(LED_SIDEBOARD);
 

	
 
	#ifdef DEBUG_OUTPUT
 
	
 
	char debugBuf[64];
 
	serial0_sendString("Discovered: \r\n");
 
	for(int i=0; i<nodeCount; i++) {
 
		snprintf(debugBuf, 64, "  %s - %s%s (%u)\r\n", slaveNames[i],slaveAddressHigh,slaveAddressLow[i], i);
 
		serial0_sendString(debugBuf);
 
	}
 
	serial0_sendString("\r\n");
 
	if(atOK != 0) {
 
		serial0_sendString("AT mode failed \r\n");
0 comments (0 inline, 0 general)