Skip to content

Instantly share code, notes, and snippets.

@truh
Last active August 29, 2015 14:06
Show Gist options
  • Save truh/a9f0af9fa344d55d8c49 to your computer and use it in GitHub Desktop.
Save truh/a9f0af9fa344d55d8c49 to your computer and use it in GitHub Desktop.
/*
* Name of file : StateMachine.h
* Author : Jakob Klepp
* Description : Introduction to Statemachines,
* implementing a traffic light control.
The MIT License (MIT)
Copyright (c)2014 Jakob Klepp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdbool.h>
#include <time.h>
/**
States of a traffic light
=========================
- Red
- Yellow
- Green
*/
typedef enum {
RED,
YELLOW,
GREEN
} TrafficState;
/**
Transitions
===========
.. image:: img/flow-chart.png
:width: 70%
- State: Red -> Green
* Command: Go
- State: Green -> Yellow
* Command: Stop
- State: Yellow -> Red
* Timeout
*/
typedef enum {
STOP,
GO
} Command;
/**
Reprents the actual traffic light.
If one of the bools is true the respective would be lit.
aka model
*/
typedef struct {
bool red;
bool yellow;
clock_t yellowBlinkTil;
bool green;
} TrafficLight;
/**
Magic value, indicating that the traffic light is currently
not blinking.
*/
#define NOT_BLINKING -1
/**
Updates the view.
* @param state not even sure why I have this parameter,
* maybe just consistency.
* @param light that model that should be displayed
* @param command reads user input into this reference.
*/
void do_io(TrafficState* state, TrafficLight* light, Command* command);
/**
Starts a timer
* @param state not even sure why I have this parameter,
* maybe just consistency.
* @param light timer will be set in this model
* @param command not even sure why I have this parameter,
* maybe just consistency.
*/
void start_timer(TrafficState* state, TrafficLight* light, int millies);
/**
The state machine.
Will run the evaluate the command and make changes to
state and light accordingly.
* @state The state to be changed.
* @light The model that should be changed.
* @command The command that should be evaluated.
*/
void update_state(TrafficState* state, TrafficLight* light, Command* command);
/**
Updates the timer.
* @param light check if the timer should be still running.
*/
void update_time(TrafficLight* light);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment