diff python/c3/wishes/coro.c3 @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/c3/wishes/coro.c3	Sat Aug 31 17:58:54 2013 +0200
@@ -0,0 +1,91 @@
+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;
+}
+