Skip to content

Instantly share code, notes, and snippets.

@njh
Last active September 7, 2021 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njh/de1cec7c883bcfe35df3956340fa30ab to your computer and use it in GitHub Desktop.
Save njh/de1cec7c883bcfe35df3956340fa30ab to your computer and use it in GitHub Desktop.
Experimental sketch, testing storing a MAC address in program memory and casting to __FlashStringHelper
// Experimental sketch, testing storing a MAC address in program memory and casting to __FlashStringHelper
// By Nicholas Humfrey
// This Macro isn't available for AVR architecture in the Arduino IDE
#ifndef FPSTR
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#endif
const static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
const static byte myprogmac[] PROGMEM = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
// Function to print a MAC address stored in RAM
int printMac(const byte *mac)
{
for (int i = 0; i < 6; i++) {
Serial.print(mac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
}
// Function to print a MAC address stored in program memory
int printMac(const __FlashStringHelper *mac)
{
byte ramBytes[6];
// Copy the string from flash program memory into RAM
memcpy_P(ramBytes, (const void *)mac, 6);
printMac(ramBytes);
}
void setup ()
{
Serial.begin(57600);
Serial.println("[FlashStringTest]");
// Print MAC address stored in RAM
//printMac(mymac);
// Print MAC address from an inline string stored in programme memory
//printMac(F("\x74\x69\x69\x2D\x30\x31"));
// Print Mac address stored in programme memory using the FPSTR() macro
printMac(FPSTR(myprogmac));
}
void loop ()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment