Static Scoping Trouble

Hi.

I’m having trouble getting a static constant object to compile properly, it complains that the initalization is out-of-scope even though it patently is, and I’ve used similar code elsewhere in the same sketch which does compile and function fine.

I’ve tried various different iterations of addind and removing the scoping operators in places and moving the initialization into and out of the header file (moving it in just creates more, different errors). I and my two colleagues have absolutely no idea whats going on: as far as we can work out, everything is correct.

I have also tried making it non-static and initalizing in the constructor, which does work, but will then obviously lead to unnecessary duplication at runtime as there will be a SPISettings object per Ioc_max5724_dac object, of which there will be an arbitrary number.

Potentially Relevant Information (just in case): I am compiling to a Genuine Ardunio Due (none of this Italian rubbish), and I have modified the compiler flags slightly, changing the optimization level from -Os to -O3, and adding -std=c++11 in order to get lambda expression and constant expression support.

Thank you for any help you might be able to provide!

The Error Messages:

IOC_max5724_dac.cpp:17:19: error: 'Ioc_Max5742_Dac' has not been declared
 const SPISettings Ioc_Max5742_Dac::SPI_SETTINGS(SPI_BUS_CLOCK, MSBFIRST, SPI_MODE3);
                   ^
IOC_max5724_dac.cpp:17:49: error: 'SPI_BUS_CLOCK' was not declared in this scope
 const SPISettings Ioc_Max5742_Dac::SPI_SETTINGS(SPI_BUS_CLOCK, MSBFIRST, SPI_MODE3);
                                                 ^
Error compiling.

IOC_max5724_dac.h:

#ifndef IOC_MAX5724_DAC_H
#define IOC_MAX5724_DAC_H

#ifndef __arm__ || _SAM3XA_
  #error Ioc_Max5724_Dac class only works with the Arduino Due!
#endif

#include <Arduino.h>
#include <SPI.h>

class Ioc_Max5724_Dac
{
  public:
  
    Ioc_Max5724_Dac(byte selectPin, byte clearPin);
    
    void write(byte channel, unsigned int value);

    void clear();

    //Disable unused destructor, copy assignment and copy constructor.
    ~Ioc_Max5724_Dac() = delete;
    Ioc_Max5724_Dac& operator=(const Ioc_Max5724_Dac&) = delete;
    Ioc_Max5724_Dac(const Ioc_Max5724_Dac&) = delete;
  
  private:
  
    /// <summary>
    /// Speed at which the SPI clock should run for the DACs: 4MHz.
    /// </summary>
    static constexpr uint32_t SPI_BUS_CLOCK = 4000000;
    static const SPISettings SPI_SETTINGS;
    static constexpr unsigned int ADC_MAX_VALUE = 0x3FF;
    
    byte _selectPin;
    byte _clearPin;
    
};

#endif

IOC_max5724_dac.cpp:

#include "IOC_max5724_dac.h"

const SPISettings Ioc_Max5742_Dac::SPI_SETTINGS(SPI_BUS_CLOCK, MSBFIRST, SPI_MODE3);

Ioc_Max5724_Dac::Ioc_Max5724_Dac(byte selectPin, byte clearPin) :
  _selectPin(selectPin),
  _clearPin(clearPin)
{
  //Initialise the SPI communication interface with the DAC.
  SPI.begin(selectPin);
  
  //Setup the clear/reset pin on the DAC.
  pinMode(clearPin, OUTPUT);
  digitalWrite(clearPin, HIGH);
}

void Ioc_Max5724_Dac::write(byte channel, unsigned int value)
{
  //Constrain the input value to ADC_MAX_VALUE
  value = value & ADC_MAX_VALUE;
  
  byte message[3] = {
    channel | B10000000, // CODEn + DAC selection (Page 21 MAX5724 datasheet)
    (byte)(value >> 2),
    (byte)value & B00000011
  };
  
  SPI.beginTransaction(SPI_SETTINGS);
  SPI.transfer(_selectPin, message[0], SPI_CONTINUE);
  SPI.transfer(_selectPin, message[1], SPI_CONTINUE);
  SPI.transfer(_selectPin, message[2], SPI_LAST);
  SPI.endTransaction();
}

void Ioc_Max5724_Dac::clear()
{
  digitalWrite(_clearPin, LOW);
  digitalWrite(_clearPin, HIGH);
}

Given the many vagaries of the Arduino build process, my guess would be the problem is the names of the .cpp and .h files do not match the name of the class…

Regards,
Ray L.

Hi. Unfortunately changing the names of the files to match the internal class name had no effect. Any other ideas?

I think you are going to have a 'doooh' moment :) : Compare Ioc_Max5742_Dac versus Ioc_Max57*24*_Dac

:o

headdesk

:(

Thank you.