Giter Club home page Giter Club logo

task-scheduler-micro-python's Introduction

TaskScheduler in MicroPython

Overview

TaskScheduler is a simple task scheduling library written in MicroPython. It allows you to create tasks that run at specified intervals, making it easy to manage repetitive actions.

Features

  • Schedule tasks at specified intervals.
  • Run tasks on multiple cores.
  • Easy-to-use API for creating and managing tasks.

Installation

To use TaskScheduler, copy the task_scheduler.py file into your MicroPython project library using Thonny IDE, mpremote or others tools.

Usage

Example 1: Blinking LEDs

from machine import Pin
from task_scheduler import TaskScheduler, Task

class Blink(Task):
    """
    Task to blink an LED at a specified interval in ms.
    """
    def __init__(self, pin, interval):
        super().__init__(interval)
        self.led = Pin(pin, Pin.OUT)

    def loop(self):
        """
        Toggle the LED state at every interval in ms
        """
        self.led.value(not self.led.value())

#---------------------------------------------------------#
#                     CREATE TASKS                        #
#---------------------------------------------------------#

# TODO:Change accordingly to your board
LED_BUILTIN_PIN = "LED"
LED_PIN         = 20

# Create blink tasks
blink1 = Blink(LED_BUILTIN_PIN, 1000)  # ms
blink2 = Blink(LED_PIN, 500)           # ms

#---------------------------------------------------------#
#                    REGISTER TASKS                       #
#---------------------------------------------------------#

# Register blink tasks
TaskScheduler.register(blink1)
TaskScheduler.register(blink2)

#---------------------------------------------------------#
#                   START SCHEDULER                       #
#---------------------------------------------------------#

# Start the scheduler loop
TaskScheduler.start()

Example 2: Multi-core Scheduling

import _thread
from machine import Pin
from task_scheduler import TaskScheduler, Task

class Blink(Task):
    """
    Task to blink an LED at a specified interval.
    """
    def __init__(self, pin, interval):
        super().__init__(interval)
        self.led = Pin(pin, Pin.OUT)

    def loop(self):
        """
        Toggle the LED state.
        """
        self.led.value(not self.led.value())

class Button(Task):
    """
    Task to read the button state and print the result.
    """
    def __init__(self, pin, interval):
        super().__init__(interval)
        self.pin = pin
        self.button = Pin(pin, Pin.IN, Pin.PULL_UP)

    def loop(self):
        print(f"Button state : {self.button.value()}")

class Render(Task):
    """
    Task to print something at a specified interval.
    """
    def loop(self):
        print("### Hello World ###")

#---------------------------------------------------------#
#           CREATE A SCHEDULER FOR EACH CORE              #
#---------------------------------------------------------#

# Create two separate schedulers
schedulerCore0 = TaskScheduler()
schedulerCore1 = TaskScheduler()

#---------------------------------------------------------#
#                     CREATE TASKS                        #
#---------------------------------------------------------#

# TODO:Change accordingly to your board
LED_BUILTIN_PIN = "LED"
LED_PIN         = 20
BUTTON_PIN      = 18

# Create blink tasks
blink_task1 = Blink(LED_BUILTIN_PIN, 1000)
blink_task2 = Blink(LED_PIN, 500)

# Create button and render tasks
button_task = Button(BUTTON_PIN, 100)  # ms
render_task = Render(10000)            # ms

#---------------------------------------------------------#
#                    REGISTER TASKS                       #
#---------------------------------------------------------#

# Register blink tasks for schedulerCore0
schedulerCore0.register(blink1)
schedulerCore0.register(blink2)

# Register button and render tasks for schedulerCore1
schedulerCore1.register(button_task)
schedulerCore1.register(render_task)

#---------------------------------------------------------#
#                   START SCHEDULERS                      #
#---------------------------------------------------------#

# Start the schedulerCore0 on the main thread
schedulerCore0.start()

# Start the schedulerCore1 on a new thread
_thread.start_new_thread(schedulerCore1.start, ())

API

TaskScheduler

  • register(task): Registers a task with the scheduler.
  • start(): Starts the scheduler loop.

Task

  • __init__(self, period): Initializes a new task with the specified period (in milliseconds).
  • loop(self): The method to be overridden by subclasses to define the task's behavior.

task-scheduler-micro-python's People

Contributors

geo-tp avatar

Stargazers

Floatyboy avatar  avatar

Watchers

 avatar

Forkers

vtt-info ljscript

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.