summaryrefslogtreecommitdiff
path: root/src/driver/am2320.cc
blob: 532fdb6698086ad9dd19aef760c1812c4a504431 (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
50
51
52
53
54
55
56
57
58
59
60
61
#include "driver/am2320.h"
#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C)
#include "driver/i2c.h"
#else
#include "driver/soft_i2c.h"
#endif
#include "arch.h"

void AM2320::read()
{
	txbuf[0] = 0;
	i2c.xmit(address, 1, txbuf, 0, rxbuf);
	arch.delay_ms(1);
	txbuf[0] = 3;
	txbuf[1] = 0;
	txbuf[2] = 4;
	rxbuf[3] = rxbuf[4] = rxbuf[5] = rxbuf[6] = 0;
	i2c.xmit(address, 3, txbuf, 0, rxbuf);
	arch.delay_ms(3);
	i2c.xmit(address, 0, txbuf, 8, rxbuf);
}

unsigned char AM2320::getStatus()
{
	if (rxbuf[0] != 3) {
		return 1;
	}
	if (rxbuf[1] != 4) {
		return 2;
	}
	unsigned short checksum = 0xffff;
	for (unsigned char i = 0; i < 6; i++) {
		checksum ^= rxbuf[i] & 0x00ff;
		for (unsigned char j = 0; j < 8; j++) {
			if (checksum & 0x0001) {
				checksum = (checksum >> 1) ^ 0xa001;
			} else {
				checksum >>= 1;
			}
		}
	}
	if ((rxbuf[6] != (checksum & 0x00ff)) || (rxbuf[7] != (checksum >> 8))) {
		return 3;
	}
	return 0;
}

float AM2320::getTemp()
{
	if (rxbuf[5] & 0x80) {
		return (-256 * (rxbuf[4] & 0x7f) + rxbuf[5]) / 10.;
	}
	return (256 * rxbuf[4] + rxbuf[5]) / 10.;
}

float AM2320::getHumidity()
{
	return (rxbuf[2] * 256 + rxbuf[3]) / 10.;
}

AM2320 am2320(0x5c);