|
| 1 | +/* |
| 2 | + * Example05_WS2812_Neopixel.ino |
| 3 | + * |
| 4 | + * Created on: 2020-10-01 |
| 5 | + * Author: Juergen Fink |
| 6 | + * Thanks to all the other helpful people commenting here. |
| 7 | + * |
| 8 | + * This example allows to change brightness and color of a connected neopixel strip/matrix |
| 9 | + * |
| 10 | + * You should: |
| 11 | + * 1. read and use the Example01_TemperatureSensor with detailed comments |
| 12 | + * to know the basic concept and usage of this library before other examples。 |
| 13 | + * 2. erase the full flash or call homekit_storage_reset() in setup() |
| 14 | + * to remove the previous HomeKit pairing storage and |
| 15 | + * enable the pairing with the new accessory of this new HomeKit example. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +#include <Arduino.h> |
| 20 | +#include <arduino_homekit_server.h> |
| 21 | +#include <Adafruit_NeoPixel.h> |
| 22 | +#include "wifi_info.h" |
| 23 | + |
| 24 | +#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); |
| 25 | + |
| 26 | +#define NEOPIN D4 |
| 27 | +#define NUMPIXELS 64 |
| 28 | +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800); |
| 29 | + |
| 30 | +bool received_sat = false; |
| 31 | +bool received_hue = false; |
| 32 | + |
| 33 | +bool is_on = false; |
| 34 | +float current_brightness = 100.0; |
| 35 | +float current_sat = 0.0; |
| 36 | +float current_hue = 0.0; |
| 37 | +int rgb_colors[3]; |
| 38 | + |
| 39 | +void setup() { |
| 40 | + Serial.begin(115200); |
| 41 | + wifi_connect(); // in wifi_info.h |
| 42 | + |
| 43 | + pixels.begin(); |
| 44 | + for(int i = 0; i < NUMPIXELS; i++) |
| 45 | + { |
| 46 | + pixels.setPixelColor(i, 0, 0, 0); |
| 47 | + } |
| 48 | + pixels.show(); |
| 49 | + delay(1000); |
| 50 | + rgb_colors[0] = 255; |
| 51 | + rgb_colors[1] = 255; |
| 52 | + rgb_colors[2] = 255; |
| 53 | + my_homekit_setup(); |
| 54 | +} |
| 55 | + |
| 56 | +void loop() { |
| 57 | + my_homekit_loop(); |
| 58 | + delay(10); |
| 59 | +} |
| 60 | + |
| 61 | +//============================== |
| 62 | +// HomeKit setup and loop |
| 63 | +//============================== |
| 64 | + |
| 65 | +// access your HomeKit characteristics defined in my_accessory.c |
| 66 | + |
| 67 | +extern "C" homekit_server_config_t accessory_config; |
| 68 | +extern "C" homekit_characteristic_t cha_on; |
| 69 | +extern "C" homekit_characteristic_t cha_bright; |
| 70 | +extern "C" homekit_characteristic_t cha_sat; |
| 71 | +extern "C" homekit_characteristic_t cha_hue; |
| 72 | + |
| 73 | +static uint32_t next_heap_millis = 0; |
| 74 | + |
| 75 | +void my_homekit_setup() { |
| 76 | + |
| 77 | + cha_on.setter = set_on; |
| 78 | + cha_bright.setter = set_bright; |
| 79 | + cha_sat.setter = set_sat; |
| 80 | + cha_hue.setter = set_hue; |
| 81 | + |
| 82 | + arduino_homekit_setup(&accessory_config); |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +void my_homekit_loop() { |
| 87 | + arduino_homekit_loop(); |
| 88 | + const uint32_t t = millis(); |
| 89 | + if (t > next_heap_millis) { |
| 90 | + // show heap info every 5 seconds |
| 91 | + next_heap_millis = t + 5 * 1000; |
| 92 | + LOG_D("Free heap: %d, HomeKit clients: %d", |
| 93 | + ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); |
| 94 | + |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void set_on(const homekit_value_t v) { |
| 99 | + bool on = v.bool_value; |
| 100 | + cha_on.value.bool_value = on; //sync the value |
| 101 | + |
| 102 | + if(on) { |
| 103 | + is_on = true; |
| 104 | + Serial.println("On"); |
| 105 | + } else { |
| 106 | + is_on = false; |
| 107 | + Serial.println("Off"); |
| 108 | + } |
| 109 | + |
| 110 | + updateColor(); |
| 111 | +} |
| 112 | + |
| 113 | +void set_hue(const homekit_value_t v) { |
| 114 | + Serial.println("set_hue"); |
| 115 | + float hue = v.float_value; |
| 116 | + cha_hue.value.float_value = hue; //sync the value |
| 117 | + |
| 118 | + current_hue = hue; |
| 119 | + received_hue = true; |
| 120 | + |
| 121 | + updateColor(); |
| 122 | +} |
| 123 | + |
| 124 | +void set_sat(const homekit_value_t v) { |
| 125 | + Serial.println("set_sat"); |
| 126 | + float sat = v.float_value; |
| 127 | + cha_sat.value.float_value = sat; //sync the value |
| 128 | + |
| 129 | + current_sat = sat; |
| 130 | + received_sat = true; |
| 131 | + |
| 132 | + updateColor(); |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +void set_bright(const homekit_value_t v) { |
| 137 | + Serial.println("set_bright"); |
| 138 | + int bright = v.int_value; |
| 139 | + cha_bright.value.int_value = bright; //sync the value |
| 140 | + |
| 141 | + current_brightness = bright; |
| 142 | + |
| 143 | + updateColor(); |
| 144 | +} |
| 145 | + |
| 146 | +void updateColor() |
| 147 | +{ |
| 148 | + if(is_on) |
| 149 | + { |
| 150 | + |
| 151 | + if(received_hue && received_sat) |
| 152 | + { |
| 153 | + HSV2RGB(current_hue, current_sat, current_brightness); |
| 154 | + received_hue = false; |
| 155 | + received_sat = false; |
| 156 | + } |
| 157 | + |
| 158 | + int b = map(current_brightness,0, 100,75, 255); |
| 159 | + Serial.println(b); |
| 160 | + |
| 161 | + pixels.setBrightness(b); |
| 162 | + for(int i = 0; i < NUMPIXELS; i++) |
| 163 | + { |
| 164 | + |
| 165 | + pixels.setPixelColor(i, pixels.Color(rgb_colors[0],rgb_colors[1],rgb_colors[2])); |
| 166 | + |
| 167 | + } |
| 168 | + pixels.show(); |
| 169 | + |
| 170 | + } |
| 171 | + else if(!is_on) //lamp - switch to off |
| 172 | + { |
| 173 | + Serial.println("is_on == false"); |
| 174 | + pixels.setBrightness(0); |
| 175 | + for(int i = 0; i < NUMPIXELS; i++) |
| 176 | + { |
| 177 | + pixels.setPixelColor(i, pixels.Color(0,0,0)); |
| 178 | + } |
| 179 | + pixels.show(); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +void HSV2RGB(float h,float s,float v) { |
| 184 | + |
| 185 | + int i; |
| 186 | + float m, n, f; |
| 187 | + |
| 188 | + s/=100; |
| 189 | + v/=100; |
| 190 | + |
| 191 | + if(s==0){ |
| 192 | + rgb_colors[0]=rgb_colors[1]=rgb_colors[2]=round(v*255); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + h/=60; |
| 197 | + i=floor(h); |
| 198 | + f=h-i; |
| 199 | + |
| 200 | + if(!(i&1)){ |
| 201 | + f=1-f; |
| 202 | + } |
| 203 | + |
| 204 | + m=v*(1-s); |
| 205 | + n=v*(1-s*f); |
| 206 | + |
| 207 | + switch (i) { |
| 208 | + |
| 209 | + case 0: case 6: |
| 210 | + rgb_colors[0]=round(v*255); |
| 211 | + rgb_colors[1]=round(n*255); |
| 212 | + rgb_colors[2]=round(m*255); |
| 213 | + break; |
| 214 | + |
| 215 | + case 1: |
| 216 | + rgb_colors[0]=round(n*255); |
| 217 | + rgb_colors[1]=round(v*255); |
| 218 | + rgb_colors[2]=round(m*255); |
| 219 | + break; |
| 220 | + |
| 221 | + case 2: |
| 222 | + rgb_colors[0]=round(m*255); |
| 223 | + rgb_colors[1]=round(v*255); |
| 224 | + rgb_colors[2]=round(n*255); |
| 225 | + break; |
| 226 | + |
| 227 | + case 3: |
| 228 | + rgb_colors[0]=round(m*255); |
| 229 | + rgb_colors[1]=round(n*255); |
| 230 | + rgb_colors[2]=round(v*255); |
| 231 | + break; |
| 232 | + |
| 233 | + case 4: |
| 234 | + rgb_colors[0]=round(n*255); |
| 235 | + rgb_colors[1]=round(m*255); |
| 236 | + rgb_colors[2]=round(v*255); |
| 237 | + break; |
| 238 | + |
| 239 | + case 5: |
| 240 | + rgb_colors[0]=round(v*255); |
| 241 | + rgb_colors[1]=round(m*255); |
| 242 | + rgb_colors[2]=round(n*255); |
| 243 | + break; |
| 244 | + } |
| 245 | +} |
0 commit comments