Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1294

Python • Re: Trouble with GPIO Looping and Edge Detection

$
0
0
I see few issues related button debouncing and confirmations that the service is up or down.
Of course this is just my opinion :)

1. A bouncing button/switch can cause this script to fail.
2. When you shutdown the application, you don't have positive confirmation that the application has successfully shutdown; you assume it shutdown after after x time.
3. When you start application no positive way of confirming application has started; you simply relying on a PAUSE

here is you code in English with my comments showing areas that may be problematic.

Code:

// --- Initialization ---Define Constants:    BUTTON_PIN = 7    RED_LED_PIN = 32    GREEN_LED_PIN = 16    BLUE_LED_PIN = 22    SERVICE_NAME = "kismet.service"    SHORT_PAUSE = 0.1 seconds    ACTION_PAUSE = 0.5 seconds // Pause after starting/stopping service// Setup Hardware Interface (GPIO)Disable GPIO warningsSet GPIO numbering mode to BOARD (physical pins)Wait briefly (e.g., 1 second) // Allow setup timeConfigure BUTTON_PIN as Input with Pull-Up resistor    // Reads HIGH when button is NOT pressed    // Reads LOW when button IS pressedConfigure RED_LED_PIN as Output, initial state OFFConfigure GREEN_LED_PIN as Output, initial state OFFConfigure BLUE_LED_PIN as Output, initial state ON // Script ready indicatorDisplay startup message ("Monitoring service...", Pin info, "Press Ctrl+C to exit")// --- Main Program Loop ---Start Loop (runs forever until interrupted)    // Check Service Status (Requires Admin/Sudo Privileges)    Execute system command: "systemctl is-active SERVICE_NAME"    Store the result (e.g., "active", "inactive", "failed") in serviceStatus    // Check Button State    Read state from BUTTON_PIN    Store the result (e.g., HIGH or LOW) in buttonState    // --- Core Logic ---    IF serviceStatus is "active" THEN        IF buttonState is HIGH (Not Pressed) THEN            Turn GREEN_LED_PIN ON            Turn RED_LED_PIN OFF        ELSE // buttonState is LOW (Pressed) (POSSIBLE DEBOUNCING ISSUES HERE.)            Display "Stopping service..."            Execute system command: "systemctl stop SERVICE_NAME" (Requires Admin/Sudo)            Wait for ACTION_PAUSE // Allow service time to stop , YOU need to confirm app is down.         END IF    ELSE IF serviceStatus is "inactive" THEN        IF buttonState is HIGH (Not Pressed) THEN // POSSIBLE DEBOUNCING ISSUES HERE)            Turn RED_LED_PIN ON            Turn GREEN_LED_PIN OFF        ELSE // buttonState is LOW (Pressed)    // POSSIBLE DEBOUNCING ISSUES HERE)            Display "Starting service..."            Execute system command: "systemctl start SERVICE_NAME" (Requires Admin/Sudo)            Wait for ACTION_PAUSE // Allow service time to start. (POTENTIAL POBLEM HERE: you should confirm app is UP)        END IF    ELSE // Service status is something else (e.g., "failed", "activating")        Display "Service status is [serviceStatus], treating as inactive."        Turn RED_LED_PIN ON        Turn GREEN_LED_PIN OFF        // Note: No action taken on button press in this state    END IF    // Pause briefly to prevent high CPU usage    Wait for SHORT_PAUSE  End Loop // Typically interrupted by user (e.g., Ctrl+C)// --- Cleanup (Ensured to run on exit) ---Display "Exiting..."Reset all used GPIO pins to default state

Statistics: Posted by Henderson Hood — Mon Apr 14, 2025 1:51 am



Viewing all articles
Browse latest Browse all 1294

Trending Articles