Skip to content

Conversation

Yugesh-Kumar-S
Copy link
Contributor

@Yugesh-Kumar-S Yugesh-Kumar-S commented Aug 12, 2025

Fixes #2837

Changes

  • Ported APDS9960 sensor screen.

Screenshots / Recordings

screen-20250816-105628.mp4
screen-20250816-113844.mp4

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Add full support for the APDS9960 sensor by implementing its communication, provider, and UI screen, integrating it into the SensorsScreen, and updating localization and theming

New Features:

  • Introduce APDS9960 communication class to read color, proximity, lux, and gesture data via I2C
  • Add APDS9960Provider to handle sensor initialization, data polling, and chart data management
  • Create APDS9960Screen with configuration controls, raw data panels, and dynamic charts for light, proximity, and gesture modes
  • Register APDS9960Screen in the SensorsScreen navigation list
  • Add localization entries for gesture and color labels used in the APDS9960 UI
  • Define theme chart colors specifically for APDS9960 data

Bug Fixes:

  • Fix localization string for lx key from 'Lx' to 'lx'

Summary by Sourcery

Port full support for the APDS9960 sensor by implementing its communication layer, state provider, and UI screen, integrating it into the main SensorsScreen, and updating localization and theming

New Features:

  • Add APDS9960 communication class to read color, proximity, lux, and gesture data via I2C
  • Implement APDS9960Provider to initialize the sensor, poll data, and manage chart datasets
  • Create APDS9960Screen with configuration controls, raw data panels, and dynamic charts for light, proximity, and gesture modes
  • Register APDS9960Screen in the SensorsScreen navigation list
  • Add localization entries for gesture, color labels, mode, configure, proximity, light, and lux
  • Define theme chart colors specifically for APDS9960 data

Bug Fixes:

  • Correct localization string for lux unit from 'Lx' to 'lx'

Copy link
Contributor

sourcery-ai bot commented Aug 12, 2025

Reviewer's Guide

Implemented full APDS9960 sensor support by adding its I2C communication class, provider for state and data polling, a dedicated UI screen with charts and controls, integration into the main navigation, and necessary localization and theming updates.

Sequence diagram for APDS9960 sensor data polling and UI update

sequenceDiagram
  actor User
  participant APDS9960Screen
  participant APDS9960Provider
  participant APDS9960
  participant I2C

  User->>APDS9960Screen: Selects APDS9960 sensor
  APDS9960Screen->>APDS9960Provider: initializeSensors(i2c, scienceLab)
  APDS9960Provider->>APDS9960: create(i2c, scienceLab)
  APDS9960->>I2C: I2C initialization
  APDS9960Provider->>APDS9960: getRawData(mode)
  APDS9960->>I2C: Read sensor data
  APDS9960-->>APDS9960Provider: Return sensor data
  APDS9960Provider-->>APDS9960Screen: Notify UI update
  APDS9960Screen-->>User: Display updated sensor data and charts
Loading

Class diagram for APDS9960 sensor support

classDiagram
  class APDS9960 {
    +I2C i2c
    +List<int> colorData
    +double lux
    +int proximity
    +int gesture
    +Future<void> _initialize(ScienceLab)
    +Future<int> getProximity()
    +Future<List<int>> getColorData()
    +Future<double> getLux()
    +Future<int> getGesture()
    +Future<Map<String, dynamic>> getRawData(int mode)
    +String getGestureString(int gestureValue)
  }

  class APDS9960Provider {
    +APDS9960? _apds9960
    +int red
    +int green
    +int blue
    +int clear
    +double lux
    +int proximity
    +int gesture
    +String gestureString
    +List<ChartDataPoint> luxData
    +List<ChartDataPoint> proximityData
    +bool isRunning
    +bool isLooping
    +int timegapMs
    +int numberOfReadings
    +int mode
    +Future<void> initializeSensors(...)
    +void setMode(int)
    +void toggleDataCollection()
    +void clearData()
  }

  class APDS9960Screen {
    +StatefulWidget
    +APDS9960Provider provider
    +Widget _buildConfigureSection(APDS9960Provider)
    +Widget _buildRawDataSection(APDS9960Provider)
  }

  APDS9960Provider --> APDS9960
  APDS9960Screen --> APDS9960Provider
Loading

File-Level Changes

Change Details Files
Add APDS9960 I2C communication module
  • Implemented initialization and bit‐level register configuration
  • Provided methods to read color, proximity, lux, and gesture data
  • Handled raw data parsing, interrupt clearing, and gesture determination
lib/communication/sensors/apds9960.dart
Introduce APDS9960Provider to manage sensor state and data polling
  • Initialize sensor on startup and handle connection errors
  • Start/stop and optionally loop data collection on a timer
  • Buffer chart data points and manage read count, modes, and cleanup
lib/providers/apds9960_provider.dart
Create APDS9960Screen UI and integrate into SensorsScreen
  • Build configure and raw data sections with dynamic charts and controls
  • Wire up play/pause, looping, time gap, and clear actions
  • Register and import the new screen in the main sensors navigation
lib/view/apds9960_screen.dart
lib/view/sensors_screen.dart
Update localization and theme resources
  • Fixed capitalization of 'lx' string and added labels for gesture, color channels, mode, configure, proximity, light, and lux
  • Defined dedicated chart color list for APDS9960 data
lib/l10n/app_localizations.dart
lib/l10n/app_localizations_en.dart
lib/theme/colors.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#2837 Integrate the APDS9960 sensor into the codebase, including communication and data handling.
#2837 Port and implement the APDS9960 sensor screen, providing UI for configuration, data display, and charts.

Possibly linked issues


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

github-actions bot commented Aug 12, 2025

