arduino-esp32/libraries/RainMaker/src/RMakerDevice.cpp
Me No Dev 5502879a5b
v2.0.0 Add support for ESP32S2 and update ESP-IDF to 4.4 (#4996)
This is very much still work in progress and much more will change before the final 2.0.0

Some APIs have changed. New libraries have been added. LittleFS included.

Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Mike Dunston <m_dunston@comcast.net>
Co-authored-by: Unexpected Maker <seon@unexpectedmaker.com>
Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: microDev <70126934+microDev1@users.noreply.github.com>
Co-authored-by: tobozo <tobozo@users.noreply.github.com>
Co-authored-by: bobobo1618 <bobobo1618@users.noreply.github.com>
Co-authored-by: lorol <lorolouis@gmail.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Limor "Ladyada" Fried <limor@ladyada.net>
Co-authored-by: Sweety <switi.mhaiske@espressif.com>
Co-authored-by: Loick MAHIEUX <loick111@gmail.com>
Co-authored-by: Larry Bernstone <lbernstone@gmail.com>
Co-authored-by: Valerii Koval <valeros@users.noreply.github.com>
Co-authored-by: 快乐的我531 <2302004040@qq.com>
Co-authored-by: chegewara <imperiaonline4@gmail.com>
Co-authored-by: Clemens Kirchgatterer <clemens@1541.org>
Co-authored-by: Aron Rubin <aronrubin@gmail.com>
Co-authored-by: Pete Lewis <601236+lewispg228@users.noreply.github.com>
2021-04-05 14:23:58 +03:00

210 lines
6.7 KiB
C++

#include "RMakerDevice.h"
#if ESP_IDF_VERSION_MAJOR >= 4 && CONFIG_IDF_TARGET_ESP32
static esp_err_t err;
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
void (*write_cb)(Device*, Param*, param_val_t, void*, write_ctx_t*);
void (*read_cb)(Device*, Param*, void*, read_ctx_t*);
Device device;
Param param;
static esp_err_t write_callback(const device_handle_t *dev_handle, const param_handle_t *par_handle, const param_val_t val, void *priv_data, write_ctx_t *ctx)
{
device.setDeviceHandle(dev_handle);
param.setParamHandle(par_handle);
write_cb(&device, &param, val, priv_data, ctx);
return ESP_OK;
}
static esp_err_t read_callback(const device_handle_t *dev_handle, const param_handle_t *par_handle, void *priv_data, read_ctx_t *ctx)
{
device.setDeviceHandle(dev_handle);
param.setParamHandle(par_handle);
read_cb(&device, &param, priv_data, ctx);
return ESP_OK;
}
esp_err_t Device::deleteDevice()
{
err = esp_rmaker_device_delete(getDeviceHandle());
if(err != ESP_OK) {
log_e("Device deletion error");
return err;
}
return ESP_OK;
}
void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb)
{
write_cb = writeCb;
read_cb = readCb;
err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback);
if(err != ESP_OK) {
log_e("Callback register error");
}
}
esp_err_t Device::addDeviceAttr(const char *attr_name, const char *val)
{
err = esp_rmaker_device_add_attribute(getDeviceHandle(), attr_name, val);
if(err != ESP_OK) {
log_e("Failed to add attriute to the device");
return err;
}
return ESP_OK;
}
//Generic Parameter
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");
return err;
}
return ESP_OK;
}
//Standard Device Parameter
esp_err_t Device::addNameParam(const char *param_name)
{
param_handle_t *param = esp_rmaker_name_param_create(param_name, getDeviceName());
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addPowerParam(bool val, const char *param_name)
{
param_handle_t *param = esp_rmaker_power_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addBrightnessParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_brightness_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addHueParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_hue_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addSaturationParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_saturation_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addIntensityParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_intensity_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addCCTParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_cct_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addDirectionParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_direction_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addSpeedParam(int val, const char *param_name)
{
param_handle_t *param = esp_rmaker_speed_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
esp_err_t Device::addTempratureParam(float val, const char *param_name)
{
param_handle_t *param = esp_rmaker_temperature_param_create(param_name, val);
return esp_rmaker_device_add_param(getDeviceHandle(), param);
}
param_handle_t *Device::getParamByName(const char *param_name)
{
return esp_rmaker_device_get_param_by_name(getDeviceHandle(), param_name);
}
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");
}
return err;
}
const param_handle_t* getParamHandlebyName(const esp_rmaker_device_t *device_handle, const char *param_name)
{
const param_handle_t *param = esp_rmaker_device_get_param_by_name(device_handle, param_name);
return param;
}
esp_err_t Device::updateAndReportParam(const char *param_name, bool my_val)
{
const param_handle_t *param = getParamHandlebyName(getDeviceHandle(), param_name);
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");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val ? "true" : "false");
}
return ESP_OK;
}
esp_err_t Device::updateAndReportParam(const char *param_name, int my_val)
{
const param_handle_t *param = getParamHandlebyName(getDeviceHandle(), param_name);
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");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %d", getDeviceName(), param_name, my_val);
}
return ESP_OK;
}
esp_err_t Device::updateAndReportParam(const char *param_name, float my_val)
{
const param_handle_t *param = getParamHandlebyName(getDeviceHandle(), param_name);
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");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %f", getDeviceName(), param_name, my_val);
}
return ESP_OK;
}
esp_err_t Device::updateAndReportParam(const char *param_name, const char *my_val)
{
const param_handle_t *param = getParamHandlebyName(getDeviceHandle(), param_name);
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");
return err;
}else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val);
}
return ESP_OK;
}
#endif