/*-----------------------------------------------------------------------------------------
 *  This software is in the public domain, furnished "as is", without technical
 *  support, and with no warranty, express or implied, as to its usefulness for
 *  any purpose.
 *
 *  Strawap38MVF_ESP.h
 *
 *  ESP32-WROOM-32 microcontroller software project to implement functionality for a
 *  two wheel driven RC amphi-tank (Strawap38MFV).
 *  This is the main header file for definitions.
 *
 *  Author: Volker Frauenstein
 *  Date: 03/03/2021 Version 1.0: definition takeover from Strawap38MVF_ESP.ino
 *                                and RC engine sound & LED controller include Headers
 */

// VEHICLE SETTINGS: Takeover from 1_adjustmentsVehicle.h for vehicle sound definition ***
// Russian tanks -------
#include "vehicles/IS3.h" // IS-3 WW2 battle tank, V12 Diesel (dual ESC mode, good bass speaker required)

// TRANSMISSION SETTINGS: Takeover from 4_adjustmentsTransmission.h for vehicle shifting simulation
#define VIRTUAL_3_SPEED
uint16_t maxClutchSlippingRpm = 300; // The clutch will never slip above this limit! (about 300) 500 for vehicles like locomotives

// definitions to handle input signals from RC
#define PWM_CHANNELS_NUM 4                       // Number of PWM signal input pins
volatile float RC_in[PWM_CHANNELS_NUM];          // an array to store the calibrated input from receiver
bool failed_before = true;                       // counter to store previous RC signal quality state
bool failed_now = true;                          // counter to store previous RC signal quality state
int count_good = 0;                              // hysteresecounter for RC signal quality measurement
volatile int pwmRCcycle = 1;                     // semaphore to trigger main loop after new RC Signal read cycle past
volatile bool pwm_cycle = true;                  // semaphore to trigger main loop Core 0 after new RC Signal read cycle

// definitions to handle throttle value for engine sound
int16_t power = 0;                               // value from 0 to 500
uint8_t drivestate = 0;                          // Vehicle state for sound - standstill, forward ...

// definitions to handle Multiswitch state
bool switchOn[4] = {false, false, false, false}; // one bit per switch: engine/horn/gunfire/lights

// decleration of function prototypes
void setupRCpwm();                               // RC PWM sfunctions
void readPwmSignals();
bool RCpwmScattering();
void close_DualDrive();                          // motorshilds and drive mode functions
void start_DualDriveMix();
void run_DualDrive(int s, int tr, int tl);
void setup_MultiSwitch();                        // multiswitch function
void close_MultiSwitch();
void run_MultiSwitch(int ch);
void setup_LEDstate();                           // RGB-LED functions
void set_LEDstate(int state);
void AlarmLED(bool alarm);
void sound_setup();                              // Sound functions
void sound_off();
void dacOffsetFade();
void mapThrottle(int16_t gas);
void engineOnOff(bool swap);
void triggerHorn(bool swap);
void triggerSound1(bool swap);
void engineMassSimulation();
void automaticGearSelector();
void DriveState();
void setup_watersensor();                        // water alarm functions
bool checkwater();
#ifdef SIM_RC
 void simulateRC();                              // for development without RC transmitter/receiver
#endif