Skip to content

Fix charging detection when usb-c cable is plugged in#119

Open
marcoag wants to merge 2 commits intofossasia:masterfrom
marcoag:fix_charging_detection
Open

Fix charging detection when usb-c cable is plugged in#119
marcoag wants to merge 2 commits intofossasia:masterfrom
marcoag:fix_charging_detection

Conversation

@marcoag
Copy link
Member

@marcoag marcoag commented Dec 29, 2025

Summary

Adds automatic USB charging detection that enters charging mode when cable is connected during operation.

Changes

  • src/main.c:
    • Added DETECT_CHARGING event flag and periodic task (500ms interval)
    • Implemented charging status monitoring that detects USB cable connection
    • Automatically enters charging mode (BOOT) when charging status transitions from not charging to charging
    • Added forward declaration for disp_charging() function

Behavior

  • USB plugged in while device is running: Automatically enters charging mode within 500ms
  • After exiting charging mode: Stays in NORMAL/DOWNLOAD modes while USB remains connected
  • USB unplugged and re-plugged: Automatically re-enters charging mode

Technical Implementation

The DETECT_CHARGING task runs every 500ms and tracks charging state transitions using a static was_charging flag. When it detects a transition from not charging to charging (is_charging && !was_charging), it:

  1. Sets mode to BOOT (charging mode)
  2. Calls disp_charging() to show charging animation
  3. Calls mode_setup_normal() to restore normal operation after charging display completes

Test Plan

  • Plug in USB while in NORMAL mode → enters charging mode
  • Plug in USB while in DOWNLOAD mode → enters charging mode
  • Press power button in charging mode → exits to NORMAL mode
  • USB remains connected after exit → stays in NORMAL/DOWNLOAD modes
  • Unplug and re-plug USB → re-enters charging mode

Dependencies

Might require #117.

Summary by Sourcery

Introduce periodic USB charging detection to automatically enter charging mode when power is connected during operation.

Bug Fixes:

  • Ensure the device correctly transitions into charging mode when USB power is connected while the device is already running.

Enhancements:

  • Add a periodic charging detection task that monitors USB power state and triggers charging mode on transition from not-charging to charging.

Signed-off-by: Marco A. Gutierrez <marcogg@marcogg.com>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 29, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a periodic charging detection task that monitors USB power state and automatically transitions the device into charging (BOOT) mode when a cable is connected during operation, then returns to normal mode after displaying the charging screen.

Sequence diagram for DETECT_CHARGING task and charging mode transition

sequenceDiagram
    participant TMOS as TMOS_scheduler
    participant Common as common_tasks
    participant Power as charging_status
    participant Display as disp_charging
    participant Mode as mode_setup_normal

    TMOS->>Common: DETECT_CHARGING event (every 500ms)
    Common->>Power: charging_status()
    Power-->>Common: is_charging
    alt transition from not charging to charging
        Common->>Common: mode = BOOT
        Common->>Display: disp_charging()
        Display-->>Common: charging_animation_complete
        Common->>Mode: mode_setup_normal()
    else no transition to charging
        Common->>Common: update was_charging
    end
    Common-->>TMOS: clear DETECT_CHARGING flag
Loading

Flow diagram for DETECT_CHARGING periodic task logic

flowchart TD
    A[DETECT_CHARGING event triggered every 500ms] --> B[Read charging_status and assign is_charging]
    B --> C{is_charging == 1 and was_charging == 0}
    C -- Yes --> D[Set mode to BOOT]
    D --> E[Call disp_charging to show charging animation]
    E --> F[Call mode_setup_normal to restore normal operation]
    C -- No --> G[Skip charging transition]
    F --> H[Set was_charging = is_charging]
    G --> H[Set was_charging = is_charging]
    H --> I[Clear DETECT_CHARGING event flag and return]
Loading

File-Level Changes

Change Details Files
Introduce a periodic charging detection event that drives automatic transitions into charging mode when USB power is newly detected.
  • Added a DETECT_CHARGING event flag to the common event bitmask
  • Hooked DETECT_CHARGING handling into common_tasks with edge detection logic using a static was_charging flag and charging_status()
  • On transition from not charging to charging, set mode to BOOT, invoke disp_charging(), then call mode_setup_normal() to restore normal operation
  • Configured a reload task in spawn_tasks to fire DETECT_CHARGING every 500ms
  • Added a forward declaration for disp_charging() so it can be called from common_tasks
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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider extracting the 500000 / 625 interval for DETECT_CHARGING into a named constant (similar to ANI_FLASH_SPEED_T etc.) so the charging detection cadence is self-documenting and easier to tweak.
  • If disp_charging() can block for a noticeable duration, calling it directly from common_tasks may delay handling of other events; you might want to move the charging animation to its own task or make disp_charging() non-blocking.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the `500000 / 625` interval for `DETECT_CHARGING` into a named constant (similar to `ANI_FLASH_SPEED_T` etc.) so the charging detection cadence is self-documenting and easier to tweak.
- If `disp_charging()` can block for a noticeable duration, calling it directly from `common_tasks` may delay handling of other events; you might want to move the charging animation to its own task or make `disp_charging()` non-blocking.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@marcoag marcoag changed the title Fix power button behavior when USB-C charging. Fix charging detection when usb-c cable is plugged in Dec 29, 2025
Signed-off-by: Marco A. Gutierrez <marcogg@marcogg.com>
@marcoag marcoag requested a review from kienvo December 29, 2025 22:07
Copy link
Contributor

@bessman bessman left a comment

Choose a reason for hiding this comment

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

When transitioning from normal mode to the charge screen, pixels which were lit in normal mode are not turned off unless they are touched by the charging animation.


if (is_charging && !was_charging) {
mode = BOOT;
disp_charging();
Copy link
Contributor

@bessman bessman Dec 30, 2025

Choose a reason for hiding this comment

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

This fixes the leftover lit pixels:

Suggested change
disp_charging();
fb_fill(fb, 0);
disp_charging();

@jerji
Copy link

jerji commented Jan 2, 2026

When unplugging, the battery icon gets drawn on top of the text and it looks jank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants