Skip to content

Fixed memory leaks in rainmaker examples #7965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,9 @@ esp32wrover.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs
esp32wrover.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080
esp32wrover.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS)
esp32wrover.menu.PartitionScheme.fatflash.build.partitions=ffat
esp32wrover.menu.PartitionScheme.rainmaker=RainMaker
esp32wrover.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32wrover.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728

esp32wrover.menu.FlashMode.qio=QIO
esp32wrover.menu.FlashMode.qio.build.flash_mode=dio
Expand Down Expand Up @@ -1238,6 +1241,9 @@ esp32s3box.menu.PartitionScheme.fatflash.upload.maximum_size=2097152
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB=16M Flash (3MB APP/9.9MB FATFS)
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.build.partitions=app3M_fat9M_16MB
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.upload.maximum_size=3145728
esp32s3box.menu.PartitionScheme.rainmaker=RainMaker
esp32s3box.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32s3box.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728

esp32s3box.menu.DebugLevel.none=None
esp32s3box.menu.DebugLevel.none.build.code_debug=0
Expand Down Expand Up @@ -1686,6 +1692,9 @@ esp32wroverkit.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs
esp32wroverkit.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080
esp32wroverkit.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS)
esp32wroverkit.menu.PartitionScheme.fatflash.build.partitions=ffat
esp32wroverkit.menu.PartitionScheme.rainmaker=RainMaker
esp32wroverkit.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32wroverkit.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728
esp32wroverkit.menu.FlashMode.qio=QIO
esp32wroverkit.menu.FlashMode.qio.build.flash_mode=dio
esp32wroverkit.menu.FlashMode.qio.build.boot=qio
Expand Down
31 changes: 18 additions & 13 deletions libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool dimmer_state = true;

// The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
// But, you can also define custom devices using the 'Device' base class object, as shown here
static Device my_device("Dimmer", "custom.device.dimmer", &gpio_dimmer);
static Device *my_device = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
Expand Down Expand Up @@ -71,22 +71,25 @@ void setup()

Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node");

my_device = new Device("Dimmer", "custom.device.dimmer", &gpio_dimmer);
if (!my_device) {
return;
}
//Create custom dimmer device
my_device.addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
my_device->addNameParam();
my_device->addPowerParam(DEFAULT_POWER_MODE);
my_device->assignPrimaryParam(my_device->getParamByName(ESP_RMAKER_DEF_POWER_NAME));

//Create and add a custom level parameter
Param level_param("Level", "custom.param.level", value(DEFAULT_DIMMER_LEVEL), PROP_FLAG_READ | PROP_FLAG_WRITE);
level_param.addBounds(value(0), value(100), value(1));
level_param.addUIType(ESP_RMAKER_UI_SLIDER);
my_device.addParam(level_param);
my_device->addParam(level_param);

my_device.addCb(write_callback);
my_device->addCb(write_callback);

//Add custom dimmer device to the node
my_node.addDevice(my_device);
my_node.addDevice(*my_device);

//This is optional
RMaker.enableOTA(OTA_USING_TOPICS);
Expand Down Expand Up @@ -130,11 +133,13 @@ void loop()
RMakerWiFiReset(2);
} else {
// Toggle device state
dimmer_state = !dimmer_state;
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
}
dimmer_state = !dimmer_state;
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
if (my_device) {
my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
}
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow same styling across the file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are losing time with style issues when an automatic script with clang formatter could keep all PR at same pattern.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a script in IDF which uses astyle programme to achieve this: here. However this has to be run manually for this repo.

If you have a change for using automated clang formatter, would you please raise a PR for the same?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert in this subject otherwise I could make a PR. Actually I'm using uncrustify in VSCode only.

