Skip to content

feat: state path to switch into standalone mode#208

Draft
CloudyPadmal wants to merge 2 commits into
fossasia:mainfrom
CloudyPadmal:standalone
Draft

feat: state path to switch into standalone mode#208
CloudyPadmal wants to merge 2 commits into
fossasia:mainfrom
CloudyPadmal:standalone

Conversation

@CloudyPadmal

@CloudyPadmal CloudyPadmal commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Important

This is baby steps towards #46 and a purely experimental WIP pull request

When the PSLab boots, it will check for the presence of an SD card with a valid configuration file named PSLAB.CFG. If there is such a combination, PSLab will switch to a standalone mode; until serial traffic begins to appear.
If there is no such combination, then the device goes into stand by mode as usual.

Currently, the plan is to read from the SD card a set of bitmap images (or pre-conditioned image data) to display on an I2C OLED display as an animation. I expect the SD card to contain all the necessary parameters such as animation delay, duration, cycles etc to make the animation rendering possible.

Summary by Sourcery

Introduce a standalone operational state that is activated on boot when a valid SD card configuration file is present, otherwise falling back to standby.

New Features:

  • Add a standalone device state that runs until serial traffic is detected.
  • Add SD card configuration detection via a PSLAB.CFG file with a simple header check to decide whether to enter standalone mode on boot.

Enhancements:

  • Extend the global state table and main loop to support transitioning into and out of the new standalone state.
  • Track SD card mount status and provide an explicit unmount function for standalone configuration checks.

@CloudyPadmal CloudyPadmal requested a review from Copilot July 6, 2026 20:05
@CloudyPadmal CloudyPadmal self-assigned this Jul 6, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR introduces an experimental standalone operating mode triggered by an SD card configuration file, wiring it into the system state machine and SD card mounting logic.

Sequence diagram for standalone mode initialization at boot

sequenceDiagram
    participant main
    participant SYSTEM
    participant SDCARD
    participant FATFS

    main->>SYSTEM: SYSTEM_Initialize()
    main->>SDCARD: SDCARD_standalone_check()
    SDCARD->>SDCARD: SD_SPI_IsMediaPresent()
    alt SD card not present
        SDCARD-->>main: FAILED
        main->>main: current_state = STATE_STANDBY
    else SD card present
        SDCARD->>FATFS: f_mount(drive, SDCARD_DRIVE, 1)
        alt mount failed
            SDCARD-->>main: FAILED
            main->>main: current_state = STATE_STANDBY
        else mount success
            SDCARD->>SDCARD: read_config_file()
            alt config invalid or missing
                SDCARD->>SDCARD: SDCARD_standalone_unmount()
                SDCARD-->>main: FAILED
                main->>main: current_state = STATE_STANDBY
            else config valid
                SDCARD-->>main: SUCCESS
                main->>main: current_state = STATE_STANDALONE
            end
        end
    end

    loop main loop
        main->>main: STATES_RunState(current_state)
    end
Loading

State diagram for updated system states with standalone mode

stateDiagram-v2
    [*] --> STATE_STANDBY

    STATE_STANDBY --> STATE_RUNCOMMAND: RunCommand()

    STATE_STANDALONE --> STATE_STANDALONE: Standalone() / [!UART_IsRxReady(U1SELECT)]
    STATE_STANDALONE --> STATE_RUNCOMMAND: Standalone() / UART_IsRxReady(U1SELECT)

    STATE_RUNCOMMAND --> STATE_STANDBY: RunCommand()
Loading

File-Level Changes

Change Details Files
Add SD card configuration file detection and a standalone readiness check that mounts the card and validates the config.
  • Introduce CONFIG_FILENAME constant and an s_mounted flag to track SD card mount state.
  • Implement read_config_file to open PSLAB.CFG and verify it contains the expected 5-byte magic header.
  • Implement SDCARD_standalone_check to detect media, mount the filesystem, and validate the configuration file, unmounting and failing if validation fails.
  • Add SDCARD_standalone_unmount to safely unmount the SD card when no valid configuration is present.
