Skip to content

feat: Enhance Badge with Adjustable Brightness Support#1271

Closed
samruddhi-Rahegaonkar wants to merge 2 commits intofossasia:developmentfrom
samruddhi-Rahegaonkar:Add_brightnesslevel
Closed

feat: Enhance Badge with Adjustable Brightness Support#1271
samruddhi-Rahegaonkar wants to merge 2 commits intofossasia:developmentfrom
samruddhi-Rahegaonkar:Add_brightnesslevel

Conversation

@samruddhi-Rahegaonkar
Copy link
Copy Markdown
Member

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar commented Apr 8, 2025

Fixes #866

Changes

  • Added toggle to enable/disable brightness control for virtual and connected badge
  • Implemented brightness slider to set LED intensity
  • Integrated brightness rendering directly on the virtual badge using CustomPainter
  • Synced brightness level with provider state management
  • Refactored BadgePaint to adapt color intensity based on brightness value

Screenshots / Recordings

video.mp4

Checklist:

  • [ x ] No hard coding: I have used resources from constants.dart without hard coding any value.
  • [ x ] No end of file edits: No modifications done at end of resource files.
  • [ x ] Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • [ x ] Code analyzation: My code passes analyzations run in flutter analyze and tests run in flutter test.

Summary by Sourcery

Add brightness control support for virtual and connected badges, enabling users to adjust LED intensity through a slider and toggle

New Features:

  • Implement brightness control for both virtual and physical badges
  • Add a toggle to enable/disable brightness adjustment
  • Integrate brightness slider with LED rendering

Enhancements:

  • Refactor badge painting to support dynamic brightness rendering
  • Create a dedicated Bluetooth brightness service for device interaction

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 8, 2025

Reviewer's Guide by Sourcery

This pull request introduces brightness control functionality for both virtual and connected badges. It includes a toggle to enable/disable brightness control, a slider to set the LED intensity, and integration of brightness rendering on the virtual badge. The brightness level is managed using a provider, and the badge data transfer process has been updated to include brightness information.

Sequence diagram for setting brightness

sequenceDiagram
    participant User
    participant HomeScreen
    participant BadgeBrightnessProvider
    participant BleBrightnessService
    participant BluetoothDevice

    User->HomeScreen: Adjusts brightness slider
    HomeScreen->BadgeBrightnessProvider: setBrightness(brightnessLevel)
    activate BadgeBrightnessProvider
    BadgeBrightnessProvider->BleBrightnessService: setBrightness(brightnessLevel)
    activate BleBrightnessService
    alt isConnected == true
        BleBrightnessService->BluetoothDevice: Writes brightness value
        activate BluetoothDevice
        BluetoothDevice-->>BleBrightnessService: Acknowledgment
        deactivate BluetoothDevice
    else isConnected == false
        BleBrightnessService-->>BadgeBrightnessProvider: Returns false
    end
    BleBrightnessService-->>BadgeBrightnessProvider: Returns success
    deactivate BleBrightnessService
    BadgeBrightnessProvider-->>HomeScreen: Notifies listeners
    deactivate BadgeBrightnessProvider
    HomeScreen->HomeScreen: Rebuilds UI with new brightness
Loading

Updated class diagram for BadgeBrightnessProvider

classDiagram
    class BadgeBrightnessProvider {
        -BleBrightnessService _bleBrightnessService
        -double _brightness
        -bool _isConnected
        -String _errorMessage
        -bool _isBrightnessVisible
        +double brightness
        +bool isConnected
        +String errorMessage
        +bool isBrightnessVisible
        +BadgeBrightnessProvider(BleBrightnessService bleBrightnessService)
        -Future<void> _initialize()
        +void toggleBrightnessVisibility(bool value)
        +Future<bool> connectToDevice(String deviceId)
        +Future<bool> setBrightness(double brightnessLevel)
        +Future<void> getCurrentBrightness()
        +Future<void> disconnect()
        +void dispose()
    }
    class BleBrightnessService {
        +Future<bool> connectToDevice(String deviceId)
        +Future<bool> setBrightness(int brightnessLevel)
        +Future<int?> getCurrentBrightness()
        +Future<void> disconnect()
    }

    BadgeBrightnessProvider -- BleBrightnessService : depends on
Loading

Updated class diagram for BadgePaint

classDiagram
    class BadgePaint {
        -BadgeUtils badgeUtils
        -List~List~bool~~ grid
        -double brightness
        +BadgePaint(List~List~bool~~ grid, double brightness)
        +void paint(Canvas canvas, Size size)
        +bool shouldRepaint(BadgePaint oldDelegate)
    }
Loading

File-Level Changes

