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

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents
children
rev   line source
272
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
1 module coro;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
2
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
3 /*
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
4 Some co-routines doing some work.
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
5
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
6 This example demonstrates how to write co-operating
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
7 routines in a nice way.
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
8 */
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
9
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
10 int regbank[10];
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
11
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
12 function i2c_write(int address, int d)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
13 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
14 regbank[2] = address;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
15 regbank[0] |= 0x1; // Issue go command
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
16 while (regbank[1] != 0)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
17 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
18 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
19 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
20
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
21 // Wait while busy:
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
22 while (regbank[1] != 11)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
23 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
24 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
25 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
26 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
27
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
28 function int i2c_read(int address)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
29 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
30 regbank[2] = address;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
31 regbank[0] |= 0x1; // Issue go command
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
32 while (regbank[1] != 0)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
33 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
34 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
35 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
36
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
37 // Wait while busy:
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
38 while (regbank[1] != 11)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
39 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
40 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
41 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
42 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
43
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
44 function void eeprom_set(int address, int v)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
45 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
46 i2c_write(address, v);
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
47 i2c_read(address);
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
48 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
49
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
50 function int calcX(int y)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
51 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
52 var int x;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
53 x = 2;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
54 x = x + 2 + 9 * y;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
55 return x;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
56 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
57
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
58 var int counter = 0;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
59
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
60 task task1()
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
61 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
62 start task3;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
63 var int a = 200;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
64 while (a > 0)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
65 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
66 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
67 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
68 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
69
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
70 task task2()
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
71 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
72 while(true)
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
73 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
74 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
75 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
76 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
77
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
78 task task3()
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
79 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
80 eeprom_set(99, 1);
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
81 yield;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
82 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
83
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
84 task main()
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
85 {
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
86 start task1;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
87 start task2;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
88 await task1;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
89 await task2;
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
90 }
e64bae57cda8 refactor ir
Windel Bouwman
parents:
diff changeset
91