Skip to content

Commit b367e5a

Browse files
authored
Merge branch 'master' into code-cleanup
2 parents b17e346 + f8d6a65 commit b367e5a

17 files changed

+69
-63
lines changed

cpp_utils/BLEService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static const char* LOG_TAG = "BLEService"; // Tag for logging.
3535
* @param [in] uuid The UUID of the service.
3636
* @param [in] numHandles The maximum number of handles associated with the service.
3737
*/
38-
BLEService::BLEService(const char* uuid, uint32_t numHandles) : BLEService(BLEUUID(uuid), numHandles) {
38+
BLEService::BLEService(const char* uuid, uint16_t numHandles) : BLEService(BLEUUID(uuid), numHandles) {
3939
}
4040

4141

@@ -44,7 +44,7 @@ BLEService::BLEService(const char* uuid, uint32_t numHandles) : BLEService(BLEUU
4444
* @param [in] uuid The UUID of the service.
4545
* @param [in] numHandles The maximum number of handles associated with the service.
4646
*/
47-
BLEService::BLEService(BLEUUID uuid, uint32_t numHandles) {
47+
BLEService::BLEService(BLEUUID uuid, uint16_t numHandles) {
4848
m_uuid = uuid;
4949
m_handle = NULL_HANDLE;
5050
m_pServer = nullptr;

cpp_utils/BLEService.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class BLEService {
6565
uint8_t m_id = 0;
6666

6767
private:
68-
BLEService(const char* uuid, uint32_t numHandles);
69-
BLEService(BLEUUID uuid, uint32_t numHandles);
68+
BLEService(const char* uuid, uint16_t numHandles);
69+
BLEService(BLEUUID uuid, uint16_t numHandles);
7070
friend class BLEServer;
7171
friend class BLEServiceMap;
7272
friend class BLEDescriptor;
@@ -84,7 +84,7 @@ class BLEService {
8484
FreeRTOS::Semaphore m_semaphoreStartEvt = FreeRTOS::Semaphore("StartEvt");
8585
FreeRTOS::Semaphore m_semaphoreStopEvt = FreeRTOS::Semaphore("StopEvt");
8686

87-
uint32_t m_numHandles;
87+
uint16_t m_numHandles;
8888

8989
BLECharacteristic* getLastCreatedCharacteristic();
9090
void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);

cpp_utils/FreeRTOS.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void FreeRTOS::sleep(uint32_t ms) {
3232
* @param[in] param An optional parameter to be passed to the started task.
3333
* @param[in] stackSize An optional paremeter supplying the size of the stack in which to run the task.
3434
*/
35-
void FreeRTOS::startTask(void task(void*), std::string taskName, void* param, int stackSize) {
35+
void FreeRTOS::startTask(void task(void*), std::string taskName, void* param, uint32_t stackSize) {
3636
::xTaskCreate(task, taskName.data(), stackSize, param, 5, NULL);
3737
} // startTask
3838

@@ -162,7 +162,7 @@ bool FreeRTOS::Semaphore::take(std::string owner) {
162162
if (m_usePthreads) {
163163
pthread_mutex_lock(&m_pthread_mutex);
164164
} else {
165-
rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY);
165+
rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY) == pdTRUE;
166166
}
167167
m_owner = owner;
168168
if (rc) {
@@ -187,7 +187,7 @@ bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
187187
if (m_usePthreads) {
188188
assert(false); // We apparently don't have a timed wait for pthreads.
189189
} else {
190-
rc = ::xSemaphoreTake(m_semaphore, timeoutMs / portTICK_PERIOD_MS);
190+
rc = ::xSemaphoreTake(m_semaphore, timeoutMs / portTICK_PERIOD_MS) == pdTRUE;
191191
}
192192
m_owner = owner;
193193
if (rc) {
@@ -262,8 +262,8 @@ void Ringbuffer::returnItem(void* item) {
262262
* @param [in] wait How long to wait before giving up. The default is to wait indefinitely.
263263
* @return
264264
*/
265-
uint32_t Ringbuffer::send(void* data, size_t length, TickType_t wait) {
266-
return ::xRingbufferSend(m_handle, data, length, wait);
265+
bool Ringbuffer::send(void* data, size_t length, TickType_t wait) {
266+
return ::xRingbufferSend(m_handle, data, length, wait) == pdTRUE;
267267
} // send
268268

269269

cpp_utils/FreeRTOS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class FreeRTOS {
2424
public:
2525
static void sleep(uint32_t ms);
26-
static void startTask(void task(void*), std::string taskName, void* param = nullptr, int stackSize = 2048);
26+
static void startTask(void task(void*), std::string taskName, void* param = nullptr, uint32_t stackSize = 2048);
2727
static void deleteTask(TaskHandle_t pTask = nullptr);
2828

2929
static uint32_t getTimeSinceStart();
@@ -63,7 +63,7 @@ class Ringbuffer {
6363

6464
void* receive(size_t* size, TickType_t wait = portMAX_DELAY);
6565
void returnItem(void* item);
66-
uint32_t send(void* data, size_t length, TickType_t wait = portMAX_DELAY);
66+
bool send(void* data, size_t length, TickType_t wait = portMAX_DELAY);
6767
private:
6868
RingbufHandle_t m_handle;
6969
};

cpp_utils/GPIO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void ESP32CPP::GPIO::low(gpio_num_t pin) {
117117
* @return True if the pin is high, false if the pin is low.
118118
*/
119119
bool ESP32CPP::GPIO::read(gpio_num_t pin) {
120-
return ::gpio_get_level(pin);
120+
return ::gpio_get_level(pin) == 1;
121121
} // read
122122

123123

@@ -179,7 +179,7 @@ void ESP32CPP::GPIO::setOutput(gpio_num_t pin) {
179179
*/
180180
void ESP32CPP::GPIO::write(gpio_num_t pin, bool value) {
181181
//ESP_LOGD(LOG_TAG, ">> write: pin: %d, value: %d", pin, value);
182-
esp_err_t errRc = ::gpio_set_level(pin, value);
182+
esp_err_t errRc = ::gpio_set_level(pin, value ? 1 : 0);
183183
if (errRc != ESP_OK) {
184184
ESP_LOGE(LOG_TAG, "<< gpio_set_level: pin=%d, rc=%d %s", pin, errRc, GeneralUtils::errorToString(errRc));
185185
}

cpp_utils/I2C.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ uint8_t I2C::getAddress() const {
9494
* @param [in] sclPin The pin to use for SCL clock.
9595
* @return N/A.
9696
*/
97-
void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t clockSpeed, i2c_port_t portNum) {
97+
void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t clockSpeed, i2c_port_t portNum, bool pullup) {
9898
ESP_LOGD(LOG_TAG, ">> I2c::init. address=%d, sda=%d, scl=%d, clockSpeed=%d, portNum=%d", address, sdaPin, sclPin, clockSpeed, portNum);
9999
assert(portNum < I2C_NUM_MAX);
100100
m_portNum = portNum;
@@ -106,8 +106,8 @@ void I2C::init(uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t c
106106
conf.mode = I2C_MODE_MASTER;
107107
conf.sda_io_num = sdaPin;
108108
conf.scl_io_num = sclPin;
109-
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
110-
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
109+
conf.sda_pullup_en = pullup ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
110+
conf.scl_pullup_en = pullup ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
111111
conf.master.clk_speed = clockSpeed;
112112
esp_err_t errRc = ::i2c_param_config(m_portNum, &conf);
113113
if (errRc != ESP_OK) {

cpp_utils/I2C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class I2C {
3737
void beginTransaction();
3838
void endTransaction();
3939
uint8_t getAddress() const;
40-
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);
40+
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, bool pullup = true);
4141
void read(uint8_t* bytes, size_t length, bool ack = true);
4242
void read(uint8_t* byte, bool ack = true);
4343
void scan();

cpp_utils/Memory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ size_t Memory::m_lastHeapSize = 0;
4747

4848
/* STATIC */ void Memory::dumpHeapChange(std::string tag) {
4949
size_t currentUsage = heap_caps_get_free_size(MALLOC_CAP_8BIT);
50-
int diff = currentUsage - m_lastHeapSize;
50+
size_t diff = currentUsage - m_lastHeapSize;
5151
ESP_LOGD(LOG_TAG, "%s: Heap changed by %d bytes (%d to %d)", tag.c_str(), diff, m_lastHeapSize, currentUsage);
5252
m_lastHeapSize = currentUsage;
5353
} // dumpHeapChange

cpp_utils/NeoPixelWiFiEventHandler.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* Author: kolban
66
*/
77
#include <stdio.h>
8+
#include <esp_log.h>
89
#include "NeoPixelWiFiEventHandler.h"
910

11+
static const char* LOG_TAG = "NeoPixelWiFiEventHandler";
12+
1013
NeoPixelWiFiEventHandler::NeoPixelWiFiEventHandler(gpio_num_t gpioPin) {
1114
this->gpioPin = gpioPin;
1215
ws2812 = new WS2812(gpioPin, 8);
@@ -17,42 +20,42 @@ NeoPixelWiFiEventHandler::~NeoPixelWiFiEventHandler() {
1720
}
1821

1922
esp_err_t NeoPixelWiFiEventHandler::apStart() {
20-
printf("XXX apStart\n");
23+
ESP_LOGD(LOG_TAG, "XXX apStart");
2124
ws2812->setPixel(0, 0, 00, 64);
2225
ws2812->show();
2326
return ESP_OK;
2427
}
2528

2629
esp_err_t NeoPixelWiFiEventHandler::staConnected(system_event_sta_connected_t info) {
27-
printf("XXX staConnected\n");
30+
ESP_LOGD(LOG_TAG, "XXX staConnected");
2831
ws2812->setPixel(0, 57, 89, 66);
2932
ws2812->show();
3033
return ESP_OK;
3134
}
3235

3336
esp_err_t NeoPixelWiFiEventHandler::staDisconnected(system_event_sta_disconnected_t info) {
34-
printf("XXX staDisconnected\n");
37+
ESP_LOGD(LOG_TAG, "XXX staDisconnected");
3538
ws2812->setPixel(0, 64, 0, 0);
3639
ws2812->show();
3740
return ESP_OK;
3841
}
3942

4043
esp_err_t NeoPixelWiFiEventHandler::staStart() {
41-
printf("XXX staStart\n");
44+
ESP_LOGD(LOG_TAG, "XXX staStart");
4245
ws2812->setPixel(0, 64, 64, 0);
4346
ws2812->show();
4447
return ESP_OK;
4548
}
4649

4750
esp_err_t NeoPixelWiFiEventHandler::staGotIp(system_event_sta_got_ip_t info) {
48-
printf("XXX staGotIp\n");
51+
ESP_LOGD(LOG_TAG, "XXX staGotIp");
4952
ws2812->setPixel(0, 0, 64, 0);
5053
ws2812->show();
5154
return ESP_OK;
5255
}
5356

5457
esp_err_t NeoPixelWiFiEventHandler::wifiReady() {
55-
printf("XXX wifiReady\n");
58+
ESP_LOGD(LOG_TAG, "XXX wifiReady");
5659
ws2812->setPixel(0, 64, 64, 0);
5760
ws2812->show();
5861
return ESP_OK;

cpp_utils/PCF8574.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
* @param [in] address The %I2C address of the device on the %I2C bus.
1919
*/
2020
PCF8574::PCF8574(uint8_t address) {
21-
i2c.setAddress(address);
21+
i2c = new I2C();
22+
i2c->setAddress(address);
2223
lastWrite = 0;
2324
}
2425

@@ -27,6 +28,7 @@ PCF8574::PCF8574(uint8_t address) {
2728
* @brief Class instance destructor.
2829
*/
2930
PCF8574::~PCF8574() {
31+
delete i2c;
3032
}
3133

3234

@@ -36,9 +38,9 @@ PCF8574::~PCF8574() {
3638
*/
3739
uint8_t PCF8574::read() {
3840
uint8_t value;
39-
i2c.beginTransaction();
40-
i2c.read(&value,true);
41-
i2c.endTransaction();
41+
i2c->beginTransaction();
42+
i2c->read(&value,true);
43+
i2c->endTransaction();
4244
return value;
4345
} // read
4446

@@ -65,9 +67,9 @@ void PCF8574::write(uint8_t value) {
6567
if (invert) {
6668
value = ~value;
6769
}
68-
i2c.beginTransaction();
69-
i2c.write(value, true);
70-
i2c.endTransaction();
70+
i2c->beginTransaction();
71+
i2c->write(value, true);
72+
i2c->endTransaction();
7173
lastWrite = value;
7274
} // write
7375

@@ -114,5 +116,5 @@ void PCF8574::setInvert(bool value) {
114116
* @param [in] clkPin The pin to use for the %I2C CLK functions.
115117
*/
116118
void PCF8574::init(gpio_num_t sdaPin, gpio_num_t clkPin) {
117-
i2c.init(0, sdaPin, clkPin);
119+
i2c->init(0, sdaPin, clkPin);
118120
} // init

cpp_utils/PCF8574.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PCF8574 {
2929
void writeBit(uint8_t bit, bool value);
3030

3131
private:
32-
I2C i2c = I2C();
32+
I2C* i2c;
3333
uint8_t lastWrite;
3434
bool invert = false;
3535

cpp_utils/PCF8575.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
* @param [in] address The %I2C address of the device on the %I2C bus.
2020
*/
2121
PCF8575::PCF8575(uint8_t address) {
22-
i2c.setAddress(address);
22+
i2c = new I2C();
23+
i2c->setAddress(address);
2324
m_lastWrite = 0;
2425
}
2526

@@ -28,6 +29,7 @@ PCF8575::PCF8575(uint8_t address) {
2829
* @brief Class instance destructor.
2930
*/
3031
PCF8575::~PCF8575() {
32+
delete i2c;
3133
}
3234

3335

@@ -37,10 +39,10 @@ PCF8575::~PCF8575() {
3739
*/
3840
uint16_t PCF8575::read() {
3941
uint16_t value;
40-
i2c.beginTransaction();
41-
i2c.read((uint8_t*) &value, true);
42-
i2c.read(((uint8_t*) &value) + 1, true);
43-
i2c.endTransaction();
42+
i2c->beginTransaction();
43+
i2c->read((uint8_t*) &value, true);
44+
i2c->read(((uint8_t*) &value) + 1, true);
45+
i2c->endTransaction();
4446
return value;
4547
} // read
4648

@@ -67,10 +69,10 @@ void PCF8575::write(uint16_t value) {
6769
if (invert) {
6870
value = ~value;
6971
}
70-
i2c.beginTransaction();
71-
i2c.write(value & 0xff, true);
72-
i2c.write((value >> 8) & 0xff, true);
73-
i2c.endTransaction();
72+
i2c->beginTransaction();
73+
i2c->write(value & 0xff, true);
74+
i2c->write((value >> 8) & 0xff, true);
75+
i2c->endTransaction();
7476
m_lastWrite = value;
7577
} // write
7678

@@ -117,5 +119,5 @@ void PCF8575::setInvert(bool value) {
117119
* @param [in] clkPin The pin to use for the %I2C CLK functions.
118120
*/
119121
void PCF8575::init(gpio_num_t sdaPin, gpio_num_t clkPin) {
120-
i2c.init(0, sdaPin, clkPin);
122+
i2c->init(0, sdaPin, clkPin);
121123
} // init

cpp_utils/PCF8575.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class PCF8575 {
2929
void writeBit(uint16_t bit, bool value);
3030

3131
private:
32-
I2C i2c = I2C();
33-
uint8_t m_lastWrite;
32+
I2C* i2c;
33+
uint16_t m_lastWrite;
3434
bool invert = false;
3535
};
3636

cpp_utils/PWM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,5 @@ void PWM::setFrequency(uint32_t freq) {
145145
* @return N/A.
146146
*/
147147
void PWM::stop(bool idleLevel) {
148-
ESP_ERROR_CHECK(::ledc_stop(LEDC_HIGH_SPEED_MODE, m_channel, idleLevel));
148+
ESP_ERROR_CHECK(::ledc_stop(LEDC_HIGH_SPEED_MODE, m_channel, idleLevel ? 1 : 0));
149149
} // stop

cpp_utils/PubSubClient.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ void PubSubClient::setup() {
188188
UNSUBACK_Outstanding = false;
189189

190190
keepAliveTimer = new FreeRTOSTimer((char*) "keepAliveTimer",
191-
(MQTT_KEEPALIVE * 1000) / portTICK_PERIOD_MS, true, this,
191+
(MQTT_KEEPALIVE * 1000) / portTICK_PERIOD_MS, pdTRUE, this,
192192
keepAliveTimerMapper);
193193
timeoutTimer = new FreeRTOSTimer((char*) "timeoutTimer",
194-
(MQTT_KEEPALIVE * 1000) / portTICK_PERIOD_MS, true, this,
194+
(MQTT_KEEPALIVE * 1000) / portTICK_PERIOD_MS, pdTRUE, this,
195195
timeoutTimerMapper);
196196
m_task = new PubSubClientTask("PubSubClientTask");
197197
} // setup
@@ -399,8 +399,7 @@ bool PubSubClient::connect() {
399399
* @param N/A.
400400
* @return Number of received bytes.
401401
*/
402-
uint16_t PubSubClient::readPacket() {
403-
402+
size_t PubSubClient::readPacket() {
404403
size_t res = _client->receive(buffer, MQTT_MAX_PACKET_SIZE);
405404

406405
if (res > MQTT_MAX_PACKET_SIZE) {

cpp_utils/PubSubClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class PubSubClient {
160160

161161
MQTT_CALLBACK_SIGNATURE;
162162
void setup();
163-
uint16_t readPacket();
163+
size_t readPacket();
164164
bool write(uint8_t header, uint8_t* buf, uint16_t length);
165165
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
166166
void parseData(mqtt_message* msg, uint16_t len);

0 commit comments

Comments
 (0)