Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Ward committed Sep 22, 2024
2 parents f86c263 + fc3df97 commit 3455ef1
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 18 deletions.
129 changes: 123 additions & 6 deletions main/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ static uint16_t getVariable(char* buffer)
return sprintf(buffer,"%02X%02X:%02X%02X%02X%02X",
rdmUID[0],rdmUID[1],rdmUID[2],rdmUID[3],rdmUID[4],rdmUID[5]);
}
if(strcmp(buffer,"Brightness")==0)
{
return sprintf(buffer,"%d",settingsGetBrightness());
}
if(strcmp(buffer,"IdleModeTimeout")==0)
{
return sprintf(buffer,"%d",settingsGetIdleModeTimeout());
}
if(strncmp(buffer,"IdleModeIs_",11)==0)
{
char* modename = &buffer[11];
if(strcmp(modename,settingsGetidleMode())==0)
{
return sprintf(buffer," selected");
}
else
{
buffer[0]='\0';
return 0;
}
}
if(strcmp(buffer,"LocateSelectOff")==0)
{
if(indicatorsGetLocate())
Expand Down Expand Up @@ -246,13 +267,107 @@ static esp_err_t set_post_handler(httpd_req_t *req)
return ESP_OK;
}

static const httpd_uri_t page_set_mode = {
static const httpd_uri_t api_set_mode = {
.uri = "/set",
.method = HTTP_POST,
.handler = set_post_handler,
.user_ctx = NULL
};

static esp_err_t settings_post_handler(httpd_req_t *req)
{
char buf[101];
int ret, remaining = req->content_len;

if(remaining>100)
{
httpd_resp_set_status(req,"413 Payload Too Large");
httpd_resp_send(req,"Post Payload too large",HTTPD_RESP_USE_STRLEN);
return ESP_FAIL;
}

/* Read the data for the request */
if ((ret = httpd_req_recv(req, buf,MIN(remaining, sizeof(buf)))) <= 0)
{
return ESP_FAIL;
}

//null terminate string for processing
buf[ret]='\0';

uint8_t newBrightness = 0;
uint16_t newIdleTimeout = 0;
char newIdleMode[32];

//get values
char value[32];
char* end;
uint8_t missingValues = 0;

memset(value,0,32);
if(httpd_query_key_value(buf,"brightness",value,32)==ESP_OK)
{
newBrightness = strtol(value,&end,10);
}
else
{
missingValues+=1;
}

memset(value,0,32);
if(httpd_query_key_value(buf,"idleTimeout",value,32)==ESP_OK)
{
newIdleTimeout = strtol(value,&end,10);
}
else
{
missingValues+=1;
}

memset(newIdleMode,0,32);
if(httpd_query_key_value(buf,"idleMode",newIdleMode,32)!=ESP_OK)
{
httpd_resp_send_err(req,HTTPD_400_BAD_REQUEST,NULL);
return ESP_FAIL;
}

if(missingValues>0)
{
httpd_resp_send_err(req,HTTPD_400_BAD_REQUEST,NULL);
return ESP_FAIL;
}

/* Log data received */
ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
ESP_LOGI(TAG, "Brightness: %d, timeout: %d, timeout_state: %s", newBrightness, newIdleTimeout, newIdleMode);
ESP_LOGI(TAG, "====================================");

//check values
if(newBrightness<=255 && newBrightness>=1)
{
settingsSetBrightness(newBrightness);
}
else
{
httpd_resp_send_err(req,HTTPD_400_BAD_REQUEST,NULL);
return ESP_FAIL;
}

settingsSetIdleModeTimeout(newIdleTimeout);
settingsSetIdleMode(newIdleMode);

// send success
httpd_resp_send(req, "Success", HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

static const httpd_uri_t api_set_settings = {
.uri = "/settings",
.method = HTTP_POST,
.handler = settings_post_handler,
.user_ctx = NULL
};

static esp_err_t artnet_post_handler(httpd_req_t *req)
{
char buf[101];
Expand Down Expand Up @@ -357,7 +472,7 @@ static esp_err_t artnet_post_handler(httpd_req_t *req)
return ESP_OK;
}

static const httpd_uri_t artnet_set_mode = {
static const httpd_uri_t api_set_artnet = {
.uri = "/artnetConfig",
.method = HTTP_POST,
.handler = artnet_post_handler,
Expand Down Expand Up @@ -416,7 +531,7 @@ static esp_err_t locate_post_handler(httpd_req_t *req)
return ESP_OK;
}

static const httpd_uri_t locate_set_mode = {
static const httpd_uri_t api_set_locate = {
.uri = "/locate",
.method = HTTP_POST,
.handler = locate_post_handler,
Expand Down Expand Up @@ -458,6 +573,7 @@ void httpSetup()
static httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.lru_purge_enable = true;
config.max_uri_handlers = 10;

if(httpd_start(&server,&config) == ESP_OK)
{
Expand All @@ -467,8 +583,9 @@ void httpSetup()
httpd_register_uri_handler(server,&page_about);
httpd_register_uri_handler(server,&page_css);
httpd_register_uri_handler(server,&page_js);
httpd_register_uri_handler(server,&page_set_mode);
httpd_register_uri_handler(server,&artnet_set_mode);
httpd_register_uri_handler(server,&locate_set_mode);
httpd_register_uri_handler(server,&api_set_mode);
httpd_register_uri_handler(server,&api_set_settings);
httpd_register_uri_handler(server,&api_set_artnet);
httpd_register_uri_handler(server,&api_set_locate);
}
}
2 changes: 1 addition & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void app_main()

indicatorsSetup();
indicatorsSetStatus(1);
SetupOutputs();
settingsSetup();
SetupOutputs();

//set base mac address from factory set address
uint8_t baseMac[6];
Expand Down
43 changes: 39 additions & 4 deletions main/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
#include "esp_log.h"

#include "hardware.h"
#include "settings.h"

static const char *TAG = "output";

static void outputInactivityTimeout(void* arg);

esp_timer_handle_t outputInactivityTimer;

ledc_channel_config_t ledc_channel[4] = {
{
.channel = LEDC_CHANNEL_0,
Expand Down Expand Up @@ -47,6 +52,11 @@ ledc_channel_config_t ledc_channel[4] = {

void SetOutputsDMX(uint16_t offset, uint8_t* data)
{
esp_timer_stop(outputInactivityTimer);
if(settingsGetIdleModeTimeout())
{
esp_timer_start_once(outputInactivityTimer, settingsGetIdleModeTimeout()*1000000);
}
ledc_set_duty_and_update(ledc_channel[0].speed_mode,ledc_channel[0].channel,data[offset]*32,0);
ledc_set_duty_and_update(ledc_channel[1].speed_mode,ledc_channel[1].channel,data[offset+1]*32,0);
ledc_set_duty_and_update(ledc_channel[2].speed_mode,ledc_channel[2].channel,data[offset+2]*32,0);
Expand All @@ -72,19 +82,26 @@ static out_mode_t modes[] = {
{"red_buzzer", 255*32, 0, 0, 255*32},
{"yellow_buzzer",0, 255*32, 0, 255*32},
{"green_buzzer", 0, 0, 255*32, 255*32},
{0x00,0,0,0}
{0x00,0,0,0,0}
};

void SetOutputsMode(char* mode)
{
int brightness = settingsGetBrightness();

int i = 0;
while(modes[i].modeName)
{
if(strcmp(modes[i].modeName,mode)==0)
{
ledc_set_duty_and_update(ledc_channel[0].speed_mode,ledc_channel[0].channel,modes[i].red,0);
ledc_set_duty_and_update(ledc_channel[1].speed_mode,ledc_channel[1].channel,modes[i].yellow,0);
ledc_set_duty_and_update(ledc_channel[2].speed_mode,ledc_channel[2].channel,modes[i].green,0);
esp_timer_stop(outputInactivityTimer);
if(settingsGetIdleModeTimeout())
{
esp_timer_start_once(outputInactivityTimer, settingsGetIdleModeTimeout()*1000000);
}
ledc_set_duty_and_update(ledc_channel[0].speed_mode,ledc_channel[0].channel,(modes[i].red/255)*brightness,0);
ledc_set_duty_and_update(ledc_channel[1].speed_mode,ledc_channel[1].channel,(modes[i].yellow/255)*brightness,0);
ledc_set_duty_and_update(ledc_channel[2].speed_mode,ledc_channel[2].channel,(modes[i].green/255)*brightness,0);
ledc_set_duty_and_update(ledc_channel[3].speed_mode,ledc_channel[3].channel,modes[i].buzzer,0);
return;
}
Expand All @@ -93,6 +110,12 @@ void SetOutputsMode(char* mode)
ESP_LOGW(TAG, "No supported mode: %s", mode);
}

static void outputInactivityTimeout(void* arg)
{
ESP_LOGI(TAG, "Inactivity timeout triggered");
SetOutputsMode(settingsGetidleMode());
}

void SetupOutputs()
{

Expand All @@ -114,4 +137,16 @@ void SetupOutputs()
ledc_channel_config(&ledc_channel[3]);

ledc_fade_func_install(0);

const esp_timer_create_args_t InactivityTimerArgs = {
.callback = &outputInactivityTimeout,
/* name is optional, but may help identify the timer when debugging */
.name = "outputInactivity"
};
ESP_ERROR_CHECK(esp_timer_create(&InactivityTimerArgs, &outputInactivityTimer));

if(settingsGetIdleModeTimeout())
{
esp_timer_start_once(outputInactivityTimer, settingsGetIdleModeTimeout()*1000000);
}
}
48 changes: 48 additions & 0 deletions main/settings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nvs_flash.h>
#include <nvs.h>
#include <esp_log.h>
#include <string.h>

#include "settings.h"

Expand All @@ -14,6 +15,10 @@ static uint8_t artnetNet = 0;
static uint8_t artnetSubNet = 0;
static uint8_t artnetUniverse = 0;

static uint8_t brightness = 255;
static uint16_t idleModeTimeout = 0;
char idleMode[32];

void settingsSetup()
{
esp_err_t err = nvs_flash_init();
Expand All @@ -26,12 +31,19 @@ void settingsSetup()

ESP_ERROR_CHECK(err);

size_t idleModeStrlen = 32;
sprintf(idleMode,"off"); //set default idle mode

err = nvs_open("storage", NVS_READWRITE, &nvsHandle);

nvs_get_u16(nvsHandle,"dmxAddr",&dmxAddr);
nvs_get_u8(nvsHandle,"artnetNet",&artnetNet);
nvs_get_u8(nvsHandle,"artnetSubNet",&artnetSubNet);
nvs_get_u8(nvsHandle,"artnetUniverse",&artnetUniverse);

nvs_get_u8(nvsHandle,"brightness",&brightness);
nvs_get_u16(nvsHandle,"idleModeTimeout",&idleModeTimeout);
nvs_get_str(nvsHandle,"idleMode",idleMode,&idleModeStrlen);
}

uint16_t settingsGetDmxAddr()
Expand Down Expand Up @@ -81,3 +93,39 @@ void settingsSetArtnetUniverse(uint8_t newArtnetUniverse)
nvs_set_u8(nvsHandle,"artnetUniverse",artnetUniverse);
nvs_commit(nvsHandle);
}

uint8_t settingsGetBrightness()
{
return brightness;
}

void settingsSetBrightness(uint8_t newBrightness)
{
brightness=newBrightness;
nvs_set_u8(nvsHandle,"brightness",brightness);
nvs_commit(nvsHandle);
}

uint16_t settingsGetIdleModeTimeout()
{
return idleModeTimeout;
}

void settingsSetIdleModeTimeout(uint16_t newIdleModeTimeout)
{
idleModeTimeout=newIdleModeTimeout;
nvs_set_u16(nvsHandle,"idleModeTimeout",idleModeTimeout);
nvs_commit(nvsHandle);
}

char* settingsGetidleMode()
{
return idleMode;
}

void settingsSetIdleMode(char* newIdleMode)
{
strcpy(idleMode,newIdleMode);
nvs_set_str(nvsHandle,"idleMode",idleMode);
nvs_commit(nvsHandle);
}
Loading

0 comments on commit 3455ef1

Please sign in to comment.