Explorar el Código

add RGB protocol

Chris Mullins hace 8 años
padre
commit
bbed25cb0d

+ 2 - 1
lib/MiLight/MiLightButtons.h

@@ -5,7 +5,8 @@ enum MiLightRadioType {
   UNKNOWN = 0,
   RGBW  = 0xB8,
   CCT   = 0x5A,
-  RGB_CCT = 0x20
+  RGB_CCT = 0x20,
+  RGB = 0xA4
 };
 
 enum MiLightRgbCctCommand {

+ 1 - 1
lib/MiLight/MiLightClient.cpp

@@ -85,7 +85,7 @@ void MiLightClient::updateHue(const uint16_t hue) {
 void MiLightClient::updateBrightness(const uint8_t brightness) {
   const MiLightRadioType type = currentRadio->config.type;
   
-  if (type == CCT) {
+  if (type == CCT || type == RGB) {
     const unsigned int oldResend = resendCount;
     setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
     

+ 3 - 3
lib/MiLight/MiLightRadio.cpp

@@ -7,7 +7,7 @@
 
 #include "MiLightRadio.h"
 
-#define PACKET_ID(packet) ( ((packet[1] & 0xF0)<<24) | (packet[2]<<16) | (packet[3]<<8) | (packet[7]) )
+#define PACKET_ID(packet, packet_length) ( (packet[1] << 8) | packet[packet_length - 1] )
 
 MiLightRadio::MiLightRadio(AbstractPL1167 &pl1167, const MiLightRadioConfig& config)
   : _pl1167(pl1167), config(config) {
@@ -83,10 +83,10 @@ bool MiLightRadio::available() {
     if (packet_length == 0 || packet_length != _packet[0] + 1U) {
       return false;
     }
+    uint32_t packet_id = PACKET_ID(_packet, packet_length);
 #ifdef DEBUG_PRINTF
-  printf("2");
+  printf("Packet id: %d\n", packet_id);
 #endif
-    uint32_t packet_id = PACKET_ID(_packet);
     if (packet_id == _prev_packet_id) {
       _dupes_received++;
     } else {

+ 1 - 1
lib/MiLight/MiLightRadio.h

@@ -16,7 +16,7 @@
 #include "AbstractPL1167.h"
 #include <MiLightRadioConfig.h>
 
-// #define DEBUG_PRINTF
+#define DEBUG_PRINTF
 
 #ifndef MILIGHTRADIO_H_
 #define MILIGHTRADIO_H_

+ 4 - 1
lib/MiLight/MiLightRadioConfig.cpp

@@ -3,7 +3,8 @@
 const MiLightRadioConfig* MiLightRadioConfig::ALL_CONFIGS[] = {
   &MilightRgbwConfig,
   &MilightCctConfig,
-  &MilightRgbCctConfig
+  &MilightRgbCctConfig,
+  &MilightRgbConfig
 };
 
 MiLightRadioConfig* MiLightRadioConfig::fromString(const String& s) {
@@ -13,6 +14,8 @@ MiLightRadioConfig* MiLightRadioConfig::fromString(const String& s) {
     return &MilightCctConfig;
   } else if (s.equalsIgnoreCase("rgb_cct")) {
     return &MilightRgbCctConfig;
+  } else if (s.equalsIgnoreCase("rgb")) {
+    return &MilightRgbConfig;
   }
   
   return NULL;

+ 6 - 1
lib/MiLight/MiLightRadioConfig.h

@@ -3,6 +3,7 @@
 #include <RgbCctPacketFormatter.h>
 #include <RgbwPacketFormatter.h>
 #include <CctPacketFormatter.h>
+#include <RgbPacketFormatter.h>
 #include <MiLightButtons.h>
 
 #ifndef _MILIGHT_RADIO_CONFIG
@@ -38,7 +39,7 @@ public:
   const MiLightRadioType type;
   const char* name;
   
-  static const size_t NUM_CONFIGS = 3;
+  static const size_t NUM_CONFIGS = 4;
   static const MiLightRadioConfig* ALL_CONFIGS[NUM_CONFIGS];
   
   static MiLightRadioConfig* fromString(const String& s);
@@ -57,4 +58,8 @@ static MiLightRadioConfig MilightRgbCctConfig(
   0x7236, 0x1809, new RgbCctPacketFormatter(), RGB_CCT, "rgb_cct", 8, 39, 70
 );
 
+static MiLightRadioConfig MilightRgbConfig(
+  0x9AAB, 0xBCCD, new RgbPacketFormatter(), RGB, "rgb", 3, 38, 73
+);
+
 #endif

+ 1 - 1
lib/MiLight/PL1167_nRF24.h

@@ -12,7 +12,7 @@
 #include "AbstractPL1167.h"
 #include "RF24.h"
 
-// #define DEBUG_PRINTF
+#define DEBUG_PRINTF
 
 #ifndef PL1167_NRF24_H_
 #define PL1167_NRF24_H_

+ 46 - 0
lib/MiLight/RgbPacketFormatter.cpp

@@ -0,0 +1,46 @@
+#include <RgbPacketFormatter.h>
+
+void RgbPacketFormatter::reset() {
+  size_t packetPtr = 0;
+  
+  packet[packetPtr++] = RGB;
+  packet[packetPtr++] = deviceId >> 8;
+  packet[packetPtr++] = deviceId & 0xFF;
+  packet[packetPtr++] = 0;
+  packet[packetPtr++] = 0;
+  packet[packetPtr++] = sequenceNum++;
+}
+
+void RgbPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
+  command(status == ON ? RGB_ON : RGB_OFF, 0);
+}
+
+void RgbPacketFormatter::command(uint8_t command, uint8_t arg) {
+  packet[RGB_COMMAND_INDEX] = command;
+}
+  
+void RgbPacketFormatter::updateHue(uint16_t value) {
+  const int16_t remappedColor = (value + 40) % 360;
+  updateColorRaw(rescale(remappedColor, 255, 360));
+}
+
+void RgbPacketFormatter::updateColorRaw(uint8_t value) {
+  packet[RGB_COLOR_INDEX] = value;
+  command(0, 0);
+}
+
+void RgbPacketFormatter::increaseBrightness() {
+  command(RGB_BRIGHTNESS_UP, 0);
+}
+
+void RgbPacketFormatter::decreaseBrightness() {
+  command(RGB_BRIGHTNESS_DOWN, 0);
+}
+
+void RgbPacketFormatter::format(uint8_t const* packet, char* buffer) {
+  buffer += sprintf(buffer, "b0       : %02X\n", packet[0]);
+  buffer += sprintf(buffer, "ID       : %02X%02X\n", packet[1], packet[2]);
+  buffer += sprintf(buffer, "Color    : %02X\n", packet[3]);
+  buffer += sprintf(buffer, "Command  : %02X\n", packet[4]);
+  buffer += sprintf(buffer, "Sequence : %02X\n", packet[5]);
+}

+ 37 - 0
lib/MiLight/RgbPacketFormatter.h

@@ -0,0 +1,37 @@
+#include <PacketFormatter.h>
+
+#ifndef _RGB_PACKET_FORMATTER_H
+#define _RGB_PACKET_FORMATTER_H 
+
+#define RGB_COMMAND_INDEX 4
+#define RGB_COLOR_INDEX 3
+
+enum MiLightRgbButton {
+  RGB_OFF             = 0x01,
+  RGB_ON              = 0x02,
+  RGB_BRIGHTNESS_UP   = 0x03,
+  RGB_BRIGHTNESS_DOWN = 0x04,
+  RGB_SPEED_UP        = 0x05,
+  RGB_SPEED_DOWN      = 0x06,
+  RGB_MODE_UP         = 0x07,
+  RGB_MODE_DOWN       = 0x08
+};
+
+class RgbPacketFormatter : public PacketFormatter {
+public:
+  RgbPacketFormatter()
+    : PacketFormatter(6)
+  { }
+  
+  virtual void updateStatus(MiLightStatus status, uint8_t groupId);
+  virtual void increaseBrightness();
+  virtual void decreaseBrightness();
+  virtual void command(uint8_t command, uint8_t arg);
+  virtual void updateHue(uint16_t value);
+  virtual void updateColorRaw(uint8_t value);
+  virtual void format(uint8_t const* packet, char* buffer);
+  
+  virtual void reset();
+};
+
+#endif

+ 30 - 18
web/index.html

@@ -97,6 +97,10 @@
         alert("Please enter a device ID.");
         throw "Must enter device ID";
       }
+      
+      if (! $('#group-option').data('for').split(',').includes(mode)) {
+        groupId = 0;
+      }
         
       return "/gateways/" + deviceId + "/" + mode + "/" + groupId;
     }
@@ -430,24 +434,26 @@
       </div>
       
       <div class="col-sm-3">
-        <label for="groupId">Group</label>
+        <div class="mode-option" id="group-option" data-for="cct,rgbw,rgb_cct">
+          <label for="groupId">Group</label>
         
-        <div class="btn-group" id="groupId" data-toggle="buttons">
-          <label class="btn btn-secondary active">
-            <input type="radio" name="options" autocomplete="off" data-value="1" checked> 1
-          </label>
-          <label class="btn btn-secondary">
-            <input type="radio" name="options" autocomplete="off" data-value="2"> 2
-          </label>
-          <label class="btn btn-secondary">
-            <input type="radio" name="options" autocomplete="off" data-value="3"> 3
-          </label>
-          <label class="btn btn-secondary">
-            <input type="radio" name="options" autocomplete="off" data-value="4"> 4
-          </label>
-          <label class="btn btn-secondary">
-            <input type="radio" name="options" autocomplete="off" data-value="0"> All
-          </label>
+          <div class="btn-group" id="groupId" data-toggle="buttons">
+            <label class="btn btn-secondary active">
+              <input type="radio" name="options" autocomplete="off" data-value="1" checked> 1
+            </label>
+            <label class="btn btn-secondary">
+              <input type="radio" name="options" autocomplete="off" data-value="2"> 2
+            </label>
+            <label class="btn btn-secondary">
+              <input type="radio" name="options" autocomplete="off" data-value="3"> 3
+            </label>
+            <label class="btn btn-secondary">
+              <input type="radio" name="options" autocomplete="off" data-value="4"> 4
+            </label>
+            <label class="btn btn-secondary">
+              <input type="radio" name="options" autocomplete="off" data-value="0"> All
+            </label>
+          </div>
         </div>
       </div>
       
@@ -464,12 +470,15 @@
           <label class="btn btn-secondary">
             <input type="radio" name="mode" autocomplete="off" data-value="rgb_cct"> RGB+CCT
           </label>
+          <label class="btn btn-secondary">
+            <input type="radio" name="mode" autocomplete="off" data-value="rgb"> RGB
+          </label>
         </div>
       </div>
     </div>
     
     <div class="row"><div class="col-sm-12">
-    <div class="mode-option" data-for="rgbw,rgb_cct">
+    <div class="mode-option" data-for="rgbw,rgb_cct,rgb">
       <div class="row">
         <div class="col-sm-12">
           <h5>Hue</h5>
@@ -642,6 +651,9 @@
           <label class="btn btn-secondary">
             <input type="radio" name="options" autocomplete="off" data-value="rgb_cct"> RGB+CCT
           </label>
+          <label class="btn btn-secondary">
+            <input type="radio" name="options" autocomplete="off" data-value="rgb"> RGB
+          </label>
         </div>
         
         <div> &nbsp; </div>