From bdb1590839e5962741140ce2f4db722219f9d6f6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 26 Feb 2021 19:16:55 +0100 Subject: Add SCD4x driver and datalog integration --- Makefile | 18 ++++++++++++++++++ include/driver/scd4x.h | 34 ++++++++++++++++++++++++++++++++++ src/app/datalogger/main.cc | 18 ++++++++++++++++++ src/driver/scd4x.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 include/driver/scd4x.h create mode 100644 src/driver/scd4x.cc diff --git a/Makefile b/Makefile index d45e8be..4f518ae 100644 --- a/Makefile +++ b/Makefile @@ -108,6 +108,14 @@ ifneq ($(findstring sharp96,${drivers}), ) CONFIG_driver_sharp96 = y endif +ifneq ($(findstring tsl2591,${drivers}), ) + CONFIG_driver_tsl2591 = y +endif + +ifneq ($(findstring scd4x,${drivers}), ) + CONFIG_driver_scd4x = y +endif + ifneq ($(findstring resistive_load,${drivers}), ) CONFIG_driver_resistive_load = y endif @@ -192,6 +200,16 @@ ifdef CONFIG_driver_mmsubstate COMMON_FLAGS += -DDRIVER_MMSUBSTATE endif +ifdef CONFIG_driver_tsl2591 + CXX_TARGETS += src/driver/tsl2591.cc + COMMON_FLAGS += -DDRIVER_TSL2591 +endif + +ifdef CONFIG_driver_scd4x + CXX_TARGETS += src/driver/scd4x.cc + COMMON_FLAGS += -DDRIVER_SCD4X +endif + ifdef CONFIG_driver_nrf24l01 CXX_TARGETS += src/driver/nrf24l01.cc ifeq (${arch_dir}, msp430fr5994lp) diff --git a/include/driver/scd4x.h b/include/driver/scd4x.h new file mode 100644 index 0000000..19ba20d --- /dev/null +++ b/include/driver/scd4x.h @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Daniel Friesel + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#ifndef SCD4x_H +#define SCD4x_H + +class SCD4x { + private: + SCD4x(const SCD4x ©); + unsigned char const address = 0x62; + unsigned char txbuf[2]; + unsigned char rxbuf[9]; + + public: + SCD4x() {} + + unsigned short co2; + unsigned short rawTemperature; + unsigned short rawHumidity; + + void start(); + void stop(); + + void startLowPower(); + void stopLowPower(); + + void read(); +}; + +extern SCD4x scd4x; + +#endif diff --git a/src/app/datalogger/main.cc b/src/app/datalogger/main.cc index 8740035..46ff6b0 100644 --- a/src/app/datalogger/main.cc +++ b/src/app/datalogger/main.cc @@ -45,6 +45,9 @@ #ifdef DRIVER_TSL2591 #include "driver/tsl2591.h" #endif +#ifdef DRIVER_SCD4X +#include "driver/scd4x.h" +#endif void loop(void) { @@ -136,6 +139,17 @@ void loop(void) kout << dec << "TSL2591 CH0: " << tsl2591.ch0 << " / CH1: " << tsl2591.ch1; kout << hex << " (status: 0x" << tsl2591.getStatus() << ")" << endl; #endif + +#ifdef DRIVER_SCD4X + scd4x.read(); + kout << dec << "CO₂: " << scd4x.co2 << " ppm" << endl; + kout << "Temperature: "; + kout.printf_float(((175.0 * scd4x.rawTemperature) / 65536) - 45); + kout << " °c" << endl; + kout << "Humidity: "; + kout.printf_float((100.0 * scd4x.rawHumidity) / 65536); + kout << " %" << endl; +#endif } int main(void) @@ -224,6 +238,10 @@ int main(void) tsl2591.init(); #endif +#ifdef DRIVER_SCD4X + scd4x.start(); +#endif + arch.idle_loop(); return 0; diff --git a/src/driver/scd4x.cc b/src/driver/scd4x.cc new file mode 100644 index 0000000..c0901e8 --- /dev/null +++ b/src/driver/scd4x.cc @@ -0,0 +1,40 @@ +/* + * Copyright 2021 Daniel Friesel + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#include "driver/scd4x.h" +#include "driver/gpio.h" +#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C) +#include "driver/i2c.h" +#else +#include "driver/soft_i2c.h" +#endif + +void SCD4x::start() +{ + txbuf[0] = 0x21; + txbuf[1] = 0xb1; + i2c.xmit(address, 2, txbuf, 0, rxbuf); +} + +void SCD4x::stop() +{ + txbuf[0] = 0x3f; + txbuf[1] = 0x86; + i2c.xmit(address, 2, txbuf, 0, rxbuf); +} + +void SCD4x::read() +{ + txbuf[0] = 0xec; + txbuf[1] = 0x05; + + if (i2c.xmit(address, 1, txbuf, 9, rxbuf) == 0) { + co2 = (rxbuf[0] << 8) + rxbuf[1]; + rawTemperature = ((rxbuf[3] << 8) + rxbuf[4]); + rawHumidity = (rxbuf[6] << 8) + rxbuf[7]; + } +} + +SCD4x scd4x; -- cgit v1.2.3