    * A2 - Temperature
    * D3 - Water Leak detector
  
-   Created 05-MAR-2015 by Jon Brule
+   Created 15-MAR-2015 by Jon Brule
 ----------------------------------------------------------------------------- */
 #include <avr/sleep.h>
 #include <avr/power.h>

// Initialization
//
void setup () {
    #if SERIAL || DEBUG
        Serial.begin(BAUD_RATE);
        Serial.print("\n[laundryMote - ");
        Serial.print(VERSION);
        Serial.println("]");
    #endif
    setupRadio();
    setupSensors();
    setupSleep();
}

void setupRadio() {
    #if DEBUG
        Serial.print("setup radio...");
    #endif
  
    radio.initialize(FREQUENCY,NODEID,NETWORKID);
    radio.setHighPower();
    delay(1000);
  
    #if DEBUG
        Serial.println("ok!");
    #endif
}

void setupSensors() {
    sensors = new Sensors();
    sensorData = new SensorData();
    memset(sensorData, 0, sizeof(*sensorData));
    
    
}

void setupSleep() {
    // normal timer operation
    TCCR1A = 0x00;
    
    // clear the timer counter register
    TCNT1=0x0000;
    
    // configure the prescaler for 1:1024, a 4.09 sec timeout
    TCCR1B = 0x05;
    
    // enable the timer overlow interrupt
    TIMSK1=0x01;
}



// Main Loop
//
void loop () {
  if (f_timer == 1) {
    f_timer = 0;
    enterSleep();
  }   
}

//-----------------------------------------------------------------------------
// ISR - Water Leak
void waterLeak() {
  #if DEBUG
    Serial.println("WATER LEAK detcted!!!");
  #endif
  
  noInterrupts();
  digitalWrite(MOTE_LED_PIN, HIGH);        
  sensors->measure();
  report(MSG_ALERT, 1);
  digitalWrite(MOTE_LED_PIN, LOW);
  interrupts();  
}

//-----------------------------------------------------------------------------
// ISR - Sensor Measurement / Report
ISR(TIMER1_OVF_vect) {
  if (f_timer == 0) {
    f_timer = 1;
        
    noInterrupts();
    digitalWrite(MOTE_LED_PIN, HIGH);        
    sensors->measure();
    report(MSG_READING, 0);
    digitalWrite(MOTE_LED_PIN, LOW);
    interrupts();
  }
}

//-----------------------------------------------------------------------------
// Sleeps the microcontroller
void enterSleep(void) {
  set_sleep_mode(SLEEP_MODE_IDLE);
  
