Generate ARDUINO_VERSION_CODE in Python code (#3101)

Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Oxan van Leeuwen 2022-01-24 10:34:34 +01:00 committed by GitHub
parent f2d677d51a
commit cdda648360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 71 additions and 98 deletions

View File

@ -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,

View File

@ -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

View File

@ -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 <core_esp8266_waveform.h>
namespace esphome {

View File

@ -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):

View File

@ -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<HttpRequestResponseTrigger *>
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);

View File

@ -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),

View File

@ -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 {

View File

@ -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());

View File

@ -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 <ESP8266WiFi.h>
#include <ESP8266WiFiType.h>
#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 <user_interface.h>
};

View File

@ -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<ManualIP> 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

View File

@ -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

View File

@ -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 <esp_err.h>
@ -19,8 +18,6 @@
#include <esp32-hal-log.h>
#endif
#include "esphome/core/macros.h"
namespace esphome {
#define ESPHOME_LOG_LEVEL_NONE 0
@ -176,7 +173,7 @@ struct LogString;
#include <pgmspace.h>
#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

View File

@ -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 <core_version.h>
#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 <esp_arduino_version.h>
#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

View File

@ -202,6 +202,7 @@ def write_platformio_project():
DEFINES_H_FORMAT = ESPHOME_H_FORMAT = """\
#pragma once
#include "esphome/core/macros.h"
{}
"""
VERSION_H_FORMAT = """\

View File

@ -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