Notifications
Clear all

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

302 Posts
10 Users
229 Likes
20.5 K Views
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@ronsmitsnl Yeh, I'm probably going to do something with git at some point, but haven't written anything complex enough to merit it yet, in my estimation.

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


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

@binaryrhyme

Posted by: @binaryrhyme

@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. 😉

Sorry, but I haven't been here for quite a while... which example is this then... have you posted others?

In my opinion, if you want to provide tutorials, that is fantastic and I fully support it, but it is imperative that we all provide accurate information and not mislead the viewers (especially newbies), because I will call it out if I see it is wrong - it's my duty 😉

Cheers


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

@zander The conversation is helpful. I was expecting more questions/complaints about complexity on example #2, actually. 😉

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


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

@frogandtoad Yeh, I think I need to do some video walk throughs. When I replied to you, I thought I was in the thread of the 2nd example, lol. This IS the simple one. 😉

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


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

@binaryrhyme That's another of the 'modern' tools I know nothing about, is sneaker net still a thing? I am fortunate now that I am down to one main machine (Mac M1) that I can also VNC to my RasPi's (3) and of course all the Arduino stuff is actually on the Mac (source code). I have a rugged raid, a NAS, several clouds, do I really need another in the form of git? Not saying no, just need a little bit of convincing to go down yet another rabbit hole. At 80 it's getting difficult to keep learning new stuff. The list of stuff I have given up on as too difficult is getting very long. What I really mean of course is that the effort to learn new is not worth replacing what is already working for me or the cost/benefit is not great enough.

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
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@binaryrhyme

Posted by: @binaryrhyme

@zander The conversation is helpful. I was expecting more questions/complaints about complexity on example #2, actually. 😉

Oh, and example #2?

OK, let me have a search for it...


   
Biny reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7061
 
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();
}

How can I copy this code to put into the IDE?

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
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@zander The upside of C++ is, if you understand how the compiler behaves under the covers, the overhead is minimal, and often OO techniques actually result in savings (e.g. all the code in a class exists once, no matter how many instances of that class are instantiated, while often programmers clone the same code unnecessarily - but if you are doing hardcore real time stuff, you set the compiler to dump assembly code, and inspect it - changing how a loop is written can result in enormous efficiency gains - actually, true even if not using OO, lol.) That having been said, the major benefits of OO only manifest in projects of increasing complexity, as functionality is encapsulated - we've all used it, as most arduino libraries present the underlying hardware as an object (e.g. Motor)

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


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

@frogandtoad I also can't find example 2

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
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7061
 

@binaryrhyme My code didn't 'clone ...'. In fact we stole the best ideas of OO and implemented them in subset G. Couldn't do it in full set, but subset G had a wrapper concept that made each module the equivalent of a class. that and a naming convention for class private variables of a prefix of self_ made oue normal PL1 and C look a lot like OOP/++. Of course back then there was no oop available for our OS either, so not a fair comparison.

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
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@zander I hired some very bright folks outside of the parameters you describe. One lad had even failed to complete his degree, but someone very smart told me he was good - so I brought him on. He has innumerable (as in, I haven't counted them) patents and is now the VP R&D of a substantial company. Some folks have 20 years experience - some have 1 year times twenty.

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: 7061
 

@binaryrhyme YES, I also have no degree and have a couple 'patents' and was owner of my own consulting company. I interviewed a lot of people with 20x1Yr experience, and had the great pleasure of mentoring several of those no training individuals. However, I think you will admit those folks are rare.

When I joined IBM IS I was the only one without an engineering ring. I was shunned. Eventually (5ish years) I was the #1 employee and they all had to come to me for approval and code inspections. Ultimately I had 300 people needing my sign off. 

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
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@zander You can't just cut and paste the code from the forum into a new sketch? I'm happy to post the code as a .zip if that's not working for you.

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


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

@zander @frogandtoad  Object Oriented techniques – Example 2 – Display Manager – C++ – DroneBot Workshop Forums

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


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

@binaryrhyme It's too long, but maybe I can do it in 2 pieces, let me try.

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 3 / 21