Skip to content

Commit

Permalink
finally a while without files that isn't done better by a for
Browse files Browse the repository at this point in the history
  • Loading branch information
JdeH committed Aug 1, 2018
1 parent 94a7b57 commit f79aa13
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
166 changes: 166 additions & 0 deletions subject_03_switch_case_default/trafic_light.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
Copyright 2018 Jacques de Hooge, GEATEC engineering, www.geatec.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
This program features 2 statemachines.
One has 2 states, the other 3.
They each run at their own pace.
Note that state machines don't have to cycle through a fixed sequence of states.
They can go through states in arbitrary, dynamically determined order.
Also state transitions do not need to be triggered by elapsed time intervals.
The may also be caused by external triggers, like sensor signals changing.
*/

#include <stdio.h>
#include <stdbool.h>

#define offBlinkState 0
#define onBlinkState 1

#define greenPhaseState 0
#define yellowPhaseState 1
#define redPhaseState 2

int main () {

// ====== Yellow blinking traffic light

int const blinkTime = 3000;

int oldBlinkState = onBlinkState;
int blinkState = offBlinkState;

bool yellowBlinkLightIsOn = false;

// ====== Green, yellow and red phase traffic light

int const phaseTime = 2000;

int oldPhaseState = redPhaseState;
int phaseState = greenPhaseState;

bool greenPhaseLightIsOn = false;
bool yellowPhaseLightIsOn = false;
bool redPhaseLightIsOn = false;

// ====== Initialize start time

int currentTime = 0;
int startBlinkTime = currentTime;
int startPhaseTime = startBlinkTime;

// ====== Main control loop

printf ("State machines: No SLEEP but ASYNCH!\n");
printf ("(Press [ENTER] each time to advance 1 s)\n\n");

while (true) {
// ====== Read out clock only once in the main loop, so there's only one unique time

// In a realtime environment, the current system time would be retrieved
currentTime += 100;

// ====== Separate state machine for blinking traffic light, has 2 phases

switch (blinkState) {
case offBlinkState:
if (blinkState != oldBlinkState) {
// This part only done once after state transition
oldBlinkState = blinkState;
}

// Transition condition continuously monitored
if (currentTime - startBlinkTime > blinkTime) {
blinkState = onBlinkState;
}

break;

case onBlinkState:
if (blinkState != oldBlinkState) {
// This part only done once after state transition
oldBlinkState = blinkState;
yellowBlinkLightIsOn = true;
}

// Transition condition continuously monitored
if (currentTime - startBlinkTime > 2 * blinkTime) {
blinkState = offBlinkState;
startBlinkTime = currentTime;
yellowBlinkLightIsOn = false;
}

break;
}

// ====== Separate state machine for red, green, yellow phase traffic light, has 3 phases

switch (phaseState) {
case greenPhaseState:
if (phaseState != oldPhaseState) {
// This part only done once after state transition
oldPhaseState = phaseState;
greenPhaseLightIsOn = true;
}

// Transition condition continuously monitored
if (currentTime - startPhaseTime > 2 * phaseTime) {
phaseState = yellowPhaseState;
greenPhaseLightIsOn = false;
}

break;

case yellowPhaseState:
if (phaseState != oldPhaseState) {
// This part only done once after state transition
oldPhaseState = phaseState;
yellowPhaseLightIsOn = true;
}

// Transition condition continuously monitored
if (currentTime - startPhaseTime > 3 * phaseTime) {
phaseState = redPhaseState;
yellowPhaseLightIsOn = false;
}

break;

case redPhaseState:
if (phaseState != oldPhaseState) {
// This part only done once after state transition
oldPhaseState = phaseState;
redPhaseLightIsOn = true;
}

// Transition condition continuously monitored
if (currentTime - startPhaseTime > 5 * phaseTime) {
phaseState = greenPhaseState;
startPhaseTime = currentTime;
redPhaseLightIsOn = false;
}

break;
}

if (currentTime % 1000 == 0) {
printf ("%3i s %c %c %c %c", currentTime / 1000, yellowBlinkLightIsOn ? 'B' : '.', greenPhaseLightIsOn ? 'G' : '.', yellowPhaseLightIsOn ? 'Y' : '.', redPhaseLightIsOn ? 'R' : '.');
char dummy = getchar ();
}
}
return 0;
}
Empty file added subject_04_loops/do_while.c
Empty file.
40 changes: 40 additions & 0 deletions subject_04_loops/for_nested.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2018 Jacques de Hooge, GEATEC engineering, www.geatec.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

int main () {
char action [] = "p";

for (int base = 0; base < 4; base++) {
for (int exponent = 0; exponent < 4; exponent++) { // Nested "for"s are commonplace and useful
if (!strcmp (action, "g")) {
goto exit; // Use "goto" ONLY to jump out of inner loop (there are alternatives)
}
else if (!strcmp (action, "c")) {
strcpy (action, "p");
continue; // Use of "continue" is rare, in this case it's better to put the loop tail inside an "else" block
}

printf ("%d ^ %d = %f, proceed, continue after next, goto exit <p/c/g>? ", base, exponent, pow (base, exponent));
scanf ("%s", action);
}
}

exit: return 0;
}
36 changes: 36 additions & 0 deletions subject_04_loops/for_simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2018 Jacques de Hooge, GEATEC engineering, www.geatec.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

int main () {
char breakOut [] = "n";

for (int exponent = 0; exponent < 16; exponent++) {
if (!strcmp (breakOut, "y")) {
break;
}
else {
printf ("2 ^ %d = %f, break out <y/n>? ", exponent, pow (2, exponent));
}

scanf ("%s", breakOut);
}

return 0;
}
31 changes: 31 additions & 0 deletions subject_04_loops/while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2018 Jacques de Hooge, GEATEC engineering, www.geatec.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <stdio.h>

int main () {
// Almost anything in C (and C++) is an expression!
// This allows even a naive compiler to keep things in CPU registers.
// It's one of the things that made C (and C++) code fast right from the start.
// Not everyone likes this coding style, but then again not everyone likes C...

float a, b;
a = b = 1;

while ((a *= 1.1) < ++b) {
printf ("%f %f\n", a, b);
}
}

0 comments on commit f79aa13

Please sign in to comment.