<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 "Board.h"
#include "Variables.h"
#include "Hardware.h"

///
/// \brief Holds currently active multiplexer.
///
static uint8_t      activeMux;

///
/// \brief Holds currently active multiplexer input.
///
static uint8_t      activeMuxInput;

///
/// \brief Holds currently active analog index which is being read.
/// Once all analog inputs are read, analog index is reset to 0.
///
static uint8_t      analogIndex;

///
/// brief Holds currently active sample count.
/// Once all analog inputs are read, sample count is increased.
///
volatile uint8_t    analogSampleCounter;

///
/// \brief Array in which analog samples are stored.
///
volatile int16_t    analogBuffer[MAX_NUMBER_OF_ANALOG];

///
/// Due to non-linearity of standard potentiometers on their extremes (low and high values),
/// hysteresis is used to avoid incorrect values. These arrays hold information on whether
/// low or high hysteresis values should be used.
/// @{

static uint8_t      lowHysteresisActive[MAX_NUMBER_OF_ANALOG/8+1];
static uint8_t      highHysteresisActive[MAX_NUMBER_OF_ANALOG/8+1];

/// @}

///
/// brief Checks if specified hysteresis is active for requested analog index.
/// @param[in] type     Hysteresis type. Enumerated type (see hysteresisType_t).
/// @param[in] analogID Analog index for which hysteresis state is being checked.
/// \returns True if hysteresis is currently active, false otherwise.
///
static bool isHysteresisActive(hysteresisType_t type, uint8_t analogID)
{
    uint8_t arrayIndex = analogID/8;
    uint8_t analogIndex = analogID - 8*arrayIndex;

    if (type == lowHysteresis)
        return BIT_READ(lowHysteresisActive[arrayIndex], analogIndex);
    else
        return BIT_READ(highHysteresisActive[arrayIndex], analogIndex);
}

///
/// brief Enables or disables specific hysteresis type for specified analog index.
/// @param[in] type     Hysteresis type. Enumerated type (see hysteresisType_t).
/// @param[in] analogID Analog index for which hysteresis state is being changed.
/// @param[in] state    New hystersis state (true/enabled, false/disabled).
///
static void updateHysteresisState(hysteresisType_t type, uint8_t analogID, bool state)
{
    uint8_t arrayIndex = analogID/8;
    uint8_t analogIndex = analogID - 8*arrayIndex;

    if (type == lowHysteresis)
        BIT_WRITE(lowHysteresisActive[arrayIndex], analogIndex, state);
    else
        BIT_WRITE(highHysteresisActive[arrayIndex], analogIndex, state);
}

///
/// \brief Configures one of 16 inputs/outputs on 4067 multiplexer.
///
inline void setMuxInput()
{
    //add NOPs to compensate for propagation delay

    BIT_READ(muxPinOrderArray[activeMuxInput], 0) ? setHigh(MUX_S0_PORT, MUX_S0_PIN) : setLow(MUX_S0_PORT, MUX_S0_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 1) ? setHigh(MUX_S1_PORT, MUX_S1_PIN) : setLow(MUX_S1_PORT, MUX_S1_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 2) ? setHigh(MUX_S2_PORT, MUX_S2_PIN) : setLow(MUX_S2_PORT, MUX_S2_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 3) ? setHigh(MUX_S3_PORT, MUX_S3_PIN) : setLow(MUX_S3_PORT, MUX_S3_PIN);
}

