#pragma once

// ========================================================== //
//              SPIDER MICROCONTROLLER AUTODETECT             //
// ========================================================== //

// Automatically enable configurations for the microcontroller    //
// so long as we can detect it or the user has already configured //
// the corresponding macros.                                      //

#define SPIDER_MCU_FAM_GENERIC  0
#define SPIDER_MCU_FAM_AVR      1
#define SPIDER_MCU_FAM_ARM_CM   2
#define SPIDER_MCU_FAM_ARM_MBED 3
#define SPIDER_MCU_FAM_PIC      4
#define SPIDER_MCU_FAM_PIC32    5
#define SPIDER_MCU_FAM_ESP8266  6
#define SPIDER_MCU_FAM_ESP32    7
#define SPIDER_MCU_FAM_RISCV    8
#define SPIDER_MCU_FAM_RP2040   9

#if defined(SPIDER_MCU_AVR) || defined(__avr__) || defined(__AVR__)
// ========================================================== //
//                AVR (Atmel / Microchip)                     //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_AVR
    #define SPIDER_MCU_NAME "AVR"

#elif defined(SPIDER_MCU_ARM) || defined(__arm__) || defined(__ARM_ARCH) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_6M__)
// ========================================================== //
//                  ARM Cortex-M (most STM32, nRF, etc.)      //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_ARM_CM
    #define SPIDER_MCU_NAME "ARM Cortex-M"

#elif defined(SPIDER_MCU_MBED) || defined(TARGET_LIKE_MBED)
// ========================================================== //
//                  Mbed OS special detection                 //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_ARM_MBED
    #define SPIDER_MCU_NAME "ARM MBED"
    #define SPIDER_OS_NAME "MBED OS"

#elif defined(SPIDER_MCU_PIC8) || defined(__XC) && (defined(_PIC14) || defined(_PIC16))
// ========================================================== //
//                        PIC (8-bit)                         //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_PIC
    #define SPIDER_MCU_NAME "PIC (8-bit)"

#elif defined(SPIDER_MCU_PIC32) || defined(__XC32__)
// ========================================================== //
//                     PIC32 (MIPS)                           //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_PIC32
    #define SPIDER_MCU_NAME "PIC (32-bit)"

#elif defined(SPIDER_MCU_ESP8266) || defined(ESP8266)
// ========================================================== //
//                         ESP8266                            //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_ESP8266
    #define SPIDER_MCU_NAME "ESP-8266"

#elif defined(SPIDER_MCU_ESP32) || defined(ESP32)
// ========================================================== //
//                           ESP32                            //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_ESP32
    #define SPIDER_MCU_NAME "ESP-32"

#elif defined(SPIDER_MCU_PICO) || defined(PICO_PLATFORM) || defined(PICO_BOARD) || defined(RP2040) || defined(__RP2040__)
// ========================================================== //
//                   Raspberry Pi Pico (RP2040)               //
// ========================================================== //
    #define SPIDER_MCU_FAM_FAMILY SPIDER_MCU_FAM_RP2040
    #define SPIDER_MCU_NAME "ESP-32"

#elif defined(SPIDER_MCU_RISCV) || defined(__riscv) || defined(__riscv__)
// ========================================================== //
//                     RISC-V MCUs                            //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_RISCV
    #define SPIDER_MCU_NAME "RISC-V MCU"

#elif defined(SPIDER_MCU_GENERIC)
// ========================================================== //
//                      GENERIC MICRO                         //
// ========================================================== //
    #define SPIDER_MCU_FAMILY SPIDER_MCU_FAM_GENERIC
    #define SPIDER_MCU_NAME "Generic MCU"
#endif

#ifdef SPIDER_MCU_FAMILY
    
    #ifndef SPIDER_DISTRO_MICRO
        #define SPIDER_DISTRO_MICRO
    #endif

    // If SPIDER_OS_NAME was already defined, implies the SPIDER_MCU_FAM has an OS
    #if defined(SPIDER_OS_NAME) && !defined(SPIDER_OS_MCU)
        #define SPIDER_OS_MCU
    #endif

    // If no SPIDER_MCU_FAM OS defined, then it is NONE
    #ifndef SPIDER_OS_MCU
        #define SPIDER_OS_NONE
    #endif

#elif defined(SPIDER_DISTRO_MICRO)
    #error "[Spider Distro] Unsupported SPIDER_MCU_FAM. Define SPIDER_MCU_GENERIC and see if works."
#endif
