Updated emulator and keylistener

This commit is contained in:
Niellune
2025-12-01 17:28:35 +02:00
parent 542fa543a0
commit 2b96660d32
4 changed files with 40 additions and 26 deletions
+4 -3
View File
@@ -3,14 +3,15 @@
CXX := g++ CXX := g++
CXXFLAGS := -std=c++17 -O3 -Wall -Wextra -march=native -flto CXXFLAGS := -std=c++17 -O3 -Wall -Wextra -march=native -flto
LDFLAGS := -flto LDFLAGS := -flto
OUT_DIR := ./../out
all: keylistener emulator all: keylistener emulator
keylistener: keylistener.cpp keylistener: keylistener.cpp
$(CXX) $(CXXFLAGS) keylistener.cpp -o keylistener $(LDFLAGS) $(CXX) $(CXXFLAGS) keylistener.cpp -o $(OUT_DIR)/keylistener $(LDFLAGS)
emulator: emulator.cpp emulator: emulator.cpp
$(CXX) $(CXXFLAGS) emulator.cpp -o emulator $(LDFLAGS) $(CXX) $(CXXFLAGS) emulator.cpp -o $(OUT_DIR)/emulator $(LDFLAGS) -llgpio
clean: clean:
rm -f keylistener emulator rm -f $(OUT_DIR)/keylistener $(OUT_DIR)/emulator
+25 -18
View File
@@ -1,8 +1,9 @@
// lgpio_blink.cpp
#include <iostream> #include <iostream>
#include <csignal> #include <csignal>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <pigpiod_if2.h> #include <lgpio.h>
bool running = true; bool running = true;
@@ -11,37 +12,43 @@ void signal_handler(int signum) {
} }
int main() { int main() {
const int PIN_HOLD = 17; // stays ON const int PIN_HOLD = 17; // stays ON
const int PIN_BLINK = 27; // toggles const int PIN_BLINK = 27; // toggles
const double ON_INTERVAL = 0.2; // seconds ON const double ON_INTERVAL = 0.2; // seconds
const double OFF_INTERVAL = 0.8; // seconds OFF const double OFF_INTERVAL = 0.8; // seconds
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
int pi = pigpio_start(NULL, NULL); // Open the GPIO chip (0 = /dev/gpiochip0 on every Pi)
if (pi < 0) { int h = lgGpiochipOpen(0);
std::cerr << "Failed to connect to pigpiod daemon.\n"; if (h < 0) {
std::cerr << "Failed to open gpiochip0 (error " << h << ")\n";
return 1; 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;
}
set_mode(pi, PIN_HOLD, PI_OUTPUT); std::cout << "GPIO " << PIN_HOLD << " ON (constant), toggling GPIO " << PIN_BLINK << ".\n";
set_mode(pi, PIN_BLINK, PI_OUTPUT);
gpio_write(pi, PIN_HOLD, 1);
std::cout << "GPIO " << PIN_HOLD << " ON (constant), toggling GPIO " << PIN_BLINK << "...\n";
while (running) { while (running) {
gpio_write(pi, PIN_BLINK, 1); lgGpioWrite(h, PIN_BLINK, 1);
std::this_thread::sleep_for(std::chrono::duration<double>(ON_INTERVAL)); std::this_thread::sleep_for(std::chrono::duration<double>(ON_INTERVAL));
gpio_write(pi, PIN_BLINK, 0);
lgGpioWrite(h, PIN_BLINK, 0);
std::this_thread::sleep_for(std::chrono::duration<double>(OFF_INTERVAL)); std::this_thread::sleep_for(std::chrono::duration<double>(OFF_INTERVAL));
} }
std::cout << "\nStopping...\n"; std::cout << "\nStopping...\n";
gpio_write(pi, PIN_HOLD, 0); lgGpioWrite(h, PIN_HOLD, 0);
gpio_write(pi, PIN_BLINK, 0); lgGpioWrite(h, PIN_BLINK, 0);
pigpio_stop(pi);
lgGpiochipClose(h);
return 0; return 0;
} }
+10 -4
View File
@@ -56,8 +56,9 @@ public:
{ {
for (auto v : voltages) for (auto v : voltages)
{ {
button_id++; if (voltage < v)
if (voltage > v) button_id++;
else
break; break;
} }
} }
@@ -119,7 +120,7 @@ void send_key_code(uint8_t i)
// Callback examples // Callback examples
void callback0(int button_id, bool long_press) void callback0(int button_id, bool long_press)
{ {
std::cout << "[Channel 0] Button " << button_id << " pressed. Long press: " << long_press << "\n"; //std::cout << "[Channel 0] Button " << button_id << " pressed. Long press: " << long_press << "\n";
if (button_id == 4) if (button_id == 4)
{ {
if (long_press) if (long_press)
@@ -148,7 +149,7 @@ void callback0(int button_id, bool long_press)
void callback1(int button_id, bool long_press) void callback1(int button_id, bool long_press)
{ {
std::cout << "[Channel 1] Button " << button_id << " pressed. Long press: " << long_press << "\n"; //std::cout << "[Channel 1] Button " << button_id << " pressed. Long press: " << long_press << "\n";
if (button_id == 3) if (button_id == 3)
{ {
if (!long_press) if (!long_press)
@@ -201,11 +202,16 @@ int main()
constexpr double interval = 1.0 / FREQUENCY; constexpr double interval = 1.0 / FREQUENCY;
std::cout << "Keylistener started with fifo " << FIFO_PATH << "\n";
while (true) while (true)
{ {
auto loop_start = std::chrono::steady_clock::now(); auto loop_start = std::chrono::steady_clock::now();
double v0 = read_diff(file_i2c, 0x04); double v0 = read_diff(file_i2c, 0x04);
double v1 = read_diff(file_i2c, 0x05); double v1 = read_diff(file_i2c, 0x05);
//if(v0<voltages[0] || v1<voltages[0])
// std::cout << "Channel 0: " << v0 << " Channel 1: " << v1;
proc0.process_voltage(v0); proc0.process_voltage(v0);
proc1.process_voltage(v1); proc1.process_voltage(v1);
auto loop_end = std::chrono::steady_clock::now(); auto loop_end = std::chrono::steady_clock::now();
+1 -1
View File
@@ -4,7 +4,7 @@ import struct
from smbus2 import SMBus from smbus2 import SMBus
ADS1115_ADDRESS = 0x48 ADS1115_ADDRESS = 0x48
FIFO_PATH = "/tmp/myfifo" FIFO_PATH = "/tmp/fastcarplay_pipe"
LONG_PRESS = 20 LONG_PRESS = 20
fifo_file = None fifo_file = None