From 4e673a42437841bcb73e5e8eda03c91777fa845f Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Mon, 23 Jan 2017 11:06:59 -0700 Subject: [PATCH] Adding support for SAM21 and other platforms --- .../Example1_Basic_Readings.ino | 27 ++++++++++--------- src/MAX30105.cpp | 8 +++--- src/MAX30105.h | 18 +++++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/examples/Example1_Basic_Readings/Example1_Basic_Readings.ino b/examples/Example1_Basic_Readings/Example1_Basic_Readings.ino index d0361e0..6ffed90 100644 --- a/examples/Example1_Basic_Readings/Example1_Basic_Readings.ino +++ b/examples/Example1_Basic_Readings/Example1_Basic_Readings.ino @@ -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); } @@ -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(); } diff --git a/src/MAX30105.cpp b/src/MAX30105.cpp index eb214e8..be2d52b 100644 --- a/src/MAX30105.cpp +++ b/src/MAX30105.cpp @@ -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; diff --git a/src/MAX30105.h b/src/MAX30105.h index 1e03e0d..73a99c5 100644 --- a/src/MAX30105.h +++ b/src/MAX30105.h @@ -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);