/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/** BusVoodoo global definitions and methods (API)
 *  @file busvoodoo_global.h
 *  @author King Kévin <kingkevin@cuvoodoo.info>
 *  @date 2018
 *  @note peripherals used: time @ref busvoodoo_led_timer
 */
#pragma once
#include "menu.h"

/** @defgroup busvoodoo_voltages pin to control voltage regulators and pull-ups definitions
 *  @{
 */
#define BUSVOODOO_5VPULLUP_PORT B /**< 5V pull-up enable pin (active low) */
#define BUSVOODOO_5VPULLUP_PIN 4 /**< 5V pull-up enable pin (active low) */
#define BUSVOODOO_OEPULLUP_PORT A /**< bus switch output enable pin to enable embedded pull-ups  (active low) */
#define BUSVOODOO_OEPULLUP_PIN 15 /**< bus switch output enable pin to enable embedded pull-ups (active low) */
#define BUSVOODOO_LVEN_PORT A /**< low voltage (LV) enable pin (active high) */
#define BUSVOODOO_LVEN_PIN 6 /**< low voltage (LV) enable pin (active high) */
#define BUSVOODOO_HVEN_PORT A /**< high voltage (HV) enable pin (active low) */
#define BUSVOODOO_HVEN_PIN 7 /**< high voltage (HV) enable pin (active low) */
#define BUSVOODOO_VOUTEN_PORT B /**< voltage output (5V and 3.3V) enable pin (active low) */
#define BUSVOODOO_VOUTEN_PIN 3 /**< voltage output (5V and 3.3V) enable pin (active low) */
/** @} */

/** @defgroup busvoodoo_adc ADC inputs to measure voltages from voltage regulators
 *  @{
 */
#define BUSVOODOO_3V3_CHANNEL 12 /**< ADC channel to measure 5V rail */
#define BUSVOODOO_5V_CHANNEL 9 /**< ADC channel to measure 3.3V rail */
#define BUSVOODOO_LV_CHANNEL 11 /**< ADC channel to measure LV rail */
#define BUSVOODOO_HV_CHANNEL 15 /**< ADC channel to measure HV rail */
/** @} */

/** @defgroup busvoodoo_dac DAC output to set voltages of voltage regulators
 *  @{
 */
#define BUSVOODOO_LVCTL_PORT A /**< pin to control LV output voltage */
#define BUSVOODOO_LVCTL_PIN 4 /**< pin to control LV output voltage */
#define BUSVOODOO_LVCTL_CHANNEL CHANNEL_1 /**< DAC channel to control LV output voltage */
#define BUSVOODOO_LV_SET(x) ((0.8*(1+30.0/10.0)-x)*(10.0/30.0)+0.8) /**< voltage to output for the DAC to set the desired LV output voltage (based on resistor values on the LV adjust pins and LV voltage reference) */
#define BUSVOODOO_HVCTL_PORT A /**< pin to control HV output voltage */
#define BUSVOODOO_HVCTL_PIN 5 /**< pin to control HV output voltage */
#define BUSVOODOO_HVCTL_CHANNEL CHANNEL_2 /**< DAC channel to control HV output voltage */
#define BUSVOODOO_HV_SET(x) ((1.25*(1+100.0/10.0)-x)*(10.0/100.0)+1.25) /**< voltage to output for the DAC to set the desired HV output voltage (based on resistor values on the HV adjust pins and HV voltage reference) */
/** @} */

/** @defgroup busvoodoo_rs232 RS-232 transceiver connection definition
 *  @{
 */
