mirror of
https://github.com/modelec/tirette.git
synced 2026-01-18 16:57:24 +01:00
Add SIGINT and SIGTERM handle
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea
|
||||
|
||||
cmake-build-debug
|
||||
52
main.cpp
52
main.cpp
@@ -1,12 +1,22 @@
|
||||
#include <wiringPi.h>
|
||||
#include <iostream>
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
|
||||
#include "MyClient.h"
|
||||
|
||||
// Numéro du GPIO connecté à la tirette
|
||||
#define TIRETTE_GPIO 17
|
||||
|
||||
std::atomic<bool> shouldStop = false;
|
||||
|
||||
void signalHandler(int signum) {
|
||||
shouldStop = true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
signal(SIGINT, signalHandler);
|
||||
signal(SIGTERM, signalHandler);
|
||||
// Initialisation de WiringPi
|
||||
if (wiringPiSetupGpio() == -1) {
|
||||
std::cerr << "Erreur lors de l'initialisation de WiringPi." << std::endl;
|
||||
@@ -28,29 +38,35 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
client.setTiretteState(true);
|
||||
|
||||
client.start();
|
||||
try{
|
||||
client.start();
|
||||
|
||||
client.sendMessage("tirette;strat;ready;1");
|
||||
client.sendMessage("tirette;strat;ready;1");
|
||||
|
||||
int lastEtat = digitalRead(TIRETTE_GPIO);
|
||||
// Boucle principale
|
||||
while (!client.shouldStop()) {
|
||||
int etat = digitalRead(TIRETTE_GPIO);
|
||||
int lastEtat = digitalRead(TIRETTE_GPIO);
|
||||
// Boucle principale
|
||||
while (!client.shouldStop()) {
|
||||
int etat = digitalRead(TIRETTE_GPIO);
|
||||
|
||||
if (etat == LOW) {
|
||||
client.setTiretteState(true);
|
||||
} else
|
||||
{
|
||||
client.setTiretteState(false);
|
||||
if (lastEtat != etat) {
|
||||
// std::cout << "LETSGO" << std::endl;
|
||||
client.sendMessage("tirette;strat;set state;0");
|
||||
if (etat == LOW) {
|
||||
client.setTiretteState(true);
|
||||
} else
|
||||
{
|
||||
client.setTiretteState(false);
|
||||
if (lastEtat != etat) {
|
||||
// std::cout << "LETSGO" << std::endl;
|
||||
client.sendMessage("tirette;strat;set state;0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attendre un court délai avant de vérifier à nouveau
|
||||
lastEtat = etat;
|
||||
delay(50);
|
||||
// Attendre un court délai avant de vérifier à nouveau
|
||||
lastEtat = etat;
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e){
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user