From cdda648360d2e7a86c3d7677155c3f2707596740 Mon Sep 17 00:00:00 2001 From: Oxan van Leeuwen Date: Mon, 24 Jan 2022 10:34:34 +0100 Subject: [PATCH] Generate ARDUINO_VERSION_CODE in Python code (#3101) Co-authored-by: Otto winter --- esphome/components/esp32/__init__.py | 16 ++++++ esphome/components/esp8266/__init__.py | 7 ++- .../components/esp8266_pwm/esp8266_pwm.cpp | 5 +- esphome/components/esp8266_pwm/output.py | 23 ++++---- .../components/http_request/http_request.cpp | 8 +-- esphome/components/neopixelbus/light.py | 4 ++ .../neopixelbus/neopixelbus_light.h | 4 -- esphome/components/nextion/nextion_upload.cpp | 14 ++--- esphome/components/wifi/wifi_component.h | 3 +- .../wifi/wifi_component_esp8266.cpp | 12 ++--- esphome/core/defines.h | 8 +++ esphome/core/log.h | 7 +-- esphome/core/macros.h | 54 +------------------ esphome/writer.py | 1 + platformio.ini | 3 -- 15 files changed, 71 insertions(+), 98 deletions(-) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 8214886f8c..cec72e1b77 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -293,6 +293,8 @@ async def to_code(config): cg.add_platformio_option("lib_ldf_mode", "off") + framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] + conf = config[CONF_FRAMEWORK] cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION]) @@ -335,6 +337,13 @@ async def to_code(config): "CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE", False ) + cg.add_define( + "USE_ESP_IDF_VERSION_CODE", + cg.RawExpression( + f"VERSION_CODE({framework_ver.major}, {framework_ver.minor}, {framework_ver.patch})" + ), + ) + elif conf[CONF_TYPE] == FRAMEWORK_ARDUINO: cg.add_platformio_option("framework", "arduino") cg.add_build_flag("-DUSE_ARDUINO") @@ -346,6 +355,13 @@ async def to_code(config): cg.add_platformio_option("board_build.partitions", "partitions.csv") + cg.add_define( + "USE_ARDUINO_VERSION_CODE", + cg.RawExpression( + f"VERSION_CODE({framework_ver.major}, {framework_ver.minor}, {framework_ver.patch})" + ), + ) + ARDUINO_PARTITIONS_CSV = """\ nvs, data, nvs, 0x009000, 0x005000, diff --git a/esphome/components/esp8266/__init__.py b/esphome/components/esp8266/__init__.py index 7182042770..9b1f091432 100644 --- a/esphome/components/esp8266/__init__.py +++ b/esphome/components/esp8266/__init__.py @@ -198,10 +198,15 @@ async def to_code(config): cg.add_platformio_option("board_build.flash_mode", config[CONF_BOARD_FLASH_MODE]) + ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] + cg.add_define( + "USE_ARDUINO_VERSION_CODE", + cg.RawExpression(f"VERSION_CODE({ver.major}, {ver.minor}, {ver.patch})"), + ) + if config[CONF_BOARD] in ESP8266_FLASH_SIZES: flash_size = ESP8266_FLASH_SIZES[config[CONF_BOARD]] ld_scripts = ESP8266_LD_SCRIPTS[flash_size] - ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] if ver <= cv.Version(2, 3, 0): # No ld script support diff --git a/esphome/components/esp8266_pwm/esp8266_pwm.cpp b/esphome/components/esp8266_pwm/esp8266_pwm.cpp index e472edf2a7..8b3d8613b0 100644 --- a/esphome/components/esp8266_pwm/esp8266_pwm.cpp +++ b/esphome/components/esp8266_pwm/esp8266_pwm.cpp @@ -2,13 +2,10 @@ #include "esp8266_pwm.h" #include "esphome/core/macros.h" +#include "esphome/core/defines.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" -#if defined(USE_ESP8266) && ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0) -#error ESP8266 PWM requires at least arduino_version 2.4.0 -#endif - #include namespace esphome { diff --git a/esphome/components/esp8266_pwm/output.py b/esphome/components/esp8266_pwm/output.py index 3d52e5af16..7feee79ff2 100644 --- a/esphome/components/esp8266_pwm/output.py +++ b/esphome/components/esp8266_pwm/output.py @@ -23,15 +23,20 @@ ESP8266PWM = esp8266_pwm_ns.class_("ESP8266PWM", output.FloatOutput, cg.Componen SetFrequencyAction = esp8266_pwm_ns.class_("SetFrequencyAction", automation.Action) validate_frequency = cv.All(cv.frequency, cv.Range(min=1.0e-6)) -CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend( - { - cv.Required(CONF_ID): cv.declare_id(ESP8266PWM), - cv.Required(CONF_PIN): cv.All( - pins.internal_gpio_output_pin_schema, valid_pwm_pin - ), - cv.Optional(CONF_FREQUENCY, default="1kHz"): validate_frequency, - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = cv.All( + output.FLOAT_OUTPUT_SCHEMA.extend( + { + cv.Required(CONF_ID): cv.declare_id(ESP8266PWM), + cv.Required(CONF_PIN): cv.All( + pins.internal_gpio_output_pin_schema, valid_pwm_pin + ), + cv.Optional(CONF_FREQUENCY, default="1kHz"): validate_frequency, + } + ).extend(cv.COMPONENT_SCHEMA), + cv.require_framework_version( + esp8266_arduino=cv.Version(2, 4, 0), + ), +) async def to_code(config): diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index a80d095835..0fd9c6a4ae 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -1,7 +1,7 @@ #ifdef USE_ARDUINO #include "http_request.h" -#include "esphome/core/macros.h" +#include "esphome/core/defines.h" #include "esphome/core/log.h" #include "esphome/components/network/util.h" @@ -42,12 +42,12 @@ void HttpRequestComponent::send(const std::vector begin_status = this->client_.begin(url); #endif #ifdef USE_ESP8266 -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) this->client_.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); -#elif ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) this->client_.setFollowRedirects(true); #endif -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) this->client_.setRedirectLimit(3); #endif begin_status = this->client_.begin(*this->get_wifi_client_(), url); diff --git a/esphome/components/neopixelbus/light.py b/esphome/components/neopixelbus/light.py index 6bb1bc8f99..722e6f5b06 100644 --- a/esphome/components/neopixelbus/light.py +++ b/esphome/components/neopixelbus/light.py @@ -169,6 +169,10 @@ def _validate_method(value): CONFIG_SCHEMA = cv.All( cv.only_with_arduino, + cv.require_framework_version( + esp8266_arduino=cv.Version(2, 4, 0), + esp32_arduino=cv.Version(0, 0, 0), + ), light.ADDRESSABLE_LIGHT_SCHEMA.extend( { cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(NeoPixelBusLightOutputBase), diff --git a/esphome/components/neopixelbus/neopixelbus_light.h b/esphome/components/neopixelbus/neopixelbus_light.h index 34e10f2cfe..5233886075 100644 --- a/esphome/components/neopixelbus/neopixelbus_light.h +++ b/esphome/components/neopixelbus/neopixelbus_light.h @@ -9,10 +9,6 @@ #include "esphome/components/light/light_output.h" #include "esphome/components/light/addressable_light.h" -#if defined(USE_ESP8266) && ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0) -#error The NeoPixelBus library requires at least arduino_version 2.4.x -#endif - #include "NeoPixelBus.h" namespace esphome { diff --git a/esphome/components/nextion/nextion_upload.cpp b/esphome/components/nextion/nextion_upload.cpp index 1b60034bd1..9e6884398c 100644 --- a/esphome/components/nextion/nextion_upload.cpp +++ b/esphome/components/nextion/nextion_upload.cpp @@ -3,7 +3,7 @@ #ifdef USE_NEXTION_TFT_UPLOAD #include "esphome/core/application.h" -#include "esphome/core/macros.h" +#include "esphome/core/defines.h" #include "esphome/core/util.h" #include "esphome/core/log.h" #include "esphome/components/network/util.h" @@ -32,12 +32,12 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) { range_end = this->tft_size_; #ifdef USE_ESP8266 -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) http->setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); -#elif ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) http->setFollowRedirects(true); #endif -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) http->setRedirectLimit(3); #endif #endif @@ -148,12 +148,12 @@ void Nextion::upload_tft() { begin_status = http.begin(this->tft_url_.c_str()); #endif #ifdef USE_ESP8266 -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); -#elif ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) http.setFollowRedirects(true); #endif -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0) http.setRedirectLimit(3); #endif begin_status = http.begin(*this->get_wifi_client_(), this->tft_url_.c_str()); diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 118de3a7a3..d032382dc8 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -1,6 +1,5 @@ #pragma once -#include "esphome/core/macros.h" #include "esphome/core/component.h" #include "esphome/core/defines.h" #include "esphome/core/automation.h" @@ -18,7 +17,7 @@ #include #include -#if defined(USE_ESP8266) && ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0) +#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0) extern "C" { #include }; diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index 2021773209..de4253fe41 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -1,5 +1,5 @@ #include "wifi_component.h" -#include "esphome/core/macros.h" +#include "esphome/core/defines.h" #ifdef USE_ESP8266 @@ -20,7 +20,7 @@ extern "C" { #if LWIP_IPV6 #include "lwip/netif.h" // struct netif #endif -#if ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0) #include "LwipDhcpServer.h" #define wifi_softap_set_dhcps_lease(lease) dhcpSoftAP.set_dhcps_lease(lease) #define wifi_softap_set_dhcps_lease_time(time) dhcpSoftAP.set_dhcps_lease_time(time) @@ -238,7 +238,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) { conf.bssid_set = 0; } -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) if (ap.get_password().empty()) { conf.threshold.authmode = AUTH_OPEN; } else { @@ -528,7 +528,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) { ESP_LOGVV(TAG, "Event: AP receive Probe Request MAC=%s RSSI=%d", format_mac_addr(it.mac).c_str(), it.rssi); break; } -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) case EVENT_OPMODE_CHANGED: { auto it = event->event_info.opmode_changed; ESP_LOGV(TAG, "Event: Changed Mode old=%s new=%s", LOG_STR_ARG(get_op_mode_str(it.old_opmode)), @@ -614,7 +614,7 @@ bool WiFiComponent::wifi_scan_start_() { config.bssid = nullptr; config.channel = 0; config.show_hidden = 1; -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0) config.scan_type = WIFI_SCAN_TYPE_ACTIVE; if (first_scan) { config.scan_time.active.min = 100; @@ -693,7 +693,7 @@ bool WiFiComponent::wifi_ap_ip_config_(optional manual_ip) { return false; } -#if ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0) dhcpSoftAP.begin(&info); #endif diff --git a/esphome/core/defines.h b/esphome/core/defines.h index cb30c0cf66..ded98054b6 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -5,6 +5,8 @@ // // This file is only used by static analyzers and IDEs. +#include "esphome/core/macros.h" + // Informative flags #define ESPHOME_BOARD "dummy_board" #define ESPHOME_PROJECT_NAME "dummy project" @@ -60,13 +62,19 @@ #define USE_SOCKET_IMPL_BSD_SOCKETS #ifdef USE_ARDUINO +#define USE_ARDUINO_VERSION_CODE VERSION_CODE(1, 0, 6) #define USE_ETHERNET #endif + +#ifdef USE_ESP_IDF +#define USE_ARDUINO_VERSION_CODE VERSION_CODE(4, 3, 0) +#endif #endif // ESP8266-specific feature flags #ifdef USE_ESP8266 #define USE_ADC_SENSOR_VCC +#define USE_ARDUINO_VERSION_CODE VERSION_CODE(3, 0, 2) #define USE_ESP8266_PREFERENCES_FLASH #define USE_HTTP_REQUEST_ESP8266_HTTPS #define USE_SOCKET_IMPL_LWIP_TCP diff --git a/esphome/core/log.h b/esphome/core/log.h index 1e93ed4219..b1b1cf9115 100644 --- a/esphome/core/log.h +++ b/esphome/core/log.h @@ -6,10 +6,9 @@ #ifdef USE_STORE_LOG_STR_IN_FLASH #include "WString.h" +#include "esphome/core/defines.h" // for USE_ARDUINO_VERSION_CODE #endif -#include "esphome/core/macros.h" - // Include ESP-IDF/Arduino based logging methods here so they don't undefine ours later #if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF) #include @@ -19,8 +18,6 @@ #include #endif -#include "esphome/core/macros.h" - namespace esphome { #define ESPHOME_LOG_LEVEL_NONE 0 @@ -176,7 +173,7 @@ struct LogString; #include -#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 0) +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 0) #define LOG_STR_ARG(s) ((PGM_P)(s)) #else // Pre-Arduino 2.5, we can't pass a PSTR() to printf(). Emulate support by copying the message to a diff --git a/esphome/core/macros.h b/esphome/core/macros.h index b0027a276c..70ceaf58f4 100644 --- a/esphome/core/macros.h +++ b/esphome/core/macros.h @@ -1,56 +1,4 @@ #pragma once +// Helper macro to define a version code, whos evalue can be compared against other version codes. #define VERSION_CODE(major, minor, patch) ((major) << 16 | (minor) << 8 | (patch)) - -#if defined(USE_ESP8266) - -#include -#if defined(ARDUINO_ESP8266_MAJOR) && defined(ARDUINO_ESP8266_MINOR) && defined(ARDUINO_ESP8266_REVISION) // v3.0.1+ -#define ARDUINO_VERSION_CODE VERSION_CODE(ARDUINO_ESP8266_MAJOR, ARDUINO_ESP8266_MINOR, ARDUINO_ESP8266_REVISION) -#elif ARDUINO_ESP8266_GIT_VER == 0xefb0341a // version defines were screwed up in v3.0.0 -#define ARDUINO_VERSION_CODE VERSION_CODE(3, 0, 0) -#elif defined(ARDUINO_ESP8266_RELEASE_2_7_4) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 7, 4) -#elif defined(ARDUINO_ESP8266_RELEASE_2_7_3) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 7, 3) -#elif defined(ARDUINO_ESP8266_RELEASE_2_7_2) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 7, 2) -#elif defined(ARDUINO_ESP8266_RELEASE_2_7_1) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 7, 1) -#elif defined(ARDUINO_ESP8266_RELEASE_2_7_0) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 7, 0) -#elif defined(ARDUINO_ESP8266_RELEASE_2_6_3) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 6, 3) -#elif defined(ARDUINO_ESP8266_RELEASE_2_6_2) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 6, 2) -#elif defined(ARDUINO_ESP8266_RELEASE_2_6_1) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 6, 1) -#elif defined(ARDUINO_ESP8266_RELEASE_2_5_2) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 5, 2) -#elif defined(ARDUINO_ESP8266_RELEASE_2_5_1) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 5, 1) -#elif defined(ARDUINO_ESP8266_RELEASE_2_5_0) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 5, 0) -#elif defined(ARDUINO_ESP8266_RELEASE_2_4_2) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 4, 2) -#elif defined(ARDUINO_ESP8266_RELEASE_2_4_1) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 4, 1) -#elif defined(ARDUINO_ESP8266_RELEASE_2_4_0) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 4, 0) -#elif defined(ARDUINO_ESP8266_RELEASE_2_3_0) -#define ARDUINO_VERSION_CODE VERSION_CODE(2, 3, 0) -#else -#warning "Could not determine Arduino framework version, update esphome/core/macros.h!" -#endif - -#elif defined(USE_ESP32_FRAMEWORK_ARDUINO) - -#if defined(IDF_VER) // identifies v2, needed since v1 doesn't have the esp_arduino_version.h header -#include -#define ARDUINO_VERSION_CODE \ - VERSION_CODE(ESP_ARDUINO_VERSION_MAJOR, ESP_ARDUINO_VERSION_MINOR, ESP_ARDUINO_VERSION_PATH) -#else -#define ARDUINO_VERSION_CODE VERSION_CODE(1, 0, 0) // there are no defines identifying minor/patch version -#endif - -#endif diff --git a/esphome/writer.py b/esphome/writer.py index 89a074683a..31b47e243e 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -202,6 +202,7 @@ def write_platformio_project(): DEFINES_H_FORMAT = ESPHOME_H_FORMAT = """\ #pragma once +#include "esphome/core/macros.h" {} """ VERSION_H_FORMAT = """\ diff --git a/platformio.ini b/platformio.ini index c271c8092a..e2081a3ff6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -74,7 +74,6 @@ build_flags = ; This are common settings for the ESP8266 using Arduino. [common:esp8266-arduino] extends = common:arduino -; when changing this also copy it to esphome-docker-base images platform = platformio/espressif8266 @ 3.2.0 platform_packages = platformio/framework-arduinoespressif8266 @ ~3.30002.0 @@ -94,7 +93,6 @@ extra_scripts = post:esphome/components/esp8266/post_build.py.script ; This are common settings for the ESP32 (all variants) using Arduino. [common:esp32-arduino] extends = common:arduino -; when changing this also copy it to esphome-docker-base images platform = platformio/espressif32 @ 3.3.2 platform_packages = platformio/framework-arduinoespressif32 @ ~3.10006.0 @@ -113,7 +111,6 @@ extra_scripts = post:esphome/components/esp32/post_build.py.script ; This are common settings for the ESP32 (all variants) using IDF. [common:esp32-idf] extends = common:idf -; when changing this also copy it to esphome-docker-base images platform = platformio/espressif32 @ 3.3.2 platform_packages = platformio/framework-espidf @ ~3.40300.0