#define BUSVOODOO_RS232_EN_PORT B /**< RS-232 pin to enable receiver (active low, pulled up) */
#define BUSVOODOO_RS232_EN_PIN 5 /**< RS-232 pin to enable receiver (active low, pulled up) */
#define BUSVOODOO_RS232_SHDN_PORT C /**< RS-232 pin to enable transmitter (active high, pulled low) */
#define BUSVOODOO_RS232_SHDN_PIN 15 /**< RS-232 pin to enable transmitter (active high, pulled low) */
#define BUSVOODOO_RS232_RTS_PORT A /**< RS-232 Request-To-Send output pin */
#define BUSVOODOO_RS232_RTS_PIN 1 /**< RS-232 Request-To-Send output pin */
#define BUSVOODOO_RS232_CTS_PORT A /**< RS-232 Clear-To-Send input pin */
#define BUSVOODOO_RS232_CTS_PIN 0 /**< RS-232 Clear-To-Send input pin */
#define BUSVOODOO_RS232_TX_PORT A /**< RS-232 Transmit output pin */
#define BUSVOODOO_RS232_TX_PIN 2 /**< RS-232 Transmit output pin */
#define BUSVOODOO_RS232_RX_PORT A /**< RS-232 Receive input pin */
#define BUSVOODOO_RS232_RX_PIN 3 /**< RS-232 Receive input pin */
/** @} */

/** @defgroup busvoodoo_rs485 RS-485 (and RS-422) transceiver connection definition
 *  @{
 */
#define BUSVOODOO_RS485_RE_PORT C /**< RS-485 pin to enable receiver (active low, pulled up) */
#define BUSVOODOO_RS485_RE_PIN 13 /**< RS-485 pin to enable receiver (active low, pulled up) */
#define BUSVOODOO_RS485_DE_PORT C /**< RS-485 pin to enable transmitter (active high, pulled low) */
#define BUSVOODOO_RS485_DE_PIN 14 /**< RS-485 pin to enable transmitter (active high, pulled low) */
#define BUSVOODOO_RS485_TX_PORT A /**< RS-485 Transmit output pin */
#define BUSVOODOO_RS485_TX_PIN 2 /**< RS-485 Transmit output pin */
#define BUSVOODOO_RS485_RX_PORT A /**< RS-485 Receive input pin */
#define BUSVOODOO_RS485_RX_PIN 3 /**< RS-485 Receive input pin */
/** @} */

#if BUSVOODOO_HARDWARE_VERSION!=0
/** @defgroup busvoodoo_can CAN transceiver connection definition
 *  @{
 */
#define BUSVOODOO_CAN_EN_PORT C /**< signal to power CAN transceiver (active low, pulled high) */
#define BUSVOODOO_CAN_EN_PIN 7 /**< signal to power CAN transceiver (active low, pulled high) */
#define BUSVOODOO_CAN_TX_PORT B /**< CAN Transmit output pin */
#define BUSVOODOO_CAN_TX_PIN 9 /**< CAN Transmit output pin */
#define BUSVOODOO_CAN_RX_PORT B /**< CAN Receive input pin */
#define BUSVOODOO_CAN_RX_PIN 8 /**< CAN Receive input pin */
/** @} */
#endif

/** @defgroup busvoodoo_version ADC pin used to identify hardware version based on voltage
 */
#define BUSVOODOO_HW_VERSION_PORT B /**< pin to identify hardware version */
#define BUSVOODOO_HW_VERSION_PIN 0 /**< pin to identify hardware version */
#define BUSVOODOO_HW_VERSION_CHANNEL 8 /**< ADC to identify hardware version */
/** @} */

/** recommended duration in ms for pulsing LEDs to show activity */
#define BUSVOODOO_LED_PULSE 100

/** BusVoodoo mode interface */
struct busvoodoo_mode_t {
	const char* name; /**< name of the mode (i.e. protocol shortcut for the menu) */
	const char* description; /**< human readable description of the mode (i.e. full protocol name) */
	const bool full_only; /**< if this mode is available only for BusVoodoo full flavor */
	bool (*setup)(char** prefix, const char* line); /**< function to setup mode (menu prefix can be used to ask parameter, and line will be the user provided response)*/
	const struct menu_command_t* commands; /**< list of menu commands provided by mode */
	const uint8_t commands_nb; /**< number of menu commands provided by mode */
	void (*exit)(void); /**< function to exit from mode (i.e. releasing the peripheral) */
};

/** @defgroup busvoodoo_io I/O connector pin definition
 *  @{
 */
extern const char* busvoodoo_io_names[13]; /**< I/O individual signal names */
extern const uint32_t busvoodoo_io_ports[13]; /**< port of individual signals */
extern const uint32_t busvoodoo_io_pins[13]; /**< pin of individual signals */
extern const uint8_t busvoodoo_io_groups[13]; /**< which I/O pin (group) does the signal belong to */
/** @} */

