Notifications
Clear all

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

302 Posts
10 Users
229 Reactions
22.1 K Views
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 
Posted by: @binaryrhyme

@zander Get yourself into orbit. Time runs slower there. 🙂

Only your time relative to those still on Earth.  You will experience time at the same rate.

 


   
Biny and Ron reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 

@will 

Perhaps I'm wrong,, but it seems to me that those people here with the electronics backgrounds don't want to have to learn programming and the ones with programming backgrounds don't want to learn any more about electronics than they need to make their devices operate.

That is my impression as well. Indeed many know little if anything about programming or electronics. When advanced software is useful it can usually be found in some library which, unlike CODE::BLOCKS and other c++ IDE's, are easy to install using the Arduino IDE.

 


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

@robotbuilder I'm a hybrid. I taught hardware, worked in software, but my bias is toward the software, I'll admit. I'm fond of sayin' "If it ain't virtual, it ain't real." Even at the hardware level, we abstract the signals into meaningful ideas.

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


   
Inst-Tech reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
 

@binaryrhyme 

Software is simply wiring up hardware.  Useful software can be embodied in hardware.

 


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

@robotbuilder Q: How many software jockeys does it take to change a lightbulb? A: None. It's a hardware problem. 🙂 

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


   
Inst-Tech reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 
Posted by: @binaryrhyme

especially when your CPU is a Motorola 68000 😉

I loved the clean architecture of the Motorola 680X0 series. Much cleaner then the intel 8088 


   
Biny and Inst-Tech reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 

Well, bugs MUST be fixed or the git source should be deleted of course.

That is the advantage of a git project. People clone it, fix it and make a pull request to update the original with the fixed code


   
Biny and Inst-Tech reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 

As I work with Git professionally (and teach it) if people have questions, ask them and if people want I can even make a simple youtube video (not of the quality of Bill of course) to go over them and make it a simple tutorial session


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

@ronsmitsnl I preferred the Intel, IIRC Motorola had very little string support so made for some rather clumsy code. Of course Intel code looked a lot like IBM 360 so it was very familiar.

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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Biny 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?

Indeed, the lack of comments was the biggest issue.

Here's my take on mimicking a real world object, with explanatory comments below:

class MicroController {
   public:
     MicroController(int pinNumber) : gpioPin(pinNumber) {

       pinMode(gpioPin, OUTPUT);
      }

     void flashLed(int interval) {
       digitalWrite(gpioPin, HIGH);
       delay(interval);

       digitalWrite(gpioPin, LOW);
       delay(interval);
      }

   private:
     int gpioPin;
 };

MicroController Uno(LED_BUILTIN);

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

void loop() {
  Uno.flashLed(1000);
 }

Explanation:
1) Everything in C++ is considered an object, and a class is the means that allows
    us to create our own

2) To define a class (essentially a blueprint for our object) we write:

<class keyword> <space> <a name that best represents our real world object>
 { <- Opening brace
  public: <- Known as access specifiers (3 types shown)

  private:

  protected:
 }; <- Closing brace and a semicolon terminator

3) In my version I use an initializer list, which is denoted by the colon ':'
    directly following the constructor name, and before the body. I have
    defined the constructor fully within the class for this simple example

4) Access specifiers are used to control how users interact with our object
a) public:
    member functions and data members are accessible to users of our object
b) private:
    member functions and data members are only accessible internally to the class itself
c) protected:
    member functions and data members are accessible internally to the class itself as well
    as any child classes that inherit from it, and also special functions, which are outside
    the scope of this explanation - for that reason We will only use public and private here

5) To create an object of our class (also known as 'instantiating' our object), we write:
<class name> <space> <object name>;

6) Once we have instantiated our object, we can now use it as follows:
<object name> <dot> <any function or variable declared under our public access specifier>;
For example: Uno.flashLed(1000);

One thing I did not mention above, is that every class has some special features that are hidden
from us by default, but we can implement them explicitly within our class to make our objects
easier to use, and help us create better designed objects as our skills improve. These hidden
features are: 'default constructor', 'copy constructor', 'destructor' and the 'this' pointer. For
now, the most important feature to understand is the default constructor and how we can provide
a custom version of it.

Cheers


   
Biny and Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7402
 

@frogandtoad Speaking as someone who last looked briefly at C++ over 20 yrs ago.

I understand a lot of this, but don't get the syntax or meaning of 'initializer list'. I see that instantiating an object of class Microcontroller will invoke pinMode, but don't understand the meaning of

: gpioPin(pinNumber)

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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Biny reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @zander

@frogandtoad Speaking as someone who last looked briefly at C++ over 20 yrs ago.

I understand a lot of this, but don't get the syntax or meaning of 'initializer list'. I see that instantiating an object of class Microcontroller will invoke pinMode, but don't understand the meaning of

: gpioPin(pinNumber)

The colon denotes the start of our initializer list.
This list is how we provide an initial value for our data member 'gpioPin'

It says, grab the pinNumber from our constructor parameter (of which we pass it pin number 13 via the LED_BUILTIN constant), and use it to initialise our 'gpioPin' data member variable.

An initializer list is far more efficient than assigning values to our data members in the body of the
constructor, and it executes before the body of the constructor is entered.

Cheers


   
Biny and Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7402
 

@frogandtoad Ok, I think I get most of that, but the syntax is 100% unfamiliar to me. What I mean is gpioPin appears to be an integer when used in statements like digitalWrite but when I see gpioPin(PinNumber) I see an array of PinNumber entries. Sorry if I am totally missing something but this is 100% new to me. I have seen enough here to think I might be able to use this condtruct in a productive way if I can understand it. Thanks for your patience.

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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2552
 
Posted by: @zander

but when I see gpioPin(PinNumber) I see an array of PinNumber entries.

It's C, remember that [] represent an index into an array 🙂

Anything seems possible when you don't know what you're talking about.


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

@will Like I said, it's been 20 years.

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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Biny reacted
Page 8 / 21