Giter VIP home page Giter VIP logo

protothreads's Introduction

Protothreads

This is a fork of Adam Dunkels' protothreads library, ready to be used as a library in Arduino projects.

To use it e.g. in the Arduino Online Editor you can download the Zip-File from the dist folder and reimport it in the IDE.

Below is a basic example arduino sketch that shows how the library can be used.

// import library
#include <pt.h>

// declare threads
static struct pt pt1, pt2;
static unsigned long globalMillis = 0;

void setup() {

  // initialize threads
  PT_INIT(&pt1);
  PT_INIT(&pt2);
}

void loop() {

  // get count of milliseconds since last passing of loop
  unsigned long ts = millis();
  unsigned long ticks = ts - globalMillis;
  if (ticks > 60000)
    ticks = 60000;
  globalMillis = ts;

  // call threads
  thread_1(&pt1, ticks);
  thread_2(&pt2, ticks);
}

int thread_1(struct pt *pt, uint16_t ticks)
{
  // increment timer
  static uint16_t timer;
  timer += ticks;

  // protothread start
  PT_BEGIN(pt);

  while (1)
  {
    // ...
    // do something
    // ...
    
    PT_WAIT_UNTIL(pt, timer > 5000); // wait 5 seconds (switch to thread 2)
    
    // ...
    // do something else
    // ...
  }

  // protothread end
  PT_END(pt);
}

int thread_2(struct pt *pt, uint16_t ticks)
{
  static uint16_t timer;

  timer += ticks;

  PT_BEGIN(pt);

  while (1)
  {
    // ...
    // do something
    // ...
    
    PT_WAIT_UNTIL(pt, timer > 5000); // wait 5 seconds (switch to thread 1)
    
    // ...
    // do something else
    // ...
  }

  PT_END(pt);
}

protothreads's People

Contributors

markose avatar

Watchers

 avatar

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.