/* Notsch Chris, 02.08.2023
 * Implementation Example (main.c):
 * Required Apps: ETH_LWIP (1x), PWM_CCU4 (1x), INTERRUPT (1x), DIGITAL_IO (1x)
 *
 * Note: You may need to refresh DAVE's autocomplete index after adding the required apps and generating the code so the required UGUI-methods are activated:
 *       --> Rightclick on the project --> Index --> Rebuild
 *
 * Configuration Steps:
 * Dave 4.3.2: install app patch "Dave_4.3.2_LWIP_LibraryStore_Patch" - see its own readme file for instructions --> not needed for Dave 4.5.0
 * ETH_LWIP -> General Settings --> enable netif hostname --> set to true and assign valid host name
 * ETH_LWIP -> Network Interface -> Enable Setting "Poll for received data"
 * ETH_LWIP -> IP Settings --> Use DHCP --> set to true
 * (optional) ETH_LWIP -> apply desired package and memory settings (see examples below)
 * ETH_LWIP -> Assign Pins
 *
 * PWM_CCU4 -> rename instance label to PWM_CCU4_TIMER_1MS
 * PWM_CCU4 -> General Settings --> enable 'start during initialization'
 * PWM_CCU4 -> General Settings --> change frequency to 1kHz (Note: on faster processors, you may need to lower the resolution to be able to achieve 1kHz frequency; e.g.: 28ns for XMC4700)
 * PWM_CCU4 -> Timer Event Settings --> enable 'Period match' (ignore warning)
 *
 * INTERRUPT -> rename instance label to INTERRUPT_TIMER_1MS
 * INTERRUPT -> Interrupt Settings --> disable 'Enable interrupt at initialization'
 * INTERRUPT -> Interrupt Settings --> change 'Interrupt handler' to 'Timer_1ms_ISR'
 *
 * PWM_CCU4 -> Hardware Signal Connections --> add config line: 'event_period_match ----> INTERRUPT_TIMER_1MS, sr_irq'
 *
 * DIGITAL_IO -> rename instance label to DIGITAL_IO_ETH_LED
 * DIGITAL_IO -> General Settings --> set 'Pin direction' to 'Input/Output'
 * (optional) DIGITAL_IO -> General Settings --> set 'Initial Output Level' to high, if your LED is connected to the positive rail (pin is active low)
 * DIGITAL_IO -> Assign Pin
 *
 * (optional) UGUI_Lib/UGUI_Conf.c -> change buffers to fit TCP/IP packet size:
 *    when transmitting many commands to the XMC at the same time, the received packages can exceed the default buffer configuration.
 *    both receive buffer, as well as (depending on implementation) the async handle pool must be large enough to accommodate all incoming parallel requests
 *    for each parallel command, one async handle and 13 receive buffer bytes are required, some headroom should be included as well
 *
 *    e.g.: 15 commands are periodically queried by a host that is much faster than this controller, this controller also sends up to 4 requests to the host:
 *    this means up to 19 commands could be active at the same time:
 *    --> 'UGUI_ASYNC_HANDLE_POOL_SIZE' should be increased to a minimum of 19
 *    --> 'UGUI_CUSTOM_INPUT_BUFFER_SIZE' must be increased to a minimum of 19 to 20 commands (1 headroom) times 13 bytes = 247-260 bytes;
 *        the buffer is specified as exponents of 2, so the available fitting options are '8' (256 bytes) or '9' (512 bytes). If enough RAM is available, the higher option should be chosen
 *
 *
 * ### LWIP buffer and packet configuration examples: ###
 *
 * App Default Configuration (XMC4700):
 * stream buffer size: (TCP_SND_BUF-4)
 * forced 100Mbit/s (only for testing purposes)
 * file transfer on XMC4700 ~800KB/s from SD card, 3.7MB/s from RAM
 * max. command speed on XMC4700 ~5kC/s
 *
 * TCP_MSS 536
 * TCP_WND 2144
 * TCP_SND_BUF 1072
 * TCP_SND_QUEUELEN 8
 *
 * MEM_SIZE 1600
 * PBUF_POOL_SIZE 16
 * MEMP_NUM_PBUF 16
 * MEMP_NUM_RAW_PCB 4
 * MEMP_NUM_UDP_PCB 4
 * MEMP_NUM_TCP_PCB 5
 * MEMP_NUM_TCP_PCB_LISTEN 5
 * MEMP_NUM_TCP_SEG 8
 * MEMP_NUM_ARP_QUEUE 30
 *
 *
 * Performance Configuration (XMC4700):
 * stream buffer size: (TCP_SND_BUF-4)
 * forced 100Mbit/s (only for testing purposes)
 * file transfer on XMC4700 ~850KB/s from SD card, 5.7MB/s from RAM
 * max. command speed on XMC4700 ~5kC/s
 *
 * TCP_MSS 1072
 * TCP_WND 4288
 * TCP_SND_BUF 4288
 * TCP_SND_QUEUELEN 8
 *
 * MEM_SIZE 1600
 * PBUF_POOL_SIZE 16
 * MEMP_NUM_PBUF 16
 * MEMP_NUM_RAW_PCB 4
 * MEMP_NUM_UDP_PCB 4
 * MEMP_NUM_TCP_PCB 5
 * MEMP_NUM_TCP_PCB_LISTEN 5
 * MEMP_NUM_TCP_SEG 8
 * MEMP_NUM_ARP_QUEUE 30
 *
 *
*/



#include <DAVE.h>
#include "UGUI_Lib/UGUI_LIB.h"

uint64_t SysTime = 0;
UGUI_Handle_t UGUI_0;
float dummy1 = 0.0f;

int main(void)
{
    DAVE_Init();
    UGUI_Init_TCP_SOCKET(&UGUI_0);

    uint16_t cmd=1;
    UGUI_RegisterProperty_RW_Float(&UGUI_0, cmd++, UGUI_DEFAULT_QUERY_OFFSET, &dummy1, 0.0f, 1.0f, "dummy");

    INTERRUPT_Enable(&INTERRUPT_TIMER_1MS);

    while (1U)
    {
        UGUI_Maintain(&UGUI_0);
    }
}


void Timer_1ms_ISR()
{
    SysTime++;
    if((SysTime % 200) == 0)
    {
        if(!UGUI_ETH_LWIP_BlinkIpAddress(&ETH_LWIP_0)) DIGITAL_IO_SetOutputHigh(&DIGITAL_IO_ETH_LED);
        else DIGITAL_IO_SetOutputLow(&DIGITAL_IO_ETH_LED);
    }

    UGUI_TimeoutTick(&UGUI_0);
}
