Przeglądaj źródła

Add support for configuring RF24 power level

Christopher Mullins 6 lat temu
rodzic
commit
123bbf3876

+ 5 - 3
lib/Radio/MiLightRadioFactory.cpp

@@ -3,7 +3,7 @@
 MiLightRadioFactory* MiLightRadioFactory::fromSettings(const Settings& settings) {
   switch (settings.radioInterfaceType) {
     case nRF24:
-      return new NRF24Factory(settings.csnPin, settings.cePin);
+      return new NRF24Factory(settings.csnPin, settings.cePin, settings.rf24PowerLevel);
 
     case LT8900:
       return new LT8900Factory(settings.csnPin, settings.resetPin, settings.cePin);
@@ -13,9 +13,11 @@ MiLightRadioFactory* MiLightRadioFactory::fromSettings(const Settings& settings)
   }
 }
 
-NRF24Factory::NRF24Factory(uint8_t csnPin, uint8_t cePin)
+NRF24Factory::NRF24Factory(uint8_t csnPin, uint8_t cePin, RF24PowerLevel rF24PowerLevel)
   : rf24(RF24(cePin, csnPin))
-{ }
+{ 
+  rf24.setPALevel(RF24PowerLevelHelpers::rf24ValueFromValue(rF24PowerLevel));
+}
 
 MiLightRadio* NRF24Factory::create(const MiLightRadioConfig &config) {
   return new NRF24MiLightRadio(rf24, config);

+ 2 - 1
lib/Radio/MiLightRadioFactory.h

@@ -4,6 +4,7 @@
 #include <MiLightRadio.h>
 #include <NRF24MiLightRadio.h>
 #include <LT8900MiLightRadio.h>
+#include <RF24PowerLevel.h>
 #include <Settings.h>
 
 #ifndef _MILIGHT_RADIO_FACTORY_H
@@ -21,7 +22,7 @@ public:
 class NRF24Factory : public MiLightRadioFactory {
 public:
 
-  NRF24Factory(uint8_t cePin, uint8_t csnPin);
+  NRF24Factory(uint8_t cePin, uint8_t csnPin, RF24PowerLevel rF24PowerLevel);
 
   virtual MiLightRadio* create(const MiLightRadioConfig& config);
 

+ 5 - 0
lib/Settings/Settings.cpp

@@ -110,6 +110,10 @@ void Settings::patch(JsonObject& parsedSettings) {
     this->setIfPresent(parsedSettings, "led_mode_packet_count", ledModePacketCount);
     this->setIfPresent(parsedSettings, "hostname", hostname);
 
+    if (parsedSettings.containsKey("rf24_power_level")) {
+      this->rf24PowerLevel = RF24PowerLevelHelpers::valueFromName(parsedSettings["rf24_power_level"]);
+    }
+
     if (parsedSettings.containsKey("led_mode_wifi_config")) {
       this->ledModeWifiConfig = LEDStatus::stringToLEDMode(parsedSettings["led_mode_wifi_config"]);
     }
@@ -211,6 +215,7 @@ void Settings::serialize(Stream& stream, const bool prettyPrint) {
   root["led_mode_packet"] = LEDStatus::LEDModeToString(this->ledModePacket);
   root["led_mode_packet_count"] = this->ledModePacketCount;
   root["hostname"] = this->hostname;
+  root["rf24_power_level"] = RF24PowerLevelHelpers::nameFromValue(this->rf24PowerLevel);
 
   if (this->deviceIds) {
     JsonArray& arr = jsonBuffer.createArray();

+ 4 - 1
lib/Settings/Settings.h

@@ -2,6 +2,7 @@
 #include <StringStream.h>
 #include <ArduinoJson.h>
 #include <GroupStateField.h>
+#include <RF24PowerLevel.h>
 #include <Size.h>
 #include <LEDStatus.h>
 
@@ -99,7 +100,8 @@ public:
     ledModeOperating(LEDStatus::LEDMode::SlowBlip),
     ledModePacket(LEDStatus::LEDMode::Flicker),
     ledModePacketCount(3),
-    hostname("milight-hub")
+    hostname("milight-hub"),
+    rf24PowerLevel(RF24PowerLevelHelpers::defaultValue())
   {
     if (groupStateFields == NULL) {
       numGroupStateFields = size(DEFAULT_GROUP_STATE_FIELDS);
@@ -171,6 +173,7 @@ public:
   LEDStatus::LEDMode ledModePacket;
   size_t ledModePacketCount;
   String hostname;
+  RF24PowerLevel rf24PowerLevel;
 
 
 protected: