I’ve got a couple of libraries I think might be useful to the Spark community. I’m not sure how to submit them, so I’m posting something here in the hope that someone at Spark can help. They are available at the following github repository:
https://github.com/leo3linbeck/spark-softi2c-tcs347265
Here’s a quick summary of the two libraries:
1. SoftI2CMaster. This allows for I2C communication using any two arbitrary Spark pins. This can be handy when connecting multiple devices to the Spark; if, say, two sensors have the same I2C address, you can’t use them on the same bus, but you can set up separate busses for each in software using SoftI2CMaster. Also, some devices don’t play all that nicely with others on the bus, or have different timing needs (e.g. if you need a slower bus because of longer, out-of-spec bus lengths), so you can give them their own bus.
It was adapted from an Arduino library originally written by Tod E. Kurt (http://todbot.com/blog/). Repository at https://github.com/todbot/SoftI2CMaster
Use is pretty straightforward: you call the constructor function, passing the two pins as parameters:
i2c = SoftI2CMaster(sdaPin, sclPin);
This returns an SoftI2CMaster type, which you can squirrel away in a variable for use in other functions.
To write a byte, you use a pattern like this:
i2c.beginWriteTransmission(address, register);
i2c.write(byte_var);
i2c.endTransmission();
To read a byte, you call:
i2c.beginReadTransmission(address, register);
r = i2c.readLast();
i2c.endTransmission();
return r;
To read 2 bytes, you use a pattern like this:
i2c.beginReadTransmission(address, register);
t = i2c.read();
x = i2c.readLast();
i2c.endTransmission();
x <<= 8;
x |= t;
return x;
I’ve had pretty good performance using this library on a device that uses a couple of TCS34725 color sensors. Because the TCS34725 has a single address, I couldn’t use the regular I2C bus on the Spark. You can see from the second library below how the SoftI2CMaster library is used in a more detailed example.
Another use case where this might be valuable is where you need to use the built-in I2C bus pins for other purposes, but you still need to provide an I2C bus for other sensors, etc.
One more point: there is a define, i2cbitdelay, which is in microseconds and determines the frequency of the bus. It is set at 50, but if you need higher or lower frequencies, you can simply adjust this parameter.
2. TCS34725. This library allows use of one or more TCS34725 sensors. Adafruit sells a couple of nice breakout boards using this sensor, and this library should work fine with those sensors. (The library has been more thoroughly tested on my device, which has a custom PCB that has two sensor chips directly mounted, but a lot of the support circuitry is similar.) This library was adapted from the Adafruit library: https://github.com/adafruit/Adafruit_TCS34725
Anyhoo, here’s how to use that library. First, construct an object and then initialize it:
tcs = TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X, sdaPin, sclPin);
if (tcs.begin()) {
tcs.enable();
}
The integration time and gain parameters are standard defines from the header file.
Then, when you want to take a sensor reading, use the following pattern:
uint16_t clear, red, green, blue;
tcs->getRawData(&red, &green, &blue, &clear);
There are some nice functions in the original Adafruit library that allow for calculation of lux and temperature, but I didn’t include them because I’m trying to avoid the floating point library. If you want them, it would be easy enough to copy the code over from the original Arduino library on the Adafruit site.
I’ve included a pretty simple example file.
Anyway, I hope others find these helpful. If they pass muster with the Spark PTB, that would be great too. If I’m supposed to do something else to submit these, someone just needs to lemme know how.
Cheers,
L3