-
-
Notifications
You must be signed in to change notification settings - Fork 89
platform/build_hat: Add support for LEDs. #467
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) 2026 The Pybricks Authors | ||
|
|
||
| #include <pbdrv/config.h> | ||
|
|
||
| #if PBDRV_CONFIG_PWM_PICO | ||
|
|
||
| #include <pbdrv/pwm.h> | ||
| #include <pbdrv/gpio.h> | ||
| #include <pbio/error.h> | ||
|
|
||
| #include "../drv/pwm/pwm.h" | ||
| #include "../drv/pwm/pwm_pico.h" | ||
| #include "../../drv/led/led_pwm.h" | ||
|
|
||
| #include "hardware/gpio.h" | ||
| #include "hardware/pwm.h" | ||
|
|
||
| static pbio_error_t pbdrv_pwm_pico_set_duty(pbdrv_pwm_dev_t *dev, uint32_t ch, uint32_t value) { | ||
| // Blue not available. | ||
| if (ch == PBDRV_LED_PWM_CHANNEL_INVALID) { | ||
| return PBIO_SUCCESS; | ||
| } | ||
|
|
||
| if (ch >= PBDRV_CONFIG_PWM_PICO_NUM_CHANNELS) { | ||
| return PBIO_ERROR_INVALID_ARG; | ||
| } | ||
|
|
||
| const pbdrv_pwm_pico_platform_data_t *pdata = dev->pdata; | ||
| const pbdrv_pwm_pico_channel_t *channel = &pdata->channels[ch]; | ||
|
|
||
| pwm_set_gpio_level(channel->gpio, value); | ||
|
|
||
| return PBIO_SUCCESS; | ||
| } | ||
|
|
||
| static const pbdrv_pwm_driver_funcs_t pbdrv_pwm_pico_funcs = { | ||
| .set_duty = pbdrv_pwm_pico_set_duty, | ||
| }; | ||
|
|
||
| void pbdrv_pwm_pico_init(pbdrv_pwm_dev_t *devs) { | ||
| const pbdrv_pwm_pico_platform_data_t *pdata = &pbdrv_pwm_pico_platform_data; | ||
|
|
||
| devs[pdata->id].funcs = &pbdrv_pwm_pico_funcs; | ||
| devs[pdata->id].pdata = pdata; | ||
|
|
||
| for (int i = 0; i < PBDRV_CONFIG_PWM_PICO_NUM_CHANNELS; i++) { | ||
| const pbdrv_pwm_pico_channel_t *channel = &pdata->channels[i]; | ||
|
|
||
| // Tell the LED pin that the PWM is in charge of its value. | ||
| gpio_set_function(channel->gpio, GPIO_FUNC_PWM); | ||
| // Figure out which slice we just connected to the LED pin | ||
| uint slice_num = pwm_gpio_to_slice_num(channel->gpio); | ||
|
|
||
| // Get some sensible defaults for the slice configuration. By default, | ||
| // the counter is allowed to wrap over its maximum range (0 to 2**16-1). | ||
| pwm_config config = pwm_get_default_config(); | ||
| // Set divider, reduces counter clock to sysclock/this value. | ||
| pwm_config_set_clkdiv(&config, 4.f); | ||
| // Load the configuration into our PWM slice, and set it running. | ||
| pwm_init(slice_num, &config, true); | ||
| } | ||
dlech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #endif // PBDRV_CONFIG_PWM_PICO | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) 2026 The Pybricks Authors | ||
|
|
||
| // Hooks for unit picos. | ||
|
|
||
| #ifndef _INTERNAL_PBDRV_PWM_PICO_H_ | ||
| #define _INTERNAL_PBDRV_PWM_PICO_H_ | ||
|
|
||
| #include <pbdrv/config.h> | ||
|
|
||
| #if PBDRV_CONFIG_PWM_PICO | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include <pbdrv/pwm.h> | ||
|
|
||
| /** Platform-specific device information. */ | ||
|
|
||
| typedef struct { | ||
| uint8_t gpio; | ||
| } pbdrv_pwm_pico_channel_t; | ||
|
|
||
| typedef struct { | ||
| uint8_t id; | ||
| pbdrv_pwm_pico_channel_t channels[PBDRV_CONFIG_PWM_PICO_NUM_CHANNELS]; | ||
| } pbdrv_pwm_pico_platform_data_t; | ||
|
|
||
| // Defined in platform.c | ||
| extern const pbdrv_pwm_pico_platform_data_t pbdrv_pwm_pico_platform_data; | ||
|
|
||
| void pbdrv_pwm_pico_init(pbdrv_pwm_dev_t *devs); | ||
|
|
||
| #else // PBDRV_CONFIG_PWM_PICO | ||
|
|
||
| #define pbdrv_pwm_pico_init(dev) | ||
|
|
||
| #endif // PBDRV_CONFIG_PWM_PICO | ||
|
|
||
| #endif // _INTERNAL_PBDRV_PWM_PICO_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.