   kSetB,
   kGetTemp,
   kGetHumidity,
+  kGetLight,
+  kTemp,
+  kHumidity,
+  kLight,
+  kButtonPressed,
 };
 
 void attachCommandCallbacks(){
-  cmdMessenger.attach(OnUnknownCommand);
-  cmdMessenger.attach(kSetRgb,   OnSetRgb);
-  cmdMessenger.attach(kSetR,        OnSetR);
-  cmdMessenger.attach(kSetG,        OnSetG);
-  cmdMessenger.attach(kSetB,        OnSetB);
-  cmdMessenger.attach(kGetTemp,     OnGetTemp);
-  cmdMessenger.attach(kGetHumidity, OnGetHumidity);
+  cmdMessenger.attach(              OnUnknownCommand);
+  cmdMessenger.attach(kSetRgb,      OnSetRgb        );
+  cmdMessenger.attach(kSetR,        OnSetR          );
+  cmdMessenger.attach(kSetG,        OnSetG          );
+  cmdMessenger.attach(kSetB,        OnSetB          );
+  cmdMessenger.attach(kGetTemp,     OnGetTemp       );
+  cmdMessenger.attach(kGetHumidity, OnGetHumidity   );
+  cmdMessenger.attach(kGetLight,    OnGetLight      );
 }
 
 void OnUnknownCommand(){

}

void OnSetRgb(){
  cmdMessenger.sendCmd(kAcknowledge);
}

void OnSetR(){
  uint16_t r = cmdMessenger.readInt16Arg();
  analogWrite(PIN_LED_R, r);
  cmdMessenger.sendCmd(kAcknowledge);
}

void OnSetG(){
  uint16_t g = cmdMessenger.readInt16Arg();
  analogWrite(PIN_LED_G, g);
  cmdMessenger.sendCmd(kAcknowledge);
}

void OnSetB(){
  uint16_t b = cmdMessenger.readInt16Arg();
  analogWrite(PIN_LED_B, b);
  cmdMessenger.sendCmd(kAcknowledge);
}

void checkSampleTime(){
  if( (millis() - lastSampleTime) > sampleDelay){
    wdt_reset();
    DHT.read22(PIN_DHT);  
    wdt_reset();
    lastSampleTime = millis();
  }
}

void OnGetTemp(){
  checkSampleTime();
  cmdMessenger.sendCmd(kAcknowledge, DHT.temperature);
}

void OnGetHumidity(){
  checkSampleTime();
  cmdMessenger.sendCmd(kAcknowledge, DHT.humidity);
}

void setup() {

  cmdMessenger.sendCmd(kAcknowledge, "Arduino ready!");
  wdt_enable(WDTO_2S);
  lastSampleTime = 0;
  for(uint8_t i = 0; i < NUM_BUTTONS; i++){
    lastButtonTime[i] = 0;    
  }
  analogWrite(PIN_LED_R, 0);
  analogWrite(PIN_LED_G, 0);
  analogWrite(PIN_LED_B, 0);
  pinMode(PIN_D4, INPUT_PULLUP);
  pinMode(PIN_D6, INPUT_PULLUP);
  pinMode(PIN_D7, INPUT_PULLUP);
  pinMode(PIN_D8, INPUT_PULLUP);
}

void buttonCheck(uint16_t pin, uint8_t button){
  if(!digitalRead(pin)) { 
    if( (millis() - lastButtonTime[button]) > BUTTON_DELAY){
      cmdMessenger.sendCmd(kButtonPressed, button);
      lastButtonTime[button] = millis(); 
    }  
  }
}

void parseButtons(){
  buttonCheck(PIN_D4, 0);
  buttonCheck(PIN_D6, 1);
  buttonCheck(PIN_D7, 2);
  buttonCheck(PIN_D8, 3);
}

void loop() {  
  cmdMessenger.feedinSerialData();
  parseButtons();
  wdt_reset();
}

