Who can me explain some lines of code...

Hi to all of you,

i have some questions about some lines, hopefully some people can help me. i have these lines

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
	Serial.print("X: ");
	PrintHex<uint8_t>(evt->X);

it from the usb host 2 library/usbhidjoystick.h. i cant get the structur/logic behind some stuff of these short lines.My questions are:

  1. What does the “::” means between “JoystickEvents::OnGamePadChanged”? is it the “method in a class”?
  2. What does “*evt” at “const GamePadEventData evt" means (is it like a tag to get the “Part of X” in the uint8_t)? What does the "” means?
  3. what does “->” means in “PrintHex<uint8_t>(evt->X)”?

Especially the “*” and the “->” are weird for me and i didnt find anything in the documentary…

Thanks for your help

b.

http://www.cplusplus.com/

GamePadEventData is an object, most probably a struct/class.

//This is most probably not the structure, but will explain my point.
struct GamePadEventData{
  int X;
  int Y;
};

In the function the * denotes a pointer type:

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)

Here the meaning is ( RTL ): evt is a pointer to a GamePadEventData ( which is constant ). The '::' is a scope operator which in this case is showing that 'OnGamePadChanged' is a member of the object 'JoystickEvents'.

This would be passing by reference:

void JoystickEvents::OnGamePadChanged(const GamePadEventData &evt)

Or by value:

void JoystickEvents::OnGamePadChanged(const GamePadEventData evt)

So, as you are passing a pointer, we cannot access X, Y directly. You could dereference the pointer to an object then access:

GamePadEventData &c_Data = *evt;
Serial.print( c_Data.X );

But in the case of your code, the dereference is done in the pointer to member operator: '->'. So you can skip the dereference yourself.

Serial.print( evt->X );

A good C++ tutorial will help with these concepts.

Thanks a guys! This helped me a lot to understand the structure. For a newbie all the different signs and letters are a bit confusing…but hope to get the basics soon.

But before one more little question:

When i wann control something like a stepper with the value x and i have a stepper library like the stepper.h so is it right to write it like:

stepper.setSpeed(<uint8_t>(evt->X));

or is

stepper.setSpeed(evt->X);

enough?

No, your code using ‘<>’ is a template function, it has its own meaning:

PrintHex<uint8_t>(evt->X);

If evt->X is a 16 bit value but you need to pass in an 8 bit value, you can cast using ‘()’

stepper.moveTo( ( uint8_t ) evt->X );

However the top 8 bits are chopped off so high values are no use.

It should be fine to just use:

stepper.moveTo( evt->X );

Big thanks to you,pYro!

so i understand some more ;-) unfortunately i cant get the stepper-control combined with the usb-hid-joystick-control.

i think i will close this thread and do a new one for my next questions.