Skip to content

Commit

Permalink
Refactor coding style in MPU6050 library
Browse files Browse the repository at this point in the history
  • Loading branch information
xeonqq committed Jul 28, 2023
1 parent 6fd135a commit 4bc6dfc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
21 changes: 9 additions & 12 deletions Sming/Libraries/MPU6050/MPU6050.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ MPU6050::MPU6050()
* @see MPU6050_ADDRESS_AD0_LOW
* @see MPU6050_ADDRESS_AD0_HIGH
*/
MPU6050::MPU6050(uint8_t address)
MPU6050::MPU6050(uint8_t address) : devAddr{address}
{
devAddr = address;
}

/** Power on and prepare for general usage.
Expand Down Expand Up @@ -3315,10 +3314,9 @@ void MPU6050::readMemoryBlock(uint8_t* data, uint16_t dataSize, uint8_t bank, ui
{
setMemoryBank(bank);
setMemoryStartAddress(address);
uint8_t chunkSize;
for(uint16_t i = 0; i < dataSize;) {
// determine correct chunk size according to bank position and data size
chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
uint8_t chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;

// make sure we don't go past the data size
if(i + chunkSize > dataSize)
Expand Down Expand Up @@ -3357,9 +3355,9 @@ bool MPU6050::writeMemoryBlock(const uint8_t* data, uint16_t dataSize, uint8_t b
uint16_t i;
uint8_t j;
if(verify)
verifyBuffer = (uint8_t*)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
verifyBuffer = static_cast<uint8_t*>(malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE));
if(useProgMem)
progBuffer = (uint8_t*)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
progBuffer = static_cast<uint8_t*>(malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE));
for(i = 0; i < dataSize;) {
// determine correct chunk size according to bank position and data size
chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
Expand All @@ -3378,7 +3376,7 @@ bool MPU6050::writeMemoryBlock(const uint8_t* data, uint16_t dataSize, uint8_t b
progBuffer[j] = pgm_read_byte(data + i + j);
} else {
// write the chunk of data as specified
progBuffer = (uint8_t*)data + i;
progBuffer = data + i;
}

I2Cdev::writeBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, progBuffer);
Expand Down Expand Up @@ -3440,7 +3438,7 @@ bool MPU6050::writeDMPConfigurationSet(const uint8_t* data, uint16_t dataSize, b
uint8_t success, special;
uint16_t i, j;
if(useProgMem) {
progBuffer = (uint8_t*)malloc(8); // assume 8-byte blocks, realloc later if necessary
progBuffer = static_cast<uint8_t*>(malloc(8)); // assume 8-byte blocks, realloc later if necessary
}

// config set data is a long string of blocks with the following structure:
Expand Down Expand Up @@ -3468,11 +3466,11 @@ bool MPU6050::writeDMPConfigurationSet(const uint8_t* data, uint16_t dataSize, b
Serial.println(length);*/
if(useProgMem) {
if(sizeof(progBuffer) < length)
progBuffer = (uint8_t*)realloc(progBuffer, length);
progBuffer = static_cast<uint8_t*>(realloc(progBuffer, length));
for(j = 0; j < length; j++)
progBuffer[j] = pgm_read_byte(data + i + j);
} else {
progBuffer = (uint8_t*)data + i;
progBuffer = data + i;
}
success = writeMemoryBlock(progBuffer, length, bank, offset, true);
i += length;
Expand Down Expand Up @@ -3596,7 +3594,6 @@ void MPU6050::PID(uint8_t ReadAddress, float kP, float kI, uint8_t Loops)
int16_t BitZero[3];
uint8_t shift = (SaveAddress == 0x77) ? 3 : 2;
float Error, PTerm, ITerm[3];
int16_t eSample;
uint32_t eSum;
for(int i = 0; i < 3; i++) {
I2Cdev::readWord(devAddr, SaveAddress + (i * shift),
Expand All @@ -3610,7 +3607,7 @@ void MPU6050::PID(uint8_t ReadAddress, float kP, float kI, uint8_t Loops)
}
}
for(int L = 0; L < Loops; L++) {
eSample = 0;
int16_t eSample{0};
for(int c = 0; c < 100; c++) { // 100 PI Calculations
eSum = 0;
for(int i = 0; i < 3; i++) {
Expand Down
2 changes: 1 addition & 1 deletion Sming/Libraries/MPU6050/MPU6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ THE SOFTWARE.
// http://forum.arduino.cc/index.php?&topic=141571.msg1062899#msg1062899s

#undef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char*)(addr))
#define pgm_read_byte(addr) (*static_cast<const unsigned char*>(addr))

//#define PROGMEM /* empty */
//#define pgm_read_byte(x) (*(x))
Expand Down
3 changes: 2 additions & 1 deletion Sming/Libraries/MPU6050/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
Based on code from [jrowberg/i2cdevlib](https://github.com/jrowberg/i2cdevlib/tree/master/ESP32_ESP-IDF/components/MPU6050). Most of the code is the same, except:

- Removed MPU6050::ReadRegister function due to incompatibility. It is also not used anywhere in the original code.
- MPU6050_6Axis_MotionApps20.h and MPU6050_9Axis_MotionApps41.h are not included due to deps to freeRTOS. helper_3dmath.h is also not included since it is only used in the above mentioned files.
- Removed map function in favor of the Sming built-in one.
- Adapted include path and applied clangformat
- Adapted include path, coding style and applied clangformat

0 comments on commit 4bc6dfc

Please sign in to comment.