<commit_before>/*
* Sabre.cpp
*
* Created: 19-12-2013 23:05:29
* Author: CE-Dsigns
*/

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "Sabre.h"
#include <Wire.h>
#include "EEPROM.h"

#define WIRE Wire


//REGISTERS///////////////////////////////////////////////////////////////
#define REG0 0X00	// Volume of DAC0
#define REG1 0X01	// Volume of DAC1
#define REG2 0X02	// Volume of DAC2
#define REG3 0X03	// Volume of DAC3
#define REG4 0X04	// Volume of DAC4
#define REG5 0X05	// Volume of DAC5
#define REG6 0X06	// Volume of DAC6
#define REG7 0X07	// Volume of DAC7
#define REG8 0x08	// Automute_lev
#define REG9 0x09	// Automute_time
#define REG10 0x0A	// Mode Control 1
#define REG11 0x0B	// Mode Control 2
#define REG12 0x0C	// Mode Control 3: Dither control, Notch delay
#define REG13 0x0D	// DAC Polarity
#define REG14 0x0E	// DAC3/4/7/8 Source, Differential Mode, IRR Bandwidth, FIR Rolloff
#define REG15 0x0F	// Mode Control 4: Quantizer settings
#define REG16 0x10	// Automute Loopback
#define REG17 0x11	// Mode Control 5
#define REG18 0x12	// SPDIF Source
#define REG19 0x13	// DACB Polarity
#define REG23 0x17	// >Master Trim (MSB's)
#define REG22 0x16	// >Master Trim
#define REG21 0x15	// >Master Trim
#define REG20 0x14	// >Master Trim (LSB's)
#define REG24 0x18	// Phase Shift
#define REG25 0x19	// DPLL Mode Control
#define REG27 0x1B	// Status
#define REG31 0x1F	// >DPLL_NUM (MSB's)
#define REG30 0x1E	// >DPLL_NUM
#define REG29 0x1D	// >DPLL_NUM
#define REG28 0x1C	// >DPLL_NUM (LSB's)

//END REGISTERS///////////////////////////////////////////////////////////

// default constructor
Sabre::Sabre()
{
	WIRE.begin();						// Join the I2C bus as a master
} //Sabre


//PUBLIC FUNCTIONS////////////////////////////////////////////////////////

void Sabre::begin(bool DualMono, uint8_t F_crystal)
{
	Reg17.All_Mono = DualMono;			// Set flag, so all DACs are muted as early as possible
	sReg10 = 0xCE;						// use default = 8'b11001110
	muteDACS();							// mute DAC's as early as possible
	Fcrystal = F_crystal;				// store the crystal frequency (MHz)
	// Set all register defaults	
	setRegisterDefaults();	
	// Set output mode
	if (DualMono)
	{
		setOutputMode(AllMonoMode);
	}
	else
	{
		setOutputMode(NormalMode);
	}
	SelectedInput = EEPROM.read(EEPROM_SELECTED_INPUT);	
	DefaultAttenuation = EEPROM.read(EEPROM_DEF_ATTNU);
	Attenuation = DefaultAttenuation;
	if (firstRun())
	{
		useDefaultSettings();
		for (uint8_t i = 0; i < NUMBER_OF_INPUTS; i++)
		{
			writeInputConfiguration(i);
		}
		EEPROM.write(EEPROM_FIRST_RUN, FIRST_RUN);		
	}
	readInputConfiguration();
	applyInputConfiguration(SelectedInput);
}

void Sabre::setRegisterDefaults()
{
	sReg8 = 0x68;						// Use either I2S or DSD input
	sReg11 = 0x85;						// Lowest DPLL bandwidth and De-emphasis delect is 44.1kHz (8'b10000101)
	sReg13 = 0x00;						// DAC polarity set to In-Phase (8'b00000000)
	sReg12 = 0x20;						// Apply dither control, External input, no remap
	sReg14 = 0x0B;						// DAC3/4/7/8 Source, 50k IIR Bandwidth, fast FIR Rolloff (8'b10001011)
	sReg15 = 0x00;						// 6 Bits quantizer (8'b00000000)
	sReg16 = 0x00;						// Do not ramp volume down upon automute condition (8'b00000000)
	sReg17 = 0x1C;						// Use left channel in all_mono mode, use OSF filter, normal dpll_lock_rst_reg, use auto_deemph, spdif_autodetect, Fir_length=28, no fin_phase_flip, normal 8 channel mode (8'b00011100)
	sReg18 = 0x01;						// SPDIF to input #1
	sReg19 = 0x00;						// DACB polarity is set to Anti-Phase
	sReg25 = 0x00;						// Allow all settings (8'b00000000)
}


bool Sabre::firstRun()
{
	return (EEPROM.read(EEPROM_FIRST_RUN) != FIRST_RUN);	
}

