krnl
sm.c
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 
4 // .
5 typedef void *(*pF)(void); // for typecast for calling function
6 
7 #define NEXT(x,y,z) return ((void *)x)
8 // States -- you need this forward dcl for the compiler and calling
9 
10 // NEEXT(next,here,cond)
11 // |---- condition to be put on the arc in state machine
12 // |-------- state we are in just now (just for graph)
13 // |-------------- next state we will jump to
14 //
15 // all driven from main (or here setup fct)
16 
17 void *led_on(); // as name indicates ...
18 void *led_off();
19 void *led_save();
20 
21 
22 
23 pF statefunc, oldfunc = NULL; // whrere to start
24 
31  int sensors;
32 
34 {
35  /* returns 0: center
36  1: right
37  -1: left
38 
39  */
40 }
41 
42 void setMotors(int dir)
43 {
44  /* 0: center
45  1: right
46  -1: left
47 
48  */
49 
50 }
51 
52 void *left()
53 {
54 
55  setMotors(-1);
56  while (1) {
57  sensors = readSensors();
58  switch (sensors) {
59  case -1: ;// left
60  // don nothing we are already lefty
61  break;
62  case 0: NEXT(center, left, dirCenter);
63  break;
64  case 1: NEXT(right, left, dirRight);
65  break;
66  default: ; // should not come here
67  }
68  delay(100); // only 10 times a second (sampling)s
69  }
70 
71 }
72 
73 
74 void *center()
75 {
76 
77  setMotors(0);
78  while (1) {
79  sensors = readSensors();
80  switch (sensors) {
81  case -1: // left
82  NEXT(left, center, dirLeft);
83  break;
84  case 0: ;
85  break;
86  case 1: NEXT(right, center, dirRight);
87  break;
88  default: ; // should not come here
89  }
90  delay(100); // only 10 times a second (sampling)s
91  }
92 
93 }
94 
95 void *right()
96 {
97  setMotors(-1);
98  while (1) {
99  sensors = readSensors();
100  switch (sensors) {
101  case -1: NEXT(right, left, dirLeft);
102  // don nothing we are already lefty
103  break;
104  case 0: NEXT(center, right, dirCenter);
105  break;
106  case 1: ;
107  break;
108  default: ; // should not come here
109  }
110  delay(100); // only 10 times a second (sampling)s
111  }
112 }
113 
114 
115 void *doInit()
116 {
117  sensors = readSensors();
118  switch (sensors) {
119  case -1: // left
120  return ((void *) left);
121  break;
122  case 0: // left
123  return ((void *) center);
124  break;
125  case 1: // left
126  return ((void *) right);
127  break;
128  }
129 }
130 void setup() {
131  statefunc = doInit();
132 }
133 void loop() {
134 
135 
136  while (1) {
137  oldfunc = statefunc; // if you want to see/track the previous state }
138  statefunc = (pF)(*statefunc)(); // next state is called here
139  // here statefunc is last and oldfunc is last last
140  }
141  return ; // should not get here
142 }
143 
setup
void setup()
Definition: sm.c:130
center
void * center()
Definition: sm.c:74
led_off
void * led_off()
Definition: sm.c:50
NEXT
#define NEXT(x, y, z)
Definition: sm.c:7
loop
void loop()
Definition: sm.c:133
statefunc
pF statefunc
Definition: sm.c:19
left
void * left()
Definition: sm.c:52
oldfunc
pF oldfunc
Definition: sm.c:19
setMotors
void setMotors(int dir)
Definition: sm.c:42
doInit
void * doInit()
Definition: sm.c:115
readSensors
int readSensors()
Definition: sm.c:33
pF
void *(* pF)(void)
Definition: sm.c:5
void
void * void
Definition: sm.c:5
led_save
void * led_save()
Definition: sm.c:65
sensors
int sensors
Definition: sm.c:31
led_on
void * led_on()
Definition: sm.c:28
right
void * right()
Definition: sm.c:95