Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driver #1

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Driver #1

wants to merge 17 commits into from

Conversation

atkwon
Copy link
Owner

@atkwon atkwon commented Feb 28, 2025

No description provided.

Copy link

@jacobkoziej jacobkoziej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, but this still needs revision.

Just a general note, it is convention to order includes in the following order: file header, system headers, library headers, project headers.

.gitmodules Outdated
Comment on lines 2 to 3
path = lib/pico-sdk
url = https://github.com/raspberrypi/pico-sdk.git

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path = lib/pico-sdk
url = https://github.com/raspberrypi/pico-sdk.git
path = lib/pico-sdk
url = ../../raspberrypi/pico-sdk.git

You can do relative URLs so that this works with either HTTPS or SSH.

CMakeLists.txt Outdated
)

pico_add_extra_outputs(gyro_test)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

lib/pico-sdk Outdated

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to pin the pico-sdk to a specific release tag than whatever was the latest commit at the time you added the submodule.

gyro.c Outdated
Comment on lines 19 to 23


int main(){


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int main(){
int main(void)
{

Let's try to cut down on unnecessary whitespace.

gyro.c Outdated
Comment on lines 27 to 31
4, /* SDA pin # */
5, /* SCL pin # */
1, /* gyro_fs */
1, /* accel_fs */
100000);/* baudrate */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding comments for these literals, it's better to #define constants with descriptive names so that you can immediately read off what's going on and not depend on the comments. More importantly, what happens when you want to reference any of these constant values later in your program? Right now you have magic numbers floating around.

mpu6050.c Outdated
Comment on lines 8 to 14
#define MPU6050_PWR_MGMT_1 0x6B

#define MPU6050_GYRO_CONFIG 0x1B
#define MPU6050_ACCEL_CONFIG 0x1C

#define MPU6050_GYRO_XOUT 0x43
#define MPU6050_ACCEL_XOUT 0x3B

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm under the belief that these should go into mpu6050.h.

mpu6050.c Outdated
Comment on lines 17 to 18
/* store i2c instance for read and write */
static i2c_inst_t *mpu6050_i2c = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO!

You've now tied the entire library to one I2C bus and mpu6050! We should instead pass around an "mpu6050" handle object between calls so that we can reuse this for multiple sensors and/or busses.

mpu6050.c Outdated
/* store i2c instance for read and write */
static i2c_inst_t *mpu6050_i2c = NULL;

int mpu6050_init(i2c_inst_t *i2c,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should break up each of the individual init phases to smaller static functions to make it easier to read the init function. Static functions will be inlined with any modern compiler optimization enabled so there's really no downside to using them to encapsulate functionality.

mpu6050.c Outdated
return 0;
}

int mpu6050_read_gyro(int16_t *gx, int16_t *gy, int16_t *gz)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally make a struct for each of these values since they repeat, or even an array of three elements, though this latter approach would require you to define some constants as to make it easier to understand which element you're accessing.

mpu6050.c Outdated

int mpu6050_read_gyro(int16_t *gx, int16_t *gy, int16_t *gz)
{
uint8_t gyro_reg = MPU6050_GYRO_XOUT;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint8_t gyro_reg = MPU6050_GYRO_XOUT;
const uint8_t gyro_reg = MPU6050_GYRO_XOUT;

Try to use constants where possible. (This is but one instance in this code.)

@atkwon atkwon requested a review from jacobkoziej March 11, 2025 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants