Skip to content

Commit 821c70f

Browse files
committed
examples: Standardise naming of files, symbols, etc. in examples
* Use "example" in all example function & variable names, ie use i2c_example_xxx instead of i2c_xxx for example functions. Closes #198 espressif/esp-idf#198 * Mark example functions, etc. static * Replace uses of "test" & "demo" with "example" * Split the UART example into two * Rename "main" example files to end with "_main.c" for disambiguation
1 parent 8ee6f82 commit 821c70f

40 files changed

+623
-597
lines changed

examples/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ In addition, here are some tips for creating good examples:
3232

3333
* A good example is documented and the basic options can be configured.
3434
* A good example does not contain a lot of code. If there is a lot of generic code in the example, consider refactoring that code into a standalone component and then use the component's API in your example.
35+
* Names (of files, functions, variables, etc.) inside examples should be distinguishable from names of other parts of IDF (ideally, use `example` in names.)
36+
* Functions and variables used inside examples should be declared static where possible.
37+
* Examples should demonstrate one distinct thing each. Avoid multi-purposed "demo" examples, split these into multiple examples instead.
3538
* Examples must be licensed under the Apache License 2.0 or (preferably for examples) if possible you can declare the example to be Public Domain / Creative Commons Zero.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#ifndef __BLUFI_DEMO_H__
2-
#define __BLUFI_DEMO_H__
1+
#pragma once
32

4-
5-
#define BLUFI_DEMO_TAG "BLUFI_DEMO"
6-
#define BLUFI_INFO(fmt, ...) ESP_LOGI(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__)
7-
#define BLUFI_ERROR(fmt, ...) ESP_LOGE(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__)
3+
#define BLUFI_EXAMPLE_TAG "BLUFI_EXAMPLE"
4+
#define BLUFI_INFO(fmt, ...) ESP_LOGI(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__)
5+
#define BLUFI_ERROR(fmt, ...) ESP_LOGE(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__)
86

97
void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
108
int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
@@ -13,5 +11,3 @@ uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len);
1311

1412
int blufi_security_init(void);
1513
void blufi_security_deinit(void);
16-
17-
#endif /* __BLUFI_DEMO_H__ */

examples/bluetooth/blufi/main/blufi_main.c examples/bluetooth/blufi/main/blufi_example_main.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030
#include "esp_gap_ble_api.h"
3131
#include "esp_bt_main.h"
3232
#include "esp_bt_device.h"
33-
#include "blufi_demo.h"
33+
#include "blufi_example.h"
3434

35-
static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
35+
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
3636

