Using Serial inside a namespace

Hi there,
I'm experimenting using a namespace to implement something Singleton-esque as per this example. I'll provide the code below.

Background info: this is intended as a code debugging tool using PLX-DAQ to enable me to write values to a spreadsheet from within any of my class member functions. I have used PLX-DAQ extensively; it works fine. I need this code to be a) globally available b) provide data hiding for it's variables and some utility functions and c) only ever have one instance. I'm using an anonymous namespace to provide data hiding as per this thread. I want to avoid using a pure Singleton class for various reasons that I'm happy to discuss.

This code compiles fine when simply placed in the .ino file, and , tests fine. I am using Serial.print and Serial.printline to debug the debugging code (lol).

However, when I place everything into a namespace in header and source files, the compiled complains about Serial:

....DebuggerPLX-DAQ.cpp:44:7: error: 'Serial' was not declared in this scope
Serial.println(debugDataArray[i].data.aString);
^~~~~~

I've done a bunch of searching here and on other forums but can't seem to find an answer to this. I may have missed something.

Here is my code. I have commented out all the statements using Serial except one (yes, I am using String in other of the statements; I realize String is anathema; I am making very limited use of it purely for debugging).

Thanks in advance for any insight anyone can offer!

cpp file:

#include "DebuggerPLX-DAQ.h"
#include <String.h>

namespace debugger_plx_daq {

  //===============================================================
  // Statement using Serial that won't compile is in this function
  bool writeData(char* label, char* datum){
    int index = findLabelIndex(label);
    if( !(index == -1) ){
      startIndex = 0;
      copyCharArray(datum, &debugDataArray[index].data.aString[0], strlen(datum), maxStringLength);
      debugDataArray[index].dataType = STRING_TYPE;
    }
    for(int i = 0; i < 10; i++){
      Serial.println(debugDataArray[i].data.aString); 
      //^^^^ This line ^^^^
      //Serial.println((String) "           " + i);
    }
  }

  void setLabels(char* labels){
    startIndex = endIndex = dataIndex = index = 0;
    int stringLength = strlen(labels);
    while( (dataIndex < maxNumDebugDatum) && (index <= stringLength) ){
      if( (labels[index] == ',') || (index == stringLength) ){ 
        endIndex = index - startIndex; 
        copyCharArray(&labels[startIndex], &debugDataArray[dataIndex].label[0], endIndex, maxStringLength);
        dataIndex++;
        startIndex = index + 1;
      }
      index++;
    }; 
  }

  bool writeData(char* label, int datum){
    int index = findLabelIndex(label);
    if( !(index == -1) ){
      debugDataArray[index].data.anInt = datum;
      debugDataArray[index].dataType = INT_TYPE;
    }
  }

  bool writeData(char* label, unsigned long datum){
    int index = findLabelIndex(label);
    if( !(index == -1) ){
      debugDataArray[index].data.aULong = datum;
      debugDataArray[index].dataType = ULONG_TYPE;
    }
  }

  namespace{ //anonymous namespace provides hiding of member data and functions
    void copyCharArray(char* source, char* dest, int sourceEnd, int destEnd){
      int index =  0;
      //Serial.println((String) "copyCharArray: source = " + source);
      //Serial.println((String) "copyCharArray: dest = " + dest);
      while( (index < sourceEnd) && (index < destEnd) ){
        dest[index] = source[index];
        index++;
      };
      dest[index] = '\0';
    }


    int findLabelIndex(char* label){
      index = -1;
      for(int i = 0; i < 20; i++){
        if( stringsEqual(debugDataArray[i].label, label) ){
          index = i;
          break;
        }
      }
      return index;
    }

    bool stringsEqual(char* aString, char* bString){
      if( !(strlen(aString) == strlen(bString)) ){ return false; }

      for(int i = 0; i<strlen(aString); i++){
        if( !(aString[i] == bString[i]) ){ return false; }
      }
      return true;
    }
  } //end anonymous namespace
} //end namespace debugger_plx_daq

Header file:

#include <String.h>

namespace debugger_plx_daq{

  void setLabels(char* labels);
  bool writeData(char* label, int datum);
  bool writeData(char* label, unsigned long datum);
  bool writeData(char* label, char* datum);
  void copyCharArray(char* source, char* dest, int sourceEnd, int destEnd);
  int  findLabelIndex(char* label);
  bool stringsEqual(char* aString, char* bString);
  
  namespace{

    static const int maxStringLength = 30;
    static const int maxNumDebugDatum = 20;

    enum type_tag {STRING_TYPE, INT_TYPE, ULONG_TYPE};
    union DebugData{
      char aString[maxStringLength + 1];
      int anInt;
      unsigned long aULong;
    };

    struct DebugDatum{
      char label[maxStringLength + 1];
      DebugData data;
      enum type_tag dataType;
    };

    DebugDatum debugDataArray[maxNumDebugDatum];
    
    int dataIndex = 0,  //index of position in debugData[]
        startIndex = 0, //index to start copying next label from char* labels
        endIndex = 0,   //index to end copying next label from char* labels
        index = 0;      //parse index for char* labels
  }

}

ino file:


#include<String.h>

void setup() {
  Serial.begin(9600);
  Serial.println("begin");
}

void loop() {}
#include <Arduino.h>
1 Like

lol, I'm an idiot
It's interesting that that isn't required in the sketch file. I guess it makes sense though.

Should I delete this question?

No.

If you had this trouble it is possible that some one else could also have it and the solution might help them, later.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.