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

mpr121: Add GPIO support #6776

Merged
merged 9 commits into from
May 23, 2024
Merged

mpr121: Add GPIO support #6776

merged 9 commits into from
May 23, 2024

Conversation

polyfloyd
Copy link
Contributor

@polyfloyd polyfloyd commented May 19, 2024

What does this implement/fix?

The MPR121 touch controller also supports alternate modes of operation as GPIO controller on the upper 8 of its 12 input channels.

This patch makes it possible to configure such channels as GPIO.

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): fixes esphome/feature-requests/issues/1838

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#3845

Test Environment

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

Example entry for config.yaml:

# Example config.yaml

i2c:
  sda: 26
  scl: 27

mpr121:
  id: mpr121_component
  address: 0x5A
  touch_debounce: 1
  release_debounce: 1
  touch_threshold: 10
  release_threshold: 7

binary_sensor:
  - platform: mpr121
    id: touch_LEFT
    channel: 7
    name: "Touch Left"

output:
  - id: ext_5v_power
    platform: gpio
    pin:
      mpr121_id: mpr121_component
      number: 10
      mode: OUTPUT
      inverted: true

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 May 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.09%. Comparing base (4d8b5ed) to head (4b16f56).
Report is 655 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6776      +/-   ##
==========================================
+ Coverage   53.70%   54.09%   +0.38%     
==========================================
  Files          50       50              
  Lines        9408     9627     +219     
  Branches     1654     1701      +47     
==========================================
+ Hits         5053     5208     +155     
- Misses       4056     4093      +37     
- Partials      299      326      +27     

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

@polyfloyd polyfloyd marked this pull request as ready for review May 19, 2024 11:27
@polyfloyd
Copy link
Contributor Author

Alright! This is ready for a review :)

Add validation to exclude using conflicting channels
@jesserockz
Copy link
Member

@polyfloyd I took it upon myself to use this platform as a testbed to do some crazy validation =)

Now you cannot use the wrong channel as a pin while there is a higher binary sensor channel being used.

I cannot test it on device, but compilation tests are working locally.

Let me know how it goes =)

Jesse

@polyfloyd
Copy link
Contributor Author

Nope! It panics:

[11:41:28]
[11:41:28]Core  1 register dump:
[11:41:28]PC      : 0x400d6b34  PS      : 0x00060030  A0      : 0x800d6b95  A1      : 0x3ffb25f0
WARNING Decoded 0x400d6b34: esphome::mpr121::MPR121BinarySensor::setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/components/mpr121/binary_sensor/mpr121_binary_sensor.cpp:7
[11:41:28]A2      : 0x3ffb34f4  A3      : 0x0000005e  A4      : 0x3ffb261c  A5      : 0x00000001
[11:41:28]A6      : 0x00000001  A7      : 0x00000001  A8      : 0x8017eb76  A9      : 0x00000000
[11:41:28]A10     : 0x00000000  A11     : 0x00000002  A12     : 0x3ffb25f0  A13     : 0x00000002
[11:41:28]A14     : 0x00000001  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x0000001c
[11:41:28]EXCVADDR: 0x00000025  LBEG    : 0x4008976c  LEND    : 0x40089782  LCOUNT  : 0xffffffff
[11:41:28]
[11:41:28]
[11:41:28]Backtrace:0x400d6b31:0x3ffb25f00x400d6b92:0x3ffb2620 0x400d6ee5:0x3ffb2640 0x4017f629:0x3ffb2660 0x4017f6cd:0x3ffb2680 0x400e1438:0x3ffb26a0 0x400e3f79:0x3ffb26d0 0x400ea052:0x3ffb2820
WARNING Found stack trace! Trying to decode it
WARNING Decoded 0x400d6b31: unsigned char esphome::optional<unsigned char>::value_or<unsigned char>(unsigned char const&) const at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/core/optional.h:93
 (inlined by) esphome::mpr121::MPR121BinarySensor::setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/components/mpr121/binary_sensor/mpr121_binary_sensor.cpp:7
WARNING Decoded 0x400d6b92: non-virtual thunk to esphome::mpr121::MPR121BinarySensor::setup()
WARNING Decoded 0x400d6ee5: esphome::mpr121::MPR121Component::setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/components/mpr121/mpr121.cpp:26 (discriminator 2)
WARNING Decoded 0x4017f629: esphome::Component::call_setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/core/component.cpp:78
WARNING Decoded 0x4017f6cd: esphome::Component::call() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/core/component.cpp:104
WARNING Decoded 0x400e1438: esphome::Application::setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/src/esphome/core/application.cpp:38
WARNING Decoded 0x400e3f79: setup() at /home/polyfloyd/Projects/esphome-config/.esphome/build/geiger/geiger.yaml:301
WARNING Decoded 0x400ea052: loopTask(void*) at /home/polyfloyd/.platformio/packages/framework-arduinoespressif32@3.20005.220925/cores/esp32/main.cpp:42
[11:41:28]
[11:41:28]
[11:41:28]
[11:41:28]
[11:41:28]ELF file SHA256: 0000000000000000
[11:41:28]
[11:41:28]Rebooting...
[11:41:28]ets Jun  8 2016 00:22:57
[11:41:28]
[11:41:28]rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
[11:41:28]configsip: 0, SPIWP:0xee
[11:41:28]clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
[11:41:28]mode:DIO, clock div:2
[11:41:28]load:0x3fff0030,len:1184
[11:41:28]load:0x40078000,len:13132
[11:41:28]load:0x40080400,len:3036
[11:41:28]entry 0x400805e4

The pin conflict validation at compile time is really nice though :)

@polyfloyd
Copy link
Contributor Author

Also, the mpr121_id field of the gpio output config has changed to mpr121. Was this intentional? Having such references be prefixed by _id seems the common pattern esphome, e.g. uart_id

@polyfloyd
Copy link
Contributor Author

Fixed it :P

Both touch and GPIO seems to be working well!

@jesserockz
Copy link
Member

Ah woops, Yeah I guess I forgot that part. Although the set_parent function is already provided by the Parented class

@jesserockz
Copy link
Member

Also, the mpr121_id field of the gpio output config has changed to mpr121. Was this intentional? Having such references be prefixed by _id seems the common pattern esphome, e.g. uart_id

For component platforms yes, for gpio pins its just the name of the component without _id suffix

@polyfloyd
Copy link
Contributor Author

Ah! Good to know

@polyfloyd
Copy link
Contributor Author

I have updated the corresponding documentation

@jesserockz jesserockz merged commit c2d6765 into esphome:dev May 23, 2024
56 checks passed
@polyfloyd
Copy link
Contributor Author

Thanks!

@polyfloyd polyfloyd deleted the mpr-121-gpio branch May 23, 2024 21:13
@github-actions github-actions bot locked and limited conversation to collaborators May 25, 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.

Support for IO Pins on MPR121
3 participants