chipKIT® Development Platform

Inspired by Arduino™

Class Sonstruction and Header

Created Fri, 18 Dec 2015 21:37:18 +0000 by FredCailloux


FredCailloux

Fri, 18 Dec 2015 21:37:18 +0000

If I declare a Class with some internal private functions like this, within the MyClass.h header file... class clsPmodDA2 { private: void ClkPulse(); void InitSend(); void SendData(); void SendBits(bool, bool); public: bla bla bla }

Question: Do I have to repeat the same code in the MyClass.cpp code file ? Like this: class clsPmodDA2 { private: void ClkPulse() { some code } void InitSend() { some code } void SendData() { some code } void SendBits(bool, bool) { some code } public: bla bla bla } If so, why the repeat ? I am missing something here.

Thanks for your help


majenko

Fri, 18 Dec 2015 22:23:07 +0000

No, you don't need to repeat it all.

In the .h file you just define the class - what it "looks like".

Then in the CPP file you add the "flesh" of it as functions. Like this:

// H file:
class clsPmodDA2 {
    private:
        void ClkPulse();
        void InitSend();
        void SendData();
        void SendBits(bool, bool);
    public:
        bla bla bla
};
// CPP file:

#include <h file name here>

void clsPmodDA2::ClkPulse() {
    // Code for ClkPulse function here
}

void clsPmodDA2::InitSend() {
    // Code for InitSend function here
}
// etc

FredCailloux

Sat, 19 Dec 2015 15:51:23 +0000

Thank you Majenko, your help is very appreciated :!:

In your answer you didn't show the default lines added by UECIDE when user click on /Create New /Library. They list like this in the cpp tab:

clsPmodDA2::clsPmodDA2() {
	// Constructor code here	}

void clsPmodDA2::begin() {
	// Initialization code here   }

I though the Constructor code was suppose to contain all my Private and Public variables like the int and bool ... AND also my Private and Public functions. From your supplied example I see that all my functions are now under a separate definition section that now start with

void clsPmodDA2::myFunction

Then if I do that, how can I arrange the code so that I can define which functions are Private and which are Public ? Also, what would this "Constructor code here" be used for specifically ? I need to educate myself on how to implement Class. I did some reading on the WEB which layout the basics but there is something I am missing here. Can you suggest a good article to get me started properly ?

Thanks again


majenko

Sat, 19 Dec 2015 15:57:20 +0000

The constructor is a type-less function (it doesn't return anything) which is called when you create an instance of the class:

MyClass classObject; // Constructor is called now.

It is used mainly for setting up default values for variables and things like that.

The "private" and "public" is done in the header file in the class definition.


FredCailloux

Sat, 19 Dec 2015 16:13:02 +0000

This is part of my header code:

class clsPmodDA2 {
private:
	unsigned int Pd2;					// double the initial 1/4 clock wave = 1/2 period
	bool VoutA[12];						// Are 12 bits of the Analog channel value A for PmodDA2_VoutA
	bool VoutB[12];						// Are 12 bits of the Analog channel value B for PmodDA2_VoutB
	void Setup();
	void SendBits();
public:
		clsPmodDA2();
		void begin();
		unsigned int Pd1; 					    // Period Delay 1/4 clock pulse. (microSecond)
};

So if my understanding is correct, (1) the above code would be valid (2) I do not need to use the Public or Private words in the cpp code section of my class (3) I only use the cpp Constructor section to initialize Private and Public variable with their default values if necessary (4) If I want to define some functions Private or Public I do that in the header section within the Class definition, just like the above code is showing. Please say Yes to those 4 assumptions so I can feel I am progressing ;)


majenko

Sat, 19 Dec 2015 16:17:23 +0000

Yes - x4 :)


FredCailloux

Sat, 19 Dec 2015 21:28:09 +0000

:) Great :!: Now that my Library is (probably) finished and definitely working fine (so far), I am ready to include it in the default UECIDE Library folder. I found this file called KEYWORDS.TXT in a typical \IDE_UEC\cores\chipKIT\libraries\OneWire\ for example. In it we find some capital letters KEYWORDS and some names of variables ( I think ).

Questions:

  1. Is the KEYWORDS.TXT mendatory for implementing my new library in UECIDE ?
  2. Are there some guidelines to build my own KEYWORDS.TXT file ?
  3. What is this KEYWORDS.TXT file used for, why is it there ?

Thanks


majenko

Sun, 20 Dec 2015 00:43:05 +0000

No, it is not mandatory. It is used for keyword highlighting in the IDE - that is the colouring of different words in different colours. It's not needed for the library to work, only for it to all look pretty.


FredCailloux

Tue, 08 Mar 2016 23:20:09 +0000

I have problems with my KEYWORDS.TXT file. For some reason that I cannot pinpoint it does not work in UECIDE. the KEYWORD1 definitions seams to work but no change occur with KEYWORD2 and LITERAL1 definitions. I attached my KEYWORDS.TXT file. Hoping someone could take a look at it and maybe let me know what is wrong with it.

Also, since there is a LITERAL1 definition possible, I wonder if there is such a thing as a LITERAL2 and what would it do different ? And, Is there other definition WORDS available for the KEYWORDS.TXT file ? Arduino guidelines are not very elaborate, or perhaps that is all there is to know about Keywords.txt files.

Thanks


majenko

Wed, 09 Mar 2016 00:16:59 +0000

UECIDE supports the type:

LITERAL1 LITERAL2 LITERAL3 KEYWORD1 KEYWORD2 KEYWORD3 OBJECT VARIABLE FUNCTION DATATYPE RESERVED

The last 5 (IIRC) are unique to UECIDE. They map to special types in the editor:

LITERAL1 -> TokenTypes.VARIABLE LITERAL2 -> TokenTypes.PREPROCESSOR LITERAL3 -> TokenTypes.PREPROCESSOR KEYWORD1 -> TokenTypes.RESERVED_WORD KEYWORD2 -> TokenTypes.IDENTIFIER KEYWORD3 -> TokenTypes.FUNCTION OBJECT -> TokenTypes.RESERVED_WORD_2 VARIABLE -> TokenTypes.VARIABLE FUNCTION -> TokenTypes.FUNCTION DATATYPE -> TokenTypes.DATA_TYPE RESERVED -> TokenTypes.RESERVED_WORD

Those editor token types then further map to settings in the theme to give different colours and fonts.

Your file looks perfectly fine, so I don't know why it's not highlighting for you. I'm just about to test it out though...

Yep, it works fine for me. FILTER_1 goes blue, as does FREE_RUN and others...