What does this code mean void setup() { NewEncoder::EncoderState state; mean?

Hi everybody,

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

It is a variable, of type EncoderState, from the namespace/class NewEncoder- probably an enum

Take a look in NewEncoder.h

Hi tmfkaa,

thank you for answering.

Aha. it must be more than a simple variable because it allows things like

variable-name.something_knew

state.currentValue;

That is the kind of didactical approach I don't like.

curious about the amount of flexibility you can show by explaining things.

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.

The values are "similar" to an integer
example

enum myStates {
  StartDriving, 
  driveUntilStopForLowering,
  StartLowering, 
  Lowering,
  WaitAtLowPosition,
  StartWindup,
  CheckWinchButton,
  HangOutAtTop,
  ResumeDriving  
  };
enum myStates myStateVar;

code like this

prevEncoderValue = state.currentValue;

looks different. The variable has "member-functions???"

If I look up
Newencoder.h / cpp
there are things like

volatile EncoderState liveState;

volatile uint8_t currentStateVariable;

if (liveState.currentValue > _minValue) {
			liveState.currentValue--;

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.

best regards Stefan

I can't see what leads you to ask that question.
I see no function call, or even a function pointer.

A struct is almost identical to a class, the only difference is that struct members are public by default, whereas for a class, they are private.

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.

If I define a struct

typedef struct MyESP_NOW_Data_type {
  char MyESP_NOW_MsgStr[128];
  int  MyESP_NOW_Int;
} MyESP_NOW_Data_type;

MyESP_NOW_Data_type my_received_ESP_NOW_Data;     

I have to quote the exact name to access it

my_received_ESP_NOW_Data.MyESP_NOW_Int = 1024;

it is not possible to do something like
my_differentNamedStructVar.MyDifferentnamedVar= 1024;

this is the actual question to me
in the NewEncoder.cpp
the name

liveState.currentValue

liveState.currentValue is used

and in the *.ino-file

prevEncoderValue = state.currentValue;

state.currentValue; deviating name because has no preceding characters "live"

And it is not a parameter like in declaring a function

void myFunc(int myParameter1)

call

int myVar = 123;
myFunc(myVar)

in this case I understand why the names can be different.

But I have no clue why the names can be different in the NewEncoder-lib

best regards Stefan

..is a protected, volatile member of the class NewEncoder. (line 75, NewEncoder.h)

"state" is simply an instance of NewEncoder::EncoderState state;, declared at the top of setup ().

It looks a little alien to me too, because I don't expect unencumbered access to class member variables

@johnwasser

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?

best regards Stefan

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.

OK, thanks for the clarification.

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.

Hi gfvavlvo,

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

Is this scope-limiting done by the

NewEncoder::EncoderState myEncState;

I mean the NewEncoder:: EncoderState myEncState;

or somehow inside the NewEncoder-lib?

best regards Stefan

Hint: "::" is called the scope-resolution operator.

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)

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.