void Sabre::useDefaultSettings()
{
	for (byte i = 0; i < NUMBER_OF_INPUTS; i++)
	{
		Config[i].AUTO_DEEMPH = useAutoDeemph;
		Config[i].BIT_MODE = BitMode32;
		Config[i].DE_EMPHASIS_SELECT = AutoDeemphasis;
		Config[i].DEEMPH_FILTER = BypassDeemphasisFilter;
		Config[i].DIFFERENTIAL_MODE = trueDiff;
		Config[i].DPLL_BANDWIDTH = DPLL_Best;
		Config[i].DPLL_BW_128X = UseDPLLBandwidthSetting;
		Config[i].DPLL_BW_DEFAULTS = UseBestDPLLSettings;
		Config[i].FIR_FILTER = FastRolloff;
		Config[i].IIR_BANDWIDTH = use50K;
		Config[i].INPUT_ENABLED = 0x01;		
		Config[i].INPUT_NAME[0] = 'I';
		Config[i].INPUT_NAME[1] = 'N';
		Config[i].INPUT_NAME[2] = 'P';
		Config[i].INPUT_NAME[3] = 'U';
		Config[i].INPUT_NAME[4] = 'T';
		Config[i].INPUT_NAME[5] = ' ';
		Config[i].INPUT_NAME[6] = (i+49);	
		for (uint8_t x = 7; x < INPUT_NAME_SIZE; x++)
		{
			Config[i].INPUT_NAME[x] = ' ';
		}		
		Config[i].JITTER_REDUCTION = UseJitterReduction;
		Config[i].NOTCH_DELAY = NoNotch;
		Config[i].OSF_FILTER = useOSFfiler;
		Config[i].QUANTIZER = use6BitsTrue;
		Config[i].SERIAL_DATA_MODE = I2S;
		Config[i].SPDIF_ENABLE = useI2SorDSD;
		Config[i].SPDIF_SOURCE = Data1;
	}
}

