Skip to content

Commit 4e673a4

Browse files
committed
Adding support for SAM21 and other platforms
1 parent c9cede4 commit 4e673a4

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

examples/Example1_Basic_Readings/Example1_Basic_Readings.ino

+15-12
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424

2525
MAX30105 particleSensor;
2626

27+
#define debug Serial //Uncomment this line if you're using an Uno or ESP
28+
//#define debug SerialUSB //Uncomment this line if you're using a SAMD21
29+
2730
void setup()
2831
{
29-
Serial.begin(115200);
30-
Serial.println("Initializing...");
32+
debug.begin(115200);
33+
debug.println("Initializing...");
3134

3235
// Initialize sensor
3336
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
3437
{
35-
Serial.println("MAX30105 was not found. Please check wiring/power. ");
38+
debug.println("MAX30105 was not found. Please check wiring/power. ");
3639
while (1);
3740
}
3841

@@ -41,13 +44,13 @@ void setup()
4144

4245
void loop()
4346
{
44-
Serial.print(" R[");
45-
Serial.print(particleSensor.getRed());
46-
Serial.print("] IR[");
47-
Serial.print(particleSensor.getIR());
48-
Serial.print("] G[");
49-
Serial.print(particleSensor.getGreen());
50-
Serial.print("]");
51-
52-
Serial.println();
47+
debug.print(" R[");
48+
debug.print(particleSensor.getRed());
49+
debug.print("] IR[");
50+
debug.print(particleSensor.getIR());
51+
debug.print("] G[");
52+
debug.print(particleSensor.getGreen());
53+
debug.print("]");
54+
55+
debug.println();
5356
}

src/MAX30105.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,19 @@ uint16_t MAX30105::check(void)
619619
_i2cPort->write(MAX30105_FIFODATA);
620620
_i2cPort->endTransmission();
621621

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

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

637637
bytesLeftToRead -= toGet;

src/MAX30105.h

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
#define I2C_SPEED_STANDARD 100000
2626
#define I2C_SPEED_FAST 400000
2727

28+
//Define the size of the I2C buffer based on the platform the user has
29+
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
30+
31+
//I2C_BUFFER_LENGTH is defined in Wire.H
32+
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
33+
34+
#elif defined(__SAMD21G18A__)
35+
36+
//SAMD21 uses RingBuffer.h
37+
#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE
38+
39+
#else
40+
41+
//The catch-all default is 32
42+
#define I2C_BUFFER_LENGTH 32
43+
44+
#endif
45+
2846
class MAX30105 {
2947
public:
3048
MAX30105(void);

0 commit comments

Comments
 (0)