[Mod] AHT10驱动 添加AHT10源码
1. 参考 Arduino 代码
This commit is contained in:
@ -4,30 +4,60 @@
|
||||
#include <ioctl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "aht10_util.h"
|
||||
|
||||
int fd_i2c1 = -1;
|
||||
|
||||
extern bool aht1x_getEvent(sensors_event_t *humidity, sensors_event_t *temp);
|
||||
|
||||
int main() {
|
||||
int ret = 0, fd_i2c_s = 0;
|
||||
i2c_driver_info_t info = {0};
|
||||
int ret = 0, fd_i2c_s = 0;
|
||||
i2c_driver_info_t info = {0};
|
||||
|
||||
fd_i2c_s = open ("/dev/i2c1", O_RDWR);
|
||||
if (fd_i2c_s < 0) {
|
||||
printf("ErrNo(%d) %s, failed to open I2C device\n", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
fd_i2c_s = open ("/dev/i2c1", O_RDWR);
|
||||
if (fd_i2c_s < 0) {
|
||||
printf("ErrNo(%d) %s, failed to open I2C device\n", errno, strerror(errno));
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ioctl(fd_i2c_s, DCMD_I2C_DRIVER_INFO, &info);
|
||||
if (ret < 0) {
|
||||
printf("ErrNo(%d) %s, failed to read from I2C device\n", errno, strerror(errno));
|
||||
close(fd_i2c_s);
|
||||
return -1;
|
||||
}
|
||||
ret = ioctl(fd_i2c_s, DCMD_I2C_DRIVER_INFO, &info);
|
||||
if (ret < 0) {
|
||||
printf("ErrNo(%d) %s, failed to read from I2C device\n", errno, strerror(errno));
|
||||
close(fd_i2c_s);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("I2C Driver Info:\n");
|
||||
printf(" Speed : %#X\n", info.speed_mode); /* supported speeds: I2C_SPEED_* */
|
||||
printf(" Mode-Addr: %#X\n", info.addr_mode); /* supported address fmts: I2C_ADDRFMT_* */
|
||||
printf(" Verbosity: %#X\n", info.verbosity); /* Driver verbosity level */
|
||||
printf("I2C Driver Info:\n");
|
||||
printf(" Speed : %#X\n", info.speed_mode); /* supported speeds: I2C_SPEED_* */
|
||||
printf(" Mode-Addr: %#X\n", info.addr_mode); /* supported address fmts: I2C_ADDRFMT_* */
|
||||
printf(" Verbosity: %#X\n", info.verbosity); /* Driver verbosity level */
|
||||
|
||||
close(fd_i2c_s);
|
||||
|
||||
return 0;
|
||||
Adafruit_AHTX0 aht;
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println("Adafruit AHT10/AHT20 demo!");
|
||||
|
||||
if (! aht.begin()) {
|
||||
Serial.println("Could not find AHT? Check wiring");
|
||||
while (1) delay(10);
|
||||
}
|
||||
|
||||
Serial.println("AHT10 or AHT20 found");
|
||||
|
||||
while(1) {
|
||||
sensors_event_t humidity, temp;
|
||||
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
|
||||
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
|
||||
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
||||
close(fd_i2c_s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
130
src/drivers/i2c/aht10/aht10_util.c
Normal file
130
src/drivers/i2c/aht10/aht10_util.c
Normal file
@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <hw/i2c.h>
|
||||
#include <ioctl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "aht10_util.h"
|
||||
|
||||
float _humidity = 0, _temperature = 0;
|
||||
|
||||
extern int fd_i2c1;
|
||||
|
||||
/*!
|
||||
* @brief Sets up the hardware and initializes I2C
|
||||
* @param wire
|
||||
* The Wire object to be used for I2C connections.
|
||||
* @param sensor_id
|
||||
* The unique ID to differentiate the sensors from others
|
||||
* @param i2c_address
|
||||
* The I2C address used to communicate with the sensor
|
||||
* @return True if initialization was successful, otherwise false.
|
||||
*/
|
||||
bool aht1x_begin(TwoWire *wire, int32_t sensor_id, uint8_t i2c_address)
|
||||
{
|
||||
delay(20); // 20 ms to power up
|
||||
|
||||
if (i2c_dev) {
|
||||
delete i2c_dev; // remove old interface
|
||||
}
|
||||
|
||||
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
|
||||
|
||||
if (!i2c_dev->begin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t cmd[3];
|
||||
|
||||
cmd[0] = AHTX0_CMD_SOFTRESET;
|
||||
if (!i2c_dev->write(cmd, 1)) {
|
||||
return false;
|
||||
}
|
||||
delay(20);
|
||||
|
||||
while (getStatus() & AHTX0_STATUS_BUSY) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
cmd[0] = AHTX0_CMD_CALIBRATE;
|
||||
cmd[1] = 0x08;
|
||||
cmd[2] = 0x00;
|
||||
i2c_dev->write(cmd, 3); // may not 'succeed' on newer AHT20s
|
||||
|
||||
while (getStatus() & AHTX0_STATUS_BUSY) {
|
||||
delay(10);
|
||||
}
|
||||
if (!(getStatus() & AHTX0_STATUS_CALIBRATED)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
delete humidity_sensor;
|
||||
|
||||
|
||||
temp_sensor = new Adafruit_AHTX0_Temp(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the status (first byte) from AHT10/AHT20
|
||||
*
|
||||
* @returns 8 bits of status data, or 0xFF if failed
|
||||
*/
|
||||
uint8_t aht1x_getStatus(void)
|
||||
{
|
||||
uint8_t ret;
|
||||
if (!i2c_dev->read(&ret, 1)) {
|
||||
return 0xFF;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Gets the humidity sensor and temperature values as sensor events
|
||||
@param humidity Sensor event object that will be populated with humidity
|
||||
data
|
||||
@param temp Sensor event object that will be populated with temp data
|
||||
@returns true if the event data was read successfully
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool aht1x_getEvent(sensors_event_t *humidity, sensors_event_t *temp)
|
||||
{
|
||||
uint32_t t = millis();
|
||||
|
||||
// read the data and store it!
|
||||
uint8_t cmd[3] = {AHTX0_CMD_TRIGGER, 0x33, 0};
|
||||
if (!i2c_dev->write(cmd, 3)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (getStatus() & AHTX0_STATUS_BUSY) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
uint8_t data[6];
|
||||
if (!i2c_dev->read(data, 6)) {
|
||||
return false;
|
||||
}
|
||||
uint32_t h = data[1];
|
||||
h <<= 8;
|
||||
h |= data[2];
|
||||
h <<= 4;
|
||||
h |= data[3] >> 4;
|
||||
_humidity = ((float)h * 100) / 0x100000;
|
||||
|
||||
uint32_t tdata = data[3] & 0x0F;
|
||||
tdata <<= 8;
|
||||
tdata |= data[4];
|
||||
tdata <<= 8;
|
||||
tdata |= data[5];
|
||||
_temperature = ((float)tdata * 200 / 0x100000) - 50;
|
||||
|
||||
// use helpers to fill in the events
|
||||
if (temp)
|
||||
fillTempEvent(temp, t);
|
||||
if (humidity)
|
||||
fillHumidityEvent(humidity, t);
|
||||
return true;
|
||||
}
|
||||
14
src/drivers/i2c/aht10/aht10_util.h
Normal file
14
src/drivers/i2c/aht10/aht10_util.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef _ADAFRUIT_AHTX0_H
|
||||
#define _ADAFRUIT_AHTX0_H
|
||||
|
||||
#define AHTX0_I2CADDR_DEFAULT 0x38 ///< AHT default i2c address
|
||||
#define AHTX0_I2CADDR_ALTERNATE 0x39 ///< AHT alternate i2c address
|
||||
#define AHTX0_CMD_CALIBRATE 0xE1 ///< Calibration command
|
||||
#define AHTX0_CMD_TRIGGER 0xAC ///< Trigger reading command
|
||||
#define AHTX0_CMD_SOFTRESET 0xBA ///< Soft reset command
|
||||
#define AHTX0_STATUS_BUSY 0x80 ///< Status bit for busy
|
||||
#define AHTX0_STATUS_CALIBRATED 0x08 ///< Status bit for calibrated
|
||||
|
||||
bool aht1x_getEvent(sensors_event_t *humidity, sensors_event_t *temp);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user