/*
 * LEIGH:plc
 * Copyright 2022 Alex Leigh
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <esp_log.h>
#include <esp_netif.h>

#include "sacn.h"
#include "mecha.h"
#include "pwm.h"

static const char *TAG = "sacn";
static void sacnFpkt_handler(const char *buf, size_t buf_len);

/**
 * @brief The LED driver personality task
 * 
 * This task listens for sACN packets and triggers lighting output as a result. 
 * 
 * @param sacn_handler 
 */
void sacnFtask()
{
    pwmFinit();
    netFmc_listener(sacnFpkt_handler);
}

/**
 * @brief Processed a received sACN packet
 * 
 * @param buf 
 * @param buf_len 
 */
static void sacnFpkt_handler(const char *buf, size_t buf_len)
{
    // Not actually required but makes for simpler coding
    const sacnTe131_packet *pkt = (const sacnTe131_packet *)buf;
    ESP_LOGI(TAG, "sACN packet received. [universe: %u] [priority: %u] [src: %s]",
             ntohs(pkt->frame.universe), pkt->frame.priority, pkt->frame.source_name);

    ESP_LOGI(TAG, "Channels 1-5: %u %u %u %u %u",
             pkt->dmp.prop_val[0],
             pkt->dmp.prop_val[1],
             pkt->dmp.prop_val[2],
             pkt->dmp.prop_val[3],
             pkt->dmp.prop_val[4]);

             pwmFset(pkt->dmp.prop_val[0],pkt->dmp.prop_val[1]);
}