view examples/c3/coro.c3 @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 158068af716c
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;
}