Anyway there's an action with example for artistic style here:
https://github.com/marketplace/actions/artistic-style

}
delay(100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool power_state = true;

// The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
// But, you can also define custom devices using the 'Device' base class object, as shown here
static Device my_device("Air Cooler", "my.device.air-cooler", NULL);
static Device *my_device = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
Expand Down Expand Up @@ -122,31 +122,34 @@ void setup()

Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node");

my_device = new Device("Air Cooler", "my.device.air-cooler", NULL);
if (!my_device) {
return;
}
//Create custom air cooler device
my_device.addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
my_device->addNameParam();
my_device->addPowerParam(DEFAULT_POWER_MODE);
my_device->assignPrimaryParam(my_device->getParamByName(ESP_RMAKER_DEF_POWER_NAME));

Param swing("Swing", ESP_RMAKER_PARAM_TOGGLE, value(DEFAULT_SWING), PROP_FLAG_READ | PROP_FLAG_WRITE);
swing.addUIType(ESP_RMAKER_UI_TOGGLE);
my_device.addParam(swing);
my_device->addParam(swing);

Param speed("Speed", ESP_RMAKER_PARAM_RANGE, value(DEFAULT_SPEED), PROP_FLAG_READ | PROP_FLAG_WRITE);
speed.addUIType(ESP_RMAKER_UI_SLIDER);
speed.addBounds(value(0), value(255), value(1));
my_device.addParam(speed);
my_device->addParam(speed);

static const char* modes[] = { "Auto", "Cool", "Heat" };
Param mode_param("Mode", ESP_RMAKER_PARAM_MODE, value("Auto"), PROP_FLAG_READ | PROP_FLAG_WRITE);
mode_param.addValidStrList(modes, 3);
mode_param.addUIType(ESP_RMAKER_UI_DROPDOWN);
my_device.addParam(mode_param);
my_device->addParam(mode_param);

my_device.addCb(write_callback);
my_device->addCb(write_callback);

//Add custom Air Cooler device to the node
my_node.addDevice(my_device);
my_node.addDevice(*my_device);

//This is optional
// RMaker.enableOTA(OTA_USING_TOPICS);
Expand Down Expand Up @@ -181,19 +184,21 @@ void loop()
int press_duration = millis() - startTime;

if (press_duration > 10000) {
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
} else if (press_duration > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
} else {
// Toggle device state
power_state = !power_state;
Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state);
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
// Toggle device state
power_state = !power_state;
Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false");
if (my_device) {
my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state);
}
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
}
}
delay(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ LightSwitch switch_ch1 = {gpio_switch1, false};
LightSwitch switch_ch2 = {gpio_switch2, false};

//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static Switch my_switch1;
static Switch my_switch2;
static Switch *my_switch1 = NULL;
static Switch *my_switch2 = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
Expand Down Expand Up @@ -125,16 +125,19 @@ void setup()
my_node = RMaker.initNode("Sonoff Dual R3");

//Initialize switch device
my_switch1 = Switch("Switch_ch1", &gpio_relay1);
my_switch2 = Switch("Switch_ch2", &gpio_relay2);
my_switch1 = new Switch("Switch_ch1", &gpio_relay1);
my_switch2 = new Switch("Switch_ch2", &gpio_relay2);

if (!my_switch1 || !my_switch2) {
return;
}
//Standard switch device
my_switch1.addCb(write_callback);
my_switch2.addCb(write_callback);
my_switch1->addCb(write_callback);
my_switch2->addCb(write_callback);

//Add switch device to the node
my_node.addDevice(my_switch1);
my_node.addDevice(my_switch2);
my_node.addDevice(*my_switch1);
my_node.addDevice(*my_switch2);

//This is optional
RMaker.enableOTA(OTA_USING_TOPICS);
Expand Down Expand Up @@ -173,15 +176,19 @@ void loop()
// Toggle switch 1 device state
switch_state_ch1 = !switch_state_ch1;
Serial.printf("Toggle State to %s.\n", switch_state_ch1 ? "true" : "false");
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1);
if (my_switch1) {
my_switch1->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1);
}
(switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH);
} else if (switch_ch2.pressed) {
Serial.printf("Switch 2 has been changed\n");
switch_ch2.pressed = false;
// Toggle switch 2 device state
switch_state_ch2 = !switch_state_ch2;
Serial.printf("Toggle State to %s.\n", switch_state_ch2 ? "true" : "false");
my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2);
if (my_switch2) {
my_switch2->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2);
}
(switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH);
}

Expand Down
30 changes: 17 additions & 13 deletions libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static int gpio_switch = 16;
bool switch_state = true;

//The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor.
static Switch my_switch;
static Switch *my_switch = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
Expand Down Expand Up @@ -69,13 +69,15 @@ void setup()
my_node = RMaker.initNode("ESP RainMaker Node");

//Initialize switch device
my_switch = Switch("Switch", &gpio_switch);

my_switch = new Switch("Switch", &gpio_switch);
if(!my_switch) {
return;
}
//Standard switch device
my_switch.addCb(write_callback);
my_switch->addCb(write_callback);

//Add switch device to the node
my_node.addDevice(my_switch);
my_node.addDevice(*my_switch);

//This is optional
RMaker.enableOTA(OTA_USING_TOPICS);
Expand Down Expand Up @@ -111,17 +113,19 @@ void loop()

if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow consistent styling.

Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
} else {
// Toggle device state
switch_state = !switch_state;
Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false");
my_switch.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state);
switch_state = !switch_state;
Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false");
if(my_switch) {
my_switch->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state);
}
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
}
}
Expand Down
16 changes: 8 additions & 8 deletions libraries/RainMaker/src/RMakerDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ esp_err_t Device::deleteDevice()
{
err = esp_rmaker_device_delete(getDeviceHandle());
if(err != ESP_OK) {
log_e("Device deletion error");
log_e("Failed to delete device");
return err;
}
return ESP_OK;
Expand All @@ -45,7 +45,7 @@ void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb)
read_cb = readCb;
err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback);
if(err != ESP_OK) {
log_e("Callback register error");
log_e("Failed to register callback");
}
}

Expand All @@ -64,7 +64,7 @@ esp_err_t Device::addParam(Param parameter)
{
err = esp_rmaker_device_add_param(getDeviceHandle(), parameter.getParamHandle());
if(err != ESP_OK) {
log_e("Adding custom parameter error");
log_e("Failed to add custom parameter");
return err;
}
return ESP_OK;
Expand Down Expand Up @@ -140,7 +140,7 @@ esp_err_t Device::assignPrimaryParam(param_handle_t *param)
{
err = esp_rmaker_device_assign_primary_param(getDeviceHandle(), param);
if(err != ESP_OK){
log_e("Assigning primary param error");
log_e("Failed to assign primary parameter");
}
return err;
}
Expand All @@ -157,7 +157,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, bool my_val)
param_val_t val = esp_rmaker_bool(my_val);
err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) {
log_e("Update paramter failed");
log_e("Update parameter failed");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val ? "true" : "false");
Expand All @@ -171,7 +171,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, int my_val)
param_val_t val = esp_rmaker_int(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) {
log_e("Update paramter failed");
log_e("Update parameter failed");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %d", getDeviceName(), param_name, my_val);
Expand All @@ -185,7 +185,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, float my_val)
param_val_t val = esp_rmaker_float(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) {
log_e("Update paramter failed");
log_e("Update parameter failed");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %f", getDeviceName(), param_name, my_val);
Expand All @@ -199,7 +199,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, const char *my_va
param_val_t val = esp_rmaker_str(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) {
log_e("Update paramter failed");
log_e("Update parameter failed");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val);
Expand Down