Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #15 - Added optional version parameter to constructor to allow for TFMini V1.8 #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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);
};