Skip to content

sparkfun/Genericized_Library_Examples

Repository files navigation

Passing a Port Inside An Arduino Library

These are super basic examples of how to pass an I2C port or serial port to a library.

Why do I care?

Many libraries have Wire.write() and Serial.print() hard coded into them. More and more platforms like the SAMD21 Mini (3 Serial, 2 I2C ports) and Teensy 3.6 (6 serial, 4 I2C ports!) have multiple ports. Your library should not assume that the user will always be using Serial port 0 or the default I2C port. Instead, genericize your library so that we can do fun things like:

myFlameThrower.begin(Serial2);
myHTU21D.begin(thirdI2C);

Checkout the blog post for more information.

License Information

This code is public domain.

Distributed as-is; no warranty is given.

  • Your friends at SparkFun.

About

Super basic examples of how to pass I2C and serial ports as objects to Arduino compatible libraries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages