This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
65 lines (49 loc) · 1.57 KB
/
main.cpp
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! \file
//! \brief HTU21D temperature and humidity sensor usage example.
//! \details Example shows how to use HTU21D sensor to get temperature
//! and humidity data.
// For ecl::cout and ecl::endl
#include <ecl/iostream.hpp>
// For utilities
#include <ecl/thread/utils.hpp>
// For auto-generated files
#include <ecl/core_generated.hpp>
extern "C"
void board_init()
{
gpio_init_generated();
}
struct measurement {
explicit measurement(int x) : x(x) { }
int x;
};
template<typename IO_device>
ecl::ostream<IO_device> &operator<<(ecl::ostream<IO_device> &o, measurement m)
{
return o << (m.x / 1000) << "."
<< (m.x % 1000 / 100)
<< (m.x % 100 / 10)
<< (m.x % 10);
}
int main()
{
ecl::cout << "Starting HTU21D sensor..." << ecl::endl;
// Initialize HTU21D sensor
ecl::htu21d_sensor::init();
ecl::htu21d_sensor::soft_reset();
ecl::cout << "Reset done" << ecl::endl;
while (true) {
int temperature = 0;
ecl::htu21d_sensor::get_temperature(temperature);
ecl::cout << "Temperature: " << measurement(temperature) << " Celsius ";
int humidity = 0;
ecl::htu21d_sensor::get_humidity(humidity);
ecl::cout << "Humidity: " << measurement(humidity) << "%" << ecl::endl;
// Sleep for a second.
ecl::this_thread::sleep_for(1000);
}
return 0;
}