blob: ac742d8815ba298b9d2f14375566e46861194b70 (
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright 2021 Daniel Friesel
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "arch.h"
#include "driver/gpio.h"
#include "driver/stdout.h"
#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(CONFIG_driver_softi2c)
#include "driver/i2c.h"
#else
#include "driver/soft_i2c.h"
#endif
#include "driver/bme680.h"
#include "driver/bme680_util.h"
#include "driver/max44009.h"
#ifdef MULTIPASS_ARCH_arduino_nano
#define POWER_PIN GPIO::pc3
#endif
struct bme680_field_data data;
float lux;
int8_t bme680_status;
static void bme680_init(void)
{
bme680_status = bme680.init();
kout << "# BME680 init returned " << bme680_status << endl;
bme680.power_mode = BME680_FORCED_MODE;
bme680.tph_sett.os_hum = BME680_OS_2X;
bme680.tph_sett.os_pres = BME680_OS_16X;
bme680.tph_sett.os_temp = BME680_OS_2X;
bme680.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
bme680.gas_sett.heatr_dur = 100;
bme680.gas_sett.heatr_temp = 300;
bme680.setSensorSettings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL);
}
void loop(void)
{
static unsigned char i = 0;
if (lux >= 0 && bme680_status == 0) {
gpio.led_off(0);
} else {
gpio.led_on(0);
}
#ifdef POWER_PIN
if (lux < 0 || bme680_status != 0) {
if (i == 17) {
kout << "# Cycling power to I2C clients" << endl;
gpio.write(POWER_PIN, 0);
} else if (i == 18) {
gpio.write(POWER_PIN, 1);
} else if (i == 19) {
bme680_init();
}
}
#endif
#ifdef MULTIPASS_ARCH_arduino_nano
if ((i == 1) && (ADCSRA & _BV(ADIF))) {
uint8_t adcr_l = ADCL;
uint8_t adcr_h = ADCH;
uint16_t adcr = adcr_l + (adcr_h << 8);
uint16_t vcc = 1100L * 1023 / adcr;
TIFR1 |= _BV(TOV1);
ADCSRA |= _BV(ADIF);
kout << "VCC: " << vcc << endl;
}
#endif
if (i == 0) {
lux = max44009.getLux();
if (lux >= 0) {
kout << "MAX44009: ";
kout.printf_float(max44009.getLux());
kout << " lx" << endl;
} else {
kout << "# MAX44009 error" << endl;
}
}
if (i == 1 && bme680_status == 0) {
bme680_status = bme680.setSensorMode();
}
else if (i == 2) {
if (bme680_status == 0) {
bme680_status = bme680.getSensorData(&data);
}
if (bme680_status == 0) {
bme680.amb_temp = data.temperature;
kout << "BME680 temperature: " << data.temperature << " degC" << endl;
kout << "BME680 humidity: " << data.humidity << " %" << endl;
kout << "BME680 pressure: " << data.pressure / 100 << " hPa" << endl;
kout << "BME680 gas resistance: " << data.gas_resistance << endl;
} else {
kout << "# BME680 error " << bme680_status << endl;
}
}
i = (i + 1) % 20;
}
int main(void)
{
arch.setup();
gpio.setup();
kout.setup();
#ifdef POWER_PIN
gpio.output(POWER_PIN);
gpio.write(POWER_PIN, 1);
#endif
#ifdef MULTIPASS_ARCH_arduino_nano
kout << "# Reset reason: " << MCUSR << endl;
MCUSR = 0;
/* watchdog reset after ~4 seconds */
asm("wdr");
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDE) | _BV(WDP3);
// One ADC conversion per four seconds
TCCR0A = 0;
TCCR0B = _BV(CS12) | _BV(CS10);
// Measure internal 1.1V bandgap using VCC as reference on each Timer 0 overflow
ADMUX = _BV(REFS0) | 0x0e;
ADCSRB = _BV(ADTS2);
ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);
#endif
if (i2c.setup() != 0) {
kout << "# I2C setup failed" << endl;
return 1;
}
kout << "# I2C setup OK" << endl;
bme680.intf = BME680_I2C_INTF;
bme680.read = bme680_i2c_read;
bme680.write = bme680_i2c_write;
bme680.delay_ms = bme680_delay_ms;
bme680.amb_temp = 25;
bme680_init();
arch.idle_loop();
return 0;
}
|