Core Library Proposal: Watchdog-safe iterators #346
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This PR adds mgos_iterator and mgos_iterator_count.
First, I'm not assuming this feature is necessary or desired in mongoose-os itself. It could be in the library repo, or perhaps nowhere at all. I'm OK if this is rejected -- I will keep using it locally ;)
These iterators are useful when you want to iterate without blocking the
main rest of the system.
Background: I am using mongoose os for a toy projects (rgb lighting) and wanted a way to animate. Typical animation (with a for loop and
delay(...)) will cause two problems: First, it trips the watchdog, and second, it otherwise blocks the rest of the program.History: This implementation is partially influenced by my success (many years ago) using EventMachine::Iterator for similar purposes, although similar constructs are often needed in other event reactor systems like nodejs, libevent, etc.
This patch provides a general iterator concept modeled off of existing Iterator interfaces (Java, for example) having two required functions: "has next" and "next".
I am open to any modifications. I have thought maybe consolidating
has nextandnextinto a singlenextwherenextreturns a boolean "is done" or similar, which might be simpler and less coding for folks using it API.Sample code using Adafruit's Neopixel library to animate a rainbow looks like this: