|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdbool.h> |
| 4 | + |
| 5 | +#include <main.h> |
| 6 | + |
| 7 | +#include <interfaces/i2c-bus.h> |
| 8 | +#include <interfaces/waveform_source.h> |
| 9 | + |
| 10 | +#include "shtc3.h" |
| 11 | + |
| 12 | +//#define DEBUG |
| 13 | +#define MODULE_NAME "shtc3" |
| 14 | + |
| 15 | + |
| 16 | +static shtc3_ret_t shtc3_cmd(Shtc3 *self, uint16_t cmd, uint8_t *ret, size_t len) { |
| 17 | + uint8_t txbuf[2] = {cmd >> 8, cmd & 0xff}; |
| 18 | + |
| 19 | + if (self->i2c->vmt->transfer(self->i2c, SHTC3_ADDR, txbuf, sizeof(txbuf), ret, len) != I2C_BUS_RET_OK) { |
| 20 | + return SHTC3_RET_FAILED; |
| 21 | + } |
| 22 | + |
| 23 | + return SHTC3_RET_OK; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +/********************************************************************************************************************** |
| 28 | + * Temperature Sensor API |
| 29 | + **********************************************************************************************************************/ |
| 30 | + |
| 31 | +static sensor_ret_t temp_sensor_value_f(Sensor *sensor, float *value) { |
| 32 | + Shtc3 *self = sensor->parent; |
| 33 | + |
| 34 | + uint8_t d[3] = {0}; |
| 35 | + if (shtc3_cmd(self, 0x7ca2, d, sizeof(d)) != SHTC3_RET_OK) { |
| 36 | + return SENSOR_RET_FAILED; |
| 37 | + } |
| 38 | + |
| 39 | + float temp = -45 + 175 * ((d[0] << 8) | d[1]) / 65536; |
| 40 | + //float rh = 100 * ((d[3] << 8) | d[4]) / 65536; |
| 41 | + |
| 42 | + if (value != NULL) { |
| 43 | + *value = temp; |
| 44 | + return SENSOR_RET_OK; |
| 45 | + } |
| 46 | + |
| 47 | + return SENSOR_RET_FAILED; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +static const struct sensor_vmt temp_sensor_vmt = { |
| 52 | + .value_f = temp_sensor_value_f, |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +const struct sensor_info temp_sensor_info = { |
| 57 | + .description = "PCB temperature", |
| 58 | + .unit = "°C", |
| 59 | +}; |
| 60 | + |
| 61 | + |
| 62 | +/********************************************************************************************************************** |
| 63 | + * Relative humidity Sensor API |
| 64 | + **********************************************************************************************************************/ |
| 65 | + |
| 66 | +static sensor_ret_t rh_sensor_value_f(Sensor *sensor, float *value) { |
| 67 | + Shtc3 *self = sensor->parent; |
| 68 | + |
| 69 | + uint8_t d[3] = {0}; |
| 70 | + if (shtc3_cmd(self, 0x5c24, d, sizeof(d)) != SHTC3_RET_OK) { |
| 71 | + return SENSOR_RET_FAILED; |
| 72 | + } |
| 73 | + |
| 74 | + float rh = 100.0f * ((d[0] << 8) | d[1]) / 65536; |
| 75 | + |
| 76 | + if (value != NULL) { |
| 77 | + *value = rh; |
| 78 | + return SENSOR_RET_OK; |
| 79 | + } |
| 80 | + |
| 81 | + return SENSOR_RET_FAILED; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +static const struct sensor_vmt rh_sensor_vmt = { |
| 86 | + .value_f = rh_sensor_value_f, |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +const struct sensor_info rh_sensor_info = { |
| 91 | + .description = "PCB humidity", |
| 92 | + .unit = "%Rh", |
| 93 | +}; |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +shtc3_ret_t shtc3_init(Shtc3 *self, I2cBus *i2c) { |
| 98 | + memset(self, 0, sizeof(Shtc3)); |
| 99 | + self->i2c = i2c; |
| 100 | + |
| 101 | + if (shtc3_cmd(self, 0xefc8, self->id, sizeof(self->id)) != SHTC3_RET_OK) { |
| 102 | + u_log(system_log, LOG_TYPE_ERROR, U_LOG_MODULE_PREFIX("SHTC3 sensor not found")); |
| 103 | + return SHTC3_RET_FAILED; |
| 104 | + } |
| 105 | + |
| 106 | + if (!(((self->id[1] & 0x3f) == 0x07) && (self->id[0] & 0x08))) { |
| 107 | + u_log(system_log, LOG_TYPE_ERROR, U_LOG_MODULE_PREFIX("SHTC3 sensor probe failed (wrong ID)")); |
| 108 | + return SHTC3_RET_FAILED; |
| 109 | + } |
| 110 | + |
| 111 | + self->temp.parent = self; |
| 112 | + self->temp.vmt = &temp_sensor_vmt; |
| 113 | + self->temp.info = &temp_sensor_info; |
| 114 | + |
| 115 | + self->rh.parent = self; |
| 116 | + self->rh.vmt = &rh_sensor_vmt; |
| 117 | + self->rh.info = &rh_sensor_info; |
| 118 | + |
| 119 | + u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("initialized, id = 0x%02x, 0x%02x, 0x%02x"), self->id[0], self->id[1], self->id[2]); |
| 120 | + |
| 121 | + return SHTC3_RET_OK; |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +shtc3_ret_t shtc3_free(Shtc3 *self) { |
| 126 | + (void)self; |
| 127 | + |
| 128 | + return SHTC3_RET_OK; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments