Ver código fonte

select radio properly

Chris Mullins 8 anos atrás
pai
commit
5ed3076bdf

+ 14 - 7
lib/MiLight/MiLightClient.cpp

@@ -2,21 +2,28 @@
 #include <MiLightRadioConfig.h>
 
 MiLightRadio* MiLightClient::getRadio(const MiLightRadioType type) {
-  MiLightRadio* radio = NULL;
+  MiLightRadioStack* stack = NULL;
   
   if (type == RGBW) {
-    return rgbwRadio->getRadio();
+    stack = rgbwRadio;
   } else if (type == CCT) {
-    return cctRadio->getRadio();
+    stack = cctRadio;
   } else if (type == RGBW_CCT) {
-    return rgbwCctRadio->getRadio();
+    stack = rgbwCctRadio;
   }
   
-  if (radio != NULL) {
-    radio->configure();
+  if (stack != NULL) {
+    MiLightRadio *radio = stack->getRadio();
+    
+    if (currentRadio != stack->type) {
+      radio->configure();
+    }
+    
+    currentRadio = stack->type;
+    return radio;
   }
   
-  return radio;
+  return NULL;
 }
 
 uint8_t MiLightClient::nextSequenceNum() {

+ 7 - 1
lib/MiLight/MiLightClient.h

@@ -22,7 +22,9 @@ enum MiLightStatus { ON = 0, OFF = 1 };
 
 class MiLightRadioStack {
 public:
-  MiLightRadioStack(RF24& rf, const MiLightRadioConfig& config) {
+  MiLightRadioStack(RF24& rf, const MiLightRadioConfig& config) 
+    : type(config.type)
+  {
     nrf = new PL1167_nRF24(rf);
     radio = new MiLightRadio(*nrf, config);
   }
@@ -36,6 +38,8 @@ public:
     return this->radio;
   }
   
+  const MiLightRadioType& type;
+  
 private:
   PL1167_nRF24 *nrf;
   MiLightRadio *radio;
@@ -116,9 +120,11 @@ class MiLightClient {
     MiLightRadioStack* rgbwRadio;
     MiLightRadioStack* cctRadio;
     MiLightRadioStack* rgbwCctRadio;
+    MiLightRadioType currentRadio;
     
     uint8_t sequenceNum;
     uint8_t nextSequenceNum();
+    unsigned int resendCount;
 };
 
 #endif

+ 15 - 5
lib/MiLight/MiLightRadioConfig.h

@@ -3,18 +3,27 @@
 #ifndef _MILIGHT_RADIO_CONFIG
 #define _MILIGHT_RADIO_CONFIG 
 
+enum MiLightRadioType {
+  UNKNOWN = 0,
+  RGBW  = 0xB8,
+  CCT   = 0x5A,
+  RGBW_CCT = 0x99
+};
+
 class MiLightRadioConfig {
 public:
   MiLightRadioConfig(const uint16_t syncword0,
   const uint16_t syncword3,
   const size_t packetLength,
   const uint8_t* channels,
-  const size_t numChannels) 
+  const size_t numChannels,
+  const MiLightRadioType type) 
     : syncword0(syncword0),
       syncword3(syncword3),
       packetLength(packetLength),
       channels(channels),
-      numChannels(numChannels)
+      numChannels(numChannels),
+      type(type)
   {}
     
   const uint16_t syncword0;
@@ -22,21 +31,22 @@ public:
   const size_t packetLength;
   const uint8_t* channels;
   const size_t numChannels;
+  const MiLightRadioType type;
 };
 
 const uint8_t RGBW_CHANNELS[] = {9, 40, 71};
 static MiLightRadioConfig MilightRgbwConfig(
-  0x147A, 0x258B, 7, RGBW_CHANNELS, 3
+  0x147A, 0x258B, 7, RGBW_CHANNELS, 3, RGBW
 );
 
 const uint8_t CCT_CHANNELS[] = {4, 39, 74};
 static MiLightRadioConfig MilightCctConfig(
-  0x050A, 0x55AA, 7, CCT_CHANNELS, 3
+  0x050A, 0x55AA, 7, CCT_CHANNELS, 3, CCT
 );
 
 const uint8_t RGBWCCT_CHANNELS[] = {70, 39, 8};
 static MiLightRadioConfig MilightRgbwCctConfig(
-  0x7236, 0x1809, 8, RGBWCCT_CHANNELS, 3
+  0x7236, 0x1809, 8, RGBWCCT_CHANNELS, 3, RGBW_CCT
 );
 
 #endif