Created
          August 26, 2016 08:31 
        
      - 
      
- 
        Save joezuntz/888b5e1b7ce323694ec2803aaf51a106 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <vector> | |
| #include <stddef.h> | |
| #include <time.h> | |
| #define ENDED_TIMELINE_ENTRY -1 | |
| #define EMPTY_TIMELINE_ENTRY 0 | |
| typedef struct timeline_entry{ | |
| int type; | |
| double timepoint; | |
| void * data; | |
| } timeline_entry; | |
| // each entry is a type, a time, and some data (type of which depends on entry type) | |
| class AVRTimeline | |
| { | |
| // entries must be organized | |
| protected: | |
| std::vector<timeline_entry> entries; | |
| int index; | |
| virtual void callback(timeline_entry &entry, double timepoint)=0; | |
| virtual void finished(double timepoint)=0; | |
| public: | |
| AVRTimeline(); | |
| void append(int type, double timepoint, void * data); | |
| void check_for_event(double timepoint); | |
| void sort(); | |
| }; | |
| AVRTimeline::AVRTimeline() : index(0) { | |
| } | |
| static bool cmp_timeline_entry(timeline_entry &a, timeline_entry &b) | |
| { | |
| return a.timepoint < b.timepoint; | |
| } | |
| void AVRTimeline::append(int type, double timepoint, void * data){ | |
| timeline_entry t; | |
| t.type = type; | |
| t.timepoint = timepoint; | |
| t.data = data; | |
| entries.push_back(t); | |
| } | |
| void AVRTimeline::sort(){ | |
| std::sort(entries.begin(), entries.end(), cmp_timeline_entry); | |
| } | |
| void AVRTimeline::check_for_event(double timepoint){ | |
| if (index>=entries.size()){ | |
| finished(timepoint); | |
| return; | |
| } | |
| timeline_entry &entry = entries.at(index); | |
| if (entry.timepoint<timepoint){ | |
| index++; | |
| callback(entry,timepoint); | |
| } | |
| } | |
| #include <iostream> | |
| class MainLoop; | |
| class TestTimeline; | |
| class MainLoop{ | |
| public: | |
| MainLoop(); | |
| int run(TestTimeline &timeline); | |
| void finish(); | |
| protected: | |
| int flag; | |
| }; | |
| class TestTimeline : public AVRTimeline{ | |
| virtual void callback(timeline_entry &entry, double timepoint); | |
| virtual void finished(double timepoint); | |
| MainLoop * mainLoop; | |
| public: | |
| TestTimeline(MainLoop * loop); | |
| }; | |
| // Main Loop impl | |
| MainLoop::MainLoop() : flag(0) { | |
| } | |
| int MainLoop::run(TestTimeline &timeline){ | |
| double t0 = clock(); | |
| while (flag==0){ | |
| double t = (clock()-t0)/CLOCKS_PER_SEC; | |
| timeline.check_for_event(t); | |
| } | |
| return 0; | |
| } | |
| void MainLoop::finish(){ | |
| flag=1; | |
| } | |
| // Test timeline impl | |
| TestTimeline::TestTimeline(MainLoop * loop) : AVRTimeline(), mainLoop(loop) {} | |
| void TestTimeline::callback(timeline_entry &entry, double timepoint){ | |
| std::cout << "Running item " << entry.type << std::endl; | |
| } | |
| void TestTimeline::finished(double timepoint){ | |
| std::cout << "All done now after " << entries.size() << " events." << std::endl; | |
| mainLoop->finish(); | |
| } | |
| int main(){ | |
| MainLoop mainLoop; | |
| TestTimeline timeline(&mainLoop); | |
| timeline.append(2, 1.0, NULL); //event type 2 at 1 second | |
| timeline.append(3, 3.0, NULL); //event type 3 at 3 seconds | |
| timeline.append(2, 5.0, NULL); //event type 2 at 10 seconds | |
| mainLoop.run(timeline); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment