Trying to break down code logic

I’ll start out with this; I’m a total newb to arduino. I got my first uno in the mail on monday and have only been playing with it for the last two evenings. Previously I had experience with a basic stamp 2 but I haven’t touched a microprocessor in a decade and have never worked with anything related to C(++). Since it will probably be asked, my application is a self leveling headlight for my motorcycle. While I am borrowing HEAVILY from code that’s already out there, I am trying to learn what that code does along the way so I have the knowledge either for future projects or to tweak my own once it’s done to get the results I want. Realizing that I’m probably going to get at least one “start with easy stuff and work your way up to this” response, I learn best by taking on projects that are way above my pay grade and figuring it out along the way. Tedious tutorials don’t help me other than figuring out the basic structures and syntax which I’m already fairly comfortable with.

So that said, my questions relate to the code found here

Which is a sketch to read a 6DOF IMU (I will be using this particular imu for prototyping purposes but I don’t know if it’s the one I will use for the final build).

I’ve managed to work my way through pretty much the whole program and with a lot of referencing to this site, actually do understand what it’s doing and why. At least until I got tho this part:

int readGyroX() { // This really measures the y-axis of the gyro
  uint8_t* data = i2cRead(gyroAddress, 0x1F, 2);
  return ((data[0] << 8) | data[1]);

Now I read this to say that a variable named “readGyro” is defined as follows:
an unsigned 8bit variable named “data” is assigned a value based on the data provided by the gyro

That data (according to the datasheet for the gyro) is 16bits long (2’s complement format) so it is split into 2 bytes of 8 bits each and as a result “data” becomes an array with 2 values

Value 0 is bit shifted left by 8 which if I understand things correctly would result in value 0 being equal to 00000000

Value 0 is then OR’d with Value 1 and the result is stored as “readGyro” and returned to the calling function.

So I have 2 questions based on all that.

  1. Am I reading the code correctly?

  2. If so, is there a specific reason why value 0 is bit shifted vs. set to 0 by some other method?

With the disclaimer that I'm kind of new to this myself, my reading mostly follows yours.

Small point of terminology, "readGyro" isn't really a variable, it's the name of the function. (You seem to understand this in what you write later--just a slip I imagine.)

Note (just in case it's not clear) that "value 0" (ie, the data held in data[0]) is not necessarily 0. It's the first byte of what is read from the gyro. It's shifted 8 bits so that when combined (using an "or") with the unshifted 8 bits in data[1], it forms the 16 bit number (int) that is returned by readGyro() to the calling function.

Hope that's clear. (Hope I'm correct.)

--Michael

Edit: should have been typing "readGyroX()"

mj, I've been researching this ever since my original post and what I FINALLY discovered was exactly what you wrote about the first and second bytes of data from the gyro. Clearly I should have been researching harder before I posted but it's good to have some confirmation of what I found. Thanks.

Glad to find something I could help with in my newness.
There are an awful lot of very knowledgable, active, and helpful folks on this forum.
--Michael

Now I read this to say that a variable named "readGyro" is defined

That isn't a variable. It's a function!

That data (according to the datasheet for the gyro) is 16bits long (2's complement format) so it is split into 2 bytes of 8 bits each and as a result "data" becomes an array with 2 values

No. The function returns a pointer to an array of bytes.

Value 0 is bit shifted left by 8 which if I understand things correctly would result in value 0 being equal to 00000000

No. The value IN POSITION 0 is shifted. That does NOT result in a value of 0, unless the byte was 0 to start with.

soofle616:

int readGyroX() { // This really measures the y-axis of the gyro

uint8_t* data = i2cRead(gyroAddress, 0x1F, 2);
  return ((data[0] << 8) | data[1]);




Now I read this to say that a variable named "readGyro" is defined as follows:
an unsigned 8bit variable named "data" is assigned a value based on the data provided by the gyro

No.

‘data’ is a pointer to a uint8_t. It is initialised to point to a bunch of uint_8 returned by the i2cread function.

In C and C++, an array is treated like a pointer, so it’s ok to use array syntax for pointers. Thus, data[0] is the uint8 that data is pointing at, and data[1] is the uint8 following that one. The expression assembles them into a single 16-bit quantity explicitly using ‘big-endian’ ordering.