as I'm coming from delphi and now are programming Arduinos in C++ for 1,5 years.
I have quite some knowledge about programming but I did not learn in a systematic way of always proceeding to new things. I do it if I come across something that is new for me.
Here is one case
believe me or not I haven't analysed a construction like this until today:
#include "NewEncoder.h"
...
void setup() {
NewEncoder::EncoderState state;
Serial.begin(115200);
Serial.println("Starting");
if (!encoder.begin()) {
Serial.println("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting.");
while (1) {
yield();
}
} else {
encoder.getState(state);
Serial.print("Encoder Successfully Started at value = ");
prevEncoderValue = state.currentValue;
Serial.println(prevEncoderValue);
}
void loop() {
int16_t currentValue;
NewEncoder::EncoderState currentEncoderState;
if (encoder.getState(currentEncoderState)) {
Serial.print("Encoder: ");
currentValue = currentEncoderState.currentValue;
if (currentValue != prevEncoderValue) {
Serial.println(currentValue);
prevEncoderValue = currentValue;
} else
switch (currentEncoderState.currentClick) {
case NewEncoder::UpClick:
Serial.println("at upper limit.");
break;
case NewEncoder::DownClick:
Serial.println("at lower limit.");
break;
default:
break;
}
}
}
}```
What exactly is this "state"-thing / this "currentEncoderState"-thing??
NewEncoder::EncoderState **state**;
NewEncoder::EncoderState **currentEncoderState**;
I guess it is not a variable. But what is it then?
And where can I find an explanation that uses most **basic** technical terms to explain it
instead of using a lot of advanced technical terms (which I might not know too)?
best regards Stefan
I don't know what you do and don't know, so it's hard to tailor a response.
Do you understand the concept of enumerated types?
(Hint: I don't have NewEncoder.h, so you're going to have to engage a little more)
Edit: ok, on GitHub now.
It's a simple public struct member of the class NewEncoder.
The struct contains an signed sixteen bit integer, and an enum.
I'm a little surprised it's public.
just a little bit.
I have used enum for statemachines
I would describe it as this
create a new variable-type that is allowed to only assign values defined in the enum.
just looking at these lines of code I have no clue how the construction as a whole is working.
Some of the names a re similar "state" "livestate" but not exact the same. So there must be some kind of "parameterising" involved.
Hence the question for an easy to understand explanation.
I understand the comment. I wrote it just to telling more about what my thoughts are.
Yes you are right a function would have parenthesis "()" myfunc**()**
Yet there are different names like "state" "livestate"
and I don't get it yet why the names can differ.
would you mind posting a short example that explains the principles used with this code-construction with the name "enumerated types"?
maybe if the example is reduced to the bare minimum it is still easy enough to understand for me by just reading and analysing this piece of code.
The ideal way would be to explain everything starting on the level of technical terms like
variable-type, variable and go up from there in an order that all new tecnical terms are explained in an order that they build up on each other.
If this needs a lot of lines to write: do you know of a tutorial that does explain it in this way?
I don't really want to explain code that looks to me like it encourages unsafe programming practices, ie, exposing class variables, unprotected.
(Disclaimer: It may be that the code doesn't really do that, and only exposes unimportant copies of the actual data - I simply don't have the time or the will to install the library, and work through it).
You guys got it partially correct. 'EncoderState' is indeed a data type defined by the library. Yes, it's a struct that contains an int16_t and an enum type that's also defined by the library.
Both types are defined in NewEncoder scope to prevent name conflicts with any types or variables in the user's code.
These types are explained (along with the reason for them) in the README. It's all about ensuring consistent and atomic updates to multiple variables when interrupts are involved.
However, @anon73444976's assertion is incorrect in that none of the class's internal variables are exposed as 'public'. There are only 'public' functions that access these variables in a carefully controlled (and atomic) way.
There are several 'protected' variables available to users who want to create their own class that inherits from NewEncoder. This is demonstrated in the CustomEncoder Example.
As I wrote, I wasn't convinced the code merited a full explanation of what seemed to me to be a fairly simple structure, and I wasn't about to do a full read-through on my phone's browser.
thank you very much for answering. When I started with Arduino C++ I was looking up files like github readme.mdf files 15 to 20 times. Always finding a very poor documented or even empty file.
Your readme.md-file is a very positive suprise of an intensive documented library. Well done.
So I guess I have to check the readme.md-files to really see if they are useful or not.
best regards Stefan
Studying someone else's code can be an outstanding way to learn.
For questions that I have, of a type similar to yours, I have found it to be faster to look at the library myself, than to ask a forum member to look at the library, and explain.
That was the first thing I was doing looking inside the library.
Sure one way to learn. Not my preferred one.
Yes outstanding in how long it takes to understand it if the example has a higher complexity
than needed to explain the basic principles. Then this complexity needs a lot of extra time to clear things up.
Not effective. Except I want to learn it all. (including all the complexity given by the complex example)