add config period
Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
@@ -44,6 +44,7 @@ enum nconfig_type
|
||||
USB_CURRENT_LIMIT, ///< The maximum current limit for the USB out.
|
||||
PAGE_USERNAME, ///< Webpage username
|
||||
PAGE_PASSWORD, ///< Webpage password
|
||||
SENSOR_PERIOD_MS, ///< Sensor period
|
||||
NCONFIG_TYPE_MAX, ///< Sentinel for the maximum number of configuration types.
|
||||
};
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ const static char* keys[NCONFIG_TYPE_MAX] = {
|
||||
[USB_CURRENT_LIMIT] = "usb_climit",
|
||||
[PAGE_USERNAME] = "username",
|
||||
[PAGE_PASSWORD] = "password",
|
||||
[SENSOR_PERIOD_MS] = "sensor_period",
|
||||
};
|
||||
|
||||
struct default_value
|
||||
@@ -54,6 +55,7 @@ struct default_value const default_values[] = {
|
||||
{USB_CURRENT_LIMIT, "3.0"},
|
||||
{PAGE_USERNAME, "admin"},
|
||||
{PAGE_PASSWORD, "password"},
|
||||
{SENSOR_PERIOD_MS, "1000"},
|
||||
};
|
||||
|
||||
esp_err_t init_nconfig()
|
||||
|
||||
@@ -289,6 +289,25 @@ void init_status_monitor()
|
||||
xTaskCreate(shutdown_load_sw_task, "shutdown_sw_task", configMINIMAL_STACK_SIZE * 3, NULL, 15,
|
||||
&shutdown_task_handle);
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(sensor_timer, 1000000));
|
||||
nconfig_read(SENSOR_PERIOD_MS, buf, sizeof(buf));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(sensor_timer, strtol(buf, NULL, 10) * 1000));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(wifi_status_timer, 1000000 * 5));
|
||||
}
|
||||
|
||||
esp_err_t update_sensor_period(int period)
|
||||
{
|
||||
if (period < 500 || period > 10000) // 0.5 sec ~ 10 sec
|
||||
{
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
char buf[10];
|
||||
sprintf(buf, "%d", period);
|
||||
esp_err_t err = nconfig_write(SENSOR_PERIOD_MS, buf);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_timer_stop(sensor_timer);
|
||||
return esp_timer_start_periodic(sensor_timer, period * 1000);
|
||||
}
|
||||
@@ -20,5 +20,6 @@ typedef struct
|
||||
} sensor_data_t;
|
||||
|
||||
void init_status_monitor();
|
||||
esp_err_t update_sensor_period(int period);
|
||||
|
||||
#endif // ODROID_REMOTE_HTTP_MONITOR_H
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_timer.h"
|
||||
#include "monitor.h"
|
||||
#include "nconfig.h"
|
||||
#include "webserver.h"
|
||||
#include "wifi.h"
|
||||
@@ -47,6 +48,11 @@ static esp_err_t setting_get_handler(httpd_req_t* req)
|
||||
cJSON_AddStringToObject(root, "baudrate", buf);
|
||||
}
|
||||
|
||||
if (nconfig_read(SENSOR_PERIOD_MS, buf, sizeof(buf)) == ESP_OK)
|
||||
{
|
||||
cJSON_AddStringToObject(root, "period", buf);
|
||||
}
|
||||
|
||||
// Add current limits to the response
|
||||
if (nconfig_read(VIN_CURRENT_LIMIT, buf, sizeof(buf)) == ESP_OK)
|
||||
{
|
||||
@@ -174,6 +180,7 @@ static esp_err_t setting_post_handler(httpd_req_t* req)
|
||||
cJSON* net_type_item = cJSON_GetObjectItem(root, "net_type");
|
||||
cJSON* ssid_item = cJSON_GetObjectItem(root, "ssid");
|
||||
cJSON* baud_item = cJSON_GetObjectItem(root, "baudrate");
|
||||
cJSON* period_item = cJSON_GetObjectItem(root, "period");
|
||||
cJSON* vin_climit_item = cJSON_GetObjectItem(root, "vin_current_limit");
|
||||
cJSON* main_climit_item = cJSON_GetObjectItem(root, "main_current_limit");
|
||||
cJSON* usb_climit_item = cJSON_GetObjectItem(root, "usb_current_limit");
|
||||
@@ -289,6 +296,13 @@ static esp_err_t setting_post_handler(httpd_req_t* req)
|
||||
change_baud_rate(strtol(baudrate, NULL, 10));
|
||||
httpd_resp_sendstr(req, "{\"status\":\"baudrate_updated\"}");
|
||||
}
|
||||
else if (period_item && cJSON_IsString(period_item))
|
||||
{
|
||||
const char* period_str = period_item->valuestring;
|
||||
ESP_LOGI(TAG, "Received period set request: %s", period_str);
|
||||
update_sensor_period(strtol(period_str, NULL, 10));
|
||||
httpd_resp_sendstr(req, "{\"status\":\"period_updated\"}");
|
||||
}
|
||||
else if (vin_climit_item || main_climit_item || usb_climit_item)
|
||||
{
|
||||
char num_buf[10];
|
||||
|
||||
Reference in New Issue
Block a user