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

Upgraded library to support most features of WM8960 I2C protocol #2

Merged
merged 56 commits into from
Sep 9, 2024

Conversation

dcooperdalrymple
Copy link
Contributor

I was eager to use SparkFun's WM8960 breakout board with a CircuitPython project, and found this unfinished library to provide a starting point to communicate with this device but otherwise lacking.

To ameliorate this problem, I've ported over the Arduino library and examples provided by SparkFun for this board (https://github.com/sparkfun/SparkFun_WM8960_Arduino_Library) with a few minor modifications:

  • setSampleRate to automatically configure PLL, system clock, etc for most supported frequencies (8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000)
  • quick configuration methods of key features: configureI2S, configureDAC, configureHeadphones, configureSpeakers
  • Register reset and general setup on object initialization
  • Stereo paired methods for most Left/Right methods to simplify operation
  • setPPLK uses a single integer value rather than 3 separate bytes

All examples have been tested with an RP2040 and WM8960 breakout module successfully. That is except for those involving I2S input (examples 08, 09, 11, and 15) because I2SIn/I2SInOut is not yet a feature of CircuitPython (as of 9.1.1; more info: adafruit/circuitpython#2676).

I look forward to seeing this library added to the community bundle after review (https://github.com/adafruit/CircuitPython_Community_Bundle) as I think this is a very powerful codec with a large feature set for the price and is already being used in 1 Adafruit product, https://www.adafruit.com/product/4757.

I did my best to give attribution to Pete Lewis of SparkFun, the original author of the Arduino library, under the MIT license, but there may be room for improvement.

Thank you for your consideration!

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! It seems like this was autoconverted from the Arduino library. That's fine except you now need to curate and refine the API for Python.

Generally we don't want to expose everything. Instead, we only want the APIs we need for the examples we want to show. Your extended examples are a great place to start for this.

I've pointed out a couple things and linked to the dev guide. Please read the dev guide over and refine the API to match that.

Thanks!

CODE_OF_CONDUCT.md Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
adafruit_wm8960.py Outdated Show resolved Hide resolved
adafruit_wm8960.py Outdated Show resolved Hide resolved
adafruit_wm8960.py Outdated Show resolved Hide resolved
@dcooperdalrymple
Copy link
Contributor Author

dcooperdalrymple commented Aug 15, 2024

Hey Scott! Thanks for looking over this library. I think all of your suggestions so far are on the right track and I will be following your updates shortly.

The only note I'll make is that I did manually convert this library (with extensive use of find and replace, of course), which is why there might be some mistakes here and there. Though I think there is some benefit to maintaining a some compatibility with the original library, I also think it's a great idea to implement the CircuitPython methodology where possible.

However, if a tool does exist to automatically convert Arduino libraries to be compatible with CircuitPython, that would be really great!

@tannewt
Copy link
Member

tannewt commented Aug 15, 2024

However, if a tool does exist to automatically convert Arduino libraries to be compatible with CircuitPython, that would be really great!

An LLM like ChatGPT or GitHub Copilot can do this conversion pretty well. Give it the source arduino and ask it to generate python. It may even be able to convert to properties. You'll still need to check and refine it but it makes the initial conversion much quicker.

@dcooperdalrymple
Copy link
Contributor Author

dcooperdalrymple commented Aug 22, 2024

I've gotten around to revisiting the examples provided and narrowed them down to only 4 which I felt demonstrated unique features. If I2SInOut ever reaches the light in CircuitPython, I'll make sure to add an example which features ADC to MCU functionality. https://adafruit-circuitpython-wm8960.readthedocs.io/projects/dev/en/latest/examples.html

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Examples look really good. I like how readable the human friendly names are.

One comment about formatting and it looks like you are still working on adding doc strings.

adafruit_wm8960.py Outdated Show resolved Hide resolved
…it_wm8960.WM8960`, and advanced driver class, `adafruit_wm8960.advanced.WM8960_Advanced`. Documentation completed.
@dcooperdalrymple
Copy link
Contributor Author

@tannewt Here's the updated library. All the documentation has been populated as far as I'm aware. It may get a bit lackadaisical here and there, but I'll leave it up to your scrutiny.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Looks like pre-commit is unhappy. One missing space. Otherwise the comments look really good! This will be a very helpful driver for folks using this chip. Thanks!

adafruit_wm8960/__init__.py Outdated Show resolved Hide resolved
@tannewt
Copy link
Member

tannewt commented Sep 6, 2024

I think you need to change this to fix the build issue:

[tool.setuptools]
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
# CHANGE `py_modules = ['...']` TO `packages = ['...']`
py-modules = ["adafruit_wm8960"]

@dcooperdalrymple
Copy link
Contributor Author

I think that finally does it! Let me know if there's anything else I need to check up on.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

All good! Thanks for iterating on this with me.

@tannewt tannewt merged commit bba4f61 into adafruit:main Sep 9, 2024
1 check passed
@dcooperdalrymple
Copy link
Contributor Author

@tannewt Likewise! I'm excited to start using this in my next project.

@ladyada
Copy link
Member

ladyada commented Sep 11, 2024

awesome, thank you @dcooperdalrymple :)
@TheKitty newsy

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