diff options
Diffstat (limited to 'mysensors/rgbtv_light')
| -rw-r--r-- | mysensors/rgbtv_light/.gitignore | 5 | ||||
| -rw-r--r-- | mysensors/rgbtv_light/platformio.ini | 26 | ||||
| -rw-r--r-- | mysensors/rgbtv_light/src/main.cpp | 241 |
3 files changed, 272 insertions, 0 deletions
diff --git a/mysensors/rgbtv_light/.gitignore b/mysensors/rgbtv_light/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/mysensors/rgbtv_light/.gitignore | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | .pio | ||
| 2 | .vscode/.browse.c_cpp.db* | ||
| 3 | .vscode/c_cpp_properties.json | ||
| 4 | .vscode/launch.json | ||
| 5 | .vscode/ipch | ||
diff --git a/mysensors/rgbtv_light/platformio.ini b/mysensors/rgbtv_light/platformio.ini new file mode 100644 index 0000000..d4ceb9b --- /dev/null +++ b/mysensors/rgbtv_light/platformio.ini | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | ; PlatformIO Project Configuration File | ||
| 2 | ; | ||
| 3 | ; Build options: build flags, source filter, extra scripting | ||
| 4 | ; Upload options: custom port, speed and extra flags | ||
| 5 | ; Library options: dependencies, extra library storages | ||
| 6 | ; | ||
| 7 | ; Please visit documentation for the other options and examples | ||
| 8 | ; http://docs.platformio.org/en/stable/projectconf.html | ||
| 9 | |||
| 10 | [platformio] | ||
| 11 | default_envs = pro8MHzatmega328 | ||
| 12 | |||
| 13 | [env:miniatmega328] | ||
| 14 | platform=atmelavr | ||
| 15 | board=miniatmega328 | ||
| 16 | framework=arduino | ||
| 17 | |||
| 18 | [env:pro8MHzatmega328] | ||
| 19 | platform=atmelavr | ||
| 20 | board=pro8MHzatmega328 | ||
| 21 | framework=arduino | ||
| 22 | ; minicore/optirun uses 38400 for 8MHz | ||
| 23 | upload_speed=38400 | ||
| 24 | |||
| 25 | [platformio] | ||
| 26 | lib_dir=/home/manuel/coding/Arduino/libraries | ||
diff --git a/mysensors/rgbtv_light/src/main.cpp b/mysensors/rgbtv_light/src/main.cpp new file mode 100644 index 0000000..98120a6 --- /dev/null +++ b/mysensors/rgbtv_light/src/main.cpp | |||
| @@ -0,0 +1,241 @@ | |||
| 1 | /** | ||
| 2 | * The MySensors Arduino library handles the wireless radio link and protocol | ||
| 3 | * between your home built sensors/actuators and HA controller of choice. | ||
| 4 | * The sensors forms a self healing radio network with optional repeaters. Each | ||
| 5 | * repeater and gateway builds a routing tables in EEPROM which keeps track of the | ||
| 6 | * network topology allowing messages to be routed to nodes. | ||
| 7 | */ | ||
| 8 | |||
| 9 | // Enable debug prints to serial monitor | ||
| 10 | #define MY_DEBUG | ||
| 11 | |||
| 12 | // configure radio | ||
| 13 | #define MY_RADIO_RFM69 | ||
| 14 | |||
| 15 | /** @brief RFM69 frequency to use (RF69_433MHZ for 433MHz, RF69_868MHZ for 868MHz or RF69_915MHZ for 915MHz). */ | ||
| 16 | #define MY_RFM69_FREQUENCY RF69_868MHZ | ||
| 17 | |||
| 18 | /** @brief Enable this if you're running the RFM69HW model. */ | ||
| 19 | //#define MY_IS_RFM69HW | ||
| 20 | |||
| 21 | /** @brief RFM69 Network ID. Use the same for all nodes that will talk to each other. */ | ||
| 22 | #define MY_RFM69_NETWORKID 1 | ||
| 23 | |||
| 24 | /** @brief Node id defaults to AUTO (tries to fetch id from controller). */ | ||
| 25 | #define MY_NODE_ID 3 | ||
| 26 | |||
| 27 | /** @brief If set, transport traffic is unmonitored and GW connection is optional */ | ||
| 28 | #define MY_TRANSPORT_DONT_CARE_MODE | ||
| 29 | |||
| 30 | /** @brief Node parent defaults to AUTO (tries to find a parent automatically). */ | ||
| 31 | #define MY_PARENT_NODE_ID 0 | ||
| 32 | |||
| 33 | /** @brief The user-defined AES key to use for EEPROM personalization */ | ||
| 34 | #include "aes_key.h" | ||
| 35 | |||
| 36 | // Enable repeater functionality for this node | ||
| 37 | //#define MY_REPEATER_FEATURE | ||
| 38 | |||
| 39 | /** @brief Enables RFM69 automatic transmit power control class. */ | ||
| 40 | //#define MY_RFM69_ATC | ||
| 41 | |||
| 42 | #ifdef MY_AES_KEY | ||
| 43 | /** @brief enables RFM69 encryption */ | ||
| 44 | #define MY_RFM69_ENABLE_ENCRYPTION | ||
| 45 | #endif | ||
| 46 | |||
| 47 | #include <Arduino.h> | ||
| 48 | #include <MySensors.h> | ||
| 49 | #include <FastLED.h> | ||
| 50 | |||
| 51 | #define RELAY_1_PIN 4 // pin number of first relay (second on pin+1 etc) | ||
| 52 | #define NUMBER_OF_RELAYS 1 // Total number of attached relays | ||
| 53 | #define RELAY_ON 1 // GPIO value to write to turn on attached relay | ||
| 54 | #define RELAY_OFF 0 // GPIO value to write to turn off attached relay | ||
| 55 | |||
| 56 | #define RGB_PIN 7 | ||
| 57 | #define NUM_LEDS 30 | ||
| 58 | #define RGB_CHIPSET WS2812B | ||
| 59 | #define RGB_COLOR_ORDER GRB | ||
| 60 | #define RGB_CHILD_ID 0 | ||
| 61 | |||
| 62 | #define TEMP_READ_INTERVAL 1000L // read temp every 1 sec | ||
| 63 | #define TEMP_N_READS_MSG 60*60 // force temp message every n reads | ||
| 64 | #define TEMP_OFFSET 0 | ||
| 65 | #define TEMP_CHILD_ID 254 | ||
| 66 | |||
| 67 | MyMessage msgRGB(RGB_CHILD_ID, 0); | ||
| 68 | static uint8_t brightness = 128; | ||
| 69 | |||
| 70 | MyMessage msgRelais(0, V_STATUS); | ||
| 71 | |||
| 72 | unsigned long lastTempUpdate = millis(); | ||
| 73 | unsigned int numTempUpdates = 0; | ||
| 74 | float lastTemp = 0; | ||
| 75 | MyMessage msgTemp(TEMP_CHILD_ID, V_TEMP); | ||
| 76 | |||
| 77 | CRGB leds[NUM_LEDS]; | ||
| 78 | |||
| 79 | void changeRelay(uint8_t relay, uint8_t val, bool send_update=false); | ||
| 80 | |||
| 81 | void before() | ||
| 82 | { | ||
| 83 | // set relay pins to output mode + restore to last known state | ||
| 84 | for (uint8_t relay = 0; relay < NUMBER_OF_RELAYS; relay++) | ||
| 85 | { | ||
| 86 | pinMode(relay + RELAY_1_PIN, OUTPUT); | ||
| 87 | digitalWrite(relay + RELAY_1_PIN, loadState(relay) ? RELAY_ON : RELAY_OFF); | ||
| 88 | } | ||
| 89 | |||
| 90 | #ifdef MY_AES_KEY | ||
| 91 | const uint8_t user_aes_key[16] = { MY_AES_KEY }; | ||
| 92 | uint8_t cur_aes_key[16]; | ||
| 93 | hwReadConfigBlock((void*)&cur_aes_key, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, sizeof(cur_aes_key)); | ||
| 94 | if (memcmp(&user_aes_key, &cur_aes_key, 16) != 0) | ||
| 95 | { | ||
| 96 | hwWriteConfigBlock((void*)user_aes_key, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, sizeof(user_aes_key)); | ||
| 97 | debug(PSTR("AES key written\n")); | ||
| 98 | } | ||
| 99 | #endif | ||
| 100 | } | ||
| 101 | |||
| 102 | void setup() | ||
| 103 | { | ||
| 104 | #ifdef MY_RFM69_ATC | ||
| 105 | _radio.enableAutoPower(-70); | ||
| 106 | debug(PSTR("ATC enabled\n")); | ||
| 107 | #endif | ||
| 108 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, NUM_LEDS); | ||
| 109 | //TODO restore mode(static/ambilight)/color/brightness from flash? | ||
| 110 | FastLED.setBrightness(brightness); | ||
| 111 | } | ||
| 112 | |||
| 113 | void presentation() | ||
| 114 | { | ||
| 115 | // Send the sketch version information to the gateway and Controller | ||
| 116 | sendSketchInfo("Ambilight", "1.0"); | ||
| 117 | |||
| 118 | // Register all sensors to gw (they will be created as child devices) | ||
| 119 | present(0, S_RGB_LIGHT, "ambilight"); | ||
| 120 | #if 0 | ||
| 121 | for (uint8_t relay = 0; relay < NUMBER_OF_RELAYS; relay++) | ||
| 122 | present(relay + 1, S_BINARY); | ||
| 123 | present(TEMP_CHILD_ID, S_TEMP); | ||
| 124 | #endif | ||
| 125 | |||
| 126 | delay(3000); | ||
| 127 | send(msgRGB.setType(V_STATUS).set(1)); | ||
| 128 | delay(500); | ||
| 129 | send(msgRGB.setType(V_DIMMER).set(FastLED.getBrightness())); | ||
| 130 | delay(500); | ||
| 131 | send(msgRGB.setType(V_RGB).set("ffffff")); | ||
| 132 | FastLED.show(); | ||
| 133 | } | ||
| 134 | |||
| 135 | void loop() | ||
| 136 | { | ||
| 137 | //TODO maybe call _radio.rcCalibration() all 1000x changes? | ||
| 138 | |||
| 139 | //FastLED.show(); | ||
| 140 | //FastLED.delay(8); | ||
| 141 | |||
| 142 | #if 0 | ||
| 143 | // check temperature | ||
| 144 | unsigned long now = millis(); | ||
| 145 | if (now - lastTempUpdate > TEMP_READ_INTERVAL) | ||
| 146 | { | ||
| 147 | float temp = _radio.readTemperature() + TEMP_OFFSET; | ||
| 148 | lastTempUpdate = now; | ||
| 149 | if (isnan(temp)) | ||
| 150 | Serial.println("Failed reading temperature"); | ||
| 151 | else if (abs(temp - lastTemp) >= 2 || numTempUpdates == TEMP_N_READS_MSG) | ||
| 152 | { | ||
| 153 | lastTemp = temp; | ||
| 154 | numTempUpdates = 0; | ||
| 155 | send(msgTemp.set(temp, 2)); | ||
| 156 | #ifdef MY_DEBUG | ||
| 157 | char str_temp[6]; | ||
| 158 | dtostrf(temp, 4, 2, str_temp); | ||
| 159 | debug(PSTR("Temperature: %s °C\n"), str_temp); | ||
| 160 | #endif | ||
| 161 | } | ||
| 162 | else | ||
| 163 | ++numTempUpdates; | ||
| 164 | } | ||
| 165 | #endif | ||
| 166 | } | ||
| 167 | |||
| 168 | void receive(const MyMessage &message) | ||
| 169 | { | ||
| 170 | Serial.println(_radio.readRSSI()); | ||
| 171 | if (message.sensor == RGB_CHILD_ID) | ||
| 172 | { | ||
| 173 | if (mGetCommand(message) == C_SET) | ||
| 174 | { | ||
| 175 | if (message.type == V_STATUS) | ||
| 176 | { | ||
| 177 | bool val = message.getBool(); | ||
| 178 | // datatype=0, message=0/1 | ||
| 179 | Serial.println("light on/off"); | ||
| 180 | //TODO restore brightness. | ||
| 181 | } | ||
| 182 | else if (message.type == V_RGB && mGetLength(message) == 6) | ||
| 183 | { | ||
| 184 | uint32_t colorcode = strtol(message.getString(), NULL, 16); | ||
| 185 | fill_solid(leds, NUM_LEDS, CRGB(colorcode)); | ||
| 186 | FastLED.show(); | ||
| 187 | } | ||
| 188 | else if (message.type == V_PERCENTAGE) | ||
| 189 | { | ||
| 190 | //TODO fade? | ||
| 191 | uint8_t val = message.getByte(); | ||
| 192 | if (val < 0 || val > 100) | ||
| 193 | return; | ||
| 194 | Serial.print("dim: "); | ||
| 195 | Serial.println(val, DEC); | ||
| 196 | brightness = map(val, 0, 100, 0, 255); | ||
| 197 | Serial.println(brightness, DEC); | ||
| 198 | // datatype=0, message=1-100 | ||
| 199 | FastLED.setBrightness(brightness); | ||
| 200 | FastLED.show(); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | #if 0 | ||
| 206 | if (message.type == V_STATUS && message.sensor >= 1) | ||
| 207 | { | ||
| 208 | uint8_t relay = message.sensor - 1; | ||
| 209 | if (relay >= NUMBER_OF_RELAYS) | ||
| 210 | { | ||
| 211 | Serial.print("Invalid relay index:"); | ||
| 212 | Serial.println(relay); | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | if (mGetCommand(message) == C_REQ) | ||
| 217 | send(msg.setSensor(relay + 1).set(digitalRead(relay + RELAY_1_PIN))); | ||
| 218 | else if (mGetCommand(message) == C_SET) | ||
| 219 | changeRelay(relay, message.getBool() ? RELAY_ON : RELAY_OFF); | ||
| 220 | } | ||
| 221 | #endif | ||
| 222 | } | ||
| 223 | |||
| 224 | void changeRelay(uint8_t relay, uint8_t value, bool send_update) | ||
| 225 | { | ||
| 226 | if (relay >= NUMBER_OF_RELAYS) | ||
| 227 | return; | ||
| 228 | Serial.print("Incoming change for relay: "); | ||
| 229 | Serial.print(relay); | ||
| 230 | Serial.print(", New status: "); | ||
| 231 | Serial.println(value); | ||
| 232 | |||
| 233 | // change relay state + store state in eeprom | ||
| 234 | digitalWrite(relay + RELAY_1_PIN, value); | ||
| 235 | saveState(relay, value); | ||
| 236 | |||
| 237 | // send msg | ||
| 238 | if (send_update) | ||
| 239 | send(msgRelais.setSensor(relay + 1).set(value)); | ||
| 240 | } | ||
| 241 | |||
