fweiss / badge

Badge is a wearable LED matrix display system for communicating visually.
MIT License
2 stars 0 forks source link

Remove drawFrame() code duplication in IndexedColorAnimation and subclasses #16

Closed fweiss closed 2 months ago

fweiss commented 2 months ago

So we reduced the index color initialization code. Static initialization of pixelIndexes[][] works best instead of std::vector>. Also using ColorIndex enum to ensure that array bounds are safe. But the enum is tricky. We tried to put the drawFrame(Frame&) in the base class, but had to repeat it in each subclass to handle the subclass's particular enum.

We tried several approaches: templates. helper classes, helper functions. We want to keep the enum as index type safety, but the drawFrame lookups can be the underlying uint. The main problem seems to be with the colorMap which is std::map<ColorIndex,uint32_t>, where ColorIndex is peculiar to the subclass. We also don't want to have too much boilerplate in the derived classes.

fweiss commented 2 months ago

This is related to #10. Maybe just hack to data reduction and then deal with the code duplication?

fweiss commented 2 months ago

An outline of the IndexedColor::drawFrame(Frame&) function:

fweiss commented 2 months ago

Maybe we can do a function relay like this:

in subclass:

void drawFrame(Frame& frame) {
    drawFrame<ColorIndex>(frame);
}

and the IndexColorAnimation, create a function template:

template<ColorIndexEnum>
void drawFrame(Frame& frame) {
    // here we have access to the particular ColorIndex for the subclass
}
fweiss commented 2 months ago

The above mentioned function template approach works.