3737
#define BLUFI_DEVICE_NAME "BLUFI_DEVICE"
38-
static uint8_t blufi_service_uuid128[32] = {
38+
static uint8_t example_service_uuid128[32] = {
3939
/* LSB <--------------------------------------------------------------------------------> MSB */
4040
//first uuid, 16bit, [12],[13] is the value
4141
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
4242
};
4343

4444
//static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56};
45-
static esp_ble_adv_data_t blufi_adv_data = {
45+
static esp_ble_adv_data_t example_adv_data = {
4646
.set_scan_rsp = false,
4747
.include_name = true,
4848
.include_txpower = true,
@@ -54,11 +54,11 @@ static esp_ble_adv_data_t blufi_adv_data = {
5454
.service_data_len = 0,
5555
.p_service_data = NULL,
5656
.service_uuid_len = 16,
57-
.p_service_uuid = blufi_service_uuid128,
57+
.p_service_uuid = example_service_uuid128,
5858
.flag = 0x6,
5959
};
6060

61-
static esp_ble_adv_params_t blufi_adv_params = {
61+
static esp_ble_adv_params_t example_adv_params = {
6262
.adv_int_min = 0x100,
6363
.adv_int_max = 0x100,
6464
.adv_type = ADV_TYPE_IND,
@@ -88,7 +88,7 @@ static uint8_t gl_sta_bssid[6];
8888
static uint8_t gl_sta_ssid[32];
8989
static int gl_sta_ssid_len;
9090

91-
static esp_err_t event_handler(void *ctx, system_event_t *event)
91+
static esp_err_t example_net_event_handler(void *ctx, system_event_t *event)
9292
{
9393
wifi_mode_t mode;
9494

@@ -146,32 +146,32 @@ static void initialise_wifi(void)
146146
{
147147
tcpip_adapter_init();
148148
wifi_event_group = xEventGroupCreate();
149-
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
149+
ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) );
150150
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
151151
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
152152
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
153153
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
154154
ESP_ERROR_CHECK( esp_wifi_start() );
155155
}
156156

157-
static esp_blufi_callbacks_t blufi_callbacks = {
158-
.event_cb = blufi_event_callback,
157+
static esp_blufi_callbacks_t example_callbacks = {
158+
.event_cb = example_event_callback,
159159
.negotiate_data_handler = blufi_dh_negotiate_data_handler,
160160
.encrypt_func = blufi_aes_encrypt,
161161
.decrypt_func = blufi_aes_decrypt,
162162
.checksum_func = blufi_crc_checksum,
163163
};
164164

165-
static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
165+
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
166166
{
167167
/* actually, should post to blufi_task handle the procedure,
168-
* now, as a demo, we do simplely */
168+
* now, as a example, we do it more simply */
169169
switch (event) {
170170
case ESP_BLUFI_EVENT_INIT_FINISH:
171171
BLUFI_INFO("BLUFI init finish\n");
172172

173173
esp_ble_gap_set_device_name(BLUFI_DEVICE_NAME);
174-
esp_ble_gap_config_adv_data(&blufi_adv_data);
174+
esp_ble_gap_config_adv_data(&example_adv_data);
175175
break;
176176
case ESP_BLUFI_EVENT_DEINIT_FINISH:
177177
BLUFI_INFO("BLUFI init finish\n");
@@ -184,7 +184,7 @@ static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_
184184
break;
185185
case ESP_BLUFI_EVENT_BLE_DISCONNECT:
186186
BLUFI_INFO("BLUFI ble disconnect\n");
187-
esp_ble_gap_start_advertising(&blufi_adv_params);
187+
esp_ble_gap_start_advertising(&example_adv_params);
188188
break;
189189
case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
190190
BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode);
@@ -297,11 +297,11 @@ static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_
297297
}
298298
}
299299

300-
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
300+
static void example_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
301301
{
302302
switch (event) {
303303
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
304-
esp_ble_gap_start_advertising(&blufi_adv_params);
304+
esp_ble_gap_start_advertising(&example_adv_params);
305305
break;
306306
default:
307307
break;
@@ -340,8 +340,8 @@ void app_main()
340340
BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version());
341341

342342
blufi_security_init();
343-
esp_ble_gap_register_callback(gap_event_handler);
343+
esp_ble_gap_register_callback(example_gap_event_handler);
344344

345-
esp_blufi_register_callbacks(&blufi_callbacks);
345+
esp_blufi_register_callbacks(&example_callbacks);
346346
esp_blufi_profile_init();
347347
}

examples/bluetooth/blufi/main/blufi_security.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "esp_bt_defs.h"
3030
#include "esp_gap_ble_api.h"
3131
#include "esp_bt_main.h"
32-
#include "blufi_demo.h"
32+
#include "blufi_example.h"
3333

3434
#include "mbedtls/aes.h"
3535
#include "mbedtls/dhm.h"

examples/ethernet/ethernet/main/ethernet_main.c examples/ethernet/ethernet/main/ethernet_example_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "driver/gpio.h"
3535
#include "tlk110_phy.h"
3636

37-
static const char *TAG = "eth_demo";
37+
static const char *TAG = "eth_example";
3838

3939
#define DEFAULT_PHY_CONFIG (AUTO_MDIX_ENABLE|AUTO_NEGOTIATION_ENABLE|AN_1|AN_0|LED_CFG)
4040
#define PIN_PHY_POWER 17

examples/peripherals/gpio/main/gpio_test.c examples/peripherals/gpio/main/gpio_example_main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141

4242
static xQueueHandle gpio_evt_queue = NULL;
4343

44-
void IRAM_ATTR gpio_isr_handler(void* arg)
44+
static void IRAM_ATTR gpio_isr_handler(void* arg)
4545
{
4646
uint32_t gpio_num = (uint32_t) arg;
4747
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
4848
}
4949

50-
void gpio_task_example(void* arg)
50+
static void gpio_task_example(void* arg)
5151
{
5252
uint32_t io_num;
5353
for(;;) {
@@ -62,7 +62,7 @@ void app_main()
6262
gpio_config_t io_conf;
6363
//disable interrupt
6464
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
65-
//set as output mode
65+
//set as output mode
6666
io_conf.mode = GPIO_MODE_OUTPUT;
6767
//bit mask of the pins that you want to set,e.g.GPIO18/19
6868
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;

examples/peripherals/i2c/main/i2c_test.c examples/peripherals/i2c/main/i2c_example_main.c

+39-35
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@
4747
#define RW_TEST_LENGTH 129 /*!<Data length for r/w test, any value from 0-DATA_LENGTH*/
4848
#define DELAY_TIME_BETWEEN_ITEMS_MS 1234 /*!< delay time between different test items */
4949

50-
#define I2C_SLAVE_SCL_IO 26 /*!<gpio number for i2c slave clock */
51-
#define I2C_SLAVE_SDA_IO 25 /*!<gpio number for i2c slave data */
52-
#define I2C_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
53-
#define I2C_SLAVE_TX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
54-
#define I2C_SLAVE_RX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
50+
#define I2C_EXAMPLE_SLAVE_SCL_IO 26 /*!<gpio number for i2c slave clock */
51+
#define I2C_EXAMPLE_SLAVE_SDA_IO 25 /*!<gpio number for i2c slave data */
52+
#define I2C_EXAMPLE_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
53+
#define I2C_EXAMPLE_SLAVE_TX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
54+
#define I2C_EXAMPLE_SLAVE_RX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
5555

56-
#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
57-
#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
58-
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
59-
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
60-
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
61-
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
56+
#define I2C_EXAMPLE_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
57+
#define I2C_EXAMPLE_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
58+
#define I2C_EXAMPLE_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
59+
#define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
60+
#define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
61+
#define I2C_EXAMPLE_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
6262

6363
#define BH1750_SENSOR_ADDR 0x23 /*!< slave address for BH1750 sensor */
6464
#define BH1750_CMD_START 0x23 /*!< Command to set measure mode */
@@ -81,7 +81,7 @@ xSemaphoreHandle print_mux;
8181
* --------|--------------------------|----------------------|--------------------|------|
8282
*
8383
*/
84-
esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
84+
static esp_err_t i2c_example_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
8585
{
8686
if (size == 0) {
8787
return ESP_OK;
@@ -110,7 +110,7 @@ esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t siz
110110
* --------|---------------------------|----------------------|------|
111111
*
112112
*/
113-
esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
113+
static esp_err_t i2c_example_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
114114
{
115115
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
116116
i2c_master_start(cmd);
@@ -135,7 +135,7 @@ esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t si
135135
* | start | slave_addr + rd_bit + ack | read 1 byte + ack | read 1 byte + nack | stop |
136136
* --------|---------------------------|--------------------|--------------------|------|
137137
*/
138-
esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l)
138+
static esp_err_t i2c_example_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l)
139139
{
140140
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
141141
i2c_master_start(cmd);
@@ -166,42 +166,46 @@ esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* d
166166
/**
167167
* @brief i2c master initialization
168168
*/
169-
void i2c_master_init()
169+
static void i2c_example_master_init()
170170
{
171-
int i2c_master_port = I2C_MASTER_NUM;
171+
int i2c_master_port = I2C_EXAMPLE_MASTER_NUM;
172172
i2c_config_t conf;
173173
conf.mode = I2C_MODE_MASTER;
174-
conf.sda_io_num = I2C_MASTER_SDA_IO;
174+
conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO;
175175
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
176-
conf.scl_io_num = I2C_MASTER_SCL_IO;
176+
conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO;
177177
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
178-
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
178+
conf.master.clk_speed = I2C_EXAMPLE_MASTER_FREQ_HZ;
179179
i2c_param_config(i2c_master_port, &conf);
180-
i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
180+
i2c_driver_install(i2c_master_port, conf.mode,
181+
I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,
182+
I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
181183
}
182184

183185
/**
184186
* @brief i2c slave initialization
185187
*/
186-
void i2c_slave_init()
188+
static void i2c_example_slave_init()
187189
{
188-
int i2c_slave_port = I2C_SLAVE_NUM;
190+
int i2c_slave_port = I2C_EXAMPLE_SLAVE_NUM;
189191
i2c_config_t conf_slave;
190-
conf_slave.sda_io_num = I2C_SLAVE_SDA_IO;
192+
conf_slave.sda_io_num = I2C_EXAMPLE_SLAVE_SDA_IO;
191193
conf_slave.sda_pullup_en = GPIO_PULLUP_ENABLE;
192-
conf_slave.scl_io_num = I2C_SLAVE_SCL_IO;
194+
conf_slave.scl_io_num = I2C_EXAMPLE_SLAVE_SCL_IO;
193195
conf_slave.scl_pullup_en = GPIO_PULLUP_ENABLE;
194196
conf_slave.mode = I2C_MODE_SLAVE;
195197
conf_slave.slave.addr_10bit_en = 0;
196198
conf_slave.slave.slave_addr = ESP_SLAVE_ADDR;
197199
i2c_param_config(i2c_slave_port, &conf_slave);
198-
i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
200+
i2c_driver_install(i2c_slave_port, conf_slave.mode,
201+
I2C_EXAMPLE_SLAVE_RX_BUF_LEN,
202+
I2C_EXAMPLE_SLAVE_TX_BUF_LEN, 0);
199203
}
200204

201205
/**
202206
* @brief test function to show buffer
203207
*/
204-
void disp_buf(uint8_t* buf, int len)
208+
static void disp_buf(uint8_t* buf, int len)
205209
{
206210
int i;
207211
for (i = 0; i < len; i++) {
@@ -213,7 +217,7 @@ void disp_buf(uint8_t* buf, int len)
213217
printf("\n");
214218
}
215219

216-
void i2c_test_task(void* arg)
220+
static void i2c_test_task(void* arg)
217221
{
218222
int i = 0;
219223
int ret;
@@ -224,7 +228,7 @@ void i2c_test_task(void* arg)
224228
uint8_t sensor_data_h, sensor_data_l;
225229

226230
while (1) {
227-
ret = i2c_master_sensor_test( I2C_MASTER_NUM, &sensor_data_h, &sensor_data_l);
231+
ret = i2c_example_master_sensor_test( I2C_EXAMPLE_MASTER_NUM, &sensor_data_h, &sensor_data_l);
228232
xSemaphoreTake(print_mux, portMAX_DELAY);
229233
printf("*******************\n");
230234
printf("TASK[%d] MASTER READ SENSOR( BH1750 )\n", task_idx);
@@ -243,12 +247,12 @@ void i2c_test_task(void* arg)
243247
for (i = 0; i < DATA_LENGTH; i++) {
244248
data[i] = i;
245249
}
246-
size_t d_size = i2c_slave_write_buffer(I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
250+
size_t d_size = i2c_slave_write_buffer(I2C_EXAMPLE_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
247251
if (d_size == 0) {
248252
printf("i2c slave tx buffer full\n");
249-
ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, DATA_LENGTH);
253+
ret = i2c_example_master_read_slave(I2C_EXAMPLE_MASTER_NUM, data_rd, DATA_LENGTH);
250254
} else {
251-
ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, RW_TEST_LENGTH);
255+
ret = i2c_example_master_read_slave(I2C_EXAMPLE_MASTER_NUM, data_rd, RW_TEST_LENGTH);
252256
}
253257
xSemaphoreTake(print_mux, portMAX_DELAY);
254258
printf("*******************\n");
@@ -270,9 +274,9 @@ void i2c_test_task(void* arg)
270274
data_wr[i] = i + 10;
271275
}
272276
//we need to fill the slave buffer so that master can read later
273-
ret = i2c_master_write_slave( I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
277+
ret = i2c_example_master_write_slave( I2C_EXAMPLE_MASTER_NUM, data_wr, RW_TEST_LENGTH);
274278
if (ret == ESP_OK) {
275-
size = i2c_slave_read_buffer( I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
279+
size = i2c_slave_read_buffer( I2C_EXAMPLE_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
276280
}
277281
xSemaphoreTake(print_mux, portMAX_DELAY);
278282
printf("*******************\n");
@@ -294,8 +298,8 @@ void i2c_test_task(void* arg)
294298
void app_main()
295299
{
296300
print_mux = xSemaphoreCreateMutex();
297-
i2c_slave_init();
298-
i2c_master_init();
301+
i2c_example_slave_init();
302+
i2c_example_master_init();
299303

300304
xTaskCreate(i2c_test_task, "i2c_test_task_0", 1024 * 2, (void* ) 0, 10, NULL);
301305
xTaskCreate(i2c_test_task, "i2c_test_task_1", 1024 * 2, (void* ) 1, 10, NULL);

0 commit comments

Comments
 (0)