/*
 * sps30.h
 *
 *  Created on: Jul 18, 2021
 *      Author: mrs
 */

#ifndef INC_SPS30_H_
#define INC_SPS30_H_

#include "stdint.h"
#include "i2c.h"
#include "crc8.h"

/*
 * Defines & macros
 */

#define SPS30_I2C_ADDRESS 0x69

/*
 * Return values
 */

#define SPS30_OK 0
#define SPS30_ERROR -1 // generic error
#define SPS30_CRC8_ERROR -2 // checksum failed

/*
 * Data types
 */

typedef enum {
	SPS30_START_MEASUREMENT = 0x0010,
	SPS30_STOP_MEASUREMENT = 0x0104,
	SPS30_READ_DATA_READY_FLAG = 0x0202,
	SPS30_READ_MEASURED_VALUES = 0x0300,
	SPS30_SLEEP = 0x1001,
	SPS30_WAKE_UP = 0x1103,
	SPS30_START_FAN_CLEANING = 0x5607,
	SPS30_READ_AUTO_CLEANING_INTERVAL = 0x8004,
	SPS30_WRITE_AUTO_CLEANING_INTERVAL = 0x8004,
	SPS30_READ_PRODUCT_TYPE = 0xD002,
	SPS30_READ_SERIAL_NUMBER = 0xD033,
	SPS30_READ_VERSION = 0xD100,
	SPS30_READ_DEVICE_STATUS_REGISTER = 0xD206,
	SPS30_CLEAR_DEVICE_STATUS_REGISTER = 0xD210,
	SPS30_RESET = 0xD304

} sps30_cmd_t;

typedef enum {
	SPS30_FLOAT_FORMAT = 0x03,
	SPS30_UINT16_FORMAT = 0x05
} sps30_data_format;

int8_t sps30_send_cmd(sps30_cmd_t cmd);

int8_t sps30_start_measurement( void );
int8_t sps30_stop_measurement( void );
int8_t sps30_read_measured_values(uint16_t *measured_values, uint8_t measured_values_len);

int8_t sps30_sleep( void );
int8_t sps30_wake_up( void );

int8_t sps30_start_fan_cleaning( void );

int8_t sps30_reset( void );

int8_t sps30_read_status_register ( void );

int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo );

uint8_t calculate_crc(uint8_t data[2]);

#endif /* INC_SPS30_H_ */
