view examples/c3/coro.c3 @ 359:b4ac28efcdf4

Reorganize files
author Windel Bouwman
date Fri, 14 Mar 2014 15:41:55 +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;
}