summaryrefslogtreecommitdiff
path: root/src/driver/scd4x.cc
blob: a219bd5fc76ba4db0fe9be3c5021c11d1239d0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright 2021 Birte Kristina Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "driver/scd4x.h"
#include "driver/gpio.h"
#if defined(CONFIG_meta_driver_hardware_i2c)
#include "driver/i2c.h"
#elif defined(CONFIG_driver_softi2c)
#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::startLowPower()
{
	txbuf[0] = 0x21;
	txbuf[1] = 0xac;
	i2c.xmit(address, 2, txbuf, 0, rxbuf);
}

bool SCD4x::read()
{
	txbuf[0] = 0xec;
	txbuf[1] = 0x05;

	if (i2c.xmit(address, 2, txbuf, 9, rxbuf)) {
		return false;
	}
	co2 = (rxbuf[0] << 8) + rxbuf[1];
	rawTemperature = ((rxbuf[3] << 8) + rxbuf[4]);
	rawHumidity = (rxbuf[6] << 8) + rxbuf[7];
	return true;
}

SCD4x scd4x;