Golang and Arduino

Small arduino and golang programs

arduino updated eeprom: writeanything.h

#include <EEPROM.h>   // We need this library
#include <Arduino.h>  // for type definitions

//We create two fucntions for writing and reading data from the EEPROM
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    EEPROM.commit(); // comment out on some boards / older versions.
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}

Usage


struct {
  char ssid[20];
  char passwd[20];
} wifiConfig;

void setup() {
  // write struct to eeprom
  EEPROM_writeAnything(0, wifiConfig);
  // read from eeprom to struct
  EEPROM_readAnything(0, wifiConfig);
}

Happy using ;)

June 1, 2021