From 3d9c446907b891016cc88991b62b5c3cc86263e3 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 24 Jan 2018 13:08:58 +0100 Subject: [PATCH] Fix build and add fans --- .travis.yml | 1 + README.md | 6 ++++++ examples/fan-example.cpp | 38 ++++++++++++++++++++++++++++++++++ lib/readme.txt | 36 ++++++++++++++++++++++++++++++++ platformio.ini | 19 ++++++++++++----- src/esphomelib/application.cpp | 18 +++++++++++++++- src/esphomelib/application.h | 21 +++++++++++++++++++ 7 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 examples/fan-example.cpp create mode 100644 lib/readme.txt diff --git a/.travis.yml b/.travis.yml index c8b9c7f8..ed953529 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ script: - platformio run -e livingroom - platformio run -e dht-dallas-sensors - platformio run -e switch-binarysensor + - platformio run -e fan-example # diff --git a/README.md b/README.md index de64c52f..6bc0c92c 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,11 @@ and the GPIO pin like this: app.make_simple_gpio_binary_sensor("Cabinet Motion", binary_sensor::device_class::MOTION, 36); ``` +#### Fan + +Fans can be created by first calling `app.make_fan("Friendly Name")` and then using the return value +to set the output channels. See [`examples/fan-example.cpp`](examples/fan-example.cpp) for an example. + ## Current Features * Powerful core that allows for easy creation of new, custom components @@ -266,6 +271,7 @@ app.make_simple_gpio_binary_sensor("Cabinet Motion", binary_sensor::device_class * Refine default options * Pulse Counter * Status LED +* Covers * **Suggestions?** Feel free to create an issue and tag it with feature-request. ## Advanced Options diff --git a/examples/fan-example.cpp b/examples/fan-example.cpp new file mode 100644 index 00000000..30b223b1 --- /dev/null +++ b/examples/fan-example.cpp @@ -0,0 +1,38 @@ +// +// Created by Otto Winter on 24.01.18. +// + +#include + +using namespace esphomelib; + +Application app; + +void setup() { + app.set_name("livingroom-fan"); + app.init_log(); + + app.init_wifi("YOUR_SSID", "YOUR_PASSWORD"); + app.init_mqtt("MQTT_HOST", "USERNAME", "PASSWORD"); + app.init_ota(); + + auto binary_fan = app.make_fan("Binary Fan"); + binary_fan.output->set_binary(app.make_gpio_binary_output(32)); + + auto speed_fan = app.make_fan("Speed Fan"); + // 0.0 -> off speed + // 0.33 -> low speed + // 0.66 -> medium speed + // 1.0 -> high speed + speed_fan.output->set_speed(app.make_ledc_component(33), 0.0, 0.33, 0.66, 1.0); + + auto oscillating_fan = app.make_fan("Oscillating Fan"); + oscillating_fan.output->set_binary(app.make_gpio_binary_output(34)); + oscillating_fan.output->set_oscillation(app.make_gpio_binary_output(35)); + + app.setup(); +} + +void loop() { + app.loop(); +} diff --git a/lib/readme.txt b/lib/readme.txt new file mode 100644 index 00000000..dbadc3d6 --- /dev/null +++ b/lib/readme.txt @@ -0,0 +1,36 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini index 3a1dd1b6..a220fe33 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,8 +10,8 @@ [platformio] env_default = livingroom -lib_dir = . -src_dir = examples +src_dir = . +include_dir = src [common] lib_deps = @@ -26,6 +26,7 @@ build_flags = -DMQTT_MAX_PACKET_SIZE=512 -DCONFIG_ARDUHAL_LOG_COLORS=1 -DCORE_DEBUG_LEVEL=5 +src_filter = + [env:livingroom] platform = espressif32 @@ -33,7 +34,7 @@ board = nodemcu-32s framework = arduino lib_deps = ${common.lib_deps} build_flags = ${common.build_flags} -src_filter = + +src_filter = ${common.src_filter} + [env:dht-dallas-sensors] platform = espressif32 @@ -41,7 +42,7 @@ board = nodemcu-32s framework = arduino lib_deps = ${common.lib_deps} build_flags = ${common.build_flags} -src_filter = + +src_filter = ${common.src_filter} + [env:switch-binarysensor] platform = espressif32 @@ -49,4 +50,12 @@ board = nodemcu-32s framework = arduino lib_deps = ${common.lib_deps} build_flags = ${common.build_flags} -src_filter = + +src_filter = ${common.src_filter} + + +[env:fan-example] +platform = espressif32 +board = nodemcu-32s +framework = arduino +lib_deps = ${common.lib_deps} +build_flags = ${common.build_flags} +src_filter = ${common.src_filter} + diff --git a/src/esphomelib/application.cpp b/src/esphomelib/application.cpp index 0b871a41..4c6eeb81 100644 --- a/src/esphomelib/application.cpp +++ b/src/esphomelib/application.cpp @@ -18,6 +18,7 @@ using namespace esphomelib::binary_sensor; using namespace esphomelib::sensor; using namespace esphomelib::output; using namespace esphomelib::light; +using namespace esphomelib::fan; using namespace esphomelib::switch_platform; static const char *TAG = "app"; @@ -258,7 +259,7 @@ input::DallasComponent *Application::make_dallas_component(uint8_t pin) { Application::SimpleGPIOSwitchStruct Application::make_simple_gpio_switch(uint8_t pin, const std::string &friendly_name) { - auto *binary_output = this->register_component(new GPIOBinaryOutputComponent(pin)); + auto *binary_output = this->make_gpio_binary_output(pin); auto *simple_switch = new SimpleSwitch(binary_output); auto *mqtt = this->make_mqtt_switch_for(friendly_name, simple_switch); @@ -272,6 +273,21 @@ const std::string &Application::get_name() const { return this->name_; } + +Application::FanStruct Application::make_fan(const std::string &friendly_name) { + FanStruct s{}; + s.state = new FanState(); + s.mqtt = this->register_mqtt_component(new MQTTFanComponent(friendly_name)); + s.output = this->register_component(new BasicFanComponent()); + s.mqtt->set_state(s.state); + s.output->set_state(s.state); + return s; +} + +output::GPIOBinaryOutputComponent *Application::make_gpio_binary_output(uint8_t pin, uint8_t mode) { + return this->register_component(new GPIOBinaryOutputComponent(pin, mode)); +} + Application::Application() { global_application = this; } diff --git a/src/esphomelib/application.h b/src/esphomelib/application.h index 68b77838..62a8e426 100644 --- a/src/esphomelib/application.h +++ b/src/esphomelib/application.h @@ -21,6 +21,9 @@ #include #include #include +#include +#include +#include #include "component.h" #include "esphomelib/mqtt/mqtt_client_component.h" #include "wifi_component.h" @@ -277,6 +280,24 @@ class Application { switch_platform::MQTTSwitchComponent *make_mqtt_switch_for(const std::string &friendly_name, switch_platform::Switch *switch_); + // ======================= FAN ======================= + + struct FanStruct { + fan::BasicFanComponent *output; + fan::FanState *state; + fan::MQTTFanComponent *mqtt; + }; + + /// Create a GPIO binary output component on the specified pin and pinMode. + output::GPIOBinaryOutputComponent *make_gpio_binary_output(uint8_t pin, uint8_t mode = OUTPUT); + + /** Create and connect a Fan with the specified friendly name. + * + * @param friendly_name The friendly name of the Fan to advertise. + * @return A FanStruct, use the output field to set your output channels. + */ + FanStruct make_fan(const std::string &friendly_name); + // ======================= FUNCTIONS ======================= template