-
Notifications
You must be signed in to change notification settings - Fork 6
/
rasprpm.cpp
90 lines (70 loc) · 1.63 KB
/
rasprpm.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
#ifdef USE_RASPRPM
#include "rasprpm.h"
#include <iostream>
#include <cstdlib>
#include <wiringPi.h>
void RaspRPM::setupGPIO(const char *tag, int mode, const struct Gpio &gpio)
{
fprintf(stderr,
"Configure '%s' GPIO: direction = %s, pin = %i, inverted = %s\n",
tag, mode == INPUT?"input":"output", gpio.pin, gpio.inverted?"true":"false");
if (!gpioIsValid(gpio))
return;
pinMode(gpio.pin, mode);
if (mode == OUTPUT)
writeGPIO(gpio, LOW);
}
void RaspRPM::prepareGPIOs()
{
if (wiringPiSetup() == -1)
{
perror("wiringPiSetup");
exit(1);
}
for (size_t i = 0; i < _computers.size(); i++)
{
fprintf(stderr, "Configure computer '%s':\n", _computers.at(i).name.toUTF8().c_str());
setupGPIO("power LED", INPUT, _computers.at(i).powerLed);
setupGPIO("power switch", OUTPUT, _computers.at(i).powerSwitch);
setupGPIO("ATX power", OUTPUT, _computers.at(i).atxSwitch);
fprintf(stderr, "\n");
}
}
RaspRPM::RaspRPM(std::shared_ptr<Wt::WServer> server, Wt::Json::Object conf) :
AbstractRPM(server, conf)
{
prepareGPIOs();
startPolling();
}
bool RaspRPM::gpioIsValid(const struct Gpio &gpio)
{
return gpio.pin >= 0 && gpio.pin <= 20;
}
bool RaspRPM::readGPIO(const struct Gpio &gpio)
{
int value = digitalRead(gpio.pin);
if (gpio.inverted)
{
if (value == HIGH)
value = LOW;
else
value = HIGH;
}
return value == HIGH;
}
void RaspRPM::writeGPIO(const struct Gpio &gpio, int value)
{
if (!gpioIsValid(gpio)) {
fprintf(stderr, "writeGPIO: Invalid write to GPIO %i\n", gpio.pin);
return;
}
if (gpio.inverted)
{
if (value == HIGH)
value = LOW;
else
value = HIGH;
}
digitalWrite(gpio.pin, value);
}
#endif