<commit_before>/**
 * @file    src/mod_musicbox.cpp
 * @brief
 *
 * @addtogroup
 * @{
 */

#include "mod_musicbox.h"

#if MOD_MUSICBOX

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ch_tools.h"
#include "chprintf.h"

#include "qhal.h"
#include "module_init_cpp.h"

#include "ff.h"
#include "minIni.h"

#include "board_buttons.h"
#include "mod_rfid.h"
#include "mod_cardreader.h"
#include "mod_player.h"
#include "mod_effects.h"

#define EVENTMASK_RFID EVENT_MASK(0)
#define EVENTMASK_BTN_PLAY EVENT_MASK(1)
#define EVENTMASK_BTN_NEXT EVENT_MASK(2)
#define EVENTMASK_BTN_PREV EVENT_MASK(3)
#define EVENTMASK_BTN_VOLUP EVENT_MASK(4)
#define EVENTMASK_BTN_VOLDOWN EVENT_MASK(5)
#define EVENTMASK_CARDREADER EVENT_MASK(6)
#define EVENTMASK_PLAYER EVENT_MASK(6)

namespace tmb_musicplayer {

template <>
ModuleMusicbox ModuleMusicboxSingelton::instance = tmb_musicplayer::ModuleMusicbox();

ModuleMusicbox::ModuleMusicbox() {
    buttons[0].button = &BoardButtons::BtnPlay;
    buttons[Play].handler = &ModuleMusicbox::OnPlayButton;
    buttons[Play].evtMask = EVENTMASK_BTN_PLAY;

    buttons[1].button = &BoardButtons::BtnNext;
    buttons[Next].handler = &ModuleMusicbox::OnNextButton;
    buttons[Next].evtMask = EVENTMASK_BTN_NEXT;

    buttons[Prev].button = &BoardButtons::BtnPrev;
    buttons[Prev].handler = &ModuleMusicbox::OnPrevButton;
    buttons[Prev].evtMask = EVENTMASK_BTN_PREV;

    buttons[VolUp].button = &BoardButtons::BtnVolUp;
    buttons[VolUp].handler = &ModuleMusicbox::OnVolUpButton;
    buttons[VolUp].evtMask = EVENTMASK_BTN_VOLUP;

    buttons[VolDown].button = &BoardButtons::BtnVolDown;
    buttons[VolDown].handler = &ModuleMusicbox::OnVolDownButton;
    buttons[VolDown].evtMask = EVENTMASK_BTN_VOLDOWN;
}

ModuleMusicbox::~ModuleMusicbox() {
}

void ModuleMusicbox::Init() {
    m_modRFID = ModuleRFIDSingelton::GetInstance();
    m_modCardreader = ModuleCardreaderSingelton::GetInstance();
    m_modPlayer = ModulePlayerSingelton::GetInstance();
    m_modEffects = ModuleEffectsSingelton::GetInstance();
}

void ModuleMusicbox::Start() {
    BaseClass::Start();
}

void ModuleMusicbox::Shutdown() {
    BaseClass::Shutdown();
    m_modRFID = NULL;
    m_modCardreader = NULL;
    m_modPlayer = NULL;
}

void ModuleMusicbox::ThreadMain() {
    chRegSetThreadName("musicbox");

    chibios_rt::EvtListener rfidEvtListener;
    chibios_rt::EvtListener cardreaderEvtListener;
    chibios_rt::EvtListener playerEvtListener;

    if (m_modRFID != NULL)
    {
        m_modRFID->RegisterListener(&rfidEvtListener, EVENTMASK_RFID);
    }

    if (m_modCardreader != NULL)
    {
        m_modCardreader->RegisterListener(&cardreaderEvtListener, EVENTMASK_CARDREADER);
    }

    if (m_modPlayer != NULL)
    {
        m_modPlayer->RegisterListener(&playerEvtListener, EVENTMASK_PLAYER);
    }

    RegisterButtonEvents();

    /*
     * Set external output to notify ready state
     */

    SetReadyOutput(true);

    while (!chThdShouldTerminateX())
    {
        eventmask_t evt = chEvtWaitAny(ALL_EVENTS);
        if (evt & EVENTMASK_RFID)
        {
            eventflags_t flags = rfidEvtListener.getAndClearFlags();
            OnRFIDEvent(flags);
        }

        if (evt & EVENTMASK_CARDREADER)
        {
            eventflags_t flags = cardreaderEvtListener.getAndClearFlags();
            OnCardReaderEvent(flags);
        }

        if (evt & EVENTMASK_PLAYER)
        {
            eventflags_t flags = playerEvtListener.getAndClearFlags();
            OnPlayerEvent(flags);
        }

        /* process buttons */
        int i;
        for (i = 0; i < 5; i++)
        {
            ButtonData& btnData = buttons[i];
            if (evt & btnData.evtMask)
            {
                eventflags_t flags = btnData.evtListener.getAndClearFlags();
                (this->*btnData.handler)(btnData.button, flags);
            }
        }
    }

    UnregisterButtonEvents();

    if (m_modPlayer != NULL)
    {
        m_modPlayer->UnregisterListener(&playerEvtListener);
    }

    if (m_modCardreader != NULL)
    {
        m_modCardreader->UnregisterListener(&cardreaderEvtListener);
    }

    if (m_modRFID != NULL)
    {
        m_modRFID->UnregisterListener(&rfidEvtListener);
    }
}

void ModuleMusicbox::RegisterButtonEvents()
{
    for (int i = 0; i < ButtonTypeCount; i++)
    {
        ButtonData& btnData = buttons[i];
        btnData.button->RegisterListener(&btnData.evtListener, btnData.evtMask);
    }
}

void ModuleMusicbox::UnregisterButtonEvents()<commit_msg>mod_musicbox: switch external output 2 1 sec after restart<commit_after>/**
 * @file    src/mod_musicbox.cpp
 * @brief
 *
 * @addtogroup
 * @{
 */

#include "mod_musicbox.h"

#if MOD_MUSICBOX

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ch_tools.h"
#include "chprintf.h"

#include "qhal.h"
#include "module_init_cpp.h"

#include "ff.h"
#include "minIni.h"

#include "board_buttons.h"
#include "mod_rfid.h"
#include "mod_cardreader.h"
#include "mod_player.h"
#include "mod_effects.h"

#define EVENTMASK_RFID EVENT_MASK(0)
#define EVENTMASK_BTN_PLAY EVENT_MASK(1)
#define EVENTMASK_BTN_NEXT EVENT_MASK(2)
#define EVENTMASK_BTN_PREV EVENT_MASK(3)
#define EVENTMASK_BTN_VOLUP EVENT_MASK(4)
#define EVENTMASK_BTN_VOLDOWN EVENT_MASK(5)
#define EVENTMASK_CARDREADER EVENT_MASK(6)
#define EVENTMASK_PLAYER EVENT_MASK(6)

namespace tmb_musicplayer {

template <>
ModuleMusicbox ModuleMusicboxSingelton::instance = tmb_musicplayer::ModuleMusicbox();

ModuleMusicbox::ModuleMusicbox() {
    buttons[0].button = &BoardButtons::BtnPlay;
    buttons[Play].handler = &ModuleMusicbox::OnPlayButton;
    buttons[Play].evtMask = EVENTMASK_BTN_PLAY;

    buttons[1].button = &BoardButtons::BtnNext;
    buttons[Next].handler = &ModuleMusicbox::OnNextButton;
    buttons[Next].evtMask = EVENTMASK_BTN_NEXT;

    buttons[Prev].button = &BoardButtons::BtnPrev;
    buttons[Prev].handler = &ModuleMusicbox::OnPrevButton;
    buttons[Prev].evtMask = EVENTMASK_BTN_PREV;

    buttons[VolUp].button = &BoardButtons::BtnVolUp;
    buttons[VolUp].handler = &ModuleMusicbox::OnVolUpButton;
    buttons[VolUp].evtMask = EVENTMASK_BTN_VOLUP;

    buttons[VolDown].button = &BoardButtons::BtnVolDown;
    buttons[VolDown].handler = &ModuleMusicbox::OnVolDownButton;
    buttons[VolDown].evtMask = EVENTMASK_BTN_VOLDOWN;
}

ModuleMusicbox::~ModuleMusicbox() {
}

void ModuleMusicbox::Init() {
    m_modRFID = ModuleRFIDSingelton::GetInstance();
    m_modCardreader = ModuleCardreaderSingelton::GetInstance();
    m_modPlayer = ModulePlayerSingelton::GetInstance();
    m_modEffects = ModuleEffectsSingelton::GetInstance();
}

void ModuleMusicbox::Start() {
    BaseClass::Start();
}

void ModuleMusicbox::Shutdown() {
    BaseClass::Shutdown();
    m_modRFID = NULL;
    m_modCardreader = NULL;
    m_modPlayer = NULL;
}

void ModuleMusicbox::ThreadMain() {
    chRegSetThreadName("musicbox");

    chibios_rt::EvtListener rfidEvtListener;
    chibios_rt::EvtListener cardreaderEvtListener;
    chibios_rt::EvtListener playerEvtListener;

    if (m_modRFID != NULL)
    {
        m_modRFID->RegisterListener(&rfidEvtListener, EVENTMASK_RFID);
    }

    if (m_modCardreader != NULL)
    {
        m_modCardreader->RegisterListener(&cardreaderEvtListener, EVENTMASK_CARDREADER);
    }

    if (m_modPlayer != NULL)
    {
        m_modPlayer->RegisterListener(&playerEvtListener, EVENTMASK_PLAYER);
    }

    RegisterButtonEvents();

    /*
     * Set external output to notify ready state
     */
    bool setReadyOutput = true;
    uint8_t readyCounter = 0;

    while (!chThdShouldTerminateX())
    {
        eventmask_t evt = chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(500));
        if (evt & EVENTMASK_RFID)
        {
            eventflags_t flags = rfidEvtListener.getAndClearFlags();
            OnRFIDEvent(flags);
        }

        if (evt & EVENTMASK_CARDREADER)
        {
            eventflags_t flags = cardreaderEvtListener.getAndClearFlags();
            OnCardReaderEvent(flags);
        }

        if (evt & EVENTMASK_PLAYER)
        {
            eventflags_t flags = playerEvtListener.getAndClearFlags();
            OnPlayerEvent(flags);
        }

        /* process buttons */
        int i;
        for (i = 0; i < 5; i++)
        {
            ButtonData& btnData = buttons[i];
            if (evt & btnData.evtMask)
            {
                eventflags_t flags = btnData.evtListener.getAndClearFlags();
                (this->*btnData.handler)(btnData.button, flags);
            }
        }

        /*
         * Delay for external output is 2 * 500ms
         */
        if (setReadyOutput && (++readyCounter > 2))
        {
            setReadyOutput = false;
            SetReadyOutput(true);
        }
    }

    UnregisterButtonEvents();

    if (m_modPlayer != NULL)
    {
        m_modPlayer->UnregisterListener(&playerEvtListener);
    }

    if (m_modCardreader != NULL)
    {
        m_modCardreader->UnregisterListener(&cardreaderEvtListener);
    }

    if (m_modRFID != NULL)
    {
        m_modRFID->UnregisterListener(&rfidEvtListener);
    }
}

void ModuleMusicbox::RegisterButtonEvents()
{
    for (int i = 0; i < ButtonTypeCount; i++)
    {
        ButtonData& btnData = buttons[i];
        btnData.button->RegisterListener(&btnData.evtListener, btnData.evtMask);
    }
}

void ModuleMusicbox::UnregisterButtonEvents()
{'fraction_non_alphanumeric': 0.11543781253324822, 'fraction_numerical': 0.004149377593360996, 'mean_word_length': 3.4339622641509435, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '120504', 'n_tokens_mistral': 3157, 'n_tokens_neox': 3123, 'n_words': 487}