#ifndef _READ_SPI_H_
#define _READ_SPI_H_

#include "spi.h"
#include "gpio.h"
#include "struct_typedef.h"
// AS5047p ??
#define NOP 0x0000
#define ERRFL 0x0001
#define PROG 0x0003
#define DIAAGC 0x3FFC
#define MAG 0x3FFD
#define ANGLEUNC 0x3FFE
#define ANGLECOM 0x3FFF

#define ZPOSM 0x0016
#define ZPOSL 0x0017
#define SETTINGS1 0x0018
#define SETTINGS2 0x0019

typedef struct {
  int32_t round_cnt;
  uint16_t ecd;
  uint16_t last_ecd;
  uint16_t offset_ecd;
  double total_angle;
} Encoder_t;

#define AS5407P_CS_H() \
  HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET)
#define AS5407P_CS_L() \
  HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET)

  uint16_t Parity_bit_Calculate(uint16_t data_2_cal);
  uint16_t SPI_ReadWrite_OneByte(uint16_t _txdata);
  uint16_t AS5047_read(uint16_t add);


  void Encoder_Init(Encoder_t *ptr);
  void Update_Encoder(Encoder_t *ptr);
  int32_t Get_Encoder_Rounds(Encoder_t *ptr);
  double Get_Encoder_Angle(Encoder_t *ptr);

#endif
