Skip to content

Commit

Permalink
Fixed opensensinglab#15 - Added optional version parameter to constru…
Browse files Browse the repository at this point in the history
…ctor to allow for TFMini V1.8

Signed-off-by: Dennis Lassiter <dennis@lassiter.de>
  • Loading branch information
Dennis Lassiter committed Nov 30, 2022
1 parent 970433a commit 1a9008b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/BasicReading/BasicReading.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Setup software serial port
SoftwareSerial mySerial(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;
TFMini tfmini(VER_1_0); // VER_1_0 or VER_1_8, default: VER_1_0

void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Expand Down
16 changes: 12 additions & 4 deletions src/TFMini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ derived from this software without specific prior written permission.
#include "TFMini.h"

// Constructor
TFMini::TFMini() {
// Empty constructor
TFMini::TFMini(int version = VER_1_0) {
setProtocolVersion(version);
}


Expand All @@ -37,7 +37,7 @@ boolean TFMini::begin(Stream* _streamPtr) {

// Set standard output mode
setStandardOutputMode();

return true;
}

Expand Down Expand Up @@ -74,6 +74,14 @@ uint16_t TFMini::getRecentSignalStrength() {
}


// Public: Set Version
void TFMini::setProtocolVersion(int version) {
checksumDiff = 2;
if (version == VER_1_8) {
checksumDiff = 1;
}
}

// Private: Set the TF Mini into the correct measurement mode
void TFMini::setStandardOutputMode() {
// Set to "standard" output mode (this is found in the debug documents)
Expand Down Expand Up @@ -174,7 +182,7 @@ int TFMini::takeMeasurement() {
frame[i] = streamPtr->read();

// Store running checksum
if (i < TFMINI_FRAME_SIZE-2) {
if (i < TFMINI_FRAME_SIZE-checksumDiff) {
checksum += frame[i];
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/TFMini.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ derived from this software without specific prior written permission.
// Defines
#define TFMINI_BAUDRATE 115200
#define TFMINI_DEBUGMODE 0
#define VER_1_0 1
#define VER_1_8 8

// The frame size is nominally 9 characters, but we don't include the first two 0x59's marking the start of the frame
#define TFMINI_FRAME_SIZE 7
Expand All @@ -49,7 +51,7 @@ derived from this software without specific prior written permission.
//
class TFMini {
public:
TFMini(void);
TFMini(int version = VER_1_0);

// Configuration
boolean begin(Stream* _streamPtr);
Expand All @@ -65,11 +67,12 @@ class TFMini {
int state;
uint16_t distance;
uint16_t strength;
int checksumDiff = 0;

// Low-level communication
void setStandardOutputMode();
void setConfigMode();
int takeMeasurement();

void setProtocolVersion(int version);
};

0 comments on commit 1a9008b

Please sign in to comment.