src/sdcard/sdcard.c
src/sdcard/sdcard.h
Extend the state machine with a standalone mode that keeps running until serial traffic appears, and select this mode on boot when a valid SD card config is present.
  • Add STATE_STANDALONE to the state_t enum and state_table, wired to a new Standalone state handler.
  • Implement Standalone state handler that polls UART for incoming data and transitions to STATE_RUNCOMMAND when serial traffic is detected.
  • Update main initialization to call SDCARD_standalone_check at boot and choose STATE_STANDALONE as the initial state when the check succeeds, otherwise falling back to STATE_STANDBY.
src/states.c
src/states.h
src/main.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an experimental boot-time path to enter a new standalone state when an SD card with a valid PSLAB.CFG configuration is detected, otherwise continuing into the existing standby/serial-command flow. This extends the firmware state machine and introduces an SD-card config probe intended to support future standalone animation/display behavior.

Changes:

  • Introduces a new STATE_STANDALONE and corresponding state handler that runs until serial RX activity is detected.
  • Adds an SD-card “standalone config present” check for PSLAB.CFG during boot, and uses it to select the initial state.
  • Adds SD-card mount tracking and an unmount helper for the standalone config check path.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/states.h Adds STATE_STANDALONE to the state enum.
src/states.c Implements the standalone state and extends the state dispatch table.
src/sdcard/sdcard.h Declares the standalone SD-card config check API (and should also declare unmount if intended public).
src/sdcard/sdcard.c Implements config file probing and (un)mount handling for the standalone check.
src/main.c Chooses initial boot state based on the SD-card standalone config check result.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sdcard/sdcard.c Outdated
Comment thread src/sdcard/sdcard.c
Comment on lines +169 to +193
response_t SDCARD_standalone_check(void) {
if (!SD_SPI_IsMediaPresent()) {
return FAILED;
}

FATFS drive;
if (f_mount(&drive, SDCARD_DRIVE, 1) != FR_OK) {
return FAILED;
}
s_mounted = true;

if (!read_config_file()) {
SDCARD_standalone_unmount();
return FAILED;
}

return SUCCESS;
}

void SDCARD_standalone_unmount(void) {
if (s_mounted) {
f_mount(0, SDCARD_DRIVE, 0);
s_mounted = false;
}
}
Comment thread src/states.c
Comment on lines +43 to +49
state_t Standalone(void) {
if (UART_IsRxReady(U1SELECT)) {
return STATE_RUNCOMMAND;
}

return STATE_STANDALONE;
}
Comment thread src/sdcard/sdcard.h
Comment on lines +100 to +109
/**
* @brief Check if SD-card is present and can be mounted.
*
* @details Checks if the SD-card can be mounted and if the configuration file is present and valid.
* If the configuration file is not present or invalid, or if the SD-card is not mounted, the function returns FAILED.
* The configuration file is expected to be named "PSLAB.CFG" and contain a magic header of 5 bytes with a null terminator.
*
* @return SUCCESS
*/
response_t SDCARD_standalone_check(void);
Copilot AI review requested due to automatic review settings July 6, 2026 20:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Comment thread src/states.c
Comment on lines +43 to +49
state_t Standalone(void) {
if (UART_IsRxReady(U1SELECT)) {
return STATE_RUNCOMMAND;
}

return STATE_STANDALONE;
}
Comment thread src/sdcard/sdcard.c

return SUCCESS;
}

Comment thread src/sdcard/sdcard.c Outdated
Comment on lines +175 to +179
FATFS drive;
if (f_mount(&drive, SDCARD_DRIVE, 1) != FR_OK) {
return FAILED;
}
s_mounted = true;
Comment thread src/sdcard/sdcard.h
Comment on lines +104 to +108
* If the configuration file is not present or invalid, or if the SD-card is not mounted, the function returns FAILED.
* The configuration file is expected to be named "PSLAB.CFG" and contain a magic header of 5 bytes with a null terminator.
*
* @return SUCCESS
*/
Comment thread src/sdcard/sdcard.h
*
* @return SUCCESS
*/
response_t SDCARD_standalone_check(void);

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread src/sdcard/sdcard.h
Comment on lines +109 to +113
* If the configuration file is not present or invalid, or if the SD-card is not mounted, the function returns FAILED.
* The configuration file is expected to be named "PSLAB.CFG" and contain a magic header of 5 bytes with a null terminator.
*
* @return SUCCESS
*/
Comment thread src/sdcard/sdcard.c
Comment on lines +147 to +150
/**
* @brief Read the configuration file from the SD card.
* @return true if the config file was read successfully, false otherwise or if the config structure is invalid.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants