165 lines
5.7 KiB
C
165 lines
5.7 KiB
C
/**********************************************************************\
|
|
* Kurzbeschreibung: automat.c
|
|
* Stellt Funktionen zur Realisierung eines Automaten zur Verfügung,
|
|
* die über die in der automat.h vorgegebene C-Schnittstelle
|
|
* mit einer grafischen Schnittstelle kommunizieren.
|
|
*
|
|
* Datum: Autor: Grund der Aenderung:
|
|
*
|
|
*
|
|
\**********************************************************************/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "automat.h"
|
|
|
|
/*--- #defines -------------------------------------------------------*/
|
|
/* Macro zur Ermittlung der Array Groesse */
|
|
#define SIZE_OF(a) (sizeof((a))/ sizeof(*(a)))
|
|
#define NR_OF_STATES (I-A+1) // Achtung: A=0, I=8, ergibt 9 Zustände
|
|
|
|
/*--- Datentypen (typedef) -------------------------------------------*/
|
|
/* Definition der Zustaende */
|
|
typedef enum {A=0,B,C,D,E,F,G,H,I} state_t;
|
|
|
|
/* Struktur zur Beschreibung eines Uebergangs mit "Don't Care"-Moeglichkeit */
|
|
typedef struct {
|
|
int input;
|
|
int mask;
|
|
state_t nxtstate;
|
|
} fsm_state_t;
|
|
|
|
/* Struktur zur Beschreibung aller Uebergaenge eines Zustands */
|
|
typedef struct {
|
|
const fsm_state_t * transitions;
|
|
int nrOfTransitions;
|
|
state_t defaultNxtState;
|
|
} fsm_full_state_t;
|
|
|
|
/*--- Modulglobale static Variablen ----------------------------------*/
|
|
/* Definition der Zustandsvariablen */
|
|
static state_t s_curstate = A; /* Initialisierung */
|
|
|
|
/* Definition aller Zustandsuebergaenge fuer jeden Zustand */
|
|
/* Die Masken sind invertiert, was ungewöhnlich ist, aber beibehalten. */
|
|
static const fsm_state_t s_transitions_A[] = {
|
|
/* input mask nxtstate */
|
|
{ 0x002, ~0x004, B },
|
|
{ 0x003, ~0x004, C }
|
|
};
|
|
static const fsm_state_t s_transitions_B[] = {
|
|
{ 0x002, ~0x004, C },
|
|
{ 0x003, ~0x004, D }
|
|
};
|
|
static const fsm_state_t s_transitions_C[] = {
|
|
{ 0x002, ~0x004, D },
|
|
{ 0x003, ~0x000, F },
|
|
{ 0x007, ~0x000, H }
|
|
};
|
|
static const fsm_state_t s_transitions_D[] = {
|
|
{ 0x002, ~0x000, F },
|
|
{ 0x003, ~0x004, E },
|
|
{ 0x006, ~0x000, H }
|
|
};
|
|
static const fsm_state_t s_transitions_E[] = {
|
|
{ 0x002, ~0x000, F },
|
|
{ 0x003, ~0x004, E },
|
|
{ 0x006, ~0x000, H }
|
|
};
|
|
static const fsm_state_t s_transitions_F[] = {
|
|
{ 0x002, ~0x001, G },
|
|
{ 0x004, ~0x001, H },
|
|
{ 0x006, ~0x001, I }
|
|
};
|
|
static const fsm_state_t s_transitions_G[] = {
|
|
{ 0x002, ~0x001, G },
|
|
{ 0x004, ~0x001, H },
|
|
{ 0x006, ~0x001, I }
|
|
};
|
|
static const fsm_state_t s_transitions_H[] = {
|
|
{ 0x000, ~0x001, A },
|
|
{ 0x002, ~0x000, B },
|
|
{ 0x003, ~0x000, C },
|
|
{ 0x006, ~0x001, I }
|
|
};
|
|
static const fsm_state_t s_transitions_I[] = {
|
|
{ 0x000, ~0x001, A },
|
|
{ 0x002, ~0x000, B },
|
|
{ 0x003, ~0x000, C },
|
|
{ 0x006, ~0x001, I }
|
|
};
|
|
|
|
/* Definition der Uebergangstabelle */
|
|
/* Die Reihenfolge der Zustaende in der enum Definition muss der
|
|
* Reihenfolge der Zustaende in der Uebergangstabelle entsprechen */
|
|
static const fsm_full_state_t s_state_table[NR_OF_STATES] = {
|
|
/* transitions nrOfTransitions defaultNxtState */
|
|
{ s_transitions_A, SIZE_OF(s_transitions_A), A },
|
|
{ s_transitions_B, SIZE_OF(s_transitions_B), B },
|
|
{ s_transitions_C, SIZE_OF(s_transitions_C), C },
|
|
{ s_transitions_D, SIZE_OF(s_transitions_D), A },
|
|
{ s_transitions_E, SIZE_OF(s_transitions_E), D },
|
|
{ s_transitions_F, SIZE_OF(s_transitions_F), F },
|
|
{ s_transitions_G, SIZE_OF(s_transitions_G), F },
|
|
{ s_transitions_H, SIZE_OF(s_transitions_H), H },
|
|
{ s_transitions_I, SIZE_OF(s_transitions_I), H }
|
|
};
|
|
|
|
/* Definition der Ausgaenge */
|
|
static const fsm_action_t s_out_table[NR_OF_STATES] = {
|
|
/* display muenz_rueck kaffee_los guthaben, display_string */
|
|
{ false, false, false, 0, "Warten" }, /* state A */
|
|
{ false, false, false, 1, "1 Euro" }, /* state B */
|
|
{ false, false, false, 2, "2 Euro" }, /* state C */
|
|
{ false, false, false, 3, "3 Euro" }, /* state D */
|
|
{ false, true, false, 3, "3 Euro" }, /* state E */
|
|
{ true, false, false, 4, "Becher unterstellen" }, /* state F */
|
|
{ true, true, false, 4, "Becher unterstellen" }, /* state G */
|
|
{ true, false, true, 0, "Kaffe kommt" }, /* state H */
|
|
{ true, true, true, 0, "Kaffe kommt" } /* state I */
|
|
};
|
|
|
|
/*--- Funktionsdefinition --------------------------------------------*/
|
|
/* Setzt den Automaten in den Startzustand zurück */
|
|
void automat_reset(void)
|
|
{
|
|
printf("---- automat_reset ----\n");
|
|
s_curstate = A;
|
|
}
|
|
|
|
/* Führt einen Zustandsübergang durch, basierend auf den Eingaben */
|
|
void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert)
|
|
{
|
|
printf("---- automat_transition becher(%0d) muenze(%0d) muenz_wert(%0d) ----\n",
|
|
becher, muenze, muenz_wert);
|
|
|
|
/* Eingabewert als Bitfeld kodieren: becher=Bit2, muenze=Bit1, muenz_wert=Bit0 */
|
|
int in = (becher << 2) | (muenze << 1) | (muenz_wert << 0);
|
|
|
|
const fsm_full_state_t * full_transition = &s_state_table[s_curstate];
|
|
const fsm_state_t * transition = full_transition->transitions;
|
|
int i;
|
|
|
|
/* Übergänge prüfen */
|
|
for(i = 0; i < full_transition->nrOfTransitions; i++)
|
|
{
|
|
/* Maskenlogik: input == (in & mask) */
|
|
if(transition[i].input == (in & transition[i].mask))
|
|
{
|
|
s_curstate = transition[i].nxtstate;
|
|
break;
|
|
}
|
|
}
|
|
/* Wenn kein Übergang passt, gehe in den Default-Zustand */
|
|
if(i == full_transition->nrOfTransitions)
|
|
{
|
|
s_curstate = full_transition->defaultNxtState;
|
|
}
|
|
}
|
|
|
|
/* Gibt die aktuellen Ausgänge des Automaten zurück */
|
|
fsm_action_t automat_output(void)
|
|
{
|
|
return s_out_table[s_curstate];
|
|
}
|
|
//gcc automat.c view.c main.c io.c checker.c -o console_automat
|