Skip to content

Instantly share code, notes, and snippets.

@spapadim
Created August 27, 2015 03:50
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 spapadim/fea587c9b0a537af2a2c to your computer and use it in GitHub Desktop.
Save spapadim/fea587c9b0a537af2a2c to your computer and use it in GitHub Desktop.
ESP8266 cross-linked object initialization
#include "Color.h"
Color WHITE(0xff, 0xff, 0xff);
#ifndef Color_h_
#define Color_h_
#include <Arduino.h>
struct Color {
uint8_t r, g, b;
Color(uint8_t r_ = 0, uint8_t g_ = 0, uint8_t b_ = 0)
: r(r_), g(g_), b(b_) { }
};
extern Color WHITE;
#endif // Color_h_
AVR:
----------------------------------------------------------------
WHITE: 255,255,255
w.getColor(): 255,255,255
wl.getColor(): 255,255,255
wls.getColor(): 255,255,255
GRAY: 85,85,85
g.getColor(): 85,85,85
gl.getColor(): 85,85,85
gls.getColor(): 85,85,85
----------------------------------------------------------------
================================================================
ESP:
----------------------------------------------------------------
WHITE: 255,255,255
w.getColor(): 0,0,0
wl.getColor(): 255,255,255
GRAY: 85,85,85
g.getColor(): 85,85,85
gl.getColor(): 85,85,85
----------------------------------------------------------------
#include "Color.h"
class Foo {
public:
Foo () : c(WHITE) { }
Foo (const Color &c_) : c(c_) { }
const Color &getColor() const { return c; }
private:
Color c;
};
static Color GRAY(0x55, 0x55, 0x55);
static Foo w;
static Foo g(GRAY);
static void debugPrintColor(const char* name, const Color& c) {
Serial.print(name); Serial.print(": ");
Serial.print(c.r); Serial.print(','); Serial.print(c.g); Serial.print(','); Serial.println(c.b);
Serial.flush();
}
void setup() {
Serial.begin(115200);
Foo wl(WHITE); // l: local
Foo gl(GRAY);
// Local static fails to link with:
// TestGlobals.cpp.o: In function `debugPrintColor(char const*, Color const&)':
// TestGlobals.ino:23: undefined reference to `__cxa_guard_acquire'
// TestGlobals.ino:23: undefined reference to `__cxa_guard_release'
// TestGlobals.cpp.o: In function `setup':
// TestGlobals.ino:26: undefined reference to `__cxa_guard_acquire'
// TestGlobals.cpp.o: In function `HardwareSerial::begin(unsigned long)':
// .../1.6.5-947-g39819f0/cores/esp8266/HardwareSerial.h:77: undefined reference to `__cxa_guard_release'
// TestGlobals.cpp.o: In function `Foo':
// TestGlobals.ino:6: undefined reference to `__cxa_guard_acquire'
// TestGlobals.ino:6: undefined reference to `__cxa_guard_release'
// collect2: error: ld returned 1 exit status
#ifndef ESP8266
static Foo wls(WHITE); // ls: local static
static Foo gls(GRAY);
#endif
debugPrintColor("WHITE", WHITE);
debugPrintColor("w.getColor()", w.getColor());
debugPrintColor("wl.getColor()", wl.getColor());
#ifndef ESP8266
debugPrintColor("wls.getColor()", wls.getColor());
#endif
Serial.println();
debugPrintColor("GRAY", GRAY);
debugPrintColor("g.getColor()", g.getColor());
debugPrintColor("gl.getColor()", gl.getColor());
#ifndef ESP8266
debugPrintColor("gls.getColor()", gls.getColor());
#endif
}
void loop() {
// Do nothing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment