<commit_before>/*
    OpenDeck MIDI platform firmware
    Copyright (C) 2015-2018 <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "LEDs.h"
#include "board/Board.h"

volatile uint8_t    pwmSteps;
volatile int8_t     transitionCounter[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding current LED status for all LEDs.
///
uint8_t             ledState[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding time after which LEDs should blink.
///
uint8_t             blinkTimer[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding RGB enable state for all LEDs.
///
uint8_t             rgbLEDenabled[MAX_NUMBER_OF_RGB_LEDS/8+1];

///
/// \brief Array holding control channel for all LEDs.
///
uint8_t             ledControlChannel[MAX_NUMBER_OF_LEDS];

///
/// \brief Holds currently active LED blink type.
///
blinkType_t         ledBlinkType;

///
/// \brief Pointer to array used to check if blinking LEDs should toggle state.
///
const uint8_t*      blinkResetArrayPtr;

///
/// \brief Array holding MIDI clock pulses after which LED state is toggled for all possible blink rates.
///
const uint8_t       blinkReset_midiClock[BLINK_SPEEDS] =
{
    255, //no blinking
    2,
    3,
    4,
    6,
    9,
    12,
    18,
    24,
    36,
    48
};

///
/// \brief Array holding time indexes (multipled by 100) after which LED state is toggled for all possible blink rates.
///
const uint8_t       blinkReset_timer[BLINK_SPEEDS] =
{
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10
};


///
/// \brief Default constructor.
///
LEDs::LEDs()
{

}

void LEDs::init(bool startUp)
{
    if (startUp)
    {
        if (database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_startUpRoutineState))
        {
            //set to slowest fading speed for effect
            #ifdef LED_FADING_SUPPORTED
            setFadeTime(1);
            #endif

            if (board.startUpAnimation != NULL)
                board.startUpAnimation();
            else
                startUpAnimation();
        }

        #ifdef LED_FADING_SUPPORTED
        setFadeTime(database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_fadeSpeed));
        #endif
    }

    setBlinkType((blinkType_t)database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_blinkMIDIclock));

    //store some parameters from eeprom to ram for faster access
    for (int i=0; i<MAX_NUMBER_OF_LEDS; i++)
        ledControlChannel[i] = database.read(DB_BLOCK_LEDS, dbSection_leds_midiChannel, i);

    for (int i=0; i<MAX_NUMBER_OF_RGB_LEDS; i++)
    {
        uint8_t arrayIndex = i/8;
        uint8_t ledIndex = i - 8*arrayIndex;

        BIT_WRITE(rgbLEDenabled[arrayIndex], ledIndex, (bool)database.read(DB_BLOCK_LEDS, dbSection_leds_rgbEnable, i));
    }
}

///
/// \brief Checks if RGB led is enabled.
/// @param [in] ledID    LED index which is being checked.
/// \returns True if RGB LED is enabled.
///
inline bool isRGBLEDenabled(uint8_t ledID)
{
    uint8_t arrayIndex = ledID/8;
    uint8_t ledIndex = ledID - 8*arrayIndex;

    return BIT_READ(rgbLEDenabled[arrayIndex], ledIndex);
}

void LEDs::update()
{
    //holds last time in miliseconds when LED blinking has been updated
    static uint32_t lastLEDblinkUpdateTime = 0;

    //holds blink state for each blink speed so that leds are in sync
    static bool blinkState[BLINK_SPEEDS] = { false };

    //array used to determine when the blink state for specific blink rate should be changed
    static uint8_t blinkCounter[BLINK_SPEEDS] = { 0 };

    switch(ledBlinkType)
    {
        case blinkType_timer:
        //update blink states every 100ms - minimum blink time
        if ((rTimeMs() - lastLEDblinkUpdateTime) < 100)
            return;

        lastLEDblinkUpdateTime = rTimeMs();
        break;

        case blinkType_midiClock:
        if ((midiClockStatus == midiClockNoChange) || (midiClockStatus == midiClockStop))
            return;

        if (midiClockStatus == midiClockReset)
        {
            //reset all counters in this case
            //also make sure all leds are in sync again
            for (int i=0; i<BLINK_SPEEDS; i++)
            {
                blinkCounter[i] = 0;
                blinkState[i] = false;
            }
        }
<commit_msg>correct led blinking state (initial)<commit_after>/*
    OpenDeck MIDI platform firmware
    Copyright (C) 2015-2018 <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "LEDs.h"
#include "board/Board.h"

volatile uint8_t    pwmSteps;
volatile int8_t     transitionCounter[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding current LED status for all LEDs.
///
uint8_t             ledState[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding time after which LEDs should blink.
///
uint8_t             blinkTimer[MAX_NUMBER_OF_LEDS];

///
/// \brief Array holding RGB enable state for all LEDs.
///
uint8_t             rgbLEDenabled[MAX_NUMBER_OF_RGB_LEDS/8+1];

///
/// \brief Array holding control channel for all LEDs.
///
uint8_t             ledControlChannel[MAX_NUMBER_OF_LEDS];

///
/// \brief Holds currently active LED blink type.
///
blinkType_t         ledBlinkType;

///
/// \brief Pointer to array used to check if blinking LEDs should toggle state.
///
const uint8_t*      blinkResetArrayPtr;

///
/// \brief Array holding MIDI clock pulses after which LED state is toggled for all possible blink rates.
///
const uint8_t       blinkReset_midiClock[BLINK_SPEEDS] =
{
    255, //no blinking
    2,
    3,
    4,
    6,
    9,
    12,
    18,
    24,
    36,
    48
};

///
/// \brief Array holding time indexes (multipled by 100) after which LED state is toggled for all possible blink rates.
///
const uint8_t       blinkReset_timer[BLINK_SPEEDS] =
{
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10
};


///
/// \brief Default constructor.
///
LEDs::LEDs()
{

}

void LEDs::init(bool startUp)
{
    if (startUp)
    {
        if (database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_startUpRoutineState))
        {
            //set to slowest fading speed for effect
            #ifdef LED_FADING_SUPPORTED
            setFadeTime(1);
            #endif

            if (board.startUpAnimation != NULL)
                board.startUpAnimation();
            else
                startUpAnimation();
        }

        #ifdef LED_FADING_SUPPORTED
        setFadeTime(database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_fadeSpeed));
        #endif
    }

    setBlinkType((blinkType_t)database.read(DB_BLOCK_LEDS, dbSection_leds_global, ledGlobalParam_blinkMIDIclock));

    //store some parameters from eeprom to ram for faster access
    for (int i=0; i<MAX_NUMBER_OF_LEDS; i++)
        ledControlChannel[i] = database.read(DB_BLOCK_LEDS, dbSection_leds_midiChannel, i);

    for (int i=0; i<MAX_NUMBER_OF_RGB_LEDS; i++)
    {
        uint8_t arrayIndex = i/8;
        uint8_t ledIndex = i - 8*arrayIndex;

        BIT_WRITE(rgbLEDenabled[arrayIndex], ledIndex, (bool)database.read(DB_BLOCK_LEDS, dbSection_leds_rgbEnable, i));
    }
}

///
/// \brief Checks if RGB led is enabled.
/// @param [in] ledID    LED index which is being checked.
/// \returns True if RGB LED is enabled.
///
inline bool isRGBLEDenabled(uint8_t ledID)
{
    uint8_t arrayIndex = ledID/8;
    uint8_t ledIndex = ledID - 8*arrayIndex;

    return BIT_READ(rgbLEDenabled[arrayIndex], ledIndex);
}

void LEDs::update()
{
    //holds last time in miliseconds when LED blinking has been updated
    static uint32_t lastLEDblinkUpdateTime = 0;

    //holds blink state for each blink speed so that leds are in sync
    static bool blinkState[BLINK_SPEEDS] = { true };

    //array used to determine when the blink state for specific blink rate should be changed
    static uint8_t blinkCounter[BLINK_SPEEDS] = { 0 };

    switch(ledBlinkType)
    {
        case blinkType_timer:
        //update blink states every 100ms - minimum blink time
        if ((rTimeMs() - lastLEDblinkUpdateTime) < 100)
            return;

        lastLEDblinkUpdateTime = rTimeMs();
        break;

        case blinkType_midiClock:
        if ((midiClockStatus == midiClockNoChange) || (midiClockStatus == midiClockStop))
            return;

        if (midiClockStatus == midiClockReset)
        {
            //reset all counters in this case
            //also make sure all leds are in sync again
            for (int i=0; i<BLINK_SPEEDS; i++)
            {
                blinkCounter[i] = 0;
                blinkState[i] = true;
            }
        }

{'fraction_non_alphanumeric': 0.09052825176254214, 'fraction_numerical': 0.01593951159701645, 'mean_word_length': 2.404521739130435, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5391805', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3010, 'n_words': 946}