Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_contrast() and get_brightness() to SSD1306 class to get protected variables #6435

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

benediktkr
Copy link
Contributor

@benediktkr benediktkr commented Mar 26, 2024

What does this implement/fix?

This adds get_contrast() and get_brightness() public methods to the SSD1306 class in the C++ API. They can then be used in lambdas or components to read the protected class variables contrast_ and brightness_, in order to represent the current brightness/contrast values without trying to keep track of the state yourself.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): Not applicable

Pull request in esphome-docs with documentation (if applicable): Not applicable

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

display: 
 - platform: ssd1306_i2c
   id: ssd1306
   model: "SSD1306 128x64"
   contrast: "33%"
   lambda: |-
      float contrast = id(ssd1306).get_contrast();
      it.printf(0, 0, id(font), TextAlign::BASELINE_LEFT, "%3.0f %%", contrast*100);

number:
  - platform: template
    id: "ssd1306_contrast"
    name: "SSD1306 contrast"
    min_value: 0
    max_value: 100
    step: 0
    entity_category: ""
    unit_of_measurement: "%"
    device_class: power_factor
    mode: slider
    lambda: |-
      float contrast = id(ssd1306).get_contrast();
      return int(contrast*100);
    set_action: 
      - lambda: |-
          id(ssd1306).set_contrast(x/100);

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.43%. Comparing base (4d8b5ed) to head (2fc03dc).
Report is 255 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6435      +/-   ##
==========================================
- Coverage   53.70%   53.43%   -0.28%     
==========================================
  Files          50       50              
  Lines        9408     9529     +121     
  Branches     1654     1684      +30     
==========================================
+ Hits         5053     5092      +39     
- Misses       4056     4128      +72     
- Partials      299      309      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@benediktkr
Copy link
Contributor Author

Please correct me if I'm wrong, but I believe this C++ component does not have any tests.

I did find some tests under tests/components/ektf2232/ but those are all YAML configs and don't use lambdas to call the SSD1306 class.

@benediktkr benediktkr marked this pull request as ready for review March 26, 2024 19:46
@benediktkr benediktkr changed the title add get_contrast() and get_brightness() to SSD1306 class to get protected variables Add get_contrast() and get_brightness() to SSD1306 class to get protected variables Mar 26, 2024
@jesserockz
Copy link
Member

You can add new tests for this platform if you want, but I am not going to enforce it for this PR.

@jesserockz jesserockz merged commit 58de8a4 into esphome:dev Mar 27, 2024
55 checks passed
@benediktkr
Copy link
Contributor Author

Can you point me to any useful example tests?

@benediktkr benediktkr deleted the ssd1306-get-contrast branch March 27, 2024 12:40
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants