-
Notifications
You must be signed in to change notification settings - Fork 0
/
impulses.cpp
150 lines (125 loc) · 4 KB
/
impulses.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <wiringPi.h>
#include <iostream>
#include <fstream>
#include <csignal>
#include <ctime>
#include <unistd.h> // Для использования getopt
// Глобальные переменные для хранения времени последнего срабатывания
volatile unsigned long lastDebounceTime23 = 0;
volatile unsigned long lastDebounceTime17 = 0;
const unsigned long debounceDelay = 1000; // Дебаунс задержка в миллисекундах
// Глобальные переменные для хранения состояния
volatile bool state23 = false;
volatile bool state17 = false;
// Флаги для параметров командной строки
bool silence = false;
bool help = false;
void updateFile(const std::string& filename) {
std::ifstream inFile(filename);
int count = 0;
if (inFile.is_open()) {
inFile >> count;
inFile.close();
}
count += 1;
std::ofstream outFile(filename);
if (outFile.is_open()) {
outFile << count;
outFile.close();
} else {
std::cerr << "Failed to open " << filename << " for writing." << std::endl;
}
if (!silence) {
std::cout << "Updated " << filename << " to " << count << std::endl;
}
}
void pulseCallback23() {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime23 > debounceDelay) {
if (!state23) {
updateFile("cold.txt");
state23 = true;
}
lastDebounceTime23 = currentTime;
}
}
void pulseCallback17() {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime17 > debounceDelay) {
if (!state17) {
updateFile("hot.txt");
state17 = true;
}
lastDebounceTime17 = currentTime;
}
}
void resetState() {
state23 = false;
state17 = false;
}
void handleSignal(int signal) {
std::cout << "Exiting program..." << std::endl;
wiringPiISR(23, INT_EDGE_FALLING, NULL); // Remove interrupt handlers
wiringPiISR(17, INT_EDGE_FALLING, NULL);
exit(0);
}
int main(int argc, char* argv[]) {
// Обработка параметров командной строки
int opt;
while ((opt = getopt(argc, argv, "sh")) != -1) {
switch (opt) {
case 's':
silence = true;
break;
case 'h':
help = true;
break;
default: /* '?' */
std::cerr << "Usage: " << argv[0] << " [-s] [-h]\n";
return 1;
}
}
if (help) {
std::cout << "Usage: " << argv[0] << " [-s] [-h]\n"
<< "Options:\n"
<< " -s Silence mode, suppresses output\n"
<< " -h Display this help message\n";
return 0;
}
// Initialize WiringPi and set up GPIO
if (wiringPiSetupGpio() == -1) {
std::cerr << "Failed to initialize WiringPi" << std::endl;
return 1;
}
// Set up GPIO pins 23 and 17 as input
pinMode(23, INPUT);
pullUpDnControl(23, PUD_UP);
pinMode(17, INPUT);
pullUpDnControl(17, PUD_UP);
// Set up ISR for GPIO pins
if (wiringPiISR(23, INT_EDGE_FALLING, &pulseCallback23) < 0) {
std::cerr << "Failed to set up ISR for pin 23" << std::endl;
return 1;
}
if (wiringPiISR(17, INT_EDGE_FALLING, &pulseCallback17) < 0) {
std::cerr << "Failed to set up ISR for pin 17" << std::endl;
return 1;
}
// Handle termination signal to clean up
signal(SIGINT, handleSignal);
if (!silence) {
std::cout << "Press Ctrl+C to exit" << std::endl;
}
// Keep the program running to listen for interrupts
while (true) {
// Check if the state has returned to open
if (digitalRead(23) == HIGH) {
state23 = false;
}
if (digitalRead(17) == HIGH) {
state17 = false;
}
delay(100); // Sleep for 0.1 second
}
return 0;
}