Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract task scheduling #149

Open
t0mpr1c3 opened this issue Sep 16, 2023 · 1 comment · May be fixed by #154
Open

Abstract task scheduling #149

t0mpr1c3 opened this issue Sep 16, 2023 · 1 comment · May be fixed by #154

Comments

@t0mpr1c3
Copy link
Contributor

It may be good to have a generic pattern for tasks like this (beeper, LEDs, watchdog, ...) including knitter, e.g.:

// Abstract class for Task
class Task {
  public:
    virtual void run() = 0;
}
...
// Classes that needs to be scheduled implements run() method
class Beeper: public Task {
  public:
    void run() {
    // non-blocking code called from the scheduler
    }
    ...
    void beep(uint8_t repeats){
    }
    ...
}
...

class Scheduler {
  public:
    bool register(const Task &task) {
    // Add task to scheduler list
    }
    void schedule() {
    // Call run() method for each registered task
    }
}

setup() {
  // Objects to schedule
  Beeper beeper;
  Knitter knitter;
  Led ledA, ledB;
  SerialCom serialCom;
  Watchdog watchdog;
  // Main Scheduler
  Scheduler scheduler;
  ...
  scheduler.register(beeper);
  scheduler.register(knitter);
  scheduler.register(ledA); // blinking (vs solid) led as "firmware alive" indication
  scheduler.register(ledB); 
  scheduler.register(serialCom);  // call PacketSerial.update()
  scheduler.register(watchdog);
  ...
}

loop () {
  scheduler.schedule()
} 
@jpcornil-git
Copy link
Contributor

jpcornil-git commented Sep 17, 2023

You can already start without a scheduler but a RR strategy in the main loop to simplify matters (scheduler may be considered when implementing the webserver interface):

// Abstract class for Task
class Task {
  public:
    virtual void update() = 0;
}
...
// Classes that needs to be scheduled implements update() method
class Beeper: public Task {
  public:
    void update() {
    // non-blocking code called from the scheduler
    }
    ...
    void beep(uint8_t repeats){
    }
    ...
}
...

setup() {
  // Task objects
  Beeper beeper;
  Knitter knitter;
  Led ledA, ledB;
  SerialCom serialCom;
  Watchdog watchdog;
  ...
}

loop () {
  beeper.update();
  knitter.update();
  ledA.update(); // blinking (vs solid) led as "firmware alive" indication
  ledB.update(); 
  serialCom.update();  // call PacketSerial.update()
  watchdog.update();
  ...
} 

@t0mpr1c3 t0mpr1c3 linked a pull request Sep 26, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants