/*
 * sps30.c
 *
 *  Created on: Jul 18, 2021
 *      Author: david
 */

#include "sps30.h"

int8_t sps30_send_cmd(sps30_cmd_t cmd)
{
	uint8_t buffer[32];
	uint8_t result;

	// start measurement
	buffer[0] = cmd >> 8;
	buffer[1] = cmd & 0x00ff;
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
	// TODO: Proc to vraci NACK? Vyresit.
	if (result != I2C_OK) {
		return SPS30_ERROR;
	}

	return SPS30_OK;
}

int8_t sps30_start_measurement( void )
{
	uint8_t i2c_tx_buffer[5];
	uint8_t data_for_crc = {SPS30_UINT16_FORMAT, 0x00};

	uint8_t result;

	i2c_tx_buffer[0] = SPS30_START_MEASUREMENT >> 8;
	i2c_tx_buffer[1] = SPS30_START_MEASUREMENT & 0x00ff;
	i2c_tx_buffer[2] = SPS30_UINT16_FORMAT;
	i2c_tx_buffer[3] = 0x00;
	i2c_tx_buffer[4] = calculate_crc(data_for_crc);

	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 5);

	// TODO: Proc to vraci NACK? Vyresit.
	if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	return SPS30_OK;
}

int8_t sps30_stop_measurement( void )
{
	return sps30_send_cmd(SPS30_STOP_MEASUREMENT);
}

int8_t sps30_read_measured_values(uint16_t *measured_values, uint8_t measured_values_len)
{

	if (measured_values_len != 10)
	{
		return -5;
	}

	uint8_t i2c_tx_buffer[2];
	uint8_t i2c_rx_buffer[30];

	uint8_t result;

	// start measurement
	i2c_tx_buffer[0] = SPS30_READ_MEASURED_VALUES >> 8;
	i2c_tx_buffer[1] = SPS30_READ_MEASURED_VALUES & 0x00ff;
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);

	// TODO: Proc to vraci NACK? Vyresit.
	/*if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	return SPS30_OK;*/

	LL_mDelay(1); // 10 ms should be enough
	// read out
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 30);
	if (result != I2C_OK)
	{
		return SPS30_ERROR;
	}

	uint8_t checksums[10];

	uint8_t j = 0;
	for (uint8_t i = 0; i < 10; i++)
	{

		measured_values[i] = (i2c_rx_buffer[j++] << 8) + i2c_rx_buffer[j++];
		checksums[i] = i2c_rx_buffer[j++];
	}

	return SPS30_OK;
}

int8_t sps30_sleep( void )
{
	return sps30_send_cmd(SPS30_SLEEP);
}

int8_t sps30_wake_up( void )
{
	return sps30_send_cmd(SPS30_WAKE_UP);
	return sps30_send_cmd(SPS30_WAKE_UP);
}

int8_t sps30_start_fan_cleaning( void )
{
	return sps30_send_cmd(SPS30_START_FAN_CLEANING);
}

int8_t sps30_reset( void )
{
	return sps30_send_cmd(SPS30_RESET);
}


int8_t sps30_read_status_register ( void )
{
	uint8_t i2c_tx_buffer[2];
	uint8_t i2c_rx_buffer[6];

	uint8_t result;

	// start measurement
	i2c_tx_buffer[0] = SPS30_READ_DEVICE_STATUS_REGISTER >> 8;
	i2c_tx_buffer[1] = SPS30_READ_DEVICE_STATUS_REGISTER & 0x00ff;
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);

	// TODO: Proc to vraci NACK? Vyresit.
	/*if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	return SPS30_OK;*/

	LL_mDelay(1); // 10 ms should be enough
	// read out
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 6);

	return 0;
}

int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo )
{
	uint8_t i2c_tx_buffer[2];
	uint8_t i2c_rx_buffer[3];

	uint8_t result;

	// start measurement
	i2c_tx_buffer[0] = SPS30_READ_VERSION >> 8;
	i2c_tx_buffer[1] = SPS30_READ_VERSION & 0x00ff;
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);

	// TODO: Proc to vraci NACK? Vyresit.
	/*if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	return SPS30_OK;*/

	LL_mDelay(1); // 10 ms should be enough
	// read out
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 3);
	/*if (result != I2C_OK)
	{
		return SPS30_ERROR;
	}*/

	*fw_ver_hi = i2c_rx_buffer[0];
	*fw_ver_lo = i2c_rx_buffer[1];

	return SPS30_OK;
}


uint8_t calculate_crc(uint8_t data[2])
{
	uint8_t crc = 0xFF;
	for(uint8_t i = 0; i < 2; i++) {
		crc ^= data[i];
		for(uint8_t bit = 8; bit > 0; --bit) {
			if(crc & 0x80) {
				crc = (crc << 1) ^ 0x31u;
			} else {
				crc = (crc << 1);
			}
		}
	}
	return crc;
}
