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

codal_port/microbit_soundeffect: Use CODAL-provided constants for fx. #218

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/codal_app/microbithal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@
*/

#include "main.h"
#include "microbithal.h"
#include "MicroBitDevice.h"
#include "neopixel.h"

#define HAL_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

// It's not possible to include the CODAL header file that defines the SFX_DEFAULT_xxx
// constants in C code, because that CODAL header file is C++. Instead we define our
// own MICROBIT_HAL_SFX_DEFAULT_xxx versions of the constants in a C-compatible header
// file, and assert here that they have the same value as the CODAL constants.
static_assert(MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM == SFX_DEFAULT_VIBRATO_PARAM, "");
static_assert(MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS == SFX_DEFAULT_VIBRATO_STEPS, "");
static_assert(MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM == SFX_DEFAULT_TREMOLO_PARAM, "");
static_assert(MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS == SFX_DEFAULT_TREMOLO_STEPS, "");
static_assert(MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM == SFX_DEFAULT_WARBLE_PARAM, "");
static_assert(MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS == SFX_DEFAULT_WARBLE_STEPS, "");

NRF52Pin *const pin_obj[] = {
&uBit.io.P0,
&uBit.io.P1,
Expand Down Expand Up @@ -83,8 +95,6 @@ static uint16_t button_state[2];

extern "C" {

#include "microbithal.h"

void microbit_hal_background_processing(void) {
// This call takes about 200us.
Event(DEVICE_ID_SCHEDULER, DEVICE_SCHEDULER_EVT_IDLE);
Expand Down
8 changes: 8 additions & 0 deletions src/codal_app/microbithal.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ extern "C" {
#define MICROBIT_HAL_LOG_TIMESTAMP_HOURS (36000)
#define MICROBIT_HAL_LOG_TIMESTAMP_DAYS (864000)

// These default fx values are the same as defined by CODAL, but here in a C-compatible header.
#define MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM (2)
#define MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS (512)
#define MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM (3)
#define MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS (900)
#define MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM (2)
#define MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS (700)

void microbit_hal_idle(void);

__attribute__((noreturn)) void microbit_hal_reset(void);
Expand Down
21 changes: 7 additions & 14 deletions src/codal_port/microbit_soundeffect.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "py/runtime.h"
#include "microbithal.h"
#include "modmicrobit.h"
#include "modaudio.h"

Expand Down Expand Up @@ -69,14 +70,6 @@
#define SOUND_EFFECT_FX_VIBRATO (1)
#define SOUND_EFFECT_FX_WARBLE (3)

// These default fx values are the same as used by MakeCode.
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM (2)
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM (3)
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM (2)
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS (512)
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS (900)
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS (700)

#define SOUND_EFFECT_DEFAULT_FREQ_START (500)
#define SOUND_EFFECT_DEFAULT_FREQ_END (2500)
#define SOUND_EFFECT_DEFAULT_DURATION (500)
Expand Down Expand Up @@ -125,15 +118,15 @@ static const soundeffect_attr_t soundeffect_attr_table[] = {
};

static const uint8_t fx_default_param[] = {
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM,
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM,
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM,
[SOUND_EFFECT_FX_VIBRATO] = MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM,
[SOUND_EFFECT_FX_TREMOLO] = MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM,
[SOUND_EFFECT_FX_WARBLE] = MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM,
};

static const uint16_t fx_default_steps[] = {
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS,
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS,
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS,
[SOUND_EFFECT_FX_VIBRATO] = MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS,
[SOUND_EFFECT_FX_TREMOLO] = MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS,
[SOUND_EFFECT_FX_WARBLE] = MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS,
};

const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in) {
Expand Down
Loading