8000 Merge remote-tracking branch 'upstream/master' · regularex/esp32-snippets@0139a57 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0139a57

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3d17166 + 571e016 commit 0139a57

File tree

15 files changed

+145
-57
lines changed

15 files changed

+145
-57
lines changed

cpp_utils/BLEDevice.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ void BLEDevice::gapEventHandler(
163163
* try and release/delete it.
164164
*/
165165
BLEScan* BLEDevice::getScan() {
166+
//ESP_LOGD(LOG_TAG, ">> getScan");
166167
if (m_pScan == nullptr) {
167168
m_pScan = new BLEScan();
169+
//ESP_LOGD(LOG_TAG, " - creating a new scan object");
168170
}
171+
//ESP_LOGD(LOG_TAG, "<< getScan: Returning object at 0x%x", (uint32_t)m_pScan);
169172
return m_pScan;
170173
} // getScan
171174

cpp_utils/BLEScan.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
177177
BLEScanResults BLEScan::start(uint32_t duration) {
178178
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
179179

180-
m_semaphoreScanEnd.take("start");
180+
m_semaphoreScanEnd.take(std::string("start"));
181181

182182
m_scanResults.m_vectorAdvertisedDevices.clear();
183183

@@ -199,8 +199,7 @@ BLEScanResults BLEScan::start(uint32_t duration) {
199199

200200
m_stopped = false;
201201

202-
m_semaphoreScanEnd.take("start");
203-
m_semaphoreScanEnd.give();
202+
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
204203

205204
ESP_LOGD(LOG_TAG, "<< start()");
206205
return m_scanResults;

cpp_utils/BLEScan.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class BLEScanResults {
4646
*/
4747
class BLEScan {
4848
public:
49-
BLEScan();
50-
5149
void setActiveScan(bool active);
5250
void setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks);
5351
void setInterval(uint16_t intervalMSecs);
@@ -56,6 +54,7 @@ class BLEScan {
5654
void stop();
5755

5856
private:
57+
BLEScan(); // One doesn't create a new instance instead one asks the BLEDevice for the singleton.
5958
friend class BLEDevice;
6059
void gapEventHandler(
6160
esp_gap_ble_cb_event_t event,

cpp_utils/FreeRTOS.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,25 @@ uint32_t FreeRTOS::getTimeSinceStart() {
7676
* @return The value associated with the semaphore.
7777
*/
7878
uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
79-
ESP_LOGV(LOG_TAG, "Semaphore waiting: %s for %s", toString().c_str(), owner.c_str());
79+
ESP_LOGV(LOG_TAG, ">> wait: Semaphore waiting: %s for %s", toString().c_str(), owner.c_str());
80+
81+
8082
if (m_usePthreads) {
8183
pthread_mutex_lock(&m_pthread_mutex);
8284
} else {
8385
xSemaphoreTake(m_semaphore, portMAX_DELAY);
8486
}
87+
8588
m_owner = owner;
89+
8690
if (m_usePthreads) {
8791
pthread_mutex_unlock(&m_pthread_mutex);
8892
} else {
8993
xSemaphoreGive(m_semaphore);
9094
}
9195

92-
ESP_LOGV(LOG_TAG, "Semaphore released: %s", toString().c_str());
93-
m_owner = "<N/A>";
96+
ESP_LOGV(LOG_TAG, "<< wait: Semaphore released: %s", toString().c_str());
97+
m_owner = std::string("<N/A>");
9498
return m_value;
9599
} // wait
96100

@@ -104,7 +108,7 @@ FreeRTOS::Semaphore::Semaphore(std::string name) {
104108
}
105109

106110
m_name = name;
107-
m_owner = "<N/A>";
111+
m_owner = std::string("<N/A>");
108112
m_value = 0;
109113
}
110114

@@ -123,6 +127,7 @@ FreeRTOS::Semaphore::~Semaphore() {
123127
* The Semaphore is given.
124128
*/
125129
void FreeRTOS::Semaphore::give() {
130+
ESP_LOGV(LOG_TAG, "Semaphore giving: %s", toString().c_str());
126131
if (m_usePthreads) {
127132
pthread_mutex_unlock(&m_pthread_mutex);
128133
} else {
@@ -131,8 +136,8 @@ void FreeRTOS::Semaphore::give() {
131136
#ifdef ARDUINO_ARCH_ESP32
132137
FreeRTOS::sleep(10);
133138
#endif
134-
ESP_LOGV(LOG_TAG, "Semaphore giving: %s", toString().c_str());
135-
m_owner = "<N/A>";
139+
140+
m_owner = std::string("<N/A>");
136141
} // Semaphore::give
137142

138143

@@ -183,13 +188,15 @@ void FreeRTOS::Semaphore::take(std::string owner)
183188
* @param [in] timeoutMs Timeout in milliseconds.
184189
*/
185190
void FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
191+
186192
ESP_LOGV(LOG_TAG, "Semaphore taking: %s for %s", toString().c_str(), owner.c_str());
187-
m_owner = owner;
193+
188194
if (m_usePthreads) {
189195
assert(false);
190196
} else {
191197
xSemaphoreTake(m_semaphore, timeoutMs/portTICK_PERIOD_MS);
192198
}
199+
m_owner = owner;
193200
ESP_LOGV(LOG_TAG, "Semaphore taken: %s", toString().c_str());
194201
} // Semaphore::take
195202

cpp_utils/FreeRTOS.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ class FreeRTOS {
3131
public:
3232
Semaphore(std::string owner = "<Unknown>");
3333
~Semaphore();
34-
void give();
35-
void giveFromISR();
36-
void give(uint32_t value);
37-
void setName(std::string name);
38-
void take(std::string owner="<Unknown>");
39-
void take(uint32_t timeoutMs, std::string owner="<Unknown>");
40-
uint32_t wait(std::string owner="<Unknown>");
34+
void give();
35+
void give(uint32_t value);
36+
void giveFromISR();
37+
void setName(std::string name);
38+
void take(std::string owner="<Unknown>");
39+
void take(uint32_t timeoutMs, std::string owner="<Unknown>");
4140
std::string toString();
41+
uint32_t wait(std::string owner="<Unknown>");
42+
4243
private:
4344
SemaphoreHandle_t m_semaphore;
4445
pthread_mutex_t m_pthread_mutex;

cpp_utils/I2C.cpp

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "I2C.h"
1313
#include "sdkconfig.h"
1414
#include <esp_log.h>
15+
#include "GeneralUtils.h"
1516

1617
static const char* LOG_TAG = "I2C";
1718

@@ -27,6 +28,7 @@ I2C::I2C() {
2728
m_cmd = 0;
2829
m_sdaPin = DEFAULT_SDA_PIN;
2930
m_sclPin = DEFAULT_CLK_PIN;
31+
m_portNum = I2C_NUM_0;
3032
} // I2C
3133

3234

@@ -41,7 +43,10 @@ void I2C::beginTransaction() {
4143
ESP_LOGD(LOG_TAG, "beginTransaction()");
4244
}
4345
m_cmd = ::i2c_cmd_link_create();
44-
ESP_ERROR_CHECK(::i2c_master_start(m_cmd));
46+
esp_err_t errRc = ::i2c_master_start(m_cmd);
47+
if (errRc != ESP_OK) {
48+
ESP_LOGE(LOG_TAG, "i2c_master_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
49+
}
4550
m_directionKnown = false;
4651
} // beginTransaction
4752

@@ -57,8 +62,15 @@ void I2C::endTransaction() {
5762
if (debug) {
5863
ESP_LOGD(LOG_TAG, "endTransaction()");
5964
}
60-
ESP_ERROR_CHECK(::i2c_master_stop(m_cmd));
61-
ESP_ERROR_CHECK(::i2c_master_cmd_begin(I2C_NUM_0, m_cmd, 1000/portTICK_PERIOD_MS));
65+
esp_err_t errRc = ::i2c_master_stop(m_cmd);
66+
if (errRc != ESP_OK) {
67+
ESP_LOGE(LOG_TAG, "i2c_master_stop: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
68+
}
69+
70+
errRc = ::i2c_master_cmd_begin(m_portNum, m_cmd, 1000/portTICK_PERIOD_MS);
71+
if (errRc != ESP_OK) {
72+
ESP_LOGE(LOG_TAG, "i2c_master_stop: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
73+
}
6274
::i2c_cmd_link_delete(m_cmd);
6375
m_directionKnown = false;
6476
} // endTransaction
@@ -83,21 +95,30 @@ uint8_t I2C::getAddress() const
8395
* @param [in] sclPin The pin to use for SCL clock.
8496
* @return N/A.
8597
*/
86-
void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin) {
87-
ESP_LOGD(LOG_TAG, ">> I2c::init. address=%d, sda=%d, scl=%d", address, sdaPin, sclPin);
88-
this->m_sdaPin = sdaPin;
89-
this->m_sclPin = sclPin;
90-
this->m_address = address;
98+
void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t clockSpeed, i2c_port_t portNum) {
99+
ESP_LOGD(LOG_TAG, ">> I2c::init. address=%d, sda=%d, scl=%d, clockSpeed=%d, portNum=%d", address, sdaPin, sclPin, clockSpeed, portNum);
100+
assert(portNum < I2C_NUM_MAX);
101+
m_portNum = portNum;
102+
m_sdaPin = sdaPin;
103+
m_sclPin = sclPin;
104+
m_address = address;
105+
91106
i2c_config_t conf;
92-
conf.mode = I2C_MODE_MASTER;
107+
conf.mode = I2C_MODE_MASTER;
93108
conf.sda_io_num = sdaPin;
94109
conf.scl_io_num = sclPin;
95110
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
96111
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
97-
conf.master.clk_speed = 100000;
98-
ESP_ERROR_CHECK(::i2c_param_config(I2C_NUM_0, &conf));
112+
conf.master.clk_speed = 100000;
113+
esp_err_t errRc = ::i2c_param_config(m_portNum, &conf);
114+
if (errRc != ESP_OK) {
115+
ESP_LOGE(LOG_TAG, "i2c_param_config: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
116+
}
99117
if (!driverInstalled) {
100-
ESP_ERROR_CHECK(::i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
118+
errRc = ::i2c_driver_install(m_portNum, I2C_MODE_MASTER, 0, 0, 0);
119+
if (errRc != ESP_OK) {
120+
ESP_LOGE(LOG_TAG, "i2c_driver_install: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
121+
}
101122
driverInstalled = true;
102123
}
103124
} // init
@@ -117,9 +138,15 @@ void I2C::read(uint8_t* bytes, size_t length, bool ack) {
117138
}
118139
if (m_directionKnown == false) {
119140
m_directionKnown = true;
120-
ESP_ERROR_CHECK(::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_READ, !ack));
141+
esp_err_t errRc = ::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_READ, !ack);
142+
if (errRc != ESP_OK) {
143+
ESP_LOGE(LOG_TAG, "i2c_master_write_byte: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
144+
}
145+
}
146+
esp_err_t errRc = ::i2c_master_read(m_cmd, bytes, length, !ack);
147+
if (errRc != ESP_OK) {
148+
ESP_LOGE(LOG_TAG, "i2c_master_read: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
121149
}
122-
ESP_ERROR_CHECK(::i2c_master_read(m_cmd, bytes, length, !ack));
123150
} // read
124151

125152

@@ -136,7 +163,10 @@ void I2C::read(uint8_t *byte, bool ack) {
136163
}
137164
if (m_directionKnown == false) {
138165
m_directionKnown = true;
139-
ESP_ERROR_CHECK(::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_READ, !ack));
166+
esp_err_t errRc = ::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_READ, !ack);
167+
if (errRc != ESP_OK) {
168+
ESP_LOGE(LOG_TAG, "i2c_master_write_byte: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
169+
}
140170
}
141171
ESP_ERROR_CHECK(::i2c_master_read_byte(m_cmd, byte, !ack));
142172
} // readByte
@@ -200,7 +230,7 @@ bool I2C::slavePresent(uint8_t address) {
200230
::i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
201231
::i2c_master_stop(cmd);
202232

203-
esp_err_t espRc = ::i2c_master_cmd_begin(I2C_NUM_0, cmd, 100/portTICK_PERIOD_MS);
233+
esp_err_t espRc = ::i2c_master_cmd_begin(m_portNum, cmd, 100/portTICK_PERIOD_MS);
204234
i2c_cmd_link_delete(cmd);
205235
return espRc == 0; // Return true if the slave is present and false otherwise.
206236
} // slavePresent
@@ -214,7 +244,10 @@ void I2C::start() {
214244
if (debug) {
215245
ESP_LOGD(LOG_TAG, "start()");
216246
}
217-
ESP_ERROR_CHECK(::i2c_master_start(m_cmd));
247+
esp_err_t errRc = ::i2c_master_start(m_cmd);
248+
if (errRc != ESP_OK) {
249+
ESP_LOGE(LOG_TAG, "i2c_master_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
250+
}
218251
} // start
219252

220253

@@ -226,7 +259,10 @@ void I2C::stop() {
226259
if (debug) {
227260
ESP_LOGD(LOG_TAG, "stop()");
228261
}
229-
ESP_ERROR_CHECK(::i2c_master_stop(m_cmd));
262+
esp_err_t errRc = ::i2c_master_stop(m_cmd);
263+
if (errRc != ESP_OK) {
264+
ESP_LOGE(LOG_TAG, "i2c_master_stop: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
265+
}
230266
m_directionKnown = false;
231267
} // stop
232268

@@ -244,9 +280,15 @@ void I2C::write(uint8_t byte, bool ack) {
244280
}
245281
if (m_directionKnown == false) {
246282
m_directionKnown = true;
247-
ESP_ERROR_CHECK(::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_WRITE, !ack));
283+
esp_err_t errRc = ::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_WRITE, !ack);
284+
if (errRc != ESP_OK) {
285+
ESP_LOGE(LOG_TAG, "i2c_master_write_byte: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
286+
}
287+
}
288+
esp_err_t errRc = ::i2c_master_write_byte(m_cmd, byte, !ack);
289+
if (errRc != ESP_OK) {
290+
ESP_LOGE(LOG_TAG, "i2c_master_write_byte: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
248291
}
249-
ESP_ERROR_CHECK(::i2c_master_write_byte(m_cmd, byte, !ack));
250292
} // write
251293

252294

@@ -264,9 +306,15 @@ void I2C::write(uint8_t *bytes, size_t length, bool ack) {
264306
}
265307
if (m_directionKnown == false) {
266308
m_directionKnown = true;
267-
ESP_ERROR_CHECK(::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_WRITE, !ack));
309+
esp_err_t errRc = ::i2c_master_write_byte(m_cmd, (m_address << 1) | I2C_MASTER_WRITE, !ack);
310+
if (errRc != ESP_OK) {
311+
ESP_LOGE(LOG_TAG, "i2c_master_write_byte: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
312+
}
313+
}
314+
esp_err_t errRc = ::i2c_master_write(m_cmd, bytes, length, !ack);
315+
if (errRc != ESP_OK) {
316+
ESP_LOGE(LOG_TAG, "i2c_master_write: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
268317
}
269-
ESP_ERROR_CHECK(::i2c_master_write(m_cmd, bytes, length, !ack));
270318
} // write
271319

272320

cpp_utils/I2C.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,29 @@ class I2C {
2323
bool m_directionKnown;
2424
gpio_num_t m_sdaPin;
2525
gpio_num_t m_sclPin;
26+
i2c_port_t m_portNum;
2627

2728
public:
2829
/**
2930
* @brief The default SDA pin.
3031
*/
3132
static const gpio_num_t DEFAULT_SDA_PIN = GPIO_NUM_25;
33+
3234
/**
3335
* @brief The default Clock pin.
3436
*/
3537
static const gpio_num_t DEFAULT_CLK_PIN = GPIO_NUM_26;
3638

39+
/**
40+
* @brief The default Clock speed.
41+
*/
42+
static const uint32_t DEFAULT_CLK_SPEED = 100000;
43+
3744
I2C();
3845
void beginTransaction();
3946
void endTransaction();
4047
uint8_t getAddress() const;
41-
void init(uint8_t address, gpio_num_t sdaPin = DEFAULT_SDA_PIN, gpio_num_t sclPin = DEFAULT_CLK_PIN);
48+
void init(uint8_t address, gpio_num_t sdaPin = DEFAULT_SDA_PIN, gpio_num_t sclPin = DEFAULT_CLK_PIN, uint32_t clkSpeed = DEFAULT_CLK_SPEED, i2c_port_t portNum = I2C_NUM_0);
4249
void read(uint8_t* bytes, size_t length, bool ack=true);
4350
void read(uint8_t* byte, bool ack=true);
4451
void scan();

cpp_utils/JSON.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class JSON {
3333
*/
3434
class JsonArray {
3535
public:
36-
JsonArray(cJSON* node);
36+
3737
int getInt(int item);
3838
JsonObject getObject(int item);
3939
std::string getString(int item);
@@ -47,6 +47,7 @@ class JsonArray {
4747
std::string toString();
4848
std::size_t size();
4949
private:
50+
JsonArray(cJSON* node);
5051
friend class JSON;
5152
friend class JsonObject;
5253
/**
@@ -61,7 +62,6 @@ class JsonArray {
6162
*/
6263
class JsonObject {
6364
public:
64-
JsonObject(cJSON* node);
6565
JsonArray getArray(std::string name);
6666
bool getBoolean(std::string name);
6767
double getDouble(std::string name);
@@ -79,6 +79,7 @@ class JsonObject {
7979
std::string toString();
8080

8181
private:
82+
JsonObject(cJSON* node);
8283
friend class JSON;
8384
friend class JsonArray;
8485
/**

0 commit comments

Comments
 (0)
0