Skip to content

Commit 5d664fc

Browse files
committed
Merge branch 'feature/idf4.0_compat' into 'master'
IDF 4.0 compatibility mode See merge request app-frameworks/esp-rainmaker!154
2 parents 0f49014 + 7d5c39e commit 5d664fc

File tree

27 files changed

+326
-401
lines changed

27 files changed

+326
-401
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ The key features of ESP RainMaker are:
1818
2. Zero configuration required on the Cloud.
1919
3. Phone apps that dynamically render the UI as per the device information.
2020

21+
## Supported ESP-IDF versions
22+
23+
ESP RainMaker can work with ESP IDF 4.0 and above.
24+
2125
## Phone Apps
2226

2327
### Android

components/esp_rainmaker/src/mqtt/esp_rmaker_mqtt.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,24 @@
1919
#include <freertos/event_groups.h>
2020
#include <esp_log.h>
2121
#include <mqtt_client.h>
22-
2322
#include <esp_rmaker_mqtt.h>
2423

24+
#include <esp_idf_version.h>
25+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
26+
// Features supported in 4.1
27+
28+
#ifdef CONFIG_ESP_RMAKER_MQTT_PORT_443
29+
#define ESP_RMAKER_MQTT_USE_PORT_443
30+
#endif
31+
32+
#else
33+
34+
#ifdef CONFIG_ESP_RMAKER_MQTT_PORT_443
35+
#warning "Port 443 not supported in idf versions below 4.1. Using 8883 instead."
36+
#endif
37+
38+
#endif /* !IDF4.1 */
39+
2540
static const char *TAG = "esp_rmaker_mqtt";
2641

