view examples/c3/coro.c3 @ 300:158068af716c

yafm
author Windel Bouwman
date Tue, 03 Dec 2013 18:00:22 +0100
parents python/c3/wishes/coro.c3@e64bae57cda8
children
line wrap: on
line source

module coro;

/*
  Some co-routines doing some work.

  This example demonstrates how to write co-operating
  routines in a nice way.
*/

int regbank[10];

function i2c_write(int address, int d)
{
    regbank[2] = address;
    regbank[0] |= 0x1; // Issue go command
    while (regbank[1] != 0)
    {
        yield;
    }

    // Wait while busy:
    while (regbank[1] != 11)
    {
        yield;
    }
}

function int i2c_read(int address)
{
    regbank[2] = address;
    regbank[0] |= 0x1; // Issue go command
    while (regbank[1] != 0)
    {
        yield;
    }

    // Wait while busy:
    while (regbank[1] != 11)
    {
        yield;
    }
}

function void eeprom_set(int address, int v)
{
    i2c_write(address, v);
    i2c_read(address);
}

function int calcX(int y)
{
  var int x;
  x = 2;
  x = x + 2 + 9 * y;
  return x;
}

var int counter = 0;

task task1()
{
    start task3;
   var int a = 200;
   while (a > 0)
   {
      yield;
   }
}

task task2()
{
    while(true)
    {
        yield;
    }
}

task task3()
{
    eeprom_set(99, 1);
   yield;
}

task main()
{
    start task1;
    start task2;
    await task1;
    await task2;
}