From b4a18d0649479b63435c2b6ce742012d89ba4ce8 Mon Sep 17 00:00:00 2001 From: MCUdude Date: Tue, 6 Oct 2020 19:26:22 +0200 Subject: [PATCH] Pass reference to Wire library in constructor Instead of using a pointer in begin(). --- examples/0_Template/0_Template.ino | 35 ++--- examples/1_Volume/1_Volume.ino | 30 +++-- .../2_First_order_EQ/2_First_order_EQ.ino | 29 +++-- .../3_Second_order_EQ/3_Second_order_EQ.ino | 29 +++-- .../4_State_variable_filter.ino | 29 +++-- .../5_Signal_generator/5_Signal_generator.ino | 28 ++-- .../6_Dynamic_bass_boost.ino | 29 +++-- .../7_Signal_detect_readout.ino | 28 ++-- src/DSPEEPROM.cpp | 97 +++++--------- src/DSPEEPROM.h | 7 +- src/SigmaDSP.cpp | 123 ++++++------------ src/SigmaDSP.h | 7 +- 12 files changed, 208 insertions(+), 263 deletions(-) diff --git a/examples/0_Template/0_Template.ino b/examples/0_Template/0_Template.ino index 9aa3daf..5d1b6f2 100644 --- a/examples/0_Template/0_Template.ino +++ b/examples/0_Template/0_Template.ino @@ -2,31 +2,34 @@ | SigmaDSP library | | https://github.com/MCUdude/SigmaDSP | | | -| 0_Template. | +| 0_Template.ino | | This example is ment to be used as a | -| template for your own projects. It brings no | -| functionality other than connecting the two | -| analog inputs directly to output 0 and 1. | +| template for your own projects. It provide | +| no functionality other than connecting the | +| two analog inputs directly to output 0 and 1 | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); void setup() @@ -34,10 +37,8 @@ void setup() Serial.begin(9600); Serial.println(F("SigmaDSP 0_Template example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/1_Volume/1_Volume.ino b/examples/1_Volume/1_Volume.ino index 0a752b5..7c97a0a 100644 --- a/examples/1_Volume/1_Volume.ino +++ b/examples/1_Volume/1_Volume.ino @@ -9,24 +9,27 @@ | to learn more, tweak or do modifications. | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); void setup() @@ -34,10 +37,9 @@ void setup() Serial.begin(9600); Serial.println(F("SigmaDSP 1_Volume example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); @@ -80,4 +82,4 @@ void loop() dsp.volume_slew(MOD_SWVOL1_ALG0_TARGET_ADDR, i); // Slew rate is default 12 delay(200); } -} +} \ No newline at end of file diff --git a/examples/2_First_order_EQ/2_First_order_EQ.ino b/examples/2_First_order_EQ/2_First_order_EQ.ino index d7e61e2..076a0f1 100644 --- a/examples/2_First_order_EQ/2_First_order_EQ.ino +++ b/examples/2_First_order_EQ/2_First_order_EQ.ino @@ -10,24 +10,28 @@ | to learn more, tweak or do modifications. | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); + // Create an instance for each EQ block firstOrderEQ eq1; @@ -38,10 +42,9 @@ void setup() Serial.begin(9600); Serial.println(F("SigmaDSP 2_First_order_EQ example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/3_Second_order_EQ/3_Second_order_EQ.ino b/examples/3_Second_order_EQ/3_Second_order_EQ.ino index 9bdd7b8..0ce5c27 100644 --- a/examples/3_Second_order_EQ/3_Second_order_EQ.ino +++ b/examples/3_Second_order_EQ/3_Second_order_EQ.ino @@ -10,24 +10,28 @@ | to learn more, tweak or do modifications. | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); + // Create an instance for each EQ block secondOrderEQ eqBand1; @@ -40,10 +44,9 @@ void setup() Serial.begin(9600); Serial.println(F("3_Second_order_EQ example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/4_State_variable_filter/4_State_variable_filter.ino b/examples/4_State_variable_filter/4_State_variable_filter.ino index b10146b..7c925ac 100644 --- a/examples/4_State_variable_filter/4_State_variable_filter.ino +++ b/examples/4_State_variable_filter/4_State_variable_filter.ino @@ -11,24 +11,28 @@ | to learn more, tweak or do modifications. | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); + // Variable to store calculated sweep tone uint16_t frequency; @@ -38,10 +42,9 @@ void setup() Serial.begin(9600); Serial.println(F("SigmaDSP 4_State_variable_filter example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/5_Signal_generator/5_Signal_generator.ino b/examples/5_Signal_generator/5_Signal_generator.ino index 80d6a54..5e75252 100644 --- a/examples/5_Signal_generator/5_Signal_generator.ino +++ b/examples/5_Signal_generator/5_Signal_generator.ino @@ -9,24 +9,27 @@ | to learn more, tweak or do modifications. | |**********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); // Variable to store calculated sweep tone uint16_t frequency; @@ -36,10 +39,9 @@ void setup() Serial.begin(9600); Serial.println(F("5_Signal_generator example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/6_Dynamic_bass_boost/6_Dynamic_bass_boost.ino b/examples/6_Dynamic_bass_boost/6_Dynamic_bass_boost.ino index c8c5727..6c526ba 100644 --- a/examples/6_Dynamic_bass_boost/6_Dynamic_bass_boost.ino +++ b/examples/6_Dynamic_bass_boost/6_Dynamic_bass_boost.ino @@ -9,34 +9,37 @@ | to learn more, tweak or do modifications. | |***********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); + void setup() { Serial.begin(9600); Serial.println(F("6_Dynamic_bass_boost example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/examples/7_Signal_detect_readout/7_Signal_detect_readout.ino b/examples/7_Signal_detect_readout/7_Signal_detect_readout.ino index 96b2c81..e67e80d 100644 --- a/examples/7_Signal_detect_readout/7_Signal_detect_readout.ino +++ b/examples/7_Signal_detect_readout/7_Signal_detect_readout.ino @@ -9,24 +9,27 @@ | to learn more, tweak or do modifications. | |***********************************************/ -// Include SigmaDSP library +// Include Wire and SigmaDSP library +#include #include // Include generated parameter file #include "SigmaDSP_parameters.h" -// The first parameter is the i2c address, which is defined in the parameter file. -// The next parameter is the SigmaDSP type -// An optional third parameter is the pin to physically reset the DSP -SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/); +// The first parameter is the Wire object we'll be using when communicating wth the DSP +// The second parameter is the DSP i2c address, which is defined in the parameter file +// The third parameter is the SigmaDSP type +// An optional fourth parameter is the pin to physically reset the DSP +SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, ADAU1701 /*,12*/); // Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode -// The first parameter is the i2c address, which is defined in the parameter file. -// The second parameter is the EEPROM size in kilobits -// An optional third parameter is the pin to toggle while writing content to EEPROM -//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); +// The first parameter is the Wire object we'll be using when communicating wth the EEPROM +// The second parameter is the EEPROM i2c address, which is defined in the parameter file +// The third parameter is the EEPROM size in kilobits (kb) +// An optional fourth parameter is the pin to toggle while writing content to EEPROM +//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED_BUILTIN); // Variables to hold the current signal detect value uint32_t previousLevelLeft = 1; @@ -39,10 +42,9 @@ void setup() Serial.begin(9600); Serial.println(F("7_Signal_detect_readout example\n")); - // dsp and ee supports re-mapping of the SDA and SCL signals, but - // can only be done if the microcontroller supports it (ESP8266 for example) - dsp.begin(/* &Wire, SDA, SCL */); - //ee.begin(/* &Wire, SDA, SCL */); + Wire.begin(); + dsp.begin(); + //ee.begin(); delay(2000); diff --git a/src/DSPEEPROM.cpp b/src/DSPEEPROM.cpp index 286e816..07a8904 100644 --- a/src/DSPEEPROM.cpp +++ b/src/DSPEEPROM.cpp @@ -4,13 +4,14 @@ /*************************************** Function: DSPEEPROM() Purpose: The constructor creates the object -Inputs: uint8_t i2cAddress; 7-bit i2c address - uint16_t kbitSize; Size of EEPROM in kilobit - int8_t ledPin; Pin to toggle when flashing EEPROM +Inputs: TwoWire &WireObject; Reference to Wire object + uint8_t i2cAddress; 7-bit i2c address + uint16_t kbitSize; Size of EEPROM in kilobit + int8_t ledPin; Pin to toggle when flashing EEPROM Returns: None ***************************************/ -DSPEEPROM::DSPEEPROM(uint8_t i2cAddress, uint16_t kbitSize, int8_t ledPin) - : _eepromAddress(i2cAddress), _kbitSize(kbitSize), _ledPin(ledPin) +DSPEEPROM::DSPEEPROM(TwoWire &WireObject,uint8_t i2cAddress, uint16_t kbitSize, int8_t ledPin) + : _WireObject(WireObject), _eepromAddress(i2cAddress), _kbitSize(kbitSize), _ledPin(ledPin) { // The DSP program itself will never exceed 9248 bytes in total // The last byte in EEPROM will be used to keep the firmware version @@ -37,16 +38,10 @@ DSPEEPROM::DSPEEPROM(uint8_t i2cAddress, uint16_t kbitSize, int8_t ledPin) /*************************************** Function: begin() Purpose: Starts the i2c interface -Inputs: TwoWire Wire; Wire object (optional parameter) Returns: None ***************************************/ -void DSPEEPROM::begin(TwoWire *WireObject) +void DSPEEPROM::begin() { - // Store copy the passed object - _WireObject = WireObject; - - _WireObject->begin(); - // If LED is present if(_ledPin >= 0) { @@ -56,36 +51,6 @@ void DSPEEPROM::begin(TwoWire *WireObject) } -/*************************************** -Function: begin() -Purpose: Starts the i2c interface -Inputs: TwoWire Wire; Wire object - uint8_t sdaPin; SDA pin - uint8_t sclPin; SCL pin -Returns: None -***************************************/ -void DSPEEPROM::begin(TwoWire *WireObject, uint8_t sdaPin, uint8_t sclPin) -{ - // Store copy the passed object - _WireObject = WireObject; - - // This hardware supports redefining SDA and SCL in begin() - #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) - _WireObject->begin(sdaPin, sclPin); - #else // This does not - (void)sdaPin; - (void)sclPin; - _WireObject->begin(); - #endif - - // If LED is present - if(_ledPin >= 0) - { - pinMode(_ledPin, OUTPUT); - digitalWrite(_ledPin, LOW); - } -} - /*************************************** Function: ping() @@ -98,8 +63,8 @@ Returns: 0 - success: ack received ***************************************/ uint8_t DSPEEPROM::ping() { - _WireObject->beginTransmission(_eepromAddress); - return _WireObject->endTransmission(); + _WireObject.beginTransmission(_eepromAddress); + return _WireObject.endTransmission(); } @@ -112,15 +77,15 @@ Returns: Firmware version. 0 indicated that no EEPROM is found, ***************************************/ uint8_t DSPEEPROM::getFirmwareVersion() { - _WireObject->beginTransmission(_eepromAddress); - _WireObject->write((uint16_t)_firmwareVersionAddress >> 8); // MSB - _WireObject->write(_firmwareVersionAddress & 0xFF); // LSB - _WireObject->endTransmission(); + _WireObject.beginTransmission(_eepromAddress); + _WireObject.write((uint16_t)_firmwareVersionAddress >> 8); // MSB + _WireObject.write(_firmwareVersionAddress & 0xFF); // LSB + _WireObject.endTransmission(); - _WireObject->requestFrom((uint8_t)_eepromAddress, (uint8_t)1); + _WireObject.requestFrom((uint8_t)_eepromAddress, (uint8_t)1); - if(_WireObject->available()) - return _WireObject->read(); + if(_WireObject.available()) + return _WireObject.read(); else return 0; } @@ -147,11 +112,11 @@ uint8_t DSPEEPROM::writeFirmware(const uint8_t *firmware, uint16_t size, int8_t // Write new firmware for(uint16_t i = 0; i < size; i++) { - _WireObject->beginTransmission(_eepromAddress); - _WireObject->write(i >> 8); // High memory address - _WireObject->write(i & 0xFF); // Low memory address - _WireObject->write(pgm_read_byte(&firmware[i])); // Content - _WireObject->endTransmission(); // End + _WireObject.beginTransmission(_eepromAddress); + _WireObject.write(i >> 8); // High memory address + _WireObject.write(i & 0xFF); // Low memory address + _WireObject.write(pgm_read_byte(&firmware[i])); // Content + _WireObject.endTransmission(); // End // EEPROM is a slooow kind of memory.. delay(5); @@ -166,11 +131,11 @@ uint8_t DSPEEPROM::writeFirmware(const uint8_t *firmware, uint16_t size, int8_t // Make sure all tracks of old FW is gone, by overwriting a few more bytes for(uint16_t i = size; i < 0x2000; i++) { - _WireObject->beginTransmission(_eepromAddress); - _WireObject->write(i >> 8); // High memory address - _WireObject->write(i & 0xFF); // Low memory address - _WireObject->write(0xFF); // Content - _WireObject->endTransmission(); // End + _WireObject.beginTransmission(_eepromAddress); + _WireObject.write(i >> 8); // High memory address + _WireObject.write(i & 0xFF); // Low memory address + _WireObject.write(0xFF); // Content + _WireObject.endTransmission(); // End delay(5); // Toggle LED every 16th byte if LED is present when filling the rest @@ -182,11 +147,11 @@ uint8_t DSPEEPROM::writeFirmware(const uint8_t *firmware, uint16_t size, int8_t } // Write firmware version to the last byte in EEPROM - _WireObject->beginTransmission(_eepromAddress); - _WireObject->write((uint16_t)_firmwareVersionAddress >> 8); // High memory address - _WireObject->write(_firmwareVersionAddress & 0xFF); // Low memory address - _WireObject->write(firmwareVersion); // Content - _WireObject->endTransmission(); // End + _WireObject.beginTransmission(_eepromAddress); + _WireObject.write((uint16_t)_firmwareVersionAddress >> 8); // High memory address + _WireObject.write(_firmwareVersionAddress & 0xFF); // Low memory address + _WireObject.write(firmwareVersion); // Content + _WireObject.endTransmission(); // End delay(5); // Turn off LED after flashing is finished if LED is present diff --git a/src/DSPEEPROM.h b/src/DSPEEPROM.h index edc6900..deaf688 100644 --- a/src/DSPEEPROM.h +++ b/src/DSPEEPROM.h @@ -8,15 +8,14 @@ class DSPEEPROM { public: - DSPEEPROM(uint8_t i2cAddress, uint16_t kbitSize, int8_t ledPin = -1); - void begin(TwoWire *WireObject = &Wire); - void begin(TwoWire *WireObject, uint8_t sdaPin, uint8_t sclPin); + DSPEEPROM(TwoWire &WireObject, uint8_t i2cAddress, uint16_t kbitSize, int8_t ledPin = -1); + void begin(); uint8_t ping(); uint8_t getFirmwareVersion(); uint8_t writeFirmware(const uint8_t *firmware, uint16_t size, int8_t firmwareVersion = -1); private: - TwoWire *_WireObject; // Pointer to passed Wire object + TwoWire &_WireObject; // Reference to passed Wire object const uint8_t _eepromAddress; // i2c address for our EEPROM const uint16_t _kbitSize; // Size of our EEPROM in kilobits diff --git a/src/SigmaDSP.cpp b/src/SigmaDSP.cpp index 26ef523..15fc83d 100644 --- a/src/SigmaDSP.cpp +++ b/src/SigmaDSP.cpp @@ -8,13 +8,14 @@ /*************************************** Function: SigmaDSP() Purpose: Constructor of the class. -Inputs: uint8_t i2cAddress; DSP i2c address - uint8_t device; The DSP type, e.g ADAU1701 - uint8_t resetPin; pin to reset DSP (optional parameter) +Inputs: TwoWire &WireObject; Reference to Wire object + uint8_t i2cAddress; DSP i2c address + uint8_t device; The DSP type, e.g ADAU1701 + uint8_t resetPin; pin to reset DSP (optional parameter) Returns: None ***************************************/ -SigmaDSP::SigmaDSP(uint8_t i2cAddress, uint8_t device, int8_t resetPin) - : _dspAddress(i2cAddress), _deviceType(device), _resetPin(resetPin) +SigmaDSP::SigmaDSP(TwoWire &WireObject, uint8_t i2cAddress, uint8_t device, int8_t resetPin) + : _WireObject(WireObject), _dspAddress(i2cAddress), _deviceType(device), _resetPin(resetPin) { } @@ -23,48 +24,10 @@ SigmaDSP::SigmaDSP(uint8_t i2cAddress, uint8_t device, int8_t resetPin) /*************************************** Function: begin() Purpose: Starts the i2c interface -Inputs: TwoWire Wire; Wire object (optional parameter) Returns: None ***************************************/ -void SigmaDSP::begin(TwoWire *WireObject) +void SigmaDSP::begin() { - // Store copy the passed object - _WireObject = WireObject; - - _WireObject->begin(); - - // Reset DSP if pin is present - if(_resetPin >= 0) - { - pinMode(_resetPin, OUTPUT); - digitalWrite(_resetPin, HIGH); - reset(); - } -} - - -/*************************************** -Function: begin() -Purpose: Starts the i2c interface -Inputs: TwoWire Wire; Wire object - uint8_t sdaPin; SDA pin - uint8_t sclPin; SCL pin -Returns: None -***************************************/ -void SigmaDSP::begin(TwoWire *WireObject, uint8_t sdaPin, uint8_t sclPin) -{ - // Store copy the passed object - _WireObject = WireObject; - - // This hardware supports redefining SDA and SCL in begin() - #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) - _WireObject->begin(sdaPin, sclPin); - #else // This does not - (void)sdaPin; - (void)sclPin; - _WireObject->begin(); - #endif - // Reset DSP if pin is present if(_resetPin >= 0) { @@ -83,7 +46,7 @@ Returns: None ***************************************/ void SigmaDSP::i2cClock(uint32_t clock) { - _WireObject->setClock(clock); + _WireObject.setClock(clock); } @@ -115,8 +78,8 @@ Returns: 0 - success: ack received ***************************************/ uint8_t SigmaDSP::ping() { - _WireObject->beginTransmission(_dspAddress); - return _WireObject->endTransmission(); + _WireObject.beginTransmission(_dspAddress); + return _WireObject.endTransmission(); } @@ -997,15 +960,15 @@ void SigmaDSP::writeRegister(uint16_t memoryAddress, uint8_t length, uint8_t *da uint8_t LSByte = (uint8_t)memoryAddress & 0xFF; uint8_t MSByte = memoryAddress >> 8; - _WireObject->beginTransmission(_dspAddress); // Begin write + _WireObject.beginTransmission(_dspAddress); // Begin write - _WireObject->write(MSByte); // Send high address - _WireObject->write(LSByte); // Send low address + _WireObject.write(MSByte); // Send high address + _WireObject.write(LSByte); // Send low address for(uint8_t i = 0; i < length; i++) - _WireObject->write(data[i]); // Send all bytes in passed array + _WireObject.write(data[i]); // Send all bytes in passed array - _WireObject->endTransmission(); // Write out data to I2C and stop transmitting + _WireObject.endTransmission(); // Write out data to I2C and stop transmitting } @@ -1023,15 +986,15 @@ void SigmaDSP::writeRegister(uint16_t memoryAddress, uint8_t length, const uint8 uint8_t LSByte = (uint8_t)memoryAddress & 0xFF; uint8_t MSByte = memoryAddress >> 8; - _WireObject->beginTransmission(_dspAddress); // Begin write + _WireObject.beginTransmission(_dspAddress); // Begin write - _WireObject->write(MSByte); // Send high address - _WireObject->write(LSByte); // Send low address + _WireObject.write(MSByte); // Send high address + _WireObject.write(LSByte); // Send low address for(uint8_t i = 0; i < length; i++) - _WireObject->write(pgm_read_byte(&data[i])); // Send all bytes in passed array + _WireObject.write(pgm_read_byte(&data[i])); // Send all bytes in passed array - _WireObject->endTransmission(); // Write out data to I2C and stop transmitting + _WireObject.endTransmission(); // Write out data to I2C and stop transmitting } @@ -1057,15 +1020,15 @@ void SigmaDSP::writeRegisterBlock(uint16_t memoryAddress, uint16_t length, const MSByte = memoryAddress >> 8; LSByte = (uint8_t)memoryAddress & 0xFF; - _WireObject->beginTransmission(_dspAddress); - _WireObject->write(MSByte); // Send high address - _WireObject->write(LSByte); // Send low address + _WireObject.beginTransmission(_dspAddress); + _WireObject.write(MSByte); // Send high address + _WireObject.write(LSByte); // Send low address for(uint8_t i = 0; i < registerSize; i++) // Send n bytes { - _WireObject->write(pgm_read_byte(&data[bytesSent])); + _WireObject.write(pgm_read_byte(&data[bytesSent])); bytesSent++; } - _WireObject->endTransmission(); + _WireObject.endTransmission(); memoryAddress++; // Increase address } @@ -1085,26 +1048,26 @@ int32_t SigmaDSP::readBack(uint16_t memoryAddress, uint16_t readout, uint8_t num uint8_t LSByte = (uint8_t)memoryAddress & 0xFF; uint8_t MSByte = memoryAddress >> 8; - _WireObject->beginTransmission(_dspAddress); // Begin write - _WireObject->write(MSByte); // Send high address - _WireObject->write(LSByte); // Send low address + _WireObject.beginTransmission(_dspAddress); // Begin write + _WireObject.write(MSByte); // Send high address + _WireObject.write(LSByte); // Send low address LSByte = (uint8_t)readout & 0xFF; MSByte = readout >> 8; - _WireObject->write(MSByte); // Send high register to read - _WireObject->write(LSByte); // Send low register to read - _WireObject->endTransmission(); + _WireObject.write(MSByte); // Send high register to read + _WireObject.write(LSByte); // Send low register to read + _WireObject.endTransmission(); - _WireObject->beginTransmission(_dspAddress); + _WireObject.beginTransmission(_dspAddress); LSByte = (uint8_t)memoryAddress & 0xFF; MSByte = memoryAddress >> 8; - _WireObject->write(MSByte); - _WireObject->write(LSByte); - _WireObject->endTransmission(false); + _WireObject.write(MSByte); + _WireObject.write(LSByte); + _WireObject.endTransmission(false); int32_t returnVal = 0; - _WireObject->requestFrom(_dspAddress, numberOfBytes); + _WireObject.requestFrom(_dspAddress, numberOfBytes); for(uint8_t i = 0; i < numberOfBytes; i++) - returnVal = returnVal << 8 | _WireObject->read(); + returnVal = returnVal << 8 | _WireObject.read(); return returnVal; } @@ -1122,15 +1085,15 @@ uint32_t SigmaDSP::readRegister(dspRegister reg, uint8_t numberOfBytes) uint8_t LSByte = (uint8_t)reg & 0xFF; uint8_t MSByte = reg >> 8; - _WireObject->beginTransmission(_dspAddress); // Begin write - _WireObject->write(MSByte); // Send high address - _WireObject->write(LSByte); // Send low address - _WireObject->endTransmission(false); + _WireObject.beginTransmission(_dspAddress); // Begin write + _WireObject.write(MSByte); // Send high address + _WireObject.write(LSByte); // Send low address + _WireObject.endTransmission(false); uint32_t returnVal = 0; - _WireObject->requestFrom(_dspAddress, numberOfBytes); + _WireObject.requestFrom(_dspAddress, numberOfBytes); for(uint8_t i = 0; i < numberOfBytes; i++) - returnVal = returnVal << 8 | _WireObject->read(); + returnVal = returnVal << 8 | _WireObject.read(); return returnVal; } diff --git a/src/SigmaDSP.h b/src/SigmaDSP.h index 4beb6fd..ddca06b 100644 --- a/src/SigmaDSP.h +++ b/src/SigmaDSP.h @@ -51,11 +51,10 @@ class SigmaDSP { public: // Store passed device type and i2c address to private constants - SigmaDSP(uint8_t i2cAddress, uint8_t device, int8_t resetPin = -1); + SigmaDSP(TwoWire &WireObject, uint8_t i2cAddress, uint8_t device, int8_t resetPin = -1); // Init and setup - void begin(TwoWire *WireObject = &Wire); - void begin(TwoWire *WireObject, uint8_t sdaPin, uint8_t sclPin); + void begin(); void i2cClock(uint32_t clock); void reset(); uint8_t ping(); @@ -177,7 +176,7 @@ class SigmaDSP void linspace(float x1, float x2, float n, float *vect); // Objects - TwoWire *_WireObject; // Pointer to passed Wire object + TwoWire &_WireObject; // Reference to passed Wire object // Private constants const uint8_t _dspAddress; // Passed device i2c address