// 1 2
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
// ?,X,X,X, X,X,X,X, X,X,X,X, X,X,X,X, ?,?,?,?, ?,Y,?,?
// ? = Not sure
// X's for a binary number with least significant bit at the start of the string. The binary number is the distance in
// mm times 100. Y = Sign bit if Y = 1 number is negative if Y = 0 number is positive.
constexpr byte SIGN_POS {21};
// char buffer[25] {"100110110110000000000000"}; // 8,76 mm
// char buffer[25] {"100011001010111000000000"}; // 150.00 mm
char buffer[25] {"100010111110000000000100"}; // -10.00 mm
float decodeMeasuredValue(const char *buf) {
uint16_t rawValue;
for (byte bit = 0; bit < 16; ++bit) { rawValue |= ('1' == buf[bit]) ? 1 << (bit) : 0; }
float mValue = static_cast<float>((rawValue >>= 1) / 100.0); // >>=1 remove lsb (? not sure)
return ('1' == buf[SIGN_POS]) ? -mValue : mValue;
}
void setup() {
Serial.begin(115200);
Serial.print(decodeMeasuredValue(buffer));
Serial.println(" mm");
}
void loop() {}