Adapted OpenMote-CC2538 sensor drivers to Contiki sensor API.
This commit is contained in:
parent
3e00ea55d1
commit
d9f4d97a41
7 changed files with 304 additions and 186 deletions
|
@ -83,31 +83,29 @@ static struct broadcast_conn bc;
|
|||
PROCESS_THREAD(openmote_demo_process, ev, data)
|
||||
{
|
||||
static struct etimer et;
|
||||
static unsigned int raw, counter;
|
||||
static uint8_t adxl346_present, max44009_present, sht21_present;
|
||||
static float light, temperature, humidity;
|
||||
static int16_t counter;
|
||||
static uint16_t adxl346_present, sht21_present, max44009_present;
|
||||
static uint16_t accel, light, temperature, humidity;
|
||||
|
||||
PROCESS_EXITHANDLER(broadcast_close(&bc))
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
adxl346_init();
|
||||
adxl346_present = adxl346_is_present();
|
||||
if(!adxl346_present) {
|
||||
/* Initialize and activate the ADXL346 sensor */
|
||||
adxl346_present = SENSORS_ACTIVATE(adxl346);
|
||||
if(adxl346_present == ADXL346_ERROR) {
|
||||
printf("ADXL346 sensor is NOT present!\n");
|
||||
leds_on(LEDS_YELLOW);
|
||||
}
|
||||
|
||||
max44009_init();
|
||||
max44009_present = max44009_is_present();
|
||||
if(!max44009_present) {
|
||||
max44009_present = SENSORS_ACTIVATE(max44009);
|
||||
if(max44009_present == MAX44009_ERROR) {
|
||||
printf("MAX44009 sensor is NOT present!\n");
|
||||
leds_on(LEDS_ORANGE);
|
||||
}
|
||||
|
||||
sht21_init();
|
||||
sht21_present = sht21_is_present();
|
||||
if(!sht21_present) {
|
||||
sht21_present = SENSORS_ACTIVATE(sht21);
|
||||
if(sht21_present == SHT21_ERROR) {
|
||||
printf("SHT21 sensor is NOT present!\n");
|
||||
leds_on(LEDS_RED);
|
||||
}
|
||||
|
@ -123,33 +121,30 @@ PROCESS_THREAD(openmote_demo_process, ev, data)
|
|||
PROCESS_YIELD();
|
||||
|
||||
if(ev == PROCESS_EVENT_TIMER) {
|
||||
if(adxl346_present) {
|
||||
if(adxl346_present != ADXL346_ERROR) {
|
||||
leds_on(LEDS_YELLOW);
|
||||
raw = adxl346_read_x();
|
||||
printf("X Acceleration: %u\n", raw);
|
||||
raw = adxl346_read_y();
|
||||
printf("Y Acceleration: %u\n", raw);
|
||||
raw = adxl346_read_z();
|
||||
printf("Z Acceleration: %u\n", raw);
|
||||
accel = adxl346.value(ADXL346_READ_X);
|
||||
printf("X Acceleration: %u\n", accel);
|
||||
accel = adxl346.value(ADXL346_READ_Y);
|
||||
printf("Y Acceleration: %u\n", accel);
|
||||
accel = adxl346.value(ADXL346_READ_Z);
|
||||
printf("Z Acceleration: %u\n", accel);
|
||||
leds_off(LEDS_YELLOW);
|
||||
}
|
||||
|
||||
if(max44009_present) {
|
||||
if(max44009_present != MAX44009_ERROR) {
|
||||
leds_on(LEDS_ORANGE);
|
||||
raw = max44009_read_light();
|
||||
light = max44009_convert_light(raw);
|
||||
printf("Light: %u.%ulux\n", (unsigned int)light, (unsigned int)(light * 100) % 100);
|
||||
light = max44009.value(MAX44009_READ_LIGHT);
|
||||
printf("Light: %u.%ulux\n", light / 100, light % 100);
|
||||
leds_off(LEDS_ORANGE);
|
||||
}
|
||||
|
||||
if(sht21_present) {
|
||||
if(sht21_present != SHT21_ERROR) {
|
||||
leds_on(LEDS_RED);
|
||||
raw = sht21_read_temperature();
|
||||
temperature = sht21_convert_temperature(raw);
|
||||
printf("Temperature: %u.%uC\n", (unsigned int)temperature, (unsigned int)(temperature * 100) % 100);
|
||||
raw = sht21_read_humidity();
|
||||
humidity = sht21_convert_humidity(raw);
|
||||
printf("Rel. humidity: %u.%u%%\n", (unsigned int)humidity, (unsigned int)(humidity * 100) % 100);
|
||||
temperature = sht21.value(SHT21_READ_TEMP);
|
||||
printf("Temperature: %u.%uC\n", temperature / 100, temperature % 100);
|
||||
humidity = sht21.value(SHT21_READ_RHUM);
|
||||
printf("Rel. humidity: %u.%u%%\n", humidity / 100, humidity % 100);
|
||||
leds_off(LEDS_RED);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,14 @@
|
|||
/*---------------------------------------------------------------------------*/
|
||||
#include "dev/i2c.h"
|
||||
#include "dev/adxl346.h"
|
||||
#include "lib/sensors.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \name ADXL346 address and device identifier
|
||||
|
@ -147,7 +155,9 @@
|
|||
#define ADXL346_DATA_FORMAT_RANGE_PM_16g (3)
|
||||
/** @} */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
static uint8_t enabled;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
adxl346_init(void)
|
||||
{
|
||||
uint8_t config[2];
|
||||
|
@ -167,12 +177,7 @@ adxl346_init(void)
|
|||
i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
adxl346_reset(void)
|
||||
{
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint8_t
|
||||
static uint8_t
|
||||
adxl346_is_present(void)
|
||||
{
|
||||
uint8_t is_present;
|
||||
|
@ -183,47 +188,15 @@ adxl346_is_present(void)
|
|||
return is_present == ADXL346_DEVID_VALUE;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
adxl346_read_x(void)
|
||||
{
|
||||
uint8_t acceleration[2];
|
||||
uint16_t x;
|
||||
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAX0_ADDR);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]);
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAX1_ADDR);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]);
|
||||
|
||||
x = (acceleration[0] << 8) | acceleration[1];
|
||||
|
||||
return x;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
adxl346_read_y(void)
|
||||
{
|
||||
uint8_t acceleration[2];
|
||||
uint16_t y;
|
||||
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAY0_ADDR);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]);
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAY1_ADDR);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]);
|
||||
|
||||
y = (acceleration[0] << 8) | acceleration[1];
|
||||
|
||||
return y;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
adxl346_read_z(void)
|
||||
static uint16_t
|
||||
adxl346_read_accel(uint8_t addr1, uint8_t addr2)
|
||||
{
|
||||
uint8_t acceleration[2];
|
||||
uint16_t z;
|
||||
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAZ0_ADDR);
|
||||
i2c_single_send(ADXL346_ADDRESS, addr1);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]);
|
||||
i2c_single_send(ADXL346_ADDRESS, ADXL346_DATAZ1_ADDR);
|
||||
i2c_single_send(ADXL346_ADDRESS, addr2);
|
||||
i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]);
|
||||
|
||||
z = (acceleration[0] << 8) | acceleration[1];
|
||||
|
@ -231,4 +204,56 @@ adxl346_read_z(void)
|
|||
return z;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
status(int type)
|
||||
{
|
||||
switch(type) {
|
||||
case SENSORS_ACTIVE:
|
||||
case SENSORS_READY:
|
||||
return enabled;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
value(int type)
|
||||
{
|
||||
if(!enabled) {
|
||||
PRINTF("ADXL346: sensor not started\n");
|
||||
return ADXL346_ERROR;
|
||||
}
|
||||
|
||||
if(type == ADXL346_READ_X) {
|
||||
return adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR);
|
||||
} else if(type == ADXL346_READ_Y) {
|
||||
return adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
|
||||
} else if(type == ADXL346_READ_Z) {
|
||||
return adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR);
|
||||
} else {
|
||||
PRINTF("ADXL346: invalid value requested\n");
|
||||
return ADXL346_ERROR;
|
||||
}
|
||||
|
||||
return ADXL346_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
configure(int type, int value)
|
||||
{
|
||||
if(type == ADXL346_ACTIVATE) {
|
||||
if(!adxl346_is_present()) {
|
||||
PRINTF("ADXL346: is not present\n");
|
||||
return ADXL346_ERROR;
|
||||
} else {
|
||||
adxl346_init();
|
||||
enabled = 1;
|
||||
return ADXL346_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
return ADXL346_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
SENSORS_SENSOR(adxl346, ADXL346_SENSOR, value, configure, status);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @} */
|
||||
|
|
|
@ -47,35 +47,17 @@
|
|||
#ifndef ADXL346_H_
|
||||
#define ADXL346_H_
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Initialize the ADXL346 sensor
|
||||
*/
|
||||
void adxl346_init(void);
|
||||
#define ADXL346_ERROR (-1)
|
||||
#define ADXL346_SUCCESS (0)
|
||||
#define ADXL346_ACTIVATE (SENSORS_ACTIVE)
|
||||
#define ADXL346_READ_X (2)
|
||||
#define ADXL346_READ_Y (3)
|
||||
#define ADXL346_READ_Z (4)
|
||||
#define ADXL346_NONE (5)
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Reset the ADXL346 sensor
|
||||
*/
|
||||
void adxl346_reset(void);
|
||||
#define ADXL346_SENSOR "ADXL346 Sensor"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Check if the ADXL346 sensor is present
|
||||
*/
|
||||
uint8_t adxl346_is_present(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the x-axis from the ADXL346 sensor
|
||||
*/
|
||||
uint16_t adxl346_read_x(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the y-axis from the ADXL346 sensor
|
||||
*/
|
||||
uint16_t adxl346_read_y(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the z-axis from the ADXL346 sensor
|
||||
*/
|
||||
uint16_t adxl346_read_z(void);
|
||||
extern const struct sensors_sensor adxl346;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#endif /* ADXL346_H_ */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
@ -43,6 +43,14 @@
|
|||
/*---------------------------------------------------------------------------*/
|
||||
#include "dev/i2c.h"
|
||||
#include "dev/max44009.h"
|
||||
#include "lib/sensors.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define DEBUG 1
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \name MAX44009 address and device identifier
|
||||
|
@ -94,9 +102,17 @@
|
|||
MAX44009_CONFIG_AUTO | \
|
||||
MAX44009_CONFIG_CDR_NORMAL | \
|
||||
MAX44009_CONFIG_INTEGRATION_100ms)
|
||||
|
||||
#define MAX44009_USER_CONFIGURATION (MAX44009_CONFIG_DEFAULT | \
|
||||
MAX44009_CONFIG_AUTO | \
|
||||
MAX44009_CONFIG_CDR_NORMAL | \
|
||||
MAX44009_CONFIG_INTEGRATION_800ms)
|
||||
|
||||
/** @} */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
static uint8_t enabled;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
max44009_init(void)
|
||||
{
|
||||
uint8_t max44009_address[5] = { MAX44009_INT_ENABLE_ADDR, MAX44009_CONFIG_ADDR, \
|
||||
|
@ -106,8 +122,8 @@ max44009_init(void)
|
|||
uint8_t max44009_data[2];
|
||||
uint8_t i;
|
||||
|
||||
max44009_value[0] = (MAX44009_INT_STATUS_ON);
|
||||
max44009_value[1] = (MAX44009_DEFAULT_CONFIGURATION);
|
||||
max44009_value[0] = (MAX44009_INT_STATUS_OFF);
|
||||
max44009_value[1] = (MAX44009_USER_CONFIGURATION);
|
||||
max44009_value[2] = (0xFF);
|
||||
max44009_value[3] = (0x00);
|
||||
max44009_value[4] = (0xFF);
|
||||
|
@ -119,7 +135,7 @@ max44009_init(void)
|
|||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
static void
|
||||
max44009_reset(void)
|
||||
{
|
||||
uint8_t max44009_address[5] = { MAX44009_INT_ENABLE_ADDR, MAX44009_CONFIG_ADDR, \
|
||||
|
@ -136,7 +152,7 @@ max44009_reset(void)
|
|||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint8_t
|
||||
static uint8_t
|
||||
max44009_is_present(void)
|
||||
{
|
||||
uint8_t status;
|
||||
|
@ -151,12 +167,12 @@ max44009_is_present(void)
|
|||
return is_present != MAX44009_NOT_FOUND;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
static uint16_t
|
||||
max44009_read_light(void)
|
||||
{
|
||||
uint8_t exponent, mantissa;
|
||||
uint8_t max44009_data[2];
|
||||
uint16_t result;
|
||||
uint32_t result;
|
||||
|
||||
i2c_single_send(MAX44009_ADDRESS, MAX44009_LUX_HIGH_ADDR);
|
||||
i2c_single_receive(MAX44009_ADDRESS, &max44009_data[0]);
|
||||
|
@ -171,20 +187,77 @@ max44009_read_light(void)
|
|||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
float
|
||||
static uint16_t
|
||||
max44009_convert_light(uint16_t lux)
|
||||
{
|
||||
uint8_t exponent, mantissa;
|
||||
float result = 0.045;
|
||||
uint32_t result;
|
||||
|
||||
exponent = (lux >> 8) & 0xFF;
|
||||
exponent = (exponent == 0x0F ? exponent & 0x0E : exponent);
|
||||
|
||||
mantissa = (lux >> 0) & 0xFF;
|
||||
|
||||
result *= 2 ^ exponent * mantissa;
|
||||
result = 45 * (2 ^ exponent * mantissa) / 10;
|
||||
|
||||
return result;
|
||||
return (uint16_t)result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
status(int type)
|
||||
{
|
||||
switch(type) {
|
||||
case SENSORS_ACTIVE:
|
||||
case SENSORS_READY:
|
||||
return enabled;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
value(int type)
|
||||
{
|
||||
uint16_t value;
|
||||
|
||||
if(!enabled) {
|
||||
PRINTF("MAX44009: sensor not started\n");
|
||||
return MAX44009_ERROR;
|
||||
}
|
||||
|
||||
if(type == MAX44009_READ_RAW_LIGHT) {
|
||||
return max44009_read_light();
|
||||
} else if(type == MAX44009_READ_LIGHT) {
|
||||
value = max44009_read_light();
|
||||
return max44009_convert_light(value);
|
||||
} else {
|
||||
PRINTF("MAX44009: invalid value requested\n");
|
||||
return MAX44009_ERROR;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
configure(int type, int value)
|
||||
{
|
||||
if(type == MAX44009_ACTIVATE) {
|
||||
if(!max44009_is_present()) {
|
||||
return MAX44009_ERROR;
|
||||
} else {
|
||||
max44009_init();
|
||||
enabled = 1;
|
||||
return MAX44009_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
if((type == MAX44009_RESET) && enabled) {
|
||||
max44009_reset();
|
||||
return MAX44009_SUCCESS;
|
||||
} else {
|
||||
PRINTF("MAX44009: is not enabled\n");
|
||||
return MAX44009_ERROR;
|
||||
}
|
||||
|
||||
return MAX44009_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
SENSORS_SENSOR(max44009, MAX44009_SENSOR, value, configure, status);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @} */
|
||||
|
|
|
@ -47,30 +47,17 @@
|
|||
#ifndef MAX44009_H_
|
||||
#define MAX44009_H_
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Initialize the MAX44009 sensor
|
||||
*/
|
||||
void max44009_init(void);
|
||||
#define MAX44009_ERROR (-1)
|
||||
#define MAX44009_SUCCESS (0)
|
||||
#define MAX44009_ACTIVATE (SENSORS_ACTIVE)
|
||||
#define MAX44009_READ_RAW_LIGHT (2)
|
||||
#define MAX44009_READ_LIGHT (3)
|
||||
#define MAX44009_RESET (4)
|
||||
#define MAX44009_NONE (5)
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Reset the MAX44009 sensor
|
||||
*/
|
||||
void max44009_reset(void);
|
||||
#define MAX44009_SENSOR "MAX44009 Sensor"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Check if the MAX44009 sensor is present
|
||||
*/
|
||||
uint8_t max44009_is_present(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the light from the MAX44009 sensor
|
||||
*/
|
||||
uint16_t max44009_read_light(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Convert the light from the MAX44009 sensor
|
||||
*/
|
||||
float max44009_convert_light(uint16_t light);
|
||||
extern const struct sensors_sensor max44009;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#endif /* MAX44009_H_ */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
@ -41,8 +41,16 @@
|
|||
* Pere Tuset <peretuset@openmote.com>
|
||||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "i2c.h"
|
||||
#include "sht21.h"
|
||||
#include "dev/i2c.h"
|
||||
#include "dev/sht21.h"
|
||||
#include "lib/sensors.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \name SHT21 address
|
||||
|
@ -87,13 +95,15 @@
|
|||
SHT21_BATTERY_ABOVE_2V25 | \
|
||||
SHT21_OTP_RELOAD_DISABLE)
|
||||
|
||||
#define SHT21_USER_CONFIG (SHT21_RESOLUTION_8b_12b | \
|
||||
#define SHT21_USER_CONFIG (SHT21_RESOLUTION_12b_14b | \
|
||||
SHT21_ONCHIP_HEATER_DISABLE | \
|
||||
SHT21_BATTERY_ABOVE_2V25 | \
|
||||
SHT21_OTP_RELOAD_DISABLE)
|
||||
/** @} */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
static uint8_t enabled;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
sht21_init(void)
|
||||
{
|
||||
uint8_t config[2];
|
||||
|
@ -116,14 +126,14 @@ sht21_init(void)
|
|||
i2c_burst_send(SHT21_ADDRESS, config, sizeof(config));
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
static void
|
||||
sht21_reset(void)
|
||||
{
|
||||
/* Send a soft-reset command according to the datasheet (pag. 9, fig. 17) */
|
||||
i2c_single_send(SHT21_ADDRESS, SHT21_RESET_CMD);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint8_t
|
||||
static uint8_t
|
||||
sht21_is_present(void)
|
||||
{
|
||||
uint8_t status;
|
||||
|
@ -142,7 +152,7 @@ sht21_is_present(void)
|
|||
return (is_present == SHT21_USER_CONFIG) || (is_present == SHT21_DEFAULT_CONFIG);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
static uint32_t
|
||||
sht21_read_temperature(void)
|
||||
{
|
||||
uint8_t sht21_temperature[2];
|
||||
|
@ -152,23 +162,24 @@ sht21_read_temperature(void)
|
|||
i2c_single_send(SHT21_ADDRESS, SHT21_TEMPERATURE_HM_CMD);
|
||||
i2c_burst_receive(SHT21_ADDRESS, sht21_temperature, sizeof(sht21_temperature));
|
||||
|
||||
temperature = (sht21_temperature[0] << 8) | (sht21_temperature[1] & SHT21_STATUS_MASK);
|
||||
temperature = (sht21_temperature[0] << 8) | ((sht21_temperature[1] & SHT21_STATUS_MASK));
|
||||
|
||||
return temperature;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
float
|
||||
sht21_convert_temperature(uint16_t temperature)
|
||||
static int16_t
|
||||
sht21_convert_temperature(uint32_t temperature)
|
||||
{
|
||||
float result;
|
||||
int16_t result;
|
||||
|
||||
result = -46.85;
|
||||
result += 175.72 * (float)temperature / 65536.0;
|
||||
temperature *= 17572;
|
||||
temperature = temperature >> 16;
|
||||
result = (int16_t)temperature - 4685;
|
||||
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
static uint32_t
|
||||
sht21_read_humidity(void)
|
||||
{
|
||||
uint8_t sht21_humidity[2];
|
||||
|
@ -178,20 +189,86 @@ sht21_read_humidity(void)
|
|||
i2c_single_send(SHT21_ADDRESS, SHT21_HUMIDITY_HM_CMD);
|
||||
i2c_burst_receive(SHT21_ADDRESS, sht21_humidity, sizeof(sht21_humidity));
|
||||
|
||||
humidity = (sht21_humidity[0] << 8) | (sht21_humidity[1] & SHT21_STATUS_MASK);
|
||||
humidity = (sht21_humidity[0] << 8) | ((sht21_humidity[1] & SHT21_STATUS_MASK));
|
||||
|
||||
return humidity;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
float
|
||||
sht21_convert_humidity(uint16_t humidity)
|
||||
static int16_t
|
||||
sht21_convert_humidity(uint32_t humidity)
|
||||
{
|
||||
float result;
|
||||
int16_t result;
|
||||
|
||||
result = -6.0;
|
||||
result += 125.0 * (float)humidity / 65536.0;
|
||||
humidity *= 12500;
|
||||
humidity = humidity >> 16;
|
||||
result = (int16_t)humidity - 600;
|
||||
result = (result > 10000) ? 10000 : result;
|
||||
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
status(int type)
|
||||
{
|
||||
switch(type) {
|
||||
case SENSORS_ACTIVE:
|
||||
case SENSORS_READY:
|
||||
return enabled;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
value(int type)
|
||||
{
|
||||
uint32_t value;
|
||||
|
||||
if(!enabled) {
|
||||
PRINTF("SHT21: sensor not started\n");
|
||||
return SHT21_ERROR;
|
||||
}
|
||||
|
||||
if(type == SHT21_READ_RAW_TEMP) {
|
||||
return sht21_read_temperature();
|
||||
} else if(type == SHT21_READ_RAW_RHUM) {
|
||||
return sht21_read_humidity();
|
||||
} else if(type == SHT21_READ_TEMP) {
|
||||
value = sht21_read_temperature();
|
||||
return sht21_convert_temperature(value);
|
||||
} else if(type == SHT21_READ_RHUM) {
|
||||
value = sht21_read_humidity();
|
||||
return sht21_convert_humidity(value);
|
||||
} else {
|
||||
PRINTF("SHT21: invalid value requested\n");
|
||||
return SHT21_ERROR;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
configure(int type, int value)
|
||||
{
|
||||
if(type == SHT21_ACTIVATE) {
|
||||
if(!sht21_is_present()) {
|
||||
PRINTF("SHT21: is not present\n");
|
||||
return SHT21_ERROR;
|
||||
} else {
|
||||
sht21_init();
|
||||
enabled = 1;
|
||||
return SHT21_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
if(type == SHT21_RESET && enabled) {
|
||||
sht21_reset();
|
||||
return SHT21_SUCCESS;
|
||||
} else {
|
||||
PRINTF("SHT21: is not enabled\n");
|
||||
return SHT21_ERROR;
|
||||
}
|
||||
|
||||
return SHT21_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
SENSORS_SENSOR(sht21, SHT21_SENSOR, value, configure, status);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @} */
|
||||
|
|
|
@ -47,40 +47,19 @@
|
|||
#ifndef SHT21_H_
|
||||
#define SHT21_H_
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Initialize the SHT21 sensor
|
||||
*/
|
||||
void sht21_init(void);
|
||||
#define SHT21_ERROR (-1)
|
||||
#define SHT21_SUCCESS (0)
|
||||
#define SHT21_ACTIVATE (SENSORS_ACTIVE)
|
||||
#define SHT21_READ_RAW_TEMP (2)
|
||||
#define SHT21_READ_RAW_RHUM (3)
|
||||
#define SHT21_READ_TEMP (4)
|
||||
#define SHT21_READ_RHUM (5)
|
||||
#define SHT21_RESET (6)
|
||||
#define SHT21_NONE (7)
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Reset the SHT21 sensor
|
||||
*/
|
||||
void sht21_reset(void);
|
||||
#define SHT21_SENSOR "SHT21 Sensor"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Check if the SHT21 sensor is present
|
||||
*/
|
||||
uint8_t sht21_is_present(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the temperature from the SHT21 sensor
|
||||
*/
|
||||
uint16_t sht21_read_temperature(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Convert the temperature from the SHT21 sensor
|
||||
*/
|
||||
float sht21_convert_temperature(uint16_t temperature);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Read the relative humidity from the SHT21 sensor
|
||||
*/
|
||||
uint16_t sht21_read_humidity(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Convert the relative humidity from the SHT21 sensor
|
||||
*/
|
||||
float sht21_convert_humidity(uint16_t humidity);
|
||||
extern const struct sensors_sensor sht21;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#endif /* SHT21_H_ */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Reference in a new issue