/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 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
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g0xx.h"

/* Private includes ----------------------------------------------------------*/
#include "board.h"
#include "sensor.h"
#include "i2c.h"
#include "ds3231.h"
#include "bme280.h"
#include "rtos.h"
#include "event-system.h"
#include "list_event.h"
#include "clock.h"

/* Exported types ------------------------------------------------------------*/
typedef enum {
  DOWN = 0,
  UP = 1
} updown_t;

typedef enum {
  OFF = 0,
  ON = 1
} onoff_t;

typedef struct {
  uint32_t RTC_IRQ:     1;
  uint32_t SPI_TX_End:  1;
  uint32_t I2C_TX_End:  1;
  uint32_t I2C_RX_End:  1;
  uint32_t I2C_TX_Err:  1;
  uint32_t I2C_RX_Err:  1;
  uint32_t BME280:      1;
  uint32_t Blink_1:     1;
  uint32_t Blink_2:     1;
  uint32_t Blink_3:     1;
  uint32_t Blink_4:     1;
  uint32_t Blink_5:     1;
  uint32_t _reserv:    20;
} flag_t;
extern volatile flag_t Flag;

typedef union {
  uint16_t u16;           // element specifier for accessing whole u16
  int16_t  i16;           // element specifier for accessing whole i16
  struct {
    #ifdef LITTLE_ENDIAN  // Byte-order is little endian
    uint8_t u8L;          // element specifier for accessing low u8
    uint8_t u8H;          // element specifier for accessing high u8
    #else                 // Byte-order is big endian
    uint8_t u8H;          // element specifier for accessing low u8
    uint8_t u8L;          // element specifier for accessing high u8
    #endif
  } s16;                  // element spec. for acc. struct with low or high u8
} nt16_t;

typedef union {
  uint32_t u32;           // element specifier for accessing whole u32
  int32_t i32;            // element specifier for accessing whole i32
 struct {
    #ifdef LITTLE_ENDIAN  // Byte-order is little endian
    uint16_t u16L;        // element specifier for accessing low u16
    uint16_t u16H;        // element specifier for accessing high u16
    #else                 // Byte-order is big endian
    uint16_t u16H;        // element specifier for accessing low u16
    uint16_t u16L;        // element specifier for accessing high u16
    #endif
  } s32;                  // element spec. for acc. struct with low or high u16
} nt32_t;

/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* Private defines -----------------------------------------------------------*/
/* time constant in ms */
#define BTN_SCAN_PERIOD     10
#define BTN_SCAN_PAUSE      200
#define BTN_TIME_PRESSED    30
#define BTN_TIME_HOLDED     500
#define BTN_TIME_REPEATED   50

typedef struct {
  uint8_t     time;
  es_event_t  pressed;
  es_event_t  holded;
  uint32_t    pin;
  //(GPIO_TypeDef *) GPIOA; // ?->IDR
} btn_t;

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
