/*!
 * \file jfet_common.h
 *
 * \brief common struct, enum, externs, prototypes
 *
 * \copyright  J Factor Embedded Technologies LLC. 2023
 * \copyright  TJM Embedded Software LLC. 2023
 * \author  T. J. Mulrooney
 *
 */
/*
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies.
 *
 */
#ifndef JFET_COMMON_H_
#define JFET_COMMON_H_

#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION_MAJOR 0
#define VERSION_MINOR 3

#define VERSION_DATE "Temp/Humidity BLE Sensor 09/29/25"
#define BOARD_NAME "Temp/Humidity BLE Sensor Prototype"

#define VERSION_DATE_EXT " (Bd 1.0 DK)"
#define BOARD_NAME_EXT " V1.0 DK"

#define FALSE 0
#define TRUE 1

#define MAX_PARAMETERS 3
#define FLASHPARAMETERSTART 0
#define MAX_FLASH_BYTES 0x200

#define SPI_BUF_SIZE 128
#define SPI_SLAVE 0
#define SLOW_FREQ 150000
#define FAST_FREQ 16000000

#define DATA_LENGTH 20

#define LOWTEMPENABLE 0x01
#define LOWTEMPACTIVE 0x02
#define HIGHTEMPENABLE 0x04
#define HIGHTEMPACTIVE 0x08
#define LOWHUMIDITYENABLE 0x01
#define LOWHUMIDITYACTIVE 0x02
#define HIGHHUMIDITYENABLE 0x04
#define HIGHHUMIDITYACTIVE 0x08

enum SENSORTYPE
{
        SENSORTYPEHDC2080,
        SENSORTYPEW25Q64JV,
        SENSORTYPEUNKNOWN,
};

/* prototypes */
void myPrintf(char *str);
void myPrintkW(char *str);
void myPrintkI(char *str);
void myPrintkS(char *str);
void myPrintkE(char *str);
int init_i2c();
void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins);
uint8_t bleCommand(uint8_t *data);
int getCmd(uint8_t *line);
int initHDC2080(void);
int initW25Q64JV(void);
void readNVFlashxID(uint8_t *flashId);
void readNVFlashxStatus(uint8_t *flashStatus);
void flashxErasePage(uint32_t address);
void flashxEraseBlock1(uint32_t address);
void flashxEraseBlock2(uint32_t address);
void flashxEraseSector(uint32_t address);
void flashxErase(void);
void flashxReadPage(uint32_t address, uint8_t *flashRead);
void flashxWritePage(uint32_t page, uint32_t address, uint8_t *data, uint16_t count);
void flashxFillPage(uint32_t address, uint16_t data);
void flashxClearParameters(void);
void setFlashxParameters(uint8_t display);
void displayFlashxParameters(void);
void rescan(void);
void executeCmd(char *line);
int8_t getCLICmd(char *line);
int adc_sample(void);

/* externals */
extern uint16_t manfID;
extern uint16_t deviceID;
extern const struct device *const i2c_dev;
extern float hdcTemp;
extern float hdcHumidity;
extern char *cmdLineParameter[];
extern uint8_t numParameters;
extern char devIDStr[];
extern int ret;
extern uint8_t buffer_tx[];
extern uint8_t buffer_rx[];
extern struct spi_buf tx_bufs[];
extern struct spi_buf rx_bufs[];
extern struct spi_config spi_cfg1;
extern struct spi_config spi_cfg2;
extern const struct spi_buf_set tx;
extern const struct spi_buf_set rx;
extern const struct spi_cs_control spi_cs1;
extern const struct spi_cs_control spi_cs2;
extern const struct gpio_dt_spec spics1n;
extern const struct gpio_dt_spec spics2n;
extern const struct device *const spi_dev;
extern uint32_t sensorTypePresentAll;
extern float adc_voltage[];
extern uint8_t flashReadIn[];
extern const struct gpio_dt_spec baten;
extern struct k_sem hdc_rdy_seen;
extern char resetCause[];

#endif /* JFET_COMMON_H_ */