/** is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side) */
extern bool busvoodoo_full;
/** version of the hardware board */
extern char busvoodoo_version;
/** list of supported commands for base BusVoodoo */
extern const struct menu_command_t busvoodoo_global_commands[];
/** number supported commands for base BusVoodoo */
extern const uint8_t busvoodoo_global_commands_nb;
/** list of supported commands for BusVoodoo full only */
extern const struct menu_command_t busvoodoo_global_full_commands[];
/** number supported commands for BusVoodoo full only */
extern const uint8_t busvoodoo_global_full_commands_nb;

/** I/O connector pinout */
extern const char* busvoodoo_global_pinout_io[10];
/** RS/CAN connector pinout */
extern const char* busvoodoo_global_pinout_rscan[5];

/** shared string buffer, i.e. used when configuring modes */
extern char busvoodoo_global_string[64];

/** setup BusVoodoo board */
void busvoodoo_setup(void);
/** set safe state by disabling all outputs */
void busvoodoo_safe_state(void);
/** switch 3V3 and 5V power outputs on I/O connector
 *  @param[in] on switch on (true) or off (false) power output
 *  @return if switching succeeded. else it means there is a probably a short on one of the outputs
 */
bool busvoodoo_vout_switch(bool on);
/** read voltage from power rail
 *  @param[in] channel which power rail to read voltage from @ref busvoodoo_adc
 *  @return voltage, or NaN if channel is invalid (or error happened)
 */
float busvoodoo_vreg_get(uint8_t channel);
/** set voltage on low voltage adjustable voltage regulator
 *  @note for voltages above 4.85V it will use the 5.0V from USB
 *  @param[in] voltage voltage to set on adjustable voltage regulator (0, 0.3-4.8, 5V)
 *  @return voltage output, or NaN if error happened
 */
float busvoodoo_lv_set(float voltage);
/** set voltage on high voltage adjustable voltage regulator
 *  @param[in] voltage voltage to set on adjustable voltage regulator (0, 3.3-24V) 
 *  @return voltage output, or NaN if error happened
 */
float busvoodoo_hv_set(float voltage);
/** enable embedded pull-up resistors
 *  @note the embedded pull-up resistor use the voltage on the LV power rail (set or externally provided)
 *  @param[in] on enable (true) or disable (false) embedded pull-up resistors
 *  @return voltage applied on pull-up resistors, or NaN if channel is invalid (or error happened)
 */
float busvoodoo_embedded_pullup(bool on);
/** switch on blue LED
 *  @note the red LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_blue_on(void);
/** switch off blue LED
 *  @note the red LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_blue_off(void);
/** switch on red LED
 *  @note the blue LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_red_on(void);
/** switch off red LED
 *  @note the blue LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_red_off(void);
/** switch off blue and red LEDs
 *  @note this will stop the blinking pattern
 */
void busvoodoo_leds_off(void);
/** pulse blue LED for short duration
 *  @param[in] ms duration in ms
 *  @note the red LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_blue_pulse(uint32_t ms);
/** pulse red LED for short duration
 *  @param[in] ms duration in ms
 *  @note the red LED is handled independently 
 *  @note this will stop the blinking pattern
 */
void busvoodoo_led_red_pulse(uint32_t ms);
/** set LED blinking pattern
 *  @param[in] period blue+red pattern duration in seconds (up to 3+3)
 *  @param[in] duty blue LED on duty cycle, before switching to red (0-1)
 *  @note when exiting blink the blue and red LEDs will be off if they were pulsing before
 */
void busvoodoo_leds_blink(double period, double duty);
/** parse and perform actions
 *  @note performing action is a common command in mode and this function helps parsing them
 *  @param[in] actions actions to perform
 *  @param[in] perform the action (true) or just check it (false)
 *  @param[in] action_handler function handling the individual actions
 *  @return true if the actions have been performed, false if any of them is malformed
 *  @note while parsing the actions the string will be modified
 */
bool busvoodoo_global_actions(char* actions, bool perform, bool (*action_handler)(const char* action, uint32_t repetition, bool perform));
