sensors.c 7.49 KB
#include "sensors.h"

sensor light_sensor 		= {LIGHT_ADDR, "Light sensor", "illumination", "lux",NULL};
sensor ultrasound_sensor 	= {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm",NULL};
sensor pressure_sensor 		= {PRESSURE_ADDR, "Pressure sensor", "pressure", "kPa",NULL};
sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Humidity and Temperature","ºC,RH",NULL};

uint32_t get_light_data(void)
{
	uint32_t data = 0;

	data = get_light_ch1();
	data = data << 16;
	data = data | get_light_ch0();

	return data;
}
uint16_t get_light_ch0(void)
{
	uint16_t data = 0;

	I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x0D);
	I2C_stop(I2C1);

	I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
 	data = I2C_read_ack(I2C1);
 	data = data << 8;

 	I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x0C);
	I2C_stop(I2C1);

	I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
 	data = data | I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	return data;
}
uint16_t get_light_ch1(void)
{
	uint16_t data = 0;

	I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x0F);
	I2C_stop(I2C1);
	
	I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
 	data = I2C_read_ack(I2C1);
 	data = data << 8;

 	I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x0E);
	I2C_stop(I2C1);

	I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
 	data = data | I2C_read_ack(I2C1);
 	I2C_stop(I2C1);
 	
 	return data;
}

uint16_t get_distance_data(void)
{
	init_ultrasound();
	uint16_t data = get_distance_high();
	data = data << 8;
	data = data | get_distance_low();
	return data;
}
void init_ultrasound(void)
{
	I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x00);
	I2C_stop(I2C1);

	I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x50);
	I2C_stop(I2C1);

	chThdSleepMilliseconds(70);
}
uint8_t get_distance_low(void)
{
	uint8_t data;
	I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x03);
	I2C_stop(I2C1);
	
	I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
 	data = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);
 	return data;
}
uint8_t get_distance_high(void)
{
	uint8_t data;
	I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0x02);
	I2C_stop(I2C1);
	
	I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
 	data = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);
 	return data;
}

uint16_t get_pressure_data(void)
{
	uint16_t data;
	init_pressure();

	I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xF6);
	I2C_stop(I2C1);

	I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
 	data = I2C_read_ack(I2C1);
 	data = data << 8;
 	data = data | I2C_read_nack(I2C1);
	return data;
}

bmp085_callibration get_pressure_callibration_data(void)
{
	bmp085_callibration calib_data;

	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xAA);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC1 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xAC);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC2 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xAE);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC3 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xB0);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC4 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xB2);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC5 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xB4);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.AC6 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xB6);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.B1 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xB8);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.B2 = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xBA);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.MB = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xBC);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.MC = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xBE);
	I2C_stop(I2C1);
	I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
 	calib_data.MD = I2C_read_ack(I2C1);
 	I2C_stop(I2C1);

 	return calib_data;
}
char* callibration_pressure_data_csv(bmp085_callibration parameters)
{
	char *str = chHeapAlloc(NULL,sizeof(char)*(4*11-1));
	memset(str,0x00,sizeof(char)*(4*11-1));
	sprintf(str + strlen(str),"%d,",parameters.AC1);
	sprintf(str + strlen(str),"%d,",parameters.AC2);
	sprintf(str + strlen(str),"%d,",parameters.AC3);
	sprintf(str + strlen(str),"%d,",parameters.AC4);
	sprintf(str + strlen(str),"%d,",parameters.AC5);
	sprintf(str + strlen(str),"%d,",parameters.AC6);
	sprintf(str + strlen(str),"%d,",parameters.B1);
	sprintf(str + strlen(str),"%d,",parameters.B2);
	sprintf(str + strlen(str),"%d,",parameters.MB);
	sprintf(str + strlen(str),"%d,",parameters.MC);
	sprintf(str + strlen(str),"%d",parameters.MD);
	return str;
}

void init_pressure(void)
{
	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
	I2C_write(I2C1, 0xF4);
	I2C_write(I2C1, 0x34);
	I2C_stop(I2C1);
	
  	chThdSleepMilliseconds(5);
}

void init_humidity_temp(void)
{
	I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter);
	I2C_stop(I2C1);
}
uint16_t get_humidity_data(void)
{
	uint16_t data = 0;
	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
	data = I2C_read_ack(I2C1) & 0x3F;
	data = data << 8;
	data |= I2C_read_nack(I2C1);
	I2C_stop(I2C1);
	return data;
}
uint16_t get_temperature_data(void)
{
	uint16_t data = 0;
	I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
	I2C_read_ack(I2C1);
	I2C_read_ack(I2C1);
	data = I2C_read_ack(I2C1);
	data = data << 8;
	data |= I2C_read_nack(I2C1) & 0xFC ;
	data = data >> 2;
	I2C_stop(I2C1);
	return data;
}

char* light_value(uint32_t light)
{
	char *value = chHeapAlloc(NULL,10 + 1);
	sprintf(value,"%d",light);
	return value;
}
char* distance_value(uint16_t distance)
{
	char *value = chHeapAlloc(NULL,5 + 1);
	sprintf(value,"%d",distance);
	return value;
}
char* pressure_value(uint16_t pressure)
{
	char *value = chHeapAlloc(NULL,5 + 1);
	sprintf(value,"%d",pressure);
	return value;
}
char* temp_humidity_value(uint16_t temp, uint16_t humidity)
{
	char *value = chHeapAlloc(NULL,21 + 1);
	sprintf(value,"%d,%d",temp,humidity);
	return value;
}

//light_value(get_light_data());
//temp_humidity_value(get_temperature_data(),get_humidity_data());