///
/// \brief Initializes analog variables and ADC peripheral.
///
void Board::initAnalog()
{
    disconnectDigitalInADC(MUX_1_IN_PIN);
    disconnectDigitalInADC(MUX_2_IN_PIN);

    adcConf adcConfiguration;

    adcConfiguration.prescaler = ADC_PRESCALER_128;
    adcConfiguration.vref = ADC_VREF_AREF;

    setUpADC(adcConfiguration);
    setMuxInput();
    setADCchannel(muxInPinArray[0]);<commit_msg>remove incorrect info<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 "Board.h"
#include "Variables.h"
#include "Hardware.h"

///
/// \brief Holds currently active multiplexer.
///
static uint8_t      activeMux;

///
/// \brief Holds currently active multiplexer input.
///
static uint8_t      activeMuxInput;

///
/// \brief Holds currently active analog index which is being read.
/// Once all analog inputs are read, analog index is reset to 0.
///
static uint8_t      analogIndex;

///
/// brief Holds currently active sample count.
/// Once all analog inputs are read, sample count is increased.
///
volatile uint8_t    analogSampleCounter;

///
/// \brief Array in which analog samples are stored.
///
volatile int16_t    analogBuffer[MAX_NUMBER_OF_ANALOG];

///
/// Due to non-linearity of standard potentiometers on their extremes (low and high values),
/// hysteresis is used to avoid incorrect values. These arrays hold information on whether
/// low or high hysteresis values should be used.
/// @{

static uint8_t      lowHysteresisActive[MAX_NUMBER_OF_ANALOG/8+1];
static uint8_t      highHysteresisActive[MAX_NUMBER_OF_ANALOG/8+1];

/// @}

///
/// brief Checks if specified hysteresis is active for requested analog index.
/// @param[in] type     Hysteresis type. Enumerated type (see hysteresisType_t).
/// @param[in] analogID Analog index for which hysteresis state is being checked.
/// \returns True if hysteresis is currently active, false otherwise.
///
static bool isHysteresisActive(hysteresisType_t type, uint8_t analogID)
{
    uint8_t arrayIndex = analogID/8;
    uint8_t analogIndex = analogID - 8*arrayIndex;

    if (type == lowHysteresis)
        return BIT_READ(lowHysteresisActive[arrayIndex], analogIndex);
    else
        return BIT_READ(highHysteresisActive[arrayIndex], analogIndex);
}

///
/// brief Enables or disables specific hysteresis type for specified analog index.
/// @param[in] type     Hysteresis type. Enumerated type (see hysteresisType_t).
/// @param[in] analogID Analog index for which hysteresis state is being changed.
/// @param[in] state    New hystersis state (true/enabled, false/disabled).
///
static void updateHysteresisState(hysteresisType_t type, uint8_t analogID, bool state)
{
    uint8_t arrayIndex = analogID/8;
    uint8_t analogIndex = analogID - 8*arrayIndex;

    if (type == lowHysteresis)
        BIT_WRITE(lowHysteresisActive[arrayIndex], analogIndex, state);
    else
        BIT_WRITE(highHysteresisActive[arrayIndex], analogIndex, state);
}

///
/// \brief Configures one of 16 inputs/outputs on 4067 multiplexer.
///
inline void setMuxInput()
{
    BIT_READ(muxPinOrderArray[activeMuxInput], 0) ? setHigh(MUX_S0_PORT, MUX_S0_PIN) : setLow(MUX_S0_PORT, MUX_S0_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 1) ? setHigh(MUX_S1_PORT, MUX_S1_PIN) : setLow(MUX_S1_PORT, MUX_S1_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 2) ? setHigh(MUX_S2_PORT, MUX_S2_PIN) : setLow(MUX_S2_PORT, MUX_S2_PIN);
    BIT_READ(muxPinOrderArray[activeMuxInput], 3) ? setHigh(MUX_S3_PORT, MUX_S3_PIN) : setLow(MUX_S3_PORT, MUX_S3_PIN);
}

///
/// \brief Initializes analog variables and ADC peripheral.
///
void Board::initAnalog()
{
    disconnectDigitalInADC(MUX_1_IN_PIN);
    disconnectDigitalInADC(MUX_2_IN_PIN);

    adcConf adcConfiguration;

    adcConfiguration.prescaler = ADC_PRESCALER_128;
    adcConfiguration.vref = ADC_VREF_AREF;

    setUpADC(adcConfiguration);
    setMuxInput();
    setADCchannel(muxInPinArray[0]);
{'fraction_non_alphanumeric': 0.0946073793755913, 'fraction_numerical': 0.015137180700094607, 'mean_word_length': 3.759144625773776, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2489942', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2620, 'n_words': 820}