Pārlūkot izejas kodu

Dynamically populate radios

Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
35b8451bcb

+ 4 - 1
lib/MiLight/MiLightClient.cpp

@@ -5,7 +5,7 @@
 MiLightRadio* MiLightClient::switchRadio(const MiLightRadioType type) {
   RadioStack* stack = NULL;
   
-  for (int i = 0; i < NUM_RADIOS; i++) {
+  for (int i = 0; i < numRadios; i++) {
     if (radios[i]->config.type == type) {
       stack = radios[i];
       break;
@@ -22,6 +22,9 @@ MiLightRadio* MiLightClient::switchRadio(const MiLightRadioType type) {
     currentRadio = stack;
     formatter = stack->config.packetFormatter;
     return radio;
+  } else {
+    Serial.print("MiLightClient - tried to get radio for unknown type: ");
+    Serial.println(type);
   }
   
   return NULL;

+ 9 - 8
lib/MiLight/MiLightClient.h

@@ -10,20 +10,20 @@
 
 #define MILIGHT_CCT_INTERVALS 10
 #define MILIGHT_DEFAULT_RESEND_COUNT 10
-#define NUM_RADIOS 3
 
 class MiLightClient {
   public:
     MiLightClient(uint8_t cePin, uint8_t csnPin)
       : rf(RF24(cePin, csnPin)),
       resendCount(MILIGHT_DEFAULT_RESEND_COUNT),
-      currentRadio(NULL)
+      currentRadio(NULL),
+      numRadios(MiLightRadioConfig::NUM_CONFIGS)
     {
-      size_t ix = 0;
-      radios = new RadioStack*[NUM_RADIOS];
-      radios[ix++] = new RadioStack(rf, MilightRgbwConfig);
-      radios[ix++] = new RadioStack(rf, MilightCctConfig);
-      radios[ix++] = new RadioStack(rf, MilightRgbCctConfig);
+      radios = new RadioStack*[numRadios];
+      
+      for (size_t i = 0; i < numRadios; i++) {
+        radios[i] = new RadioStack(rf, *MiLightRadioConfig::ALL_CONFIGS[i]);
+      }
       
       currentRadio = radios[0];
       currentRadio->getRadio()->configure();
@@ -34,7 +34,7 @@ class MiLightClient {
     }
     
     void begin() {
-      for (size_t i = 0; i < NUM_RADIOS; i++) {
+      for (size_t i = 0; i < numRadios; i++) {
         radios[i]->getRadio()->begin();
       }
     }
@@ -75,6 +75,7 @@ class MiLightClient {
     RadioStack** radios;
     RadioStack* currentRadio;
     PacketFormatter* formatter;
+    const size_t numRadios;
     
     unsigned int resendCount;
     

+ 2 - 2
lib/MiLight/RadioStack.h

@@ -8,7 +8,7 @@
 
 class RadioStack {
 public:
-  RadioStack(RF24& rf, MiLightRadioConfig& config) 
+  RadioStack(RF24& rf, const MiLightRadioConfig& config) 
     : config(config)
   {
     nrf = new PL1167_nRF24(rf);
@@ -24,7 +24,7 @@ public:
     return this->radio;
   }
   
-  MiLightRadioConfig& config;
+  const MiLightRadioConfig& config;
   
 private:
   PL1167_nRF24 *nrf;