/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "sdkconfig.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"

#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"

#include "driver/gpio.h"
#include "driver/spi_master.h"

#include "mcp2521.hpp"

#define SPI_PIN_SCLK GPIO_NUM_18
#define SPI_PIN_MISO GPIO_NUM_19
#define SPI_PIN_MOSI GPIO_NUM_23

#define SPI_PIN_CS0 GPIO_NUM_25
#define SPI_PIN_CS1 GPIO_NUM_27

#define CAN_INT0_PIN GPIO_NUM_5
#define CAN_INT1_PIN GPIO_NUM_26

void onRX0(void *arg) {
    MCP2521 *mcp2521 = (MCP2521 *)arg;

    rx_info info = mcp2521->get_rx_id(MCP2521_RX_BUFFER::RXB0);
    uint8_t data[8];

    //mcp2521->read_rx_buf(MCP2521_RX_BUFFER::RXB0, MCP2521_BUFFER_TYPE::DATA, data, info.length);

    printf("RX0: (%x) ", info.id);
    //for (int i = 0; i < 8; i++) {
    //    printf("%x ", data[i]);
    //}
    printf("\n");
}

void onRX1(void *arg) {
    MCP2521 *mcp2521 = (MCP2521 *)arg;

    rx_info info = mcp2521->get_rx_id(MCP2521_RX_BUFFER::RXB0);
    uint8_t data[8];

    //mcp2521->read_rx_buf(MCP2521_RX_BUFFER::RXB0, MCP2521_BUFFER_TYPE::DATA, data, info.length);
    printf("RX1: (%x) ", info.id);
    //for (int i = 0; i < 8; i++) {
    //    printf("%x ", data[i]);
    //}

    printf("\n");
}

extern "C" void app_main() {
    printf("Hello world!\n");

    const gpio_num_t LED_PIN = GPIO_NUM_2;
    gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);

    vTaskDelay(100 / portTICK_PERIOD_MS);
    
    MCP2521_HardwareHandleFactory_ESPBus mcp2521_hardware_factory(
        VSPI_HOST,
        SPI_PIN_MOSI,
        SPI_PIN_MISO,
        SPI_PIN_SCLK
    );

    MCP2521_HardwareHandle_ESPBus hardware_mcp2521_0 = mcp2521_hardware_factory.create(CAN_INT0_PIN, SPI_PIN_CS0);
    MCP2521_HardwareHandle_ESPBus hardware_mcp2521_1 = mcp2521_hardware_factory.create(CAN_INT1_PIN, SPI_PIN_CS1);

    vTaskDelay(100 / portTICK_PERIOD_MS);

    MCP2521 mcp2521_0(&hardware_mcp2521_0);
    MCP2521 mcp2521_1(&hardware_mcp2521_1);

    uint8_t data[8] = {0x1, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

    mcp2521_0.reset();
    mcp2521_1.reset();
    mcp2521_0.enable_interrupts(true, true, true, true, true, true, true, true);
    mcp2521_1.enable_interrupts(true, true, true, true, true, true, true, true);

    mcp2521_0.register_rx0_handler(onRX0, &mcp2521_0);
    mcp2521_1.register_rx0_handler(onRX1, &mcp2521_1);
    mcp2521_0.register_rx1_handler(onRX0, &mcp2521_0);
    mcp2521_1.register_rx1_handler(onRX1, &mcp2521_1);
    
    mcp2521_0.set_mode_of_operation(MCP2521_OPERATION_MODE::LOOPBACK, true);
    mcp2521_1.set_mode_of_operation(MCP2521_OPERATION_MODE::LOOPBACK, true);
    
    //printf("CANSTAT0: %x\n", mcp2521_0.read_reg(MCP2521_CANSTAT));
    //printf("CANSTAT1: %x\n", mcp2521_1.read_reg(MCP2521_CANSTAT));

    vTaskDelay(20 / portTICK_PERIOD_MS);

    mcp2521_1.prepare_tx(
        MCP2521_TX_BUFFER::TXB0, 0x042, data, 8, false, false);
    //mcp2521_1.prepare_tx(
    //    MCP2521_TX_BUFFER::TXB0, 0x041, data, 4, false, false);


    vTaskDelay(20 / portTICK_PERIOD_MS);
    mcp2521_1.request_to_send(MCP2521_TX_BUFFER::TXB0);
    //vTaskDelay(10 / portTICK_PERIOD_MS);
    //mcp2521_1.request_to_send(MCP2521_TX_BUFFER::TXB0);

    //vTaskDelay(20 / portTICK_PERIOD_MS);

    //mcp2521_0.set_tx_id(MCP2521_TX_BUFFER::TXB0, 0x042, false);
    //mcp2521_0.request_to_send(MCP2521_TX_BUFFER::TXB0);

    vTaskDelay(20 / portTICK_PERIOD_MS);

    bool flag = true;
    while (true) {
        gpio_set_level(LED_PIN, flag);
        flag = !flag;

        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}
