Skip to content

Commit

Permalink
Transitional config packet & background sound option
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehaenger committed Oct 11, 2024
1 parent c2ff588 commit dd050da
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
4 changes: 0 additions & 4 deletions Firmware/LowLevel/include/nv_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@

#define NV_CONFIG_MAX_SAVE_INTERVAL 60000UL // Don't save more often than once a minute

// config_bitmask. Don't mistake with LL_HIGH_LEVEL_CONFIG_BIT. Similar, but not mandatory equal!
#define NV_CONFIG_BIT_DFPIS5V 1 << 0 // DFP is set to 5V

#define NV_RECORD_ID 0x4F4D4331 // Record struct identifier "OMC1" for future flexible length Record.config
#define NV_RECORD_ALIGNMENT (_Alignof(uint32_t)) // Ptr alignment of Record.id for quick in memory access

Expand All @@ -51,7 +48,6 @@ namespace nv_config {
// (a new extension-crc isn't valid with a old Record.config. Thus the new extension values may get set i.e. with default values)
struct Config {
// Config bitmask:
// Bit 0: DFP is 5V (enable full sound). See NV_CONFIG_BIT_DFPIS5V
uint8_t config_bitmask = 0; // Don't mistake with LL_HIGH_LEVEL_CONFIG_BIT. Similar, but not mandatory equal!
uint8_t volume = VOLUME_DEFAULT; // Sound volume (0-100%)
iso639_1 language = {'e', 'n'}; // Default to 'en'
Expand Down
1 change: 1 addition & 0 deletions Firmware/LowLevel/include/soundsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ namespace soundSystem {
void playSoundAdHoc(const TrackDef&); // Play sound track number immediately without waiting until the end of sound

void setDFPis5V(const bool t_dfpis5v); // Set if DFP is set to 5V Vcc
void setEnableBackground(const bool); // Set if background sounds shall get played (true) or not (false)

void setLanguage(const iso639_1 language_p, const bool quiet = false); // Set language to the pointing ISO639-1 (2 char) language code and announce if changed and not quiet

Expand Down
28 changes: 9 additions & 19 deletions Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ uint16_t ui_interval = 1000; // UI send msg (LED/State) interval (

nv_config::Config *nv_cfg; // Non-volatile configuration

// Some vars related to PACKET_ID_LL_HIGH_LEVEL_CONFIG_*
uint8_t config_comms_version = 0; // comms packet version (>0 if implemented)
uint8_t config_bitmask = 0; // See LL_HIGH_LEVEL_CONFIG_BIT_*

void sendMessage(void *message, size_t size);
void sendUIMessage(void *message, size_t size);
void onPacketReceived(const uint8_t *buffer, size_t size);
Expand Down Expand Up @@ -419,7 +415,8 @@ void setup() {
sound_available = soundSystem::begin();
if (sound_available) {
p.neoPixelSetValue(0, 0, 0, 255, true);
soundSystem::setDFPis5V(nv_cfg->config_bitmask & NV_CONFIG_BIT_DFPIS5V);
soundSystem::setDFPis5V(nv_cfg->config_bitmask & LL_HIGH_LEVEL_CONFIG_BIT_DFPIS5V);
soundSystem::setEnableBackground(nv_cfg->config_bitmask & LL_HIGH_LEVEL_CONFIG_BIT_BACKGROUND_SOUNDS);
soundSystem::setLanguage(nv_cfg->language, true);
soundSystem::setVolume(nv_cfg->volume);
// Do NOT play any initial sound now, as we've to handle the special case of
Expand Down Expand Up @@ -565,7 +562,7 @@ void onUIPacketReceived(const uint8_t *buffer, size_t size) {
void sendConfigMessage(uint8_t pkt_type) {
struct ll_high_level_config ll_config;
ll_config.type = pkt_type;
ll_config.config_bitmask = config_bitmask;
ll_config.config_bitmask = nv_cfg->config_bitmask;
ll_config.volume = nv_cfg->volume;
strncpy(ll_config.language, nv_cfg->language, 2);
sendMessage(&ll_config, sizeof(struct ll_high_level_config));
Expand Down Expand Up @@ -605,27 +602,20 @@ void onPacketReceived(const uint8_t *buffer, size_t size) {
} else if (buffer[0] == PACKET_ID_LL_HIGH_LEVEL_STATE && size == sizeof(struct ll_high_level_state)) {
// copy the state
last_high_level_state = *((struct ll_high_level_state *)buffer);
} else if ((buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_REQ || buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_RSP) && size == sizeof(struct ll_high_level_config)) {
// Read and handle received config
} else if (buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_REQ || buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_RSP) {
// Transitional flexible length preparation (see PR #110)
// FIXME: Check Ptr!
struct ll_high_level_config *pkt = (struct ll_high_level_config *)buffer;

config_comms_version = min(pkt->comms_version, LL_HIGH_LEVEL_CONFIG_MAX_COMMS_VERSION); // The lower comms_version is leading
config_bitmask = pkt->config_bitmask; // Take over as sent. HL is leading (for now)

// nv_config.Config specific members ...
// config_bitmask. Do NOT mistake with global config_bitmask (ll_high_level_config.config_bitmask). Similar, but not mandatory the same in future,
// to ensure that a possible instable/flipping future global config_bitmask bit doesn't wear level our flash, we only add those which are known to be stable
// and useful to store.
nv_cfg->config_bitmask = config_bitmask & (LL_HIGH_LEVEL_CONFIG_BIT_DFPIS5V | LL_HIGH_LEVEL_CONFIG_BIT_BACKGROUND_SOUNDS);
nv_cfg->config_bitmask = pkt->config_bitmask;
#ifdef ENABLE_SOUND_MODULE
soundSystem::setDFPis5V(nv_cfg->config_bitmask & NV_CONFIG_BIT_DFPIS5V);
soundSystem::setDFPis5V(nv_cfg->config_bitmask & LL_HIGH_LEVEL_CONFIG_BIT_DFPIS5V);
soundSystem::setEnableBackground(nv_cfg->config_bitmask & LL_HIGH_LEVEL_CONFIG_BIT_BACKGROUND_SOUNDS);
#endif
// Volume
if (pkt->volume >= 0) {
nv_cfg->volume = pkt->volume;
#ifdef ENABLE_SOUND_MODULE
soundSystem::setVolume(nv_cfg->volume);
// TODO: Set sound background option
#endif
}

Expand Down
7 changes: 7 additions & 0 deletions Firmware/LowLevel/src/soundsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace soundSystem
etl::queue<TrackDef, SOUND_QUEUE_SIZE, etl::memory_model::MEMORY_MODEL_SMALL> active_sounds_;
bool sound_available_ = false; // Sound module available as well as SD-Card with some kind of files
bool dfp_is_5v = false; // Enable full sound if DFP is set to 5V Vcc
bool enable_background_ = false; // Enable background sounds
uint8_t volume = VOLUME_DEFAULT; // Last set volume (%)
std::string language_str = "en"; // Default ISO639-1 language string
uint8_t play_folder = 1; // Default play folder, has to be related to language_str
Expand Down Expand Up @@ -181,6 +182,10 @@ namespace soundSystem
dfp_is_5v = t_dfpis5v;
}

void setEnableBackground(const bool t_bool) {
enable_background_ = t_bool;
}

void setLanguage(const iso639_1 language_p, const bool quiet) { // Set language to the pointing ISO639-1 (2 char) language code and announce if changed && not quiet
uint8_t last_play_folder = play_folder;

Expand Down Expand Up @@ -244,6 +249,8 @@ namespace soundSystem
switch (t_track_def.type)
{
case TrackTypes::background:
if(!enable_background_)
return;
myMP3.stop();
delay(50); // (sometimes) required for "MH2024K-24SS"
myMP3.playMp3FolderTrack(t_track_def.num);
Expand Down

0 comments on commit dd050da

Please sign in to comment.