Object Orientation on the photon/P0?


I was wondering if there were any specific guides or documents on creating Object Oriented programs using the particle photon? I intend to use an abstract class, and therefore some inheritance and polymorphism.

Specifically I am using a bunch of sensors that I would like to be able to access each through reading and/or initialization functions. I only intend to have one instance of each sensor although the sensors will be stored together in a polymorphic array so I can cycle through them and call their read functions.

I’ve used OO before in Java although I’m not too sure about the limitations of C. I’ve had a trawl through google results on this although for obvious reasons, OO isn’t too popular on MCUs so there aren’t that many results

The Particle devices use a standard gcc C++11 compiler, so all of the object-oriented features of C++ are available. Of course you have to be a aware of code size, memory size, and stack size available to programs, but in terms of object-oriented features you can use all of them you’d like to.


I understand that in theory I should be able to, although now i’ve finished doing a test program i’m having a few issues:

-One is that the code doesn’t seem to want to go on there, I’ve tried flashing the code and particle dev says ‘success’ although its still printing out values from the old code on the serial monitor as if I hadn’t uploaded anything.

-Another is that it seems to ignore all my errors. I have a custom class called CO2_Sensor that i instansiated as co2sensor. I can type ‘co3snener’ or any kind of rubbish and it won’t throw up any errors when I validate the code. Same goes for any erroneous function calls that I make.

What i’ve done is made an abstract called ‘Sensors’ and in that same cpp file have defined a child of that class called ‘MG811_CO2’. There are no errors from what I can see and the compiler doesn’t seem to be throwing up any.

In my main code I am simply creating an instance of the MG811_CO2 class and using some of its functions.

I was wondering if maybe I should look into compiling locally or something? Maybe separating each class into its own file? I don’t know where to start with fixing this…

@wsw, is there any chance you can point to a repo with your code? Separating classes into their own files is always a good idea and it makes debugging a lot easier. As for local compiling, I tend to favor it over using the IDE. However, I use Particle CLI and DEV when I need to check code against the Particle compile farm or for quick and dirty OTA flashing. :smiley:

Here’s the sensor class: http://pastebin.com/K4KapKXn

And here’s the main ino file: http://pastebin.com/hFwXsnrC

I think i’ll look into local compiling on monday!

The symptoms you are describing there are not uncommon with Dev.
It behaves somewhat counterintuitive.

  • make sure you save all your files before building (no cyan dots in the file tabs left)
  • don’t only push the build and flash button but first the build :cloud: button and only then the flash (lightning bolt) button - or remove any present .bin files in your project folder
  • make sure you got the project folder selected and not just a project file as only the folder will be built. The selected file will only be edited in Dev.
  • make sure you selected the correct target device

Other members could be helped when they provided a screenshot of their Dev window to spot other issues.

In your code is one puzzling line

  bool checkRead*long previousMillis, int interval)         

You should also do your millis() checks slightly different
See here

1 Like

Ah yes the * was a typo for (

Its hard to see when the compiler doesn’t flag up anything! I’ve managed to get local compiling going after some hacking about with the toolchain. and I’m getting some strange errors flagging up. Here’s the full list of errors:

In file included from applications/Sensio/Application.cpp:11:0:
applications/Sensio/MG811_CO2.cpp:6:1: error: expected class-name before '{' token
applications/Sensio/MG811_CO2.cpp: In constructor 'MG811_CO2::MG811_CO2(int, int)':
applications/Sensio/MG811_CO2.cpp:14:13: error: 'class MG811_CO2' has no member named 'pin'
     this -> pin = pinNum;
applications/Sensio/MG811_CO2.cpp:15:13: error: 'class MG811_CO2' has no member named 'sampleNum'
     this -> sampleNum = sampleCount;
applications/Sensio/MG811_CO2.cpp:15:25: error: 'sampleCount' was not declared in this scope
     this -> sampleNum = sampleCount;
applications/Sensio/MG811_CO2.cpp: In member function 'float MG811_CO2::read()':
applications/Sensio/MG811_CO2.cpp:20:5: error: 'previousMillis' was not declared in this scope
     previousMillis = millis();                                                  //Reset timer
applications/Sensio/MG811_CO2.cpp:21:31: error: 'class MG811_CO2' has no member named 'pin'
     return analogRead(this -> pin);                                                //Return analog value
applications/Sensio/MG811_CO2.cpp: In member function 'void MG811_CO2::init()':
applications/Sensio/MG811_CO2.cpp:26:5: error: 'previousMillis' was not declared in this scope
     previousMillis = millis();
applications/Sensio/Application.cpp: In function 'void loop()':
applications/Sensio/Application.cpp:57:17: error: 'class MG811_CO2' has no member named 'checkSample'
   if (co2sensor.checkSample(previousTime, interval)  == true)
applications/Sensio/Application.cpp:59:15: error: 'class MG811_CO2' has no member named 'sample'
applications/Sensio/Application.cpp:62:17: error: 'class MG811_CO2' has no member named 'checkRead'
   if (co2sensor.checkRead(previousTime, interval) == true)

Allow me to familiarise myself with the code again and I will get back to you all with a more useful response.