Go Down

Topic: Implementing a singleton class. (Read 10 times) previous topic - next topic

PaulS

Quote
Its Serial.begin(9600) not Serial.instance.begin()

But, Serial IS an instance of the HardwareSerial class.

Si

#6
Jan 20, 2011, 02:21 pm Last Edit: Jan 20, 2011, 02:22 pm by simon.monk Reason: 1
Ah, ok - I just thought it was a class with a load of static methods on it (I am actually talking Java and hoping its the same in C++).

So is Serial just a global variable assigned to an instance of a class called something like 'SerialImpelementation' ?

In that case, it sounds like that would be a good thing for our original poster to copy.
--
My New Arduino Book: http://www.arduinobook.com

Si

Just read the original post properly and I'm talking rubbish.

Ignore everything I have said.
--
My New Arduino Book: http://www.arduinobook.com

robtillaart

Quote
Does this mean an Arduino can be multithreaded? if so how? I thought arduino was single threaded only?


ALthough PaulS is right that the Arduino has no OS there are efforts in such that direction, see - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256745982

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dafid

Here is my sort of singlton in your code//
Code: [Select]
#include <SdFat.h>
#include <SdFatmainpage.h>
#include <SdFatUtil.h>
#include <SdInfo.h>


class Logger
{
 public:
   Logger() {} ; // Constructor does nothing now as no per logger state
   void writeLine(char * line);
   void close() ;

 private:
   void open() ;

   // Static Member variables that implement the singleton
   static MySDFat logFile;
   static char state;
   static int lineCount;      

};


and the library code is
Code: [Select]
#include <Logger.h>

// initialise the static memeber variables.

MySDFat Logger::logFile ; // set constructor parameters here as/and if required.

#define CLOSED 0
#define OPENED 1
char Logger::state = CLOSED ;

int  Logger::lineCount = 0 ;

// Ensure the single chosen file is open for logging
void Logger::open()
{
 if (state != OPENED)
 {
    // open the file :)
    state = OPENED ;
 }
}

void Logger::close()
{
 if (state == OPENED)
 {
   // close the file :)
   state = CLOSED ;
 }
}

void Logger::writeLine(char *line)
{
 open() ; // open file      
 // logFile.writeLine(line) ;
 lineCount ++ ;
}



This pattern (adjusted for the way the code really works) should make your test example (above) log all of the lines from both Logger objects.

It will not work if you use logging in the interrupt handlers as well as in the main loop of the code.

And doing that is a MUCH bigger problem, and also not sensible IMHO.

Dave


Go Up