#include <user_code.hpp>

//Timer Components
ESPMega_Timer timer1(0, 50, timer1_callback, 15001);

// Display Componets
// Link with Dual state button object with id 2 on page 1 named bt0
NexDSButton bt0 = NexDSButton(1, 2, "bt0");
// List of Component ID Message to listen to
NexTouch *nex_listen_list[] =
    {
        &bt0,
        NULL};

void bt0PopCallback(void *ptr)
{
    pwm_toggle(2);
}

/*
This Code will run right after ESPMega PRO's
Peripheral Initialization Routine
*/
void user_pre_init()
{
    nexInit();
}

/*
This code will run after every component is initialized
*/
void user_init()
{
    timer1.begin();
    ESPMega_EXTLCD.print("page home");
    ESPMega_EXTLCD.write(0xFF);
    ESPMega_EXTLCD.write(0xFF);
    ESPMega_EXTLCD.write(0xFF);
    bt0.attachPop(bt0PopCallback, &bt0);
}

/*
This code will run once every event loop
*/
void user_loop()
{
    nexLoop(nex_listen_list);
}

/*
This code will run when an input pin changed state
*/
void virtual_interrupt_user_callback(int pin, int state)
{
}

void pwm_changed_user_callback(int pin)
{
    if (pin == 2)
    {
        // inform the lcd when pwm 2 changed
        bt0.setValue(pwm_get_state(2));
    }
}

/*
This code will run every 15 seconds
*/
void timer_tick_callback()
{
    if (standalone)
    {
        timer1.loop();
    }
}

void timer1_callback()
{
    for (int i = 0; i < 16; i++)
    {
        pwm_set_state(i, 1);
    }
}

void ac_changed_user_callback(int mode, int temperature, int fan_speed) {
    
}

void mqtt_connected_user_callback() {

}

void user_state_request_callback() {

}

void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length) {

}