Change Details Files
Introduced a BadgeBrightnessProvider to manage the brightness level of the badge, including connection status and error handling.
  • Added BleBrightnessService for handling Bluetooth Low Energy (BLE) communication to control badge brightness.
  • Implemented methods for connecting to a device, discovering services, setting brightness, and reading the current brightness.
  • Created a provider to manage the brightness state, connection status, and error messages.
  • Added methods to toggle brightness visibility and set the brightness level.
  • Integrated the provider into the UI to display connection status, brightness controls, and error messages.
lib/view/homescreen.dart
lib/main.dart
lib/virtualbadge/view/draw_badge.dart
lib/virtualbadge/view/badge_paint.dart
lib/virtualbadge/view/animated_badge.dart
lib/services/BleBrightnessService.dart
lib/providers/BadgeBrightnessProvider.dart
Enabled brightness control for both virtual and connected badges.
  • Added a toggle to enable/disable brightness control.
  • Implemented a slider to set the LED intensity.
  • Integrated brightness rendering directly on the virtual badge using CustomPainter.
  • Synced the brightness level with the provider state management.
  • Refactored BadgePaint to adapt color intensity based on the brightness value.
lib/view/homescreen.dart
lib/virtualbadge/view/draw_badge.dart
lib/virtualbadge/view/badge_paint.dart
lib/virtualbadge/view/animated_badge.dart
Modified the badge data transfer process to include brightness information.
  • Updated the badge data transfer logic to include the current brightness level.
  • Ensured that the brightness value is passed as part of the transfer parameters when sending data to the badge.
  • Included the connection status in the transfer parameters.
lib/view/homescreen.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#866 Add a setting to the application to control the display brightness.
#866 Allow users to adjust the brightness to save battery, especially at night.

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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
Copy Markdown
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 @samruddhi-Rahegaonkar - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a loading state to the UI while connecting to the badge, to improve user experience.
  • The YOUR_DEVICE_ID string should be replaced with a persistent storage lookup.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

Comment thread lib/view/homescreen.dart
.getAnimationIndex()],
null,
false);
brightnessProvider.brightness.round()
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.

issue (bug_risk): Suspicious cast from int to Map<String, dynamic>.

The call to badgeData.checkAndTransfer is passing brightnessProvider.brightness.round() casted to Map<String, dynamic>? even though brightness.round() produces an int. If the intent is to pass brightness as a parameter, consider wrapping it in a map (e.g., {"brightness": brightnessProvider.brightness.round()}) rather than casting an int. Ensure the parameter type expected by checkAndTransfer is correctly respected.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 8, 2025

Build Status

Build successful. APKs to test: https://github.com/fossasia/badgemagic-app/actions/runs/14384460232/artifacts/2920793486.

Screenshots (Android)

Screenshots (iPhone)

Screenshots (iPad)

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar changed the title Enhance Badge with Adjustable Brightness Support feat: Enhance Badge with Adjustable Brightness Support Apr 8, 2025
@samruddhi-Rahegaonkar

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@Jhalakupadhyay

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@samruddhi-Rahegaonkar
Copy link
Copy Markdown
Member Author

@Jhalakupadhyay should i close PR then ?

@Jhalakupadhyay

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@Jhalakupadhyay

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@Jhalakupadhyay

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@samruddhi-Rahegaonkar

This comment was marked as outdated.

@hpdang

This comment was marked as outdated.

nope3472

This comment was marked as outdated.

@samruddhi-Rahegaonkar
Copy link
Copy Markdown
Member Author

@hpdang @nope3472 this feature is limited only to the app side to make it integrated with the badge we need to enable the live Sync in our app.

@nope3472
Copy link
Copy Markdown
Contributor

@samruddhi-Rahegaonkar if we can add brightnesslevel in the json we transfer if possible will be good because making livesync with the firmware at the moment is not implemented and it will take time

@samruddhi-Rahegaonkar
Copy link
Copy Markdown
Member Author

@nope3472 adding brightnessLevel to the JSON only sets the brightness once at the time of message transfer.
The main scope to this issue is to save Battery of the badge making brightness down at night time or whenever not required that much flash.

Copy link
Copy Markdown
Member

@mariobehling mariobehling left a comment

Choose a reason for hiding this comment

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

So, it seems you understand it at the end. The brightness only needs to be set once. You do not need to connect to the app continuously. Please update this PR accordingly and share a short video showcasing the feature.

@samruddhi-Rahegaonkar
Copy link
Copy Markdown
Member Author

So, it seems you understand it at the end. The brightness only needs to be set once. You do not need to connect to the app continuously. Please update this PR accordingly and share a short video showcasing the feature.

@mariobehling I will take this PR shortly after completing the open ones.

@mariobehling
Copy link
Copy Markdown
Member

This feature is still nice to have. Please update the PR or close it if you are no longer working on it.

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.

Add setting for the display brightness

5 participants