Skip to content

Commit

Permalink
Patch 2.0.2 - replace Serial.printf
Browse files Browse the repository at this point in the history
  • Loading branch information
joba-1 committed Feb 21, 2018
1 parent d4ac4dc commit 4ae0df1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
26 changes: 18 additions & 8 deletions examples/Autogain/Autogain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ This file is part of the Joba_Tsl2561 Library.

#include <Tsl2561Util.h>

// to mimic Serial.printf() of esp8266 core for other platforms
char *format( const char *fmt, ... ) {
static char buf[128];
va_list arg;
va_start(arg, fmt);
vsnprintf(buf, sizeof(buf), fmt, arg);
buf[sizeof(buf)-1] = '\0';
va_end(arg);
return buf;
}

Tsl2561 Tsl(Wire);
uint8_t id;

Expand All @@ -43,23 +54,22 @@ void loop() {
if( Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) ) {
if( Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) ) {
if( Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id)) ) {
Serial.printf("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, full: %5u, ir: %5u, gain: %d, exp: %d, lux: %5u.%03u\n",
Tsl.address(), id, scaledFull, scaledIr, full, ir, gain, exposure, milliLux/1000, milliLux%1000);
Serial.print(format("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, full: %5u, ir: %5u, gain: %d, exp: %d, lux: %5u.%03u\n",
Tsl.address(), id, scaledFull, scaledIr, full, ir, gain, exposure, milliLux/1000, milliLux%1000));
}
else {
Serial.printf("Tsl2561Util::milliLux(full=%u, ir=%u) error\n", full, ir);
Serial.print(format("Tsl2561Util::milliLux(full=%u, ir=%u) error\n", full, ir));
}
}
else {
Serial.printf("Tsl2561Util::normalizedLuminosity(gain=%u, exposure=%u, sfull=%u, sir=%u, full=%u, ir=%u) error\n",
gain, exposure, scaledFull, scaledIr, full, ir);
Serial.print(format("Tsl2561Util::normalizedLuminosity(gain=%u, exposure=%u, sfull=%u, sir=%u, full=%u, ir=%u) error\n",
gain, exposure, scaledFull, scaledIr, full, ir));
}
}
else {
Serial.printf("Tsl2561Util::autoGain(gain=%u, exposure=%u, sfull=%u, sir=%u) error\n",
gain, exposure, scaledFull, scaledIr);
Serial.print(format("Tsl2561Util::autoGain(gain=%u, exposure=%u, sfull=%u, sir=%u) error\n",
gain, exposure, scaledFull, scaledIr));
}

delay(1000);
}

13 changes: 12 additions & 1 deletion examples/Simple/Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ This file is part of the Joba_Tsl2561 Library.

#include <Tsl2561.h>

// to mimic Serial.printf() of esp8266 core for other platforms
char *format( const char *fmt, ... ) {
static char buf[128];
va_list arg;
va_start(arg, fmt);
vsnprintf(buf, sizeof(buf), fmt, arg);
buf[sizeof(buf)-1] = '\0';
va_end(arg);
return buf;
}

Tsl2561 Tsl(Wire);

