Notifications
Clear all

[Closed] Object Oriented techniques - Example 1 - "Hello Class"

302 Posts
10 Users
229 Likes
19.9 K Views
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 
Posted by: @binaryrhyme

Β with the sketches sync'd on one drive, which was itself a minor rabbit hole of its own.Β 

Glad you like.

As I use 2 machines to write code I just create a private git repository on gitlab.com and commit and push. That way the code is available where ever I want it.


   
Biny and Inst-Tech reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@binaryrhyme

Posted by: @binaryrhyme

Folks:

As promised, I am providing a few examples of how to use OO techniques in C++ for microcontrollers. The first example is a rewrite of the classic Arduino "Hello World" sketch. It's very basic, but at the same time will seem over complex for what it accomplishes. The benefits will become clear in subsequent examples, I hope, but thought we should start with how classes are constructed using a familiar example.

/* ********** Constant Declarations ********** */
const int PIN_GeenLed = 8;


/* ********** LED Class ********** */
class CLed
{
   public:
      CLed(int LedPin, int blinkDelay);
      void LoopTick();
   private:
      int _ledPin;
      int _blinkDelay;
};

/* CLed Constructor */
CLed::CLed(int LedPin, int blinkDelay) {
   _ledPin = LedPin;
   _blinkDelay = blinkDelay;

  // Initialize LED Pin
  pinMode(_ledPin, OUTPUT); // Green LED Pin
}

void CLed::LoopTick() {
   digitalWrite(_ledPin, HIGH); // turn on Green LED
   delay(_blinkDelay);

   digitalWrite(_ledPin, LOW); // turn off Green LED
   delay(_blinkDelay); 
}

/* CLed - End Class */


/* ************************************************** */
/* ********** Global Variable Declarations ********** */
int BlinkWait = 100;

/* ********** Global Class Instances ********** */
CLed GreenLed(PIN_GeenLed, BlinkWait);



/* ************************************ */
/* ********** Setup Function ********** */
void setup() {

  // Initialize Serial Ports
  Serial.begin(9600); // Serial Monitor

}

/* ********** Main Loop ********** */
void loop() {

   Serial.print("Top of Loop\n");

   GreenLed.LoopTick();
}

Firstly, I commend you for trying to promote the benefits of OOP in C++ under Arduino, it's something I have tried to promote in the past without support.

However, I think your example is far too complicated for the Arduino C++ newbie, and sorry to say that this kind of example is exactly what puts them off learning OOP πŸ™


   
Biny and Ron reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 
Posted by: @frogandtoadFirstly, I commend you for trying to promote the benefits of OOP in C++ under Arduino, it's something I have tried to promote in the past without support.

However, I think your example is far too complicated for the Arduino C++ newbie, and sorry to say that this kind of example is exactly what puts them off learning OOP πŸ™

Β 

To be honest I dont think there is a more simple example of a C++ class that does something on an Arduino. Maybe a bit more comments to explain what is happening would work?


   
Ron and Inst-Tech reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@ronsmitsnl

Posted by: @ronsmitsnl
Posted by: @frogandtoadFirstly, I commend you for trying to promote the benefits of OOP in C++ under Arduino, it's something I have tried to promote in the past without support.

However, I think your example is far too complicated for the Arduino C++ newbie, and sorry to say that this kind of example is exactly what puts them off learning OOP πŸ™

To be honest I dont think there is a more simple example of a C++ class that does something on an Arduino. Maybe a bit more comments to explain what is happening would work?

Well, explanation and comments are number one priority, and none at all were provided...Β 

For example:

1) Does the newbie understand what a class is?
2) Was the newbie instructed as to how to declare a class?
3) Was the newbie instructed as to what a constructor is and its role?
4) Was the newbie instructed how to implement a constructor?
5) Was the newbie instructed in how and why functions are implemented outside the class?
6) Was the newbie infomed about scope resolution?
7) Was the newbie informed about access specifiers and their effects?
8) Was the newbie informed of how to instantiate an object?
9) Was the newbie informed that everything in C++ is an object?

I could go on...

None of this information was provided?
Perhaps this is why so many nebies are scared away from C++ so fast?

Please feel free to refute the above, but please provide some facts to back your position, should you respond.

Cheers.


   
Biny, Ron and ron bentley reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@ronsmitsnl

