-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGpio.cpp
More file actions
115 lines (93 loc) · 2.83 KB
/
Gpio.cpp
File metadata and controls
115 lines (93 loc) · 2.83 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
113
114
115
#include "config.h"
#if USE_GPIO == 1
#include "Gpio.h"
#include <iostream>
#include <string>
#if USE_WIRING_Pi == 1
#include <wiringPi.h>
#endif
Gpio::~Gpio() = default;
void Gpio::run()
{
}
void Gpio::initialize()
{
#if USE_WIRING_Pi == 1
status.gpioSatus = (wiringPiSetupGpio() == 0) ? READY : INIT;
#else
status.gpioSatus = READY;
#endif
// init servos
createThread = false;
IO::initialize();
}
bool Gpio::isInitialized()
{
return (status.gpioSatus == READY);
}
GpioData Gpio::setOutputs(const GpioData &data)
{
for (std::pair<std::string, int> output : data.pwmOutputMap)
{
pwmOutputsMap.at(output.first).setValue(output.second);
}
for (std::pair<std::string, int> output : data.digitalOutputMap)
{
digitalOutputsMap.at(output.first).setValue(output.second);
}
for (std::pair<std::string, int> output : data.dcOutputMap)
{
dcMotorOutputsMap.at(output.first).setValue(output.second);
}
GpioData result;
result.digitalOutputMap = toRawMap(digitalOutputsMap);
result.pwmOutputMap = toRawMap(pwmOutputsMap);
result.dcOutputMap = toRawMap(dcMotorOutputsMap);
return result;
}
void Gpio::createNewGpioOutput(const std::string &name, int pinNbr, int safeState)
{
digitalOutputsMap.insert({name, DigitalOutput(name, pinNbr, safeState)});
}
void Gpio::createNewGpioPwmOutput(const std::string &name, int pinNbr, int safePosition, bool softPWM)
{
pwmOutputsMap.insert({name, PwmOutput(name, pinNbr, safePosition, softPWM)});
}
void Gpio::createNewGpioDCMotorOutput(const std::string &name, int pinForward, int pinReverse, int motorPower,
int limitSwitchMinPin, int limitSwitchMaxPin, int potentiometerPin)
{
dcMotorOutputsMap.insert({name, DCMotorOutput(name, pinForward, pinReverse, motorPower, limitSwitchMinPin,
limitSwitchMaxPin, potentiometerPin)});
}
GpioState Gpio::getCurrentState()
{
GpioState state;
for (auto i : digitalOutputsMap)
{
state.digitalStateMap.insert({i.first, i.second.getCurrentState()});
}
for (auto i : pwmOutputsMap)
{
state.pwmStateMap.insert({i.first, i.second.getCurrentState()});
}
for (auto i : dcMotorOutputsMap)
{
state.dcMotorStateMap.insert({i.first, i.second.getCurrentState()});
}
state.loadCellState = loadCell.getCurrentState();
return state;
}
/**
* Convert a map with Output to a map with numbers
*/
template <typename T, typename std::enable_if<std::is_base_of<Output, T>::value>::type *>
std::map<std::string, int> Gpio::toRawMap(std::map<std::string, T> map)
{
std::map<std::string, int> result;
for (std::pair<const std::string, T> output : map)
{
result.insert({output.first, output.second.getValue()});
}
return result;
}
#endif