Seeed-Studio / Seeed_Arduino_AS5600

The library comes with AS5600. Through this library, we can realize read the angles 、get magnetic from a magnet underneath the sensor.

Repository from Github https://github.comSeeed-Studio/Seeed_Arduino_AS5600Repository from Github https://github.comSeeed-Studio/Seeed_Arduino_AS5600

Improvements to detectMagnet() / getMagnetStrength()

sheffieldnikki opened this issue · comments

Some suggested improvements to detectMagnet():

  1. Fix typo "examines MH bit" should be MD bit
  2. Comments inside function use // instead of /* */ for consistency with rest of library
  3. Added "Status bits: " to explain what the comment is about
  4. Removed comments about ML/MH bits which are not used
  5. Folded variable declarations/logic into fewer lines
  6. Terminate description comment with period, for consistency with rest of library
/*******************************************************
  Method: detectMagnet
  In: none
  Out: 1 if magnet is detected, 0 if not
  Description: reads status register and examines the 
  MD bit.
*******************************************************/
int AMS_5600::detectMagnet()
{
  // Status bits: 0 0 MD ML MH 0 0 0 
  // MD high = magnet detected  
  int magStatus = readOneByte(_addr_status);
  return (magStatus & 0x20) ? 1 : 0;
}

Some suggested improvements to getMagnetStrength():

  1. Comments inside function use // instead of /* */ for consistency with rest of library
  2. Added "Status bits: " to explain what the comment is about
  3. Fixed typo "andexamins" and wrapped long comment to next line
  4. Fixed spelling "to weak/to strong"
  5. Folded variable declaration into fewer lines
  6. Removed detectMagnet() call which was not needed since we have just read the status register. Now twice as fast
  7. Terminate description comment with period, for consistency with rest of library
/*******************************************************
  Method: getMagnetStrength
  In: none
  Out: 0 if magnet not detected
       1 if magnet is too weak
       2 if magnet is just right
       3 if magnet is too strong
  Description: reads status register and examines the 
  MH,ML,MD bits.
*******************************************************/
int AMS_5600::getMagnetStrength()
{
  int retVal = 0; // no magnet
  // Status bits: 0 0 MD ML MH 0 0 0 
  // MD high = magnet detected  
  // ML high = AGC maximum overflow, magnet too weak
  // MH high = AGC minimum overflow, magnet too strong
  int magStatus = readOneByte(_addr_status);
  if (magStatus & 0x20) {
    retVal = 2;   // magnet detected
    if (magStatus & 0x10)
      retVal = 1; // too weak
    else if (magStatus & 0x08)
      retVal = 3; // too strong
  }
  
  return retVal;
}

@sheffieldnick welcome to PR.

@Pillar1989 no PR, but you've got my code so you're welcome to merge it if you want.

Hello,

I appreciate what you did.

Your suggested changes have been merged, so I will close this issue.

If you have other questions, please feel free to open it.