void setup() {
Expand All @@ -44,7 +55,7 @@ void loop() {
Tsl.fullLuminosity(full);
Tsl.irLuminosity(ir);

Serial.printf("Tsl2561 at 0x%02x(id=0x%02x) luminosity is %5u (full) and %5u (ir)\n", Tsl.address(), id, full, ir);
Serial.print(format("Tsl2561 at 0x%02x(id=0x%02x) luminosity is %5u (full) and %5u (ir)\n", Tsl.address(), id, full, ir));

Tsl.off();
}
Expand Down
24 changes: 17 additions & 7 deletions examples/Testing/Testing.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ This file is part of the Joba_Tsl2561 Library.

#include <Tsl2561.h>

// to mimic Serial.printf() of esp8266 core for other platforms
char *format( const char *fmt, ... ) {
static char buf[128];
va_list arg;
va_start(arg, fmt);
vsnprintf(buf, sizeof(buf), fmt, arg);
buf[sizeof(buf)-1] = '\0';
va_end(arg);
return buf;
}

Tsl2561 Tsl(Wire);

void showError( Tsl2561 &tsl ) {
Tsl2561::status_t status = tsl.status();
Serial.printf("Error was %u: ", status);
Serial.print(format("Error was %u: ", status));
switch( status ) {
case Tsl2561::ERR_OK: Serial.println("None"); break;
case Tsl2561::ERR_RW: Serial.println("Read/Write"); break;
Expand All @@ -40,7 +51,7 @@ void showError( Tsl2561 &tsl ) {
void testSensitivity( Tsl2561 &tsl, bool newGain, Tsl2561::exposure_t newExp ) {
if( tsl.on() ) {
uint32_t start = millis();
Serial.printf("Chip powered on at %u\n", start);
Serial.print(format("Chip powered on at %u\n", start));

bool chipGain;
Tsl2561::exposure_t chipExp;
Expand All @@ -58,7 +69,7 @@ void testSensitivity( Tsl2561 &tsl, bool newGain, Tsl2561::exposure_t newExp ) {
bool check = true;
if( change ) {
if( tsl.setSensitivity(newGain, newExp) ) {
Serial.printf("New gain = %d, exposure = 0x%02x\n", newGain, newExp);
Serial.print(format("New gain = %d, exposure = 0x%02x\n", newGain, newExp));
}
else {
check = false;
Expand Down Expand Up @@ -89,7 +100,7 @@ void testSensitivity( Tsl2561 &tsl, bool newGain, Tsl2561::exposure_t newExp ) {
Serial.println("No luminosity reading after 1s. Too dark?");
}
else {
Serial.printf("Got luminosity after %d ms. Full spectrum is %d and IR only is %d\n", millis() - start, full, ir);
Serial.print(format("Got luminosity after %d ms. Full spectrum is %d and IR only is %d\n", millis() - start, full, ir));
}
}

Expand All @@ -107,7 +118,7 @@ void testSensitivity( Tsl2561 &tsl, bool newGain, Tsl2561::exposure_t newExp ) {
bool testPackage( Tsl2561 &tsl ) {
uint8_t id;
if( tsl.id(id) ) {
Serial.printf("Chip has type %02x and revision %x\n", Tsl2561::type(id), Tsl2561::revision(id) );
Serial.print(format("Chip has type %02x and revision %x\n", Tsl2561::type(id), Tsl2561::revision(id)));
if( Tsl2561::packageT_FN_CL(id) ) {
Serial.println("Chip is a T, FN or CL type package");
}
Expand All @@ -128,7 +139,7 @@ bool testPackage( Tsl2561 &tsl ) {

void test( Tsl2561 &tsl ) {
bool ok = tsl.available();
Serial.printf("\nTesting Tsl2561 at address %02x: %sfound\n", tsl.address(), ok ? "" : "NOT ");
Serial.print(format("\nTesting Tsl2561 at address %02x: %sfound\n", tsl.address(), ok ? "" : "NOT "));
if( ok ) {
if( testPackage(tsl) ) {
testSensitivity(tsl, Tsl2561::GAIN_OFF, Tsl2561::EXP_402);
Expand Down Expand Up @@ -160,4 +171,3 @@ void loop() {
Serial.println("\nNext test in 5s\n");
delay(5000);
}

21 changes: 16 additions & 5 deletions examples/Utility/Utility.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ This file is part of the Joba_Tsl2561 Library.

#include <Tsl2561Util.h>

// to mimic Serial.printf() of esp8266 core for other platforms
char *format( const char *fmt, ... ) {
static char buf[128];
va_list arg;
va_start(arg, fmt);
vsnprintf(buf, sizeof(buf), fmt, arg);
buf[sizeof(buf)-1] = '\0';
va_end(arg);
return buf;
}

Tsl2561::address_t addr[] = { Tsl2561::ADDR_GND, Tsl2561::ADDR_FLOAT, Tsl2561::ADDR_VDD };
Tsl2561 Tsl(Wire);

Expand Down Expand Up @@ -58,18 +69,19 @@ void loop() {
Tsl.fullLuminosity(scaledFull);
Tsl.irLuminosity(scaledIr);

Serial.printf("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, gain: %d, exp: %d", addr[i], id, scaledFull, scaledIr, gain, exposure);
Serial.print(format("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, gain: %d, exp: %d",
addr[i], id, scaledFull, scaledIr, gain, exposure));

if( Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) ) {
if( Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id)) ) {
Serial.printf(", full: %5u, ir: %5u, lux: %5u.%03u\n", full, ir, milliLux/1000, milliLux%1000);
Serial.print(format(", full: %5u, ir: %5u, lux: %5u.%03u\n", full, ir, milliLux/1000, milliLux%1000));
}
else {
Serial.printf(", full: %5u, ir: %5u: Tsl2561Util::milliLux() error\n", full, ir);
Serial.print(format(", full: %5u, ir: %5u: Tsl2561Util::milliLux() error\n", full, ir));
}
}
else {
Serial.printf(", full: %5u, ir: %5u: Tsl2561Util::normalizedLuminosity() error\n", full, ir);
Serial.print(format(", full: %5u, ir: %5u: Tsl2561Util::normalizedLuminosity() error\n", full, ir));
}

Tsl.off();
Expand All @@ -84,4 +96,3 @@ void loop() {

delay(5000);
}

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Joba_Tsl2561",
"version": "2.0.1",
"version": "2.0.2",
"keywords": "twowire, i2c, bus, sensor, luminosity, illuminance, lux",
"description": "Arduino Library for ams (taos) luminance chip Tsl2561 with autogain",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Joba Tsl2561 Library
version=2.0.1
version=2.0.2
author=joba-1
maintainer=joba-1 <joban123.psn@gmail.com>
sentence=IoT library for using the Tsl2561 luminosity sensor
Expand Down

0 comments on commit 4ae0df1

Please sign in to comment.