PWM Melodies¶
A landed rocket needs a way to communicate its position to the searching squad. One simple way of making it easier to find is to play loud buzzer sounds in an endless loop until the recovery team finds it.
But only playing loud noises is boring and annoying in testing. Instead, use the PWM Melody API to play music of your liking:
/ {
buzzer0: buzzer_0 {
compatible = "auxspaceev,pwm-buzzer";
pwms = <&ledc0 0 PWM_MSEC(200) PWM_POLARITY_NORMAL>;
};
};
#include <aurora/lib/pwm_melody.h>
// dt node that contains a "pwms" child node
static const struct pwm_dt_spec buzzer =
PWM_DT_SPEC_GET(DT_NODELABEL(buzzer0));
// play astronomia from aurora/lib/pwm_melody.h
PWM_MELODY_CTX_DEFINE(melody_ctx, &buzzer, astronomia, 1024);
int main()
{
pwm_melody_start(&melody_ctx);
// --snip--
// play as long as needed
// --snip
pwm_melody_stop(&melody_ctx);
}
API Reference¶
-
static const struct pwm_melody_note astronomia[] = {{466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {466, 4}, {587, 4}, {587, 4}, {587, 4}, {587, 4}, {523, 4}, {523, 4}, {523, 4}, {523, 4}, {698, 4}, {698, 4}, {698, 4}, {698, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {784, 4}, {523, 4}, {466, 4}, {440, 4}, {349, 4}, {392, 4}, {0, 4}, {392, 4}, {587, 4}, {523, 4}, {0, 4}, {466, 4}, {0, 4}, {440, 4}, {0, 4}, {440, 4}, {440, 4}, {523, 4}, {0, 4}, {466, 4}, {440, 4}, {392, 4}, {0, 4}, {392, 4}, {932, 4}, {880, 4}, {932, 4}, {880, 4}, {932, 4}, {392, 4}, {0, 4}, {392, 4}, {932, 4}, {880, 4}, {932, 4}, {880, 4}, {932, 4}, {392, 4}, {0, 4}, {392, 4}, {587, 4}, {523, 4}, {0, 4}, {466, 4}, {0, 4}, {440, 4}, {0, 4}, {440, 4}, {440, 4}, {523, 4}, {0, 4}, {466, 4}, {440, 4}, {392, 4}, {0, 4}, {392, 4}, {932, 4}, {880, 4}, {932, 4}, {880, 4}, {932, 4}, {392, 4}, {0, 4}, {392, 4}, {932, 4}, {880, 4}, {932, 4}, {880, 4}, {932, 4},}¶
Astronomia (Coffin Dance) melody. Ideal for post-flight celebration.
-
int pwm_melody_start(struct pwm_melody_ctx *ctx)¶
Start playing a melody.
Spawns a thread that iterates over the notes in
ctxand drives the PWM output accordingly.- Parameters:
ctx – Melody player context (must have been initialised, e.g. via PWM_MELODY_CTX_DEFINE).
- Returns:
0 on success, or a negative error code.
-
void pwm_melody_stop(struct pwm_melody_ctx *ctx)¶
Stop a melody that is currently playing.
Signals the playback thread to stop and waits for it to terminate. The PWM output is turned off before returning.
- Parameters:
ctx – Melody player context.
-
PWM_MELODY_CTX_DEFINE(_name, _pwm, _notes, _stack_size)¶
Statically define and initialize a melody player context.
This macro allocates the thread stack and initialises the context struct.
- Parameters:
_name – Variable name for the context.
_pwm – Pointer to a
pwm_dt_specfor the buzzer output._notes – Array of pwm_melody_note to play.
_stack_size – Stack size in bytes for the playback thread.
-
struct pwm_melody_note¶
- #include <pwm_melody.h>
A single note in a melody.
-
struct pwm_melody_ctx¶
- #include <pwm_melody.h>
Runtime context for a melody player instance.