
/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.h
 * @brief          : Header for main.c file.
 *                   This file contains the common defines of the application.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#if defined SEMIHOSTING
#include <stdio.h>
extern void initialise_monitor_handles(void);
#endif

#include "display.h"
#include "nvram.h"

#include "libPLX/plx.h"

  /* USER CODE END Includes */

  /* Exported types ------------------------------------------------------------*/
  /* USER CODE BEGIN ET */

#define TX_USART_BUFF_SIZ 512
#define RX_USART_BUFF_SIZ 512

enum { INFO_SIZE = PLX_MAX_OBS * PLX_MAX_INST_LIMIT}; // number of information storage elements

  extern const int DialTimeout;

  typedef struct
  {
    enum PLX_Observations Obs; ///<  Observation type
    int8_t Instance;           ///< Observation instance
  } uniqueObs_t;

  typedef struct
  {
    int Max;
    int Min;
    uniqueObs_t observation; //< observation identifier
    uint16_t data;           ///< Data value
    uint32_t sum;            ///< Sum of readings since last logged
    uint32_t lastUpdated;    ///< Time of last update
    uint16_t count;          ///< Count of readings since last logged .
    uint8_t updated;         ///< Set when updated
  } info_t;

  typedef struct
  {
    int8_t knobPos;             ///< User interface knob position
    uniqueObs_t OldObservation; ///< Previous item observed
    int16_t dial0;              ///< Display needle position 0
    int16_t dial1;              ///< Display needle position 1
    uint32_t dial_timer;        ///< Timestamp for next  NVRAM store
  } context_t;

  extern uniqueObs_t const nullObs;

  extern context_t const nullContext;

  extern info_t Info[INFO_SIZE];

  extern context_t contexts[MAX_DISPLAYS];

  // declarations need to be visible elsewhere
  extern SPI_HandleTypeDef hspi1;

  extern UART_HandleTypeDef huart1;
  extern UART_HandleTypeDef huart2;
  extern UART_HandleTypeDef huart3;

  extern TIM_HandleTypeDef htim3;
  extern TIM_HandleTypeDef htim9;

  /* USER CODE END ET */

  /* Exported constants --------------------------------------------------------*/
  /* USER CODE BEGIN EC */

  /* USER CODE END EC */

  /* Exported macro ------------------------------------------------------------*/
  /* USER CODE BEGIN EM */

  /* USER CODE END EM */

  /* Exported functions prototypes ---------------------------------------------*/



  void Error_Handler(void);

  /* USER CODE BEGIN EFP */
  
  
  extern context_t context[MAX_DISPLAYS];

/// @brief Reset the dial timer 
/// @param index 
void resetDialTimer(int index);

  extern uint8_t btConnected();

  extern uint8_t isValid(int i);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define SPI_NSS1_Pin GPIO_PIN_4
#define SPI_NSS1_GPIO_Port GPIOA
#define SPI_SCK_Pin GPIO_PIN_5
#define SPI_SCK_GPIO_Port GPIOA
#define SPI_CD_Pin GPIO_PIN_6
#define SPI_CD_GPIO_Port GPIOA
#define SPI_MOSI_Pin GPIO_PIN_7
#define SPI_MOSI_GPIO_Port GPIOA
#define SPI_RESET_Pin GPIO_PIN_4
#define SPI_RESET_GPIO_Port GPIOC
#define SPI_NSS2_Pin GPIO_PIN_5
#define SPI_NSS2_GPIO_Port GPIOC
#define BT_STATE_Pin GPIO_PIN_2
#define BT_STATE_GPIO_Port GPIOB
#define SW1_PUSH_Pin GPIO_PIN_12
#define SW1_PUSH_GPIO_Port GPIOB
#define SW1_I_Pin GPIO_PIN_13
#define SW1_I_GPIO_Port GPIOB
#define SW1_Q_Pin GPIO_PIN_14
#define SW1_Q_GPIO_Port GPIOB
#define SW2_PUSH_Pin GPIO_PIN_15
#define SW2_PUSH_GPIO_Port GPIOB
#define SW2_I_Pin GPIO_PIN_6
#define SW2_I_GPIO_Port GPIOC
#define SW2_Q_Pin GPIO_PIN_7
#define SW2_Q_GPIO_Port GPIOC
#define POWER_LATCH_Pin GPIO_PIN_8
#define POWER_LATCH_GPIO_Port GPIOC
#define IGNITION_Pin GPIO_PIN_9
#define IGNITION_GPIO_Port GPIOC
#define BT_BUTTON_Pin GPIO_PIN_8
#define BT_BUTTON_GPIO_Port GPIOA
#define PLX_TX_Pin GPIO_PIN_9
#define PLX_TX_GPIO_Port GPIOA
#define PLX_RX_Pin GPIO_PIN_10
#define PLX_RX_GPIO_Port GPIOA
#define BT_RESET_Pin GPIO_PIN_11
#define BT_RESET_GPIO_Port GPIOA
#define USB_PWR_Pin GPIO_PIN_12
#define USB_PWR_GPIO_Port GPIOC

  /* USER CODE BEGIN Private defines */

  /* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