Posted by: @ronsmitsnl
Posted by: @frogandtoadFirstly, I commend you for trying to promote the benefits of OOP in C++ under Arduino, it's something I have tried to promote in the past without support.

However, I think your example is far too complicated for the Arduino C++ newbie, and sorry to say that this kind of example is exactly what puts them off learning OOP πŸ™

To be honest I dont think there is a more simple example of a C++ class that does something on an Arduino. Maybe a bit more comments to explain what is happening would work?

Sorry, I forgot to ask... do you have C++ experience? or are you a newbie to C++?

Cheers


   
Ron reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 

@frogandtoad I don't consider myself a newbie in C++ (or OOP in general) I have developed in C for around 10 years (mainly device drivers for AT&T Unix) and C++ for around 4 years (mainly back end servers)

I dont consider myself in expert in C++ as it has been some years (2018) that I did some C++ work


   
Biny and frogandtoad reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@ronsmitsnl

Posted by: @ronsmitsnl

@frogandtoad I don't consider myself a newbie in C++ (or OOP in general) I have developed in C for around 10 years (mainly device drivers for AT&T Unix) and C++ for around 4 years (mainly back end servers)

I dont consider myself in expert in C++ as it has been some years (2018) that I did some C++ work

That's fine, I hope I didn't come across as a bully... I'm always trying to understand peoples skill set so I know how to communicate with them.

We all have different skill sets, and I'm here to seek help as much as I am here to provide it πŸ˜‰

Cheers


   
Biny reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 

@frogandtoad you did not come across as a bully. Asking something in a conversation where they are experience or knowledge wise, can make a conversation so much better.


   
Biny and frogandtoad reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@ronsmitsnl

Posted by: @ronsmitsnl

@frogandtoad you did not come across as a bully. Asking something in a conversation where they are experience or knowledge wise, can make a conversation so much better.

Thank you sir!

Unfortunately, some people just take it the wrong way, so I'm glad that you understand.

I have a lot of experience in C++ (qualified software and networking), and I like to keep up to date as best I can with the latest ISO standard, but things move so fast, that it's not easy to keep up - I am getting too old πŸ™‚

I am also a qualified fitter and turner, and toolmaker, and hydraulic's and pneumatic's, and CNC - Yeah... I have wasted 25+ years of post tertiary schooling... forgotten a lot of it, sometimes, but loving it!

Cheers πŸ˜‰


   
Biny reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@frogandtoad I managed to avoid all the OOP stuff, never saw the need and had long ago developed coding techniques that got all the benefits without the overhead. Coming from a hardware/microcode/machine code/assembly background I have a different bias. Too late for me to learn now, but it doesn't hurt to have more tools in the toolbox. Good example.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny and ron bentley reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@ronsmitsnl Just an FYI, back in the day when I was hiring, any resume with less than 10 yrs experience was binned. Most hires were 15 to 20 yrs experience, then I had to train them out of all their bad habits.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny and ron bentley reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@frogandtoad I managed to avoid all the OOP stuff, never saw the need and had long ago developed coding techniques that got all the benefits without the overhead. Coming from a hardware/microcode/machine code/assembly background I have a different bias. Too late for me to learn now, but it doesn't hurt to have more tools in the toolbox. Good example.

That's fine if you don't want to learn it, but it can be highly advantageous when your programs start to grow in size and complexity, which brings me to the point you raised regarding overhead.

This is common misconception, that C++ classes and OOP cause overhead - It is simply not true, and in fact, classes can reduce overhead due to repeated procedural code.

The size of a class is made up of its data members... a class only encapsulates them and does not add to the size.

It's never too late to learn... life is a learning experience πŸ˜‰

Cheers


   
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@frogandtoad Check out my first example. My goal on this one is to do something useful. I kept it relatively simple - no linked lists, no error checking, etc... but I agree it's a reasonably decent study in various techniques. πŸ˜‰

Edit: oops. This IS the simple one, lol. Yeh, my goal is not to do a course on OO - LOTS of them about on youtube, but I'm happy to answer questions, or if there is demand, I'm happy to record walk throughs of the examples. I thought this example too trivial, but perhaps not.

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Ron reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@ronsmitsnl Lol. No shortage of language wars about, but to each their own. πŸ˜‰

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@binaryrhyme And it started so simply.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Page 2 / 21