network/udp_hello_gpio/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/network/udp-hello-gpio
*/
#include "gos.h"
#include "example_app_util.h"
#define BUTTON_DEBOUNCE_TIME_MS 50
#define BUTTON_CLICK_TIME_MS 500
#define BUTTON_PRESS_TIME_MS 1000
#define UDP_HOST "test.zentri.com"
#define UDP_PORT 50007
#define APPLICATION_START_LINE "\r\nUDP Hello GPIO example starting ..."
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
if(GOS_FAILED(result, initialize_hardware()))
{
GOS_LOG("Failed to initialize hardware");
return;
}
{
return;
}
GOS_LOG("Connecting to remote UDP server %s:%u", UDP_HOST, UDP_PORT);
if (GOS_FAILED(result, gos_udp_connect(UDP_HOST, UDP_PORT, GOS_ANY_PORT, &handle)))
{
GOS_LOG("Failed to connect, err:%d", result);
}
else
{
char buffer[128];
const uint32_t gpio_values = gos_gpio_mask_get(UINT32_MAX);
const int len = sprintf(buffer, "Current GPIO values: %08X", (unsigned int)gpio_values);
GOS_LOG("Sending data ...");
if (GOS_FAILED(result, gos_udp_write(handle, buffer, len)))
{
GOS_LOG("Failed to send data, err:%d", result);
}
else
{
GOS_LOG("Data sent");
}
gos_udp_close(handle);
}
GOS_LOG("Done");
}
/*************************************************************************************************
* Initialize the buttons.
*/
static gos_result_t initialize_hardware(void)
{
gos_result_t result;
const gos_button_config_t config =
{
.active_level = PLATFORM_BUTTON_ACTIVE_STATE,
.debounce = BUTTON_DEBOUNCE_TIME_MS,
.click_time = BUTTON_CLICK_TIME_MS,
.press_time = BUTTON_PRESS_TIME_MS,
.event_handler.press = NULL,
.event_handler.click = NULL,
.event_handler.toggle = NULL,
.execution_context = GOS_BUTTON_CONTEXT_DEFAULT
};
GOS_LOG("Initializing button 1 ...");
if(GOS_FAILED(result, gos_button_init(PLATFORM_BUTTON1, &config, (void*)1)))
{
GOS_LOG("Failed to init PLATFORM_BUTTON1, err:%d", result);
return result;
}
GOS_LOG("Initializing button 2 ...");
if(GOS_FAILED(result, gos_button_init(PLATFORM_BUTTON2, &config, (void*)2)))
{
GOS_LOG("Failed to init PLATFORM_BUTTON2, err:%d", result);
return result;
}
return GOS_SUCCESS;
}