-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (95 loc) · 2.73 KB
/
main.cpp
File metadata and controls
112 lines (95 loc) · 2.73 KB
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
/*
* Copyright (c) 2014-2015, Sascha Schade
* Copyright (c) 2014-2017, Niklas Hauser
*
* This file is part of the modm project.
*
* 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/.
*/
// ----------------------------------------------------------------------------
#include <modm/board.hpp>
#include <modm/processing.hpp>
#include <modm/driver/position/vl6180.hpp>
/**
* Example to demonstrate a MODM driver for distance sensor VL6180
*
* This example uses I2cMaster2 of STM32F407
*
* SDA PB11
* SCL PB10
*
* GND and +3V are connected to the sensor.
*/
typedef I2cMaster2 MyI2cMaster;
// typedef BitBangI2cMaster<GpioB10, GpioB11> MyI2cMaster;
modm::vl6180::Data data;
modm::Vl6180<MyI2cMaster> distance(data);
modm::Fiber fiber_sensor([]
{
MODM_LOG_DEBUG << "Ping the device from ThreadOne" << modm::endl;
while (not distance.ping()) modm::this_fiber::sleep_for(100ms);
MODM_LOG_DEBUG << "Device responded" << modm::endl;
distance.initialize();
MODM_LOG_DEBUG << "Device initialized" << modm::endl;
distance.setIntegrationTime(10);
while (true)
{
auto stamp = modm::Clock::now();
if (distance.readDistance())
{
const auto error = distance.getRangeError();
if (error == distance.RangeErrorCode::NoError)
{
const uint8_t mm = distance.getData().getDistance();
MODM_LOG_DEBUG << "mm: " << mm;
Board::LedGreen::set(mm > 160);
Board::LedBlue::set(mm > 110);
Board::LedRed::set(mm > 25);
}
else {
MODM_LOG_DEBUG << "Error: " << (uint8_t(error) >> 4);
Board::LedGreen::set();
Board::LedBlue::set();
Board::LedRed::set();
}
}
MODM_LOG_DEBUG << "\tt=" << (modm::Clock::now() - stamp);
stamp = modm::Clock::now();
if (distance.readAmbientLight())
{
modm::vl6180::ALS_ErrorCode error = distance.getALS_Error();
if (error == distance.ALS_ErrorCode::NoError)
{
uint32_t lux = distance.getData().getAmbientLight();
MODM_LOG_DEBUG << "\tLux: " << lux;
}
else {
MODM_LOG_DEBUG << "\tError: " << (uint8_t(error) >> 4);
}
}
MODM_LOG_DEBUG << " \tt=" << (modm::Clock::now() - stamp) << modm::endl;
modm::this_fiber::sleep_for(40ms);
}
});
modm::Fiber fiber_blink([]
{
Board::LedOrange::setOutput();
while(true)
{
Board::LedOrange::toggle();
modm::this_fiber::sleep_for(0.5s);
}
});
// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
MyI2cMaster::connect<GpioB11::Sda, GpioB10::Scl>();
MyI2cMaster::initialize<Board::SystemClock, 400_kHz>();
MODM_LOG_INFO << "\n\nWelcome to VL6180X demo!\n\n";
modm::fiber::Scheduler::run();
return 0;
}