#include <Arduino.h>
#include <SoftwareSerial.h>
#include "SerialReceive.hpp"
//////////////////////////////////////////////////////////////////////////////
/// Definition of constants and global variables/objects
///
//////////////////////////////////////////////////////////////////////////////
char recBuffer[30]; // Buffer memory for receiving transmitter data
// SerialReceive rb {recBuffer, '\0', '@'}; // Object for controlling the input via a serial interface.
SerialReceive rb {recBuffer}; // Object for controlling the input via a serial interface.
//////////////////////////////////////////////////////////////////////////////
/// Forwardeclarations of used functions
///
//////////////////////////////////////////////////////////////////////////////
unsigned int strCnt(const char*, unsigned int, unsigned char);
bool checkContent(const char*, unsigned int);
//////////////////////////////////////////////////////////////////////////////
/// Main Program
///
//////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
Serial.println(F("Eingabe Anweisung:"));
}
void loop() {
static bool inpVal {false};
if (rb.receive(Serial) > 0) {
const char* bufferPtr = rb.buffer();
unsigned int strLen = rb.strLen();
if(!strcmp(bufferPtr,"set") && !inpVal) {
inpVal = true;
Serial.println("Gebe Zählerwert ein: ");
strLen=0;
}
if (inpVal && strLen) {
if (checkContent(bufferPtr,strLen)) {
Serial.print("Neuer Zählerwert: ");
Serial.println(atol(bufferPtr)); // Convert string to long int
inpVal = false;
Serial.println(F("Eingabe Anweisung:"));
} else {
Serial.println("Falsche Eingabe!");
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
/// @brief Determine whether a specific character occurs in the transferred string.
/// The number of characters found is returned.
///
/// @param string
/// @param len // Length of the String
/// @param searchChar // Character you are looking for
/// @return unsigned int // Number of characters found
//////////////////////////////////////////////////////////////////////////////
unsigned int strCnt(const char* string, unsigned int len, unsigned char searchChar) {
unsigned int count = 0;
for (unsigned int i = 0; i < len; ++i) {
if (string[i] == searchChar)
++count;
}
return count;
}
//////////////////////////////////////////////////////////////////////////////
/// @brief Check whether the string passed contains only digits
/// and the characters "," , "." and "-".
///
/// @param string
/// @param len // Length of the String
/// @return true // The string corresponds to the test pattern.
/// @return false // it do not correspod to
//////////////////////////////////////////////////////////////////////////////
bool checkContent(const char* string, unsigned int len) {
bool isOk = true;
for (unsigned int i = 0; i < len; ++i) {
unsigned char Char = string[i];
switch (Char) {
case '0' ... '9': [[fallthrough]];
case '.': [[fallthrough]];
case '-': [[fallthrough]];
case ',': break;
default: isOk = false;
}
}
return isOk;
}