#ifndef CONFIG_H
#define CONFIG_H

#include "enums.h"
#include "common/macros.h"
#include <Controllino.h>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Core settings
//

#define LOOP_DELAY 10   // Our frame time, excluding delays in some places
#define BOOT_DELAY 1000 // Wait at least this amount in ms after boot before doing anything

// Print Firmware info during boot
#define PRINT_VERSION

// Interval to read analog input
#define ANALOG_READ_INTERVAL 50

// Please consider to set this to false for production - especially with the full feature set since this is requiring extra
// time for the serial communication and will affect the overall framerate/performance
// #define DEBUG

#define DEBUG_INTERVAL  350
#define DEBUG_BAUD_RATE 19200 // Serial port speed

#define RELAY_ON 0    // The relay bank's on value (eg: normally closed)
#define RELAY_OFF 255 // The relay bank's off value (eg: normally closed)

// Time to wait til homing will be activated (direction switch / joystick - up)
#define HOMING_WAIT_THRESHOLD   800

#define MIN_DT_AUTO   2000
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Switches
//

#define DIR_SWITCH_UP_PIN       CONTROLLINO_A0
#define DIR_SWITCH_DOWN_PIN     CONTROLLINO_A1


#define PLUNGE_START_PIN        CONTROLLINO_A3
#define PLUNGE_START_MAX_PIN    CONTROLLINO_A4
#define PLUNGE_START_LEVEL      700


// Operation Mode Switch
//#define OP_MODE_1_PIN           CONTROLLINO_IN0
#define OP_MODE_ANALOG

#define PLUNGE_HOME_PIN           CONTROLLINO_IN0

// if available (VFD multi-speed), this will activate relay D2
// as alternative, check for Jogging functionality 
//#define HOME_LOW_SPEED_PIN       CONTROLLINO_D2
//#define HOME_MEDIUM_SPEED_PIN    CONTROLLINO_D3
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Machine settings
//
#define USE_CONTROLLINO // We are using a Controllino Mini board

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    VFD related
//

// VFD FWD Pin. This goes into the relay and then into the VFD.
#define FWD_PIN                     CONTROLLINO_D4

// VFD REV Pin. This goes into the relay and then into the VFD.
#define REV_PIN                     CONTROLLINO_D5

#define MOTOR_LOAD_PIN              CONTROLLINO_A5

// the time needed to boot up the VDF, only used when we control also the power via POWER_0
#define VFD_BOOT_TIME               500

#define VFD_FAULT_RESET_PIN         CONTROLLINO_D1  
/////////////////////////////////
// Semi-auto plunging

// The voltage of our controller (Controllino Mini takes 12 or 24)
#define VFD_SPEED_VOLTAGE       24

// The maximum voltage the VFD takes
#define VFD_SPEED_VOLTAGE_MAX   10

// VFD Speed Constants
#define VFD_SPEED_FULL          255 * VFD_SPEED_VOLTAGE_MAX / VFD_SPEED_VOLTAGE
#define VFD_SPEED_MIN           VFD_SPEED_FULL / 10
#define VFD_SPEED_MEDIUM        VFD_SPEED_FULL / 2

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Motor related
//

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Feedback

// Status LEDS (ok, error) - uncomment to activate
//#define HAS_STATUS
//#define STATUS_OK_PIN       CONTROLLINO_D2        // This goes into the relay
//#define STATUS_ERROR_PIN    CONTROLLINO_D3     // This goes into the relay

#define FIRMWARE_VERSION 0.6

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plastic Hub Studio - internals : used by external controller setups
// Make sure it's matching
#define FIRMATA_BAUD_RATE 19200


////////////////////////////////////////////////////////////////
//
// Instrumentation
//
// #define MEARSURE_PERFORMANCE

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    externals
//

// pull in internal constants
#include "constants.h"

// pull in internal configs
#include "config_adv.h"

// The user_config.h is initially added to the github repository but changes will be ignored via .gitignore. Please keep this file safe and possibly
// on a per tenant base stored. You can override parameters in this file by using #undef SOME_PARAMETER and then re-define again if needed, otherwise disable
// default features by using #undef FEATURE_OR_PARAMETER.
// This presents the possibilty to play with the code whilst staying in the loop with latest updates.
#include "user_config.h"

// At last we check all configs and spit compiler errors
#include "config_validator.h"

#endif
