Skip to content

Commit

Permalink
Adding support for SAM21 and other platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
nseidle committed Jan 23, 2017
1 parent c9cede4 commit 4e673a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
27 changes: 15 additions & 12 deletions examples/Example1_Basic_Readings/Example1_Basic_Readings.ino
Expand Up @@ -24,15 +24,18 @@

MAX30105 particleSensor;

#define debug Serial //Uncomment this line if you're using an Uno or ESP
//#define debug SerialUSB //Uncomment this line if you're using a SAMD21

void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
debug.begin(115200);
debug.println("Initializing...");

// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
debug.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}

Expand All @@ -41,13 +44,13 @@ void setup()

void loop()
{
Serial.print(" R[");
Serial.print(particleSensor.getRed());
Serial.print("] IR[");
Serial.print(particleSensor.getIR());
Serial.print("] G[");
Serial.print(particleSensor.getGreen());
Serial.print("]");

Serial.println();
debug.print(" R[");
debug.print(particleSensor.getRed());
debug.print("] IR[");
debug.print(particleSensor.getIR());
debug.print("] G[");
debug.print(particleSensor.getGreen());
debug.print("]");

debug.println();
}
8 changes: 4 additions & 4 deletions src/MAX30105.cpp
Expand Up @@ -619,19 +619,19 @@ uint16_t MAX30105::check(void)
_i2cPort->write(MAX30105_FIFODATA);
_i2cPort->endTransmission();

//We may need to read as many as 288 bytes so we read in blocks no larger than I2C_BUFFER_LENGTH
//I2C_BUFFER_LENGTH changes based on the platform. 64 bytes for SAMD21, 32 bytes for Uno.
//Wire.requestFrom() is limited to BUFFER_LENGTH which is 32 on the Uno
//We may need to read as many as 288 bytes so we read in blocks no larger than 32
//BUFFER_LENGTH should work with other platforms with larger requestFrom buffers
while (bytesLeftToRead > 0)
{
int toGet = bytesLeftToRead;
if (toGet > BUFFER_LENGTH)
if (toGet > I2C_BUFFER_LENGTH)
{
//If toGet is 32 this is bad because we read 6 bytes (Red+IR * 3 = 6) at a time
//32 % 6 = 2 left over. We don't want to request 32 bytes, we want to request 30.
//32 % 9 (Red+IR+GREEN) = 5 left over. We want to request 27.

toGet = BUFFER_LENGTH - (BUFFER_LENGTH % (activeLEDs * 3)); //Trim toGet to be a multiple of the samples we need to read
toGet = I2C_BUFFER_LENGTH - (I2C_BUFFER_LENGTH % (activeLEDs * 3)); //Trim toGet to be a multiple of the samples we need to read
}

bytesLeftToRead -= toGet;
Expand Down
18 changes: 18 additions & 0 deletions src/MAX30105.h
Expand Up @@ -25,6 +25,24 @@
#define I2C_SPEED_STANDARD 100000
#define I2C_SPEED_FAST 400000

//Define the size of the I2C buffer based on the platform the user has
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)

//I2C_BUFFER_LENGTH is defined in Wire.H
#define I2C_BUFFER_LENGTH BUFFER_LENGTH

#elif defined(__SAMD21G18A__)

//SAMD21 uses RingBuffer.h
#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE

#else

//The catch-all default is 32
#define I2C_BUFFER_LENGTH 32

#endif

class MAX30105 {
public:
MAX30105(void);
Expand Down

0 comments on commit 4e673a4

Please sign in to comment.