@Yugesh-Kumar-S Yugesh-Kumar-S marked this pull request as ready for review August 16, 2025 06:21
@Yugesh-Kumar-S Yugesh-Kumar-S self-assigned this Aug 16, 2025
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 there - I've reviewed your changes - here's some feedback:

  • Localize hard-coded UI strings in APDS9960Screen (modeOptions entries and the scaffold title) instead of using direct English literals.
  • Extract the duplicate Snackbar creation logic in APDS9960Screen into a shared helper or widget to avoid repeating error and success snackbars.
  • The APDS9960 class currently mixes low-level I2C communication with complex gesture detection; consider refactoring by extracting gesture processing or data conversion into separate smaller classes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Localize hard-coded UI strings in APDS9960Screen (modeOptions entries and the scaffold title) instead of using direct English literals.
- Extract the duplicate Snackbar creation logic in APDS9960Screen into a shared helper or widget to avoid repeating error and success snackbars.
- The APDS9960 class currently mixes low-level I2C communication with complex gesture detection; consider refactoring by extracting gesture processing or data conversion into separate smaller classes.

## Individual Comments

### Comment 1
<location> `lib/communication/sensors/apds9960.dart:126` </location>
<code_context>
+    await setBit(enable, bitMaskEnableColor, value);
+  }
+
+  Future<void> setProximityInterruptThreshold(List<int> settingArray) async {
+    if (settingArray.isNotEmpty &&
+        settingArray[0] >= 0 &&
</code_context>

<issue_to_address>
Potential issue: Both threshold values are written to the same register.

Please check if the sensor requires separate registers for each threshold and confirm the correct register mapping with the datasheet.
</issue_to_address>

### Comment 2
<location> `lib/communication/sensors/apds9960.dart:151` </location>
<code_context>
+  }
+
+  Future<void> setColorIntegrationTime(int value) async {
+    await i2c.write(address, [256 - value], atime);
+  }
+
</code_context>

<issue_to_address>
Possible off-by-one error in color integration time calculation.

Verify that the calculation aligns with the sensor's specification for integration time. If the expected range or formula differs, adjust accordingly.
</issue_to_address>

### Comment 3
<location> `lib/providers/apds9960_provider.dart:112` </location>
<code_context>
+  }
+
+  void _startDataCollection() {
+    if (_apds9960 == null) return;
+
+    _isRunning = true;
</code_context>

<issue_to_address>
Silent return if sensor is not initialized may hide errors.

Log a warning or notify the user when _apds9960 is null to improve error visibility and debugging.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
  void _startDataCollection() {
    if (_apds9960 == null) return;

    _isRunning = true;
=======
  void _startDataCollection() {
    if (_apds9960 == null) {
      // Log a warning for debugging purposes
      debugPrint('Warning: Tried to start data collection but _apds9960 is not initialized.');
      return;
    }

    _isRunning = true;
>>>>>>> REPLACE

</suggested_fix>

### Comment 4
<location> `lib/theme/colors.dart:87` </location>
<code_context>
 Color sensorControlIconColor = Colors.grey.shade600;
 List<Color> bmp180ChartColors = [Colors.blue, Colors.green, Colors.red];
 Color chartHintTextColor = Colors.yellow;
+List<Color> apds990ChartColors = [Colors.blue, Colors.yellow];
</code_context>

<issue_to_address>
Typo: Variable name should be apds9960ChartColors.

Rename the variable to apds9960ChartColors to match the sensor name.
</issue_to_address>

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.

await setBit(enable, bitMaskEnableColor, value);
}

Future<void> setProximityInterruptThreshold(List<int> settingArray) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Potential issue: Both threshold values are written to the same register.

Please check if the sensor requires separate registers for each threshold and confirm the correct register mapping with the datasheet.

}

Future<void> setColorIntegrationTime(int value) async {
await i2c.write(address, [256 - value], atime);
Copy link
Contributor

Choose a reason for hiding this comment

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

question (bug_risk): Possible off-by-one error in color integration time calculation.

Verify that the calculation aligns with the sensor's specification for integration time. If the expected range or formula differs, adjust accordingly.

@marcnause
Copy link
Contributor

@Yugesh-Kumar-S I just tested the PR with a sensor and I noticed that I get high numbers for the distance when my hand is close to the sensor and low values, when my hand is moving away from the sensor. I don't really remember if this is how the sensor works or if maybe there is something wrong. Do you know off the top of your head? If not, I will check the data sheet.

@Yugesh-Kumar-S
Copy link
Contributor Author

@Yugesh-Kumar-S I just tested the PR with a sensor and I noticed that I get high numbers for the distance when my hand is close to the sensor and low values, when my hand is moving away from the sensor. I don't really remember if this is how the sensor works or if maybe there is something wrong. Do you know off the top of your head? If not, I will check the data sheet.

Yes sir , this is how it works . We get proximity value from 0 to 255 (255 when closer and decreases as we move away )

@marcnause
Copy link
Contributor

@Yugesh-Kumar-S Could you please have a look at the remarks of the sourcery.ai bot before I approve the PR? I think at least the remark about the variable name (apds990ChartColors instead of apds9960ChartColors) makes sense.

@Yugesh-Kumar-S
Copy link
Contributor Author

@Yugesh-Kumar-S Could you please have a look at the remarks of the sourcery.ai bot before I approve the PR? I think at least the remark about the variable name (apds990ChartColors instead of apds9960ChartColors) makes sense.

Sure sir , i will make the changes required.

@marcnause marcnause enabled auto-merge (squash) August 24, 2025 15:21
@marcnause marcnause merged commit 4339d62 into fossasia:flutter Aug 24, 2025
7 of 8 checks passed
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.

Port the APDS9960 sensor screen.
2 participants