2742
#define MAX_MQTT_SUBSCRIPTIONS 5
@@ -212,9 +227,9 @@ esp_err_t esp_rmaker_mqtt_disconnect(void)
212227
}
213228
return err;
214229
}
215-
#ifdef CONFIG_ESP_RMAKER_MQTT_PORT_443
230+
#ifdef ESP_RMAKER_MQTT_USE_PORT_443
216231
static const char *alpn_protocols[] = { "x-amzn-mqtt-ca", NULL };
217-
#endif /* CONFIG_ESP_RMAKER_MQTT_PORT_443 */
232+
#endif /* ESP_RMAKER_MQTT_USE_PORT_443 */
218233
esp_err_t esp_rmaker_mqtt_init(esp_rmaker_mqtt_config_t *config)
219234
{
220235
if (mqtt_data) {
@@ -229,13 +244,12 @@ esp_err_t esp_rmaker_mqtt_init(esp_rmaker_mqtt_config_t *config)
229244
mqtt_data->config = config;
230245
const esp_mqtt_client_config_t mqtt_client_cfg = {
231246
.host = config->mqtt_host,
232-
#ifdef CONFIG_ESP_RMAKER_MQTT_PORT_443
247+
#ifdef ESP_RMAKER_MQTT_USE_PORT_443
233248
.port = 443,
234249
.alpn_protos = alpn_protocols,
235-
#endif /* CONFIG_ESP_RMAKER_MQTT_PORT_443 */
236-
#ifdef CONFIG_ESP_RMAKER_MQTT_PORT_8883
250+
#else
237251
.port = 8883,
238-
#endif /* CONFIG_ESP_RMAKER_MQTT_PORT_8883 */
252+
#endif /* !ESP_RMAKER_MQTT_USE_PORT_443 */
239253
.cert_pem = (const char *)config->server_cert,
240254
.client_cert_pem = (const char *)config->client_cert,
241255
.client_key_pem = (const char *)config->client_key,

components/led_strip/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

components/led_strip/component.mk

Lines changed: 0 additions & 3 deletions
This file was deleted.

components/ws2812_led/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if(CONFIG_WS2812_LED_ENABLE)
2+
set(srcs "ws2812_led.c" "led_strip_rmt_ws2812.c")
3+
else()
4+
set(srcs "ws2812_led.c")
5+
endif()
6+
7+
idf_component_register(SRCS ${srcs}
8+
INCLUDE_DIRS "."
9+
PRIV_REQUIRES "driver")

components/ws2812_led/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
menu "WS2812 RGB LED"
2+
visible if IDF_TARGET_ESP32S2
3+
4+
config WS2812_LED_ENABLE
5+
bool "Enable RGB LED"
6+
depends on IDF_TARGET_ESP32S2
7+
default y
8+
help
9+
Disable the WS2812 RGB LED.
10+
11+
config WS2812_LED_GPIO
12+
int "WS2812 LED GPIO"
13+
default 18
14+
depends on WS2812_LED_ENABLE
15+
help
16+
Set the WS2812 RGB LED GPIO.
17+
18+
endmenu

components/ws2812_led/component.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
COMPONENT_ADD_INCLUDEDIRS := .
2+
COMPONENT_SRCDIRS := .
3+
ifndef CONFIG_WS2812_LED_ENABLE
4+
COMPONENT_OBJEXCLUDE += led_strip_rmt_ws2812.c
5+
endif

components/ws2812_led/ws2812_led.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* WS2812 RGB LED helper functions
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
10+
/* It is recommended to copy this code in your example so that you can modify as
11+
* per your application's needs.
12+
*/
13+
14+
15+
#include <stdint.h>
16+
#include <esp_err.h>
17+
#include <esp_log.h>
18+
19+
static const char *TAG = "ws2812_led";
20+
21+
#ifdef CONFIG_WS2812_LED_ENABLE
22+
#include <driver/rmt.h>
23+
#include "led_strip.h"
24+
#define RMT_TX_CHANNEL RMT_CHANNEL_0
25+
26+
static led_strip_t *g_strip;
27+
28+
/**
29+
* @brief Simple helper function, converting HSV color space to RGB color space
30+
*
31+
* Wiki: https://en.wikipedia.org/wiki/HSL_and_HSV
32+
*
33+
*/
34+
static void ws2812_led_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b)
35+
{
36+
h %= 360; // h -> [0,360]
37+
uint32_t rgb_max = v * 2.55f;
38+
uint32_t rgb_min = rgb_max * (100 - s) / 100.0f;
39+
40+
uint32_t i = h / 60;
41+
uint32_t diff = h % 60;
42+
43+
// RGB adjustment amount by hue
44+
uint32_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
45+
46+
switch (i) {
47+
case 0:
48+
*r = rgb_max;
49+
*g = rgb_min + rgb_adj;
50+
*b = rgb_min;
51+
break;
52+
case 1:
53+
*r = rgb_max - rgb_adj;
54+
*g = rgb_max;
55+
*b = rgb_min;
56+
break;
57+
case 2:
58+
*r = rgb_min;
59+
*g = rgb_max;
60+
*b = rgb_min + rgb_adj;
61+
break;
62+
case 3:
63+
*r = rgb_min;
64+
*g = rgb_max - rgb_adj;
65+
*b = rgb_max;
66+
break;
67+
case 4:
68+
*r = rgb_min + rgb_adj;
69+
*g = rgb_min;
70+
*b = rgb_max;
71+
break;
72+
default:
73+
*r = rgb_max;
74+
*g = rgb_min;
75+
*b = rgb_max - rgb_adj;
76+
break;
77+
}
78+
}
79+
80+
esp_err_t ws2812_led_set_rgb(uint32_t red, uint32_t green, uint32_t blue)
81+
{
82+
if (!g_strip) {
83+
return ESP_ERR_INVALID_STATE;
84+
}
85+
g_strip->set_pixel(g_strip, 0, red, green, blue);
86+
g_strip->refresh(g_strip, 100);
87+
return ESP_OK;
88+
}
89+
90+
esp_err_t ws2812_led_set_hsv(uint32_t hue, uint32_t saturation, uint32_t value)
91+
{
92+
if (!g_strip) {
93+
return ESP_ERR_INVALID_STATE;
94+
}
95+
uint32_t red = 0;
96+
uint32_t green = 0;
97+
uint32_t blue = 0;
98+
ws2812_led_hsv2rgb(hue, saturation, value, &red, &green, &blue);
99+
return ws2812_led_set_rgb(red, green, blue);
100+
}
101+
102+
esp_err_t ws2812_led_clear(void)
103+
{
104+
if (!g_strip) {
105+
return ESP_ERR_INVALID_STATE;
106+
}
107+
g_strip->clear(g_strip, 100);
108+
return ESP_OK;
109+
}
110+
111+
esp_err_t ws2812_led_init(void)
112+
{
113+
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_WS2812_LED_GPIO, RMT_TX_CHANNEL);
114+
// set counter clock to 40MHz
115+
config.clk_div = 2;
116+
117+
if (rmt_config(&config) != ESP_OK) {
118+
ESP_LOGE(TAG, "RMT Config failed.");
119+
return ESP_FAIL;
120+
}
121+
if (rmt_driver_install(config.channel, 0, 0) != ESP_OK) {
122+
ESP_LOGE(TAG, "RMT Driver install failed.");
123+
return ESP_FAIL;
124+
}
125+
126+
// install ws2812 driver
127+
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(1, (led_strip_dev_t)config.channel);
128+
g_strip = led_strip_new_rmt_ws2812(&strip_config);
129+
if (!g_strip) {
130+
ESP_LOGE(TAG, "Install WS2812 driver failed.");
131+
return ESP_FAIL;
132+
}
133+
return ESP_OK;
134+
}
135+
#else /* !CONFIG_WS2812_LED_ENABLE */
136+
esp_err_t ws2812_led_set_rgb(uint32_t red, uint32_t green, uint32_t blue)
137+
{
138+
/* Empty function, since WS2812 RGB LED has been disabled. */
139+
return ESP_OK;
140+
}
141+
142+
esp_err_t ws2812_led_set_hsv(uint32_t hue, uint32_t saturation, uint32_t value)
143+
{
144+
/* Empty function, since WS2812 RGB LED has been disabled. */
145+
return ESP_OK;
146+
}
147+
148+
esp_err_t ws2812_led_clear(void)
149+
{
150+
/* Empty function, since WS2812 RGB LED has been disabled. */
151+
return ESP_OK;
152+
}
153+
154+
esp_err_t ws2812_led_init(void)
155+
{
156+
ESP_LOGW(TAG, "WS2812 LED is disabled");
157+
return ESP_OK;
158+
}
159+
#endif /* !CONFIG_WS2812_LED_ENABLE */

components/ws2812_led/ws2812_led.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
#pragma once
9+
#include <esp_err.h>
10+
11+
/** Initialize the WS2812 RGB LED
12+
*
13+
* @return ESP_OK on success.
14+
* @return error in case of failure.
15+
*/
16+
esp_err_t ws2812_led_init(void);
17+
18+
/** Set RGB value for the WS2812 LED
19+
*
20+
* @param[in] red Intensity of Red color (0-100)
21+
* @param[in] green Intensity of Green color (0-100)
22+
* @param[in] blue Intensity of Green color (0-100)
23+
*
24+
* @return ESP_OK on success.
25+
* @return error in case of failure.
26+
*/
27+
esp_err_t ws2812_led_set_rgb(uint32_t red, uint32_t green, uint32_t blue);
28+
29+
/** Set HSV value for the WS2812 LED
30+
*
31+
* @param[in] hue Value of hue in arc degrees (0-360)
32+
* @param[in] saturation Saturation in percentage (0-100)
33+
* @param[in] value Value (also called Intensity) in percentage (0-100)
34+
*
35+
* @return ESP_OK on success.
36+
* @return error in case of failure.
37+
*/
38+
esp_err_t ws2812_led_set_hsv(uint32_t hue, uint32_t saturation, uint32_t value);
39+
40+
/** Clear (turn off) the WS2812 LED
41+
* @return ESP_OK on success.
42+
* @return error in case of failure.
43+
*/
44+
esp_err_t ws2812_led_clear(void);

examples/common/app_wifi/app_wifi.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
#include <esp_wifi.h>
1313
#include <esp_event.h>
1414
#include <esp_log.h>
15+
#include <esp_idf_version.h>
16+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
17+
// Features supported in 4.1+
18+
#define ESP_NETIF_SUPPORTED
19+
#endif
20+
21+
#ifdef ESP_NETIF_SUPPORTED
22+
#include <esp_netif.h>
23+
#else
24+
#include <tcpip_adapter.h>
25+
#endif
26+
1527
#include <wifi_provisioning/manager.h>
1628
#ifdef CONFIG_APP_WIFI_PROV_TRANSPORT_BLE
1729
#include <wifi_provisioning/scheme_ble.h>
@@ -181,7 +193,11 @@ static esp_err_t get_device_pop(char *pop, size_t max, app_wifi_pop_type_t pop_t
181193
void app_wifi_init(void)
182194
{
183195
/* Initialize TCP/IP */
196+
#ifdef ESP_NETIF_SUPPORTED
184197
esp_netif_init();
198+
#else
199+
tcpip_adapter_init();
200+
#endif
185201

186202
/* Initialize the event loop */
187203
ESP_ERROR_CHECK(esp_event_loop_create_default());
@@ -193,7 +209,9 @@ void app_wifi_init(void)
193209
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
194210

195211
/* Initialize Wi-Fi including netif with default config */
212+
#ifdef ESP_NETIF_SUPPORTED
196213
esp_netif_create_default_wifi_sta();
214+
#endif
197215
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
198216
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
199217
}
@@ -236,7 +254,9 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
236254
/* If device is not yet provisioned start provisioning service */
237255
if (!provisioned) {
238256
ESP_LOGI(TAG, "Starting provisioning");
257+
#ifdef ESP_NETIF_SUPPORTED
239258
esp_netif_create_default_wifi_ap();
259+
#endif
240260

241261
/* What is the Device Service Name that we want
242262
* This translates to :

examples/fan/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ I (16073) app_main: Received value = true for Fan - power
1919

2020
### LED not working?
2121

22-
The ESP32-S2-Saola-1 board has the RGB LED connected to GPIO 18. However, a few earlier boards may have it on GPIO 17. Please use `CONFIG_APP_LED_GPIO` to set the appropriate value.
22+
The ESP32-S2-Saola-1 board has the RGB LED connected to GPIO 18. However, a few earlier boards may have it on GPIO 17. Please use `CONFIG_WS2812_LED_GPIO` to set the appropriate value.
2323

2424
### Reset to Factory
2525

examples/fan/main/Kconfig.projbuild

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)