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

#include "sps30.h"
#include "main.h" /* for uart_disable_interrupts() */

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;
	uart_disable_interrupts();
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
	uart_enable_interrupts();
	// TODO: Proc to vraci NACK? Vyresit.
	if (result != I2C_OK) {
		return SPS30_ERROR;
	}

	return SPS30_OK;
}

int8_t sps30_start_measurement( void )
{
	uint8_t buffer[5];
	uint8_t result;

	buffer[0] = SPS30_START_MEASUREMENT >> 8;
	buffer[1] = SPS30_START_MEASUREMENT & 0x00ff;
	buffer[2] = SPS30_UINT16_FORMAT;
	buffer[3] = 0x00;
	buffer[4] = crc8_calculate(buffer + 2, 2);

	uart_disable_interrupts();
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 5);
	uart_enable_interrupts();

	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(sps30_data_t *measured_data)
{
	uint8_t buffer[32];

	uint8_t result;

	// start measurement
	buffer[0] = SPS30_READ_MEASURED_VALUES >> 8;
	buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF;
	uart_disable_interrupts();
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
	uart_enable_interrupts();
	if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	LL_mDelay(10); // 10 ms should be enough
	// read out
	uart_disable_interrupts();
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 3 * SPS30_MEASURED_VALUES_COUNT);
	uart_enable_interrupts();
	if (result != I2C_OK)
	{
		return SPS30_ERROR;
	}
	/* check data integrity */
	for (uint8_t i = 0; i < SPS30_MEASURED_VALUES_COUNT; i++)
	{
		uint8_t checksum_calculated = crc8_calculate(buffer + 3*i, 2);
		uint8_t checksum_received = buffer[3*i + 2];
		if (checksum_calculated != checksum_received) {
			return SPS30_CRC8_ERROR;
		}
	}
	/* copy to output struct */
	/* mass concencration [ug / m^3] */
	int pos = 0;
	for (int i = 0; i < 4; i++) {
		/* i + 1 because mass concentration starts at PM1.0 (there is no PM0.5) */
		measured_data->mass_concentration[i + 1] = (buffer[pos] << 8) + buffer[pos + 1];
		pos += 3; /* 2 B data, 1 B crc */
	}
	/* number concentration [1 / cm^3] */
	for (int i = 0; i < 5; i++) {
		measured_data->number_concentration[i] = (buffer[pos] << 8) + buffer[pos + 1];
		pos += 3;
	}
	/* typical particle size [nm] */
	measured_data->typical_particle_size = (buffer[pos] << 8) + buffer[pos + 1];

	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);
}

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 buffer[6];

	uint8_t result;

	// start measurement
	buffer[0] = SPS30_READ_DEVICE_STATUS_REGISTER >> 8;
	buffer[1] = SPS30_READ_DEVICE_STATUS_REGISTER & 0x00ff;
	uart_disable_interrupts();
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
	uart_enable_interrupts();
	if (result != I2C_OK) {
		return SPS30_ERROR;
	}
	LL_mDelay(10); // 10 ms should be enough
	// read out
	uart_disable_interrupts();
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 6);
	uart_enable_interrupts();
	// TODO

	return SPS30_OK;
}

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;
	uart_disable_interrupts();
	result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);
	uart_enable_interrupts();

	// 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
	uart_disable_interrupts();
	result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 3);
	uart_enable_interrupts();
	/*if (result != I2C_OK)
	{
		return SPS30_ERROR;
	}*/

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

	return SPS30_OK;
}