void Sabre::setVolume(uint8_t value)
{	
	writeSabreReg(REG0, value);			// set volume of DAC0
	writeSabreReg(REG1, value);			// set volume of DAC1
	writeSabreReg(REG2, value);			// set volume of DAC2
	writeSabreReg(REG3, value);			// set volume of DAC3
	writeSabreReg(REG4, value);			// set volume of DAC4
	writeSabreReg(REG5, value);			// set volume of DAC5
	writeSabreReg(REG6, value);			// set volume of DAC6
	writeSabreReg(REG7, value);			// set volume of DAC7<commit_msg>Update Sabre.cpp<commit_after>/*
* Sabre.cpp
*
* Created: 19-12-2013 23:05:29
* Author: CE-Dsigns
*/

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "Sabre.h"
#include <Wire.h>
#include "EEPROM.h"

#define WIRE Wire


//REGISTERS///////////////////////////////////////////////////////////////
#define REG0 0X00	// Volume of DAC0
#define REG1 0X01	// Volume of DAC1
#define REG2 0X02	// Volume of DAC2
#define REG3 0X03	// Volume of DAC3
#define REG4 0X04	// Volume of DAC4
#define REG5 0X05	// Volume of DAC5
#define REG6 0X06	// Volume of DAC6
#define REG7 0X07	// Volume of DAC7
#define REG8 0x08	// Automute_lev
#define REG9 0x09	// Automute_time
#define REG10 0x0A	// Mode Control 1
#define REG11 0x0B	// Mode Control 2
#define REG12 0x0C	// Mode Control 3: Dither control, Notch delay
#define REG13 0x0D	// DAC Polarity
#define REG14 0x0E	// DAC3/4/7/8 Source, Differential Mode, IRR Bandwidth, FIR Rolloff
#define REG15 0x0F	// Mode Control 4: Quantizer settings
#define REG16 0x10	// Automute Loopback
#define REG17 0x11	// Mode Control 5
#define REG18 0x12	// SPDIF Source
#define REG19 0x13	// DACB Polarity
#define REG23 0x17	// >Master Trim (MSB's)
#define REG22 0x16	// >Master Trim
#define REG21 0x15	// >Master Trim
#define REG20 0x14	// >Master Trim (LSB's)
#define REG24 0x18	// Phase Shift
#define REG25 0x19	// DPLL Mode Control
#define REG27 0x1B	// Status
#define REG31 0x1F	// >DPLL_NUM (MSB's)
#define REG30 0x1E	// >DPLL_NUM
#define REG29 0x1D	// >DPLL_NUM
#define REG28 0x1C	// >DPLL_NUM (LSB's)

//END REGISTERS///////////////////////////////////////////////////////////

// default constructor
Sabre::Sabre()
{
	WIRE.begin();						// Join the I2C bus as a master
} //Sabre


//PUBLIC FUNCTIONS////////////////////////////////////////////////////////

void Sabre::begin(bool DualMono, uint8_t F_crystal)
{
	Reg17.All_Mono = DualMono;			// Set flag, so all DACs are muted as early as possible
	sReg10 = 0xCE;						// use default = 8'b11001110
	muteDACS();							// mute DAC's as early as possible
	Fcrystal = F_crystal;				// store the crystal frequency (MHz)
	// Set all register defaults	
	setRegisterDefaults();	
	// Set output mode
	if (DualMono)
	{
		setOutputMode(AllMonoMode);
	}
	else
	{
		setOutputMode(NormalMode);
	}
	SelectedInput = EEPROM.read(EEPROM_SELECTED_INPUT);	
	DefaultAttenuation = EEPROM.read(EEPROM_DEF_ATTNU);
	Attenuation = DefaultAttenuation;
	if (firstRun())
	{
		useDefaultSettings();
		for (uint8_t i = 0; i < NUMBER_OF_INPUTS; i++)
		{
			writeInputConfiguration(i);
		}
		writeDefaultAttenuation();
		EEPROM.write(EEPROM_FIRST_RUN, FIRST_RUN);
		
	}
	readInputConfiguration();
	applyInputConfiguration(SelectedInput);
}

void Sabre::setRegisterDefaults()
{
	sReg8 = 0x68;						// Use either I2S or DSD input
	sReg11 = 0x85;						// Lowest DPLL bandwidth and De-emphasis delect is 44.1kHz (8'b10000101)
	sReg13 = 0x00;						// DAC polarity set to In-Phase (8'b00000000)
	sReg12 = 0x20;						// Apply dither control, External input, no remap
	sReg14 = 0x0B;						// DAC3/4/7/8 Source, 50k IIR Bandwidth, fast FIR Rolloff (8'b10001011)
	sReg15 = 0x00;						// 6 Bits quantizer (8'b00000000)
	sReg16 = 0x00;						// Do not ramp volume down upon automute condition (8'b00000000)
	sReg17 = 0x1C;						// Use left channel in all_mono mode, use OSF filter, normal dpll_lock_rst_reg, use auto_deemph, spdif_autodetect, Fir_length=28, no fin_phase_flip, normal 8 channel mode (8'b00011100)
	sReg18 = 0x01;						// SPDIF to input #1
	sReg19 = 0x00;						// DACB polarity is set to Anti-Phase
	sReg25 = 0x00;						// Allow all settings (8'b00000000)
}


bool Sabre::firstRun()
{
	return (EEPROM.read(EEPROM_FIRST_RUN) != FIRST_RUN);	
}

void Sabre::useDefaultSettings()
{
	for (byte i = 0; i < NUMBER_OF_INPUTS; i++)
	{
		Config[i].AUTO_DEEMPH = useAutoDeemph;
		Config[i].BIT_MODE = BitMode32;
		Config[i].DE_EMPHASIS_SELECT = AutoDeemphasis;
		Config[i].DEEMPH_FILTER = BypassDeemphasisFilter;
		Config[i].DIFFERENTIAL_MODE = trueDiff;
		Config[i].DPLL_BANDWIDTH = DPLL_Best;
		Config[i].DPLL_BW_128X = UseDPLLBandwidthSetting;
		Config[i].DPLL_BW_DEFAULTS = UseBestDPLLSettings;
		Config[i].FIR_FILTER = FastRolloff;
		Config[i].IIR_BANDWIDTH = use50K;
		Config[i].INPUT_ENABLED = 0x01;		
		Config[i].INPUT_NAME[0] = 'I';
		Config[i].INPUT_NAME[1] = 'N';
		Config[i].INPUT_NAME[2] = 'P';
		Config[i].INPUT_NAME[3] = 'U';
		Config[i].INPUT_NAME[4] = 'T';
		Config[i].INPUT_NAME[5] = ' ';
		Config[i].INPUT_NAME[6] = (i+49);	
		for (uint8_t x = 7; x < INPUT_NAME_SIZE; x++)
		{
			Config[i].INPUT_NAME[x] = ' ';
		}		
		Config[i].JITTER_REDUCTION = UseJitterReduction;
		Config[i].NOTCH_DELAY = NoNotch;
		Config[i].OSF_FILTER = useOSFfiler;
		Config[i].QUANTIZER = use6BitsTrue;
		Config[i].SERIAL_DATA_MODE = I2S;
		Config[i].SPDIF_ENABLE = useI2SorDSD;
		Config[i].SPDIF_SOURCE = Data1;
		//
		DefaultAttenuation = DEFAULT_ATTNU;
		Attenuation = DefaultAttenuation;
	}
}

void Sabre::setVolume(uint8_t value)
{	
	writeSabreReg(REG0, value);			// set volume of DAC0
	writeSabreReg(REG1, value);			// set volume of DAC1
	writeSabreReg(REG2, value);			// set volume of DAC2
	writeSabreReg(REG3, value);			// set volume of DAC3
	writeSabreReg(REG4, value);			// set volume of DAC4
	writeSabreReg(REG5, value);			// set volume of DAC5
	writeSabreReg(REG6, value);			// set volume of DAC6
	writeSabreReg(REG7, value);			// set volume of DAC7
{'fraction_non_alphanumeric': 0.1520402459474567, 'fraction_numerical': 0.06689025526364822, 'mean_word_length': 3.2263779527559056, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '1000284', 'n_tokens_mistral': 5008, 'n_tokens_neox': 4196, 'n_words': 1051}