-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIOPWMThread.cpp
104 lines (89 loc) · 2.94 KB
/
GPIOPWMThread.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
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
/*
* Copyright (C) 2016 - 2023 Mikhail Sapozhnikov
*
* This file is part of ship-control.
*
* ship-control is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ship-control is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ship-control. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <thread>
#include <chrono>
#include <exception>
#include <gpiod.hpp>
#include "GPIOPWMThread.hpp"
namespace shipcontrol
{
GPIOPWMThread::GPIOPWMThread(const std::string &chip_path,
unsigned int engine_line,
unsigned int pwm_period) :
_chip_path(chip_path),
_engine_line(engine_line),
_pwm_period(pwm_period),
_pwm_duration(0)
{
_log = Log::getInstance();
_log->write(LogLevel::DEBUG, "GPIOPWMThread ctor, chip_path=%s, engine_line=%d, pwm_period=%d\n",
chip_path.c_str(), engine_line, pwm_period);
}
GPIOPWMThread::~GPIOPWMThread()
{
Log::release();
}
void GPIOPWMThread::run()
{
_log->write(LogLevel::DEBUG, "GPIOPWMThread::run()\n");
try
{
gpiod::chip chip(_chip_path);
gpiod::line line = chip.get_line(_engine_line);
line.request({"shipcontrol::GPIOPWMThread",
gpiod::line_request::DIRECTION_OUTPUT,
0},
0);
while (true)
{
if (need_to_stop() == true)
{
_log->write(LogLevel::DEBUG, "GPIOPWMThread stopping\n");
break;
}
unsigned int cur_pwm_duration = _pwm_duration;
if (cur_pwm_duration != 0)
{
// set GPIO high level and wait for PWM duration
line.set_value(1);
std::this_thread::sleep_for(std::chrono::microseconds(cur_pwm_duration));
}
// set GPIO low level and wait for the next iteration
line.set_value(0);
std::this_thread::sleep_for(std::chrono::microseconds(_pwm_period - cur_pwm_duration));
}
line.release();
}
catch (const std::exception &e)
{
_log->write(LogLevel::ERROR, "GPIOPWMThread caught exception: %s\n", e.what());
}
}
void GPIOPWMThread::set_pwm_duration(unsigned int pwm_duration)
{
_pwm_duration = pwm_duration;
if (_pwm_duration >= _pwm_period)
{
_pwm_duration = _pwm_period - 1;
}
_log->write(LogLevel::DEBUG, "GPIOPWMThread::set_pwm_duration(%d), new duration = %d\n",
pwm_duration, _pwm_duration);
}
} // namespace shipcontrol