/* *************************************************************************************************
 * File:      analog.h
 * Date:      26 Mar 2026
 * Author:    Andrew Levido
 *
 * Copyright 2026 Imbrius Pty Ltd - All Rights Reserved
 *
 **************************************************************************************************/
#ifndef _adc_dac_H
#define _adc_dac_H

#ifdef __cplusplus
extern "C" {
#endif

/* Description -----------------------------------------------------------------------------------*/
/*
#define P_VSET_Pin GPIO_PIN_8
#define P_VSET_GPIO_Port GPIOB
#define MX_RP_Pin GPIO_PIN_1
#define MX_RP_GPIO_Port GPIOB
#define MX_GP_Pin GPIO_PIN_2
#define MX_GP_GPIO_Port GPIOB
#define MX_BP_Pin GPIO_PIN_10
#define MX_BP_GPIO_Port GPIOB
#define MX_DV_Pin GPIO_PIN_11
#define MX_DV_GPIO_Port GPIOB
#define MX_RL_Pin GPIO_PIN_12
#define MX_RL_GPIO_Port GPIOB
#define MX_GL_Pin GPIO_PIN_13
#define MX_GL_GPIO_Port GPIOB
#define MX_BL_Pin GPIO_PIN_14
#define MX_BL_GPIO_Port GPIOB
#define MX_DIL_Pin GPIO_PIN_15
#define MX_DIL_GPIO_Port GPIOB
#define MX_RD_Pin GPIO_PIN_8
#define MX_RD_GPIO_Port GPIOA
#define MX_GD_Pin GPIO_PIN_9
#define MX_GD_GPIO_Port GPIOA
*/
/* Includes --------------------------------------------------------------------------------------*/
#include "app.h"

/* Defines ---------------------------------------------------------------------------------------*/
// ADC and DAC handles
extern ADC_HandleTypeDef      hadc1;
extern DAC_HandleTypeDef      hdac1;
#define AN_ADC                &hadc1
#define AN_DAC                &hdac1
// Input and output scaling factors
#define AN_RDC_SCALE          3700      // 1000 * (10K + 27K) / 10k)
#define AN_ILOAD_SCALE        3700      // 1000000 *  (10K + 27K)) / (1K * 10K)
#define AN_V_SCALE            3700      // 1000 * (1 + (10K + 27K) / 10k))mv
#define AN_IHI_SCALE          33333    // 100 * (1K2 * 10K) / 36k
#define AN_ILO_SCALE          13056     // (47K * 10K) / 36k

/* Typedefs & Enumerations -----------------------------------------------------------------------*/
// Matrix nodes and leads 
typedef enum { NODE_POWER, NODE_LOAD, NODE_DRIVE} node_t;
typedef enum { LEAD_NONE, LEAD_RED, LEAD_GREEN, LEAD_BLUE} lead_t;

/* Public Functions Declarations -----------------------------------------------------------------*/

/** Initialise analog subsystem
 *  Starts DAC and ADC, calibrar=tes ADC, makes the measurememnt of Vdda and battery voltage
 *
 * @returns           0 = OK or -ve number for error
 */
int an_init(void);

/** Set power node voltage
 *
 * @param high        true for +12V, false for 0V
 */
void an_set_power(bool high);

/** Set load node voltage
 *
 * @param load_mv     Load voltage in mV (0 to 12,000)
 */
void an_set_load_mv(int32_t load_mv);

/** Set drive node voltage
 *
 * @param drive_mv     Load voltage in mV (0 to 12,000)
 */
void an_set_drive_mv(int32_t drive_mv);

/** Set drive node current in micro apms
 *
 * @param drive_ua    Drive current in µA (0 to ±4000)
 */
void an_set_drive_ua(int32_t drive_ua);
/** Set drive node current in na (~80 nA resolution)
 *
 * @param drive_na    Drive current in nA (0 to ±4000000)
 */
void an_set_drive_na(int32_t drive_na);

void an_set_drive_none(void);

/** Set node to lead matrix
 *  Automatically unconnects so that each node only connects to one lead at a time
 *
 * @param node        Node to connect (enumerated type)
 * @param lead        Lead to connect to (enumerated type)
 */
void an_set_matrix(node_t node, lead_t lead);
void an_set_matrix_drive_short(lead_t lead_1, lead_t lead_2);

void an_set_matrix_drive_open(void);

void an_set_matrix_all_open(void);

/** Read red lead voltage in mV
 *
 * @returns          lead voltage in mV (positive) or -1 if error
 */
int32_t an_get_red_mv(void);

/** Read green lead voltage in mV
 *
 * @returns          lead voltage in mV (positive) or -1 if error
 */
int32_t an_get_green_mv(void);

/** Read blue lead voltage in mV
 *
 * @returns          lead voltage in mV (positive) or -1 if error
 */
int32_t an_get_blue_mv(void);

/** Read load current in µA
 *
 * @returns          lood current in µA (bipolar) or -1000000 if error
 */
int32_t an_get_load_ua(void);


/** Read Vdda voltage in mV
 *
 * @returns          Vdda voltage in mV (positive) or -1 if error
 */
int32_t an_get_vdda_mv(void);


#ifdef __cplusplus
}
#endif

#endif

/* End adc_dac */