#include "F2837xD_Ipc_drivers.h"
#include "driverlib.h"
#include "device.h"

#include "stdio.h"
#include "string.h"

#include "led.h"
#include "key.h"
#include "sci.h"

#include "micro_printf.h"
#include "board.h"
#include "spi_test_v1.h"



#define delay_s(x) SysCtl_delay(((((long double)(x)) / (1.0L /  \
                              (long double)DEVICE_SYSCLK_FREQ)) - 9.0L) / 5.0L)
#define delay_ms(x) SysCtl_delay(((((long double)(x)) / (1000.0L /  \
                              (long double)DEVICE_SYSCLK_FREQ)) - 9.0L) / 5.0L)
#define delay_us(x) SysCtl_delay(((((long double)(x)) / (1000000.0L /  \
                              (long double)DEVICE_SYSCLK_FREQ)) - 9.0L) / 5.0L)

uint16_t KEY_Value = 0;


//int aaa =9876;
//uint32_t ccc =0;
//float bbb =22.22;

void main(void)
{
//确定运行模式
#ifdef _STANDALONE
#ifdef _FLASH
    IPCBootCPU2(C1C2_BROM_BOOTMODE_BOOT_FROM_FLASH);
#else
    IPCBootCPU2(C1C2_BROM_BOOTMODE_BOOT_FROM_RAM);
#endif
#endif
    //设备初始化  关闭了大部分外设，需要用到再去开
    Device_init();
    //GPIO初始化
    Device_initGPIO();
//    initADC();
//    Interrupt_initModule();//初始化PIE并清除PIE寄存器。禁用CPU中断。
//    Interrupt_initVectorTable();//用指向shell Interrupt的指针初始化PIE向量表
    Board_init();
//    Interrupt_register(INT_ADCA1, adcA1ISR); // 确保使用与您的硬件相匹配的中断号(for test)
    // 使能ADC中断并确认
//    Interrupt_enable(INT_ADCA1);   //for test
//    EINT;
//    SCISendData("ADC Interrupt enabled\r\n");
//    SCISendData("Interrupt registered\r\n");
//    Interrupt_enableMaster(); //开启总中断
    //开启总中断
//    Interrupt_enableMaster();

    //LED初始化函数
    LED_Init();
    //KEY初始化
    KEY_Init();
    //按键初始化函数
    spi_initController(); // 调用spi_test.c中的初始化函数

    SCISendData("4.System initialization complete\r\n");

    LED1_OFF;
    LED2_OFF;

    while(1)
    {
        //ccc = Float_to_u32(bbb);
        //micro_print("hello %d %f\r\n",aaa,ccc);
        spi_updateController(); // 更新 SPI 控制器状态

                // 获取当前温度
//        short currentTemperature = read_current_temperature();

                // 打印温度和其他信息
//        micro_print("Temperature: %d %f\r\n", currentTemperature);
        delay_ms(1000);
        LED2_TOGGLE;

        KEY_Value = KEY_Scan(1);
        if(KEY_Value == KEY1_PRES)
        {
            LED1_ON;
            LED2_OFF;
        }
        else if(KEY_Value == KEY2_PRES)
        {
            LED2_ON;
            LED1_OFF;
        }

    }
}
