diff --git a/examples/Makefile b/examples/Makefile index a389ddd..1e46bd8 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,14 +3,15 @@ CXX := g++ CXXFLAGS := -std=c++17 -O3 -Wall -Wextra -march=native -flto LDFLAGS := -flto +OUT_DIR := ./../out all: keylistener emulator keylistener: keylistener.cpp - $(CXX) $(CXXFLAGS) keylistener.cpp -o keylistener $(LDFLAGS) + $(CXX) $(CXXFLAGS) keylistener.cpp -o $(OUT_DIR)/keylistener $(LDFLAGS) emulator: emulator.cpp - $(CXX) $(CXXFLAGS) emulator.cpp -o emulator $(LDFLAGS) + $(CXX) $(CXXFLAGS) emulator.cpp -o $(OUT_DIR)/emulator $(LDFLAGS) -llgpio clean: - rm -f keylistener emulator \ No newline at end of file + rm -f $(OUT_DIR)/keylistener $(OUT_DIR)/emulator \ No newline at end of file diff --git a/examples/emulator.cpp b/examples/emulator.cpp index 5059241..78dd7a0 100644 --- a/examples/emulator.cpp +++ b/examples/emulator.cpp @@ -1,8 +1,9 @@ +// lgpio_blink.cpp #include #include #include #include -#include +#include bool running = true; @@ -11,37 +12,43 @@ void signal_handler(int signum) { } int main() { - const int PIN_HOLD = 17; // stays ON - const int PIN_BLINK = 27; // toggles - const double ON_INTERVAL = 0.2; // seconds ON - const double OFF_INTERVAL = 0.8; // seconds OFF + const int PIN_HOLD = 17; // stays ON + const int PIN_BLINK = 27; // toggles + const double ON_INTERVAL = 0.2; // seconds + const double OFF_INTERVAL = 0.8; // seconds signal(SIGINT, signal_handler); - int pi = pigpio_start(NULL, NULL); - if (pi < 0) { - std::cerr << "Failed to connect to pigpiod daemon.\n"; + // 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; + } - set_mode(pi, PIN_HOLD, PI_OUTPUT); - 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"; + std::cout << "GPIO " << PIN_HOLD << " ON (constant), toggling GPIO " << PIN_BLINK << ".\n"; while (running) { - gpio_write(pi, PIN_BLINK, 1); + lgGpioWrite(h, PIN_BLINK, 1); std::this_thread::sleep_for(std::chrono::duration(ON_INTERVAL)); - gpio_write(pi, PIN_BLINK, 0); + + lgGpioWrite(h, PIN_BLINK, 0); std::this_thread::sleep_for(std::chrono::duration(OFF_INTERVAL)); } std::cout << "\nStopping...\n"; - gpio_write(pi, PIN_HOLD, 0); - gpio_write(pi, PIN_BLINK, 0); - pigpio_stop(pi); + lgGpioWrite(h, PIN_HOLD, 0); + lgGpioWrite(h, PIN_BLINK, 0); + lgGpiochipClose(h); return 0; } \ No newline at end of file diff --git a/examples/keylistener.cpp b/examples/keylistener.cpp index 5ec6be0..3230402 100644 --- a/examples/keylistener.cpp +++ b/examples/keylistener.cpp @@ -56,8 +56,9 @@ public: { for (auto v : voltages) { - button_id++; - if (voltage > v) + if (voltage < v) + button_id++; + else break; } } @@ -119,7 +120,7 @@ void send_key_code(uint8_t i) // Callback examples 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 (long_press) @@ -148,7 +149,7 @@ void callback0(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 (!long_press) @@ -201,11 +202,16 @@ int main() constexpr double interval = 1.0 / FREQUENCY; + std::cout << "Keylistener started with fifo " << FIFO_PATH << "\n"; + while (true) { auto loop_start = std::chrono::steady_clock::now(); double v0 = read_diff(file_i2c, 0x04); double v1 = read_diff(file_i2c, 0x05); + //if(v0