Commit 74194aad8d3a976bb546d4696ab56b7dbcd68a78

Authored by Adam Honse
Committed by Moritz Wirger
1 parent 3321666f

Address review comments for previous commit

include/hueplusplus/EntertainmentMode.h
... ... @@ -50,7 +50,7 @@ public:
50 50 bool Connect();
51 51 bool Disconnect();
52 52  
53   - bool SetColorRGB(unsigned int light_index, unsigned char red, unsigned char green, unsigned char blue);
  53 + bool SetColorRGB(uint8_t light_index, uint8_t red, uint8_t green, uint8_t blue);
54 54  
55 55 void Update();
56 56  
... ... @@ -58,9 +58,8 @@ protected:
58 58 Bridge& bridge;
59 59 Group& group;
60 60  
61   - unsigned char* entertainment_msg; //!< buffer containing the entertainment mode packet data
62   - unsigned int entertainment_msg_size; //!< size of the entertainment mode buffer
63   - unsigned int entertainment_num_lights; //!< number of lights in entertainment mode group
  61 + std::vector<uint8_t> entertainment_msg; //!< buffer containing the entertainment mode packet data
  62 + uint8_t entertainment_num_lights; //!< number of lights in entertainment mode group
64 63  
65 64 mbedtls_ssl_context ssl;
66 65 mbedtls_net_context server_fd;
... ...
src/EntertainmentMode.cpp
... ... @@ -50,15 +50,14 @@ EntertainmentMode::EntertainmentMode(Bridge&amp; bridge, Group&amp; group):bridge(bridge
50 50 entertainment_num_lights = group.getLightIds().size();
51 51  
52 52 /*-------------------------------------------------*\
53   - | Create Entertainment Mode message buffer |
  53 + | Resize Entertainment Mode message buffer |
54 54 \*-------------------------------------------------*/
55   - entertainment_msg_size = HUE_ENTERTAINMENT_HEADER_SIZE + (entertainment_num_lights * HUE_ENTERTAINMENT_LIGHT_SIZE);
56   - entertainment_msg = new unsigned char[entertainment_msg_size];
  55 + entertainment_msg.resize(HUE_ENTERTAINMENT_HEADER_SIZE + (entertainment_num_lights * HUE_ENTERTAINMENT_LIGHT_SIZE));
57 56  
58 57 /*-------------------------------------------------*\
59 58 | Fill in Entertainment Mode message header |
60 59 \*-------------------------------------------------*/
61   - memcpy(entertainment_msg, "HueStream", 9);
  60 + memcpy(&entertainment_msg[0], "HueStream", 9);
62 61 entertainment_msg[9] = 0x01; // Version Major (1)
63 62 entertainment_msg[10] = 0x00; // Version Minor (0)
64 63 entertainment_msg[11] = 0x00; // Sequence ID
... ... @@ -149,22 +148,45 @@ EntertainmentMode::EntertainmentMode(Bridge&amp; bridge, Group&amp; group):bridge(bridge
149 148 } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
150 149 }
151 150  
152   -bool EntertainmentMode::SetColorRGB(unsigned int light_index, unsigned char red, unsigned char green, unsigned char blue)
  151 +bool EntertainmentMode::SetColorRGB(uint8_t light_index, uint8_t red, uint8_t green, uint8_t blue)
153 152 {
154   - unsigned int msg_idx = HUE_ENTERTAINMENT_HEADER_SIZE + (light_index * HUE_ENTERTAINMENT_LIGHT_SIZE);
  153 + if(light_index < entertainment_num_lights)
  154 + {
  155 + unsigned int msg_idx = HUE_ENTERTAINMENT_HEADER_SIZE + (light_index * HUE_ENTERTAINMENT_LIGHT_SIZE);
155 156  
156   - entertainment_msg[msg_idx + 3] = red; // Red MSB
157   - entertainment_msg[msg_idx + 4] = red; // Red LSB;
158   - entertainment_msg[msg_idx + 5] = green; // Green MSB;
159   - entertainment_msg[msg_idx + 6] = green; // Green LSB;
160   - entertainment_msg[msg_idx + 7] = blue; // Blue MSB;
161   - entertainment_msg[msg_idx + 8] = blue; // Blue LSB;
  157 + entertainment_msg[msg_idx + 3] = red; // Red MSB
  158 + entertainment_msg[msg_idx + 4] = red; // Red LSB;
  159 + entertainment_msg[msg_idx + 5] = green; // Green MSB;
  160 + entertainment_msg[msg_idx + 6] = green; // Green LSB;
  161 + entertainment_msg[msg_idx + 7] = blue; // Blue MSB;
  162 + entertainment_msg[msg_idx + 8] = blue; // Blue LSB;
162 163  
163   - return false;
  164 + return true;
  165 + }
  166 + else
  167 + {
  168 + return false;
  169 + }
164 170 }
165 171  
166 172 void EntertainmentMode::Update()
167 173 {
168   - mbedtls_ssl_write(&ssl, (const unsigned char*)entertainment_msg, entertainment_msg_size);
  174 + int ret;
  175 + unsigned int total = 0;
  176 +
  177 + while(total < entertainment_msg.size())
  178 + {
  179 + ret = mbedtls_ssl_write(&ssl, (const unsigned char*)&entertainment_msg[total], entertainment_msg.size());
  180 +
  181 + if(ret < 0)
  182 + {
  183 + // Return if mbedtls_ssl_write errors
  184 + return;
  185 + }
  186 + else
  187 + {
  188 + total += ret;
  189 + }
  190 + }
169 191 }
170 192 } // namespace hueplusplus
... ...