demo/led_matrix/led_matrix.c

See examples/demo/led-matrix

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
/* Documentation for this app is available online.
* See https://docs.zentri.com/wifi/sdk/latest/examples/demo/led-matrix
*/
#include "zos.h"
#include "led_matrix8x8.h"
HTTP_SERVER_DYNAMIC_PAGE("/led_matrix/update/text", update_text_processor),
HTTP_SERVER_DYNAMIC_PAGE("/led_matrix/update/blink", update_blink_processor),
HTTP_SERVER_DYNAMIC_PAGE("/led_matrix/update/brightness", update_brightness_processor),
HTTP_SERVER_DYNAMIC_PAGE("/led_matrix/update/scroll", update_scroll_processor),
HTTP_SERVER_DYNAMIC_PAGE("/led_matrix/retrieve/all", retrieve_all_processor),
static zos_bool_t initialized;
/*************************************************************************************************/
void zn_app_init(void)
{
zos_result_t result;
char buffer[32];
ZOS_LOG("Starting 8x8 LED Matrix Demo");
if(zn_load_app_settings("settings.ini") != ZOS_SUCCESS)
{
ZOS_LOG("Failed to load settings");
return;
}
if(ZOS_FAILED(result, led_matrix8x8_init(PLATFORM_STD_I2C)))
{
ZOS_LOG("Failed to initialize LED Matrix Library");
return;
}
led_matrix8x8_set_text("ZentriOS 8x8 LED Matrix Demo");
{
ZOS_LOG("Failed to restart network: %d\r\n\r\n", result);
ZOS_LOG("----------------------------------------------------------------------");
ZOS_LOG("This basic app expects valid network credentials have been configured.");
ZOS_LOG("Join a network and save credentials to non-volatile memory using the ");
ZOS_LOG("ZentriOS commands shown below ");
ZOS_LOG(" ");
ZOS_LOG("> network_up -s ");
ZOS_LOG("----------------------------------------------------------------------");
ZOS_LOG("\r\n ");
}
else
{
ZOS_LOG("From your browser, enter the URL: http://%s.local/ to control the display", ZOS_GET_SETTING_STR("mdns.name", buffer));
}
initialized = ZOS_TRUE;
}
/*************************************************************************************************/
void zn_app_deinit(void)
{
led_matrix8x8_deinit();
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
// Return TRUE so the event loop idles
// It will wakeup when the LED matrix needs to be updated or the HTTP server receives a request
return initialized;
}
/*************************************************************************************************/
static zos_result_t update_text_processor(const http_server_request_t *request, const char *arg)
{
const http_server_param_t *param = zn_hs_get_param(request, "data");
led_matrix8x8_set_text(param->value);
}
/*************************************************************************************************/
static zos_result_t update_blink_processor(const http_server_request_t *request, const char *arg)
{
const http_server_param_t *param = zn_hs_get_param(request, "data");
const uint8_t val = str_to_uint32(param->value);
led_matrix8x8_set_blink_rate(val);
}
/*************************************************************************************************/
static zos_result_t update_brightness_processor(const http_server_request_t *request, const char *arg)
{
const http_server_param_t *param = zn_hs_get_param(request, "data");
const uint8_t val = str_to_uint32(param->value);
led_matrix8x8_set_brightness(val);
}
/*************************************************************************************************/
static zos_result_t update_scroll_processor(const http_server_request_t *request, const char *arg)
{
const http_server_param_t *param = zn_hs_get_param(request, "data");
const uint16_t val = str_to_uint32(param->value);
led_matrix8x8_set_scroll_rate(val);
}
/*************************************************************************************************/
static zos_result_t retrieve_all_processor(const http_server_request_t *request, const char *arg)
{
zos_result_t result;
char buffer[128];
char *ptr = buffer;
const led_matrix8x8_context_t *context = led_matrix8x8_get_context();
ptr += sprintf(ptr, "{\"brightness\":%d,", context->brightness);
ptr += sprintf(ptr, "\"blink\":%d,", context->blink_rate);
ptr += sprintf(ptr, "\"scroll\":%d,", context->scroll.rate);
ptr += sprintf(ptr, "\"msg\":\"");
if(ZOS_FAILED(result, zn_hs_write_reply_header(request, "application/json", -1, HTTP_SERVER_HEADER_NONE)))
{
}
else if(ZOS_FAILED(result, zn_hs_write_chunked_data(request, buffer, ptr - buffer, ZOS_FALSE)))
{
}
else if(ZOS_FAILED(result, zn_hs_write_chunked_data(request, context->text, strlen(context->text), ZOS_FALSE)))
{
}
else if(ZOS_FAILED(result, zn_hs_write_chunked_data(request, "\"}", 2, ZOS_TRUE)))
{
}
return result;
}
/*************************************************************************************************/
//static void print_settings(void)
//{
// const led_matrix8x8_context_t *settings = led_matrix8x8_get_context();
// ZOS_LOG("Brightness: %d", settings->brightness);
// ZOS_LOG("Blink rate: %d", settings->blink_rate);
// ZOS_LOG("Scroll rate: %d", settings->scroll.rate);
//}