Open foxblock opened 3 years ago
Hi @foxblock,
Sorry for not reacting... I was not looking into this for quite some time.
It is an interesting point. I'm not aware of this, since I'm not a "natural" C Developer.
Would you be so kind, to provide a Pull-Request? I would merge it into the Main Branch, if this fixes the issue.
Pull-request is up. Sorry it took a while. I had no access to hardware to test this until today. Tested the code on an ESP32 (using local variables works fine now), but it should work on an esp8266 as well.
Hi there,
I think there is a bug in all examples of SevSegShift which use the following (or similar) setup function:
The problem lies herein ( https://github.com/bridystone/SevSegShift/blob/ShiftRegister/SevSegShift.cpp#L51 ):
SevSegShift::begin assigns the pointers to the array of pins passed as digitPins and segmentPins in the above examples to the corresponding internal variables. Since digitPins and segmentPins are declared locally in setup() however, they might get deallocated as soon as the program leaves the scope (i.e. the setup function ends) - in any case this probably is undefined behaviour according to the C standard. At least this is what happens on the ESP32.
Compare this to how SevSeg copies the values explicitly: https://github.com/DeanIsMe/SevSeg/blob/master/SevSeg.cpp#L170
A quick fix would just be moving the declaration of digitPins and segmentPins in the examples to the global scope. However I would advise against doing so, since the difference in behaviour compared to SevSeg is certainly unexpected and makes switching between the libraries harder.
A better solution would be to just malloc the member variables (_shiftRegisterMap*) in SegSevShift::begin and memcpy the passed values to them. Or copy the values by hand as SevSeg does (which probably is a tad slower).
Cheers!