Files
FastCarPlay/examples/emulator.cpp
T
2026-02-28 19:29:09 +02:00

59 lines
1.6 KiB
C++

// lgpio_blink.cpp
#include <iostream>
#include <csignal>
#include <thread>
#include <chrono>
#include <lgpio.h>
bool running = true;
void signal_handler(int signum) {
running = false;
}
int main(int argc, char* argv[]) {
const int PIN_HOLD = 5; // stays ON
const int PIN_BLINK = 6; // toggles
const double ON_INTERVAL = 0.5; // seconds
const double OFF_INTERVAL = 0.5; // seconds
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
if (argc > 1) {
running = false;
}
// Open the GPIO chip (0 = /dev/gpiochip0 on every Pi)
int h = lgGpiochipOpen(0);
if (h < 0) {
std::cerr << "Failed to open gpiochip0 (error " << h << ")\n";
return 1;
}
// Claim pins as outputs
int rc1 = lgGpioClaimOutput(h, 0, PIN_HOLD, 1); // 1 = initial HIGH
int rc2 = lgGpioClaimOutput(h, 0, PIN_BLINK, 0); // 0 = initial LOW
if (rc1 < 0 || rc2 < 0) {
std::cerr << "Failed to claim pins (rc1=" << rc1 << ", rc2=" << rc2 << ")\n";
lgGpiochipClose(h);
return 1;
}
std::cout << "GPIO " << PIN_HOLD << " ON (constant), toggling GPIO " << PIN_BLINK << ".\n";
while (running) {
lgGpioWrite(h, PIN_BLINK, 1);
std::this_thread::sleep_for(std::chrono::duration<double>(ON_INTERVAL));
lgGpioWrite(h, PIN_BLINK, 0);
std::this_thread::sleep_for(std::chrono::duration<double>(OFF_INTERVAL));
}
std::cout << "\nStopping...\n";
lgGpioWrite(h, PIN_HOLD, 0);
lgGpioWrite(h, PIN_BLINK, 0);
lgGpiochipClose(h);
return 0;
}