Creating a memcmp_E function [SOLVED]

Hexspeak is fun :slight_smile:

The code I posted compiled for me on a Mac with the latest Arduino IDE without warning and runs as posted

Weird

westfw: Note that you're being non-standard;

Is this because of returning only true/false or something else?

non-standard;

Is this because of returning only true/false

Yes. Although, I think I thought you were going to return "true" for "match" (which would be opposite of the standard memcmp())

westfw:
Although, I think I thought you were going to return ‚Äútrue‚ÄĚ for ‚Äúmatch‚ÄĚ (which would be opposite of the standard memcmp())

No, I did get that part right. But, although my original thought *was *to return only T/F, that’s been amended. It turns out the cost, if one looks at it that way, of comporting to memcmp() by returning a value is insignificant since memcmp() is doing the work anyway.

I wrestled with the overloaded function approach but could not work out how to deal with endianness. Doubtless ignorance on my part.

What appears below is essentially @J-M-L’s template solution with a few changed names, another test or two, and some added commentary.

.ino

// by J-M-L http://forum.arduino.cc/index.php?topic=584108.msg3978283#msg3978283

// version with print frills removed.
// -------------------------------------------------
// TEMPLATE DECLARATION
// -------------------------------------------------
#include <EEPROM.h>
#include "Header.h"

// -------------------------------------------------

const uint32_t v1 = 0xDEADBEEF; // establish 32-bit values
const uint32_t v2 = 0xBADCAFFE;
const uint32_t v3 = 0xFEE4ABED;

// these strings start at eeprom addr 100
char ts1[] = "test str 1"; // each is eleven bytes total
char ts2[] = "test str 2";
char ts3[] = "test str 1";

uint8_t aByte = 0xEF;
uint8_t aBadByte = 0xFE;
uint8_t combo[] = {2, 2, 1, 0, 0}; // current lock combination in EEPROM

uint8_t aByteArray[] = {0xEF, 0xBE, 0xAD, 0xDE}; // because of LITTLE ENDIAN architecture
const size_t nbItemsInaByteArray = sizeof(aByteArray) / sizeof(aByteArray[0]);

uint32_t anUnsignedLongArray[] = {v1, v2, v3};
const size_t nbItemsInanUnsignedLongArray = sizeof(anUnsignedLongArray) / sizeof(anUnsignedLongArray[0]);

uint32_t aBadUnsignedLongArray[] = {v1, v3};
const size_t nbItemsInaBadUnsignedLongArray = sizeof(aBadUnsignedLongArray) / sizeof(aBadUnsignedLongArray[0]);

void setup() {
  Serial.begin(9600);
  //  Serial.println("Stored data used for compares:");
  //  Serial.println("const uint32_t v1 = 0xDEADBEEF @ EEPROM addr 8");
  //  Serial.println("const uint32_t v2 = 0xBADCAFFE @ EEPROM addr 12");
  //  Serial.println("const uint32_t v3 = 0xFEE4ABED @ EEPROM addr 16");
  //  Serial.println("aByteArray[] = {0xEF, 0xBE, 0xAD, 0xDE}\n");
  int EEPROM_addr = 8;
  if (!memcmp_E(&aByte, EEPROM_addr, 1)) Serial.println(F("test on aByte OK")); else Serial.println(F("test on aByte NOT OK"));
  Serial.println();

  if (!memcmp_E(&aBadByte, 8, 1)) Serial.println(F("test on aBadByte OK")); else Serial.println(F("test on aBadByte NOT OK"));
  Serial.println();

  if (!memcmp_E(aByteArray, 8, nbItemsInaByteArray)) Serial.println(F("test on aByteArray OK")); else Serial.println(F("test on aByteArray NOT OK"));
  Serial.println();

  if (!memcmp_E(anUnsignedLongArray, 8, nbItemsInanUnsignedLongArray)) Serial.println(F("test on anUnsignedLongArray OK")); else Serial.println(F("test on anUnsignedLongArray NOT OK"));
  Serial.println();

  if (!memcmp_E(aBadUnsignedLongArray, 8, nbItemsInaBadUnsignedLongArray)) Serial.println(F("test on aBadUnsignedLongArray OK")); else Serial.println(F("test on aBadUnsignedLongArray NOT OK"));
  //string compare
  if (!memcmp_E(ts2, 111, 5)) Serial.println(F("\ntest on string OK")); else Serial.println(F("\ntest on string NOT OK"));
  //  Serial.println();
  if (!memcmp_E(combo, 20, 5)) Serial.println(F("\ntest on string OK")); else Serial.println(F("\ntest on string NOT OK"));
}

void loop() {}

.h

// memcmp_E function
//
// by J-M-L http://forum.arduino.cc/index.php?topic=584108.msg3978283#msg3978283
// -------------------------
// Takes: a pointer to a RAM address, eeprom address, and length.
// Function compares the two pairs of addresses for
// len elements and returns true/false status of
// comparison.  Since memcmp() is used, if all elements match
// a zero (false) is returned, +- value otherwise.
// Compares char arrays, bytes, ints, doubles

template<typename T>
int memcmp_E(const T* ramStart, int eeprom_start, size_t  len )
{
  int result = 0; // create and initialize return value
  uint8_t sample[sizeof(T)]; // allocate a buffer for EEPROM.get
  for (size_t i = 0; i < len * sizeof(T); i += sizeof(T)) {
    EEPROM.get( eeprom_start + i, sample); // copy EEPROM data out to RAM
    // then use memcmp to do the compare - avoids dealing with endianness
    result = memcmp ( ((uint8_t *) ramStart) + i, (uint8_t *) sample, sizeof(T) );
    if (result) break; // ie. non-zero
  }
  return result;
}

the output
memcmp_E_display.PNG

Thanks to @J-M-L and @westfw for helping!