halo
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
/**********************************************************************\
|
||||
* 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 <string.h>
|
||||
|
||||
#include "automat.h"
|
||||
|
||||
typedef enum {A=0,B,C,D,E,F,G,H,I} state_t;
|
||||
|
||||
typedef struct {
|
||||
|
||||
int input;
|
||||
|
||||
int mask;
|
||||
|
||||
state_t nxtstate;
|
||||
|
||||
} fsm_state_t;
|
||||
|
||||
typedef struct {
|
||||
|
||||
const fsm_state_t * transitions;
|
||||
|
||||
int nrOfTransitions;
|
||||
|
||||
state_t defaultNxtState;
|
||||
|
||||
} fsm_full_state_t;
|
||||
|
||||
#define SIZE_OF(a) (sizeof((a))/ sizeof(*(a)))
|
||||
|
||||
#define NR_OF_STATES (I-A + 1)
|
||||
|
||||
static state_t s_curstate = A;
|
||||
|
||||
static const fsm_state_t s_transitions_A[] = {
|
||||
|
||||
{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[] = {
|
||||
|
||||
{0x006, ~0x000, H},
|
||||
|
||||
{0x002, ~0x004, F},
|
||||
|
||||
{0x003, ~0x004, G}
|
||||
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_F[] = {
|
||||
|
||||
{0x006, ~0x000, H}
|
||||
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_G[] = {
|
||||
|
||||
{0x006, ~0x000, I}
|
||||
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_H[] = {
|
||||
|
||||
{0x000, ~0x000, A}
|
||||
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_I[] = {
|
||||
|
||||
{0x000, ~0x000, A}
|
||||
|
||||
};
|
||||
|
||||
static const fsm_full_state_t s_state_table[NR_OF_STATES] = {
|
||||
|
||||
{ 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), D },
|
||||
|
||||
{ s_transitions_E, SIZE_OF(s_transitions_E), E },
|
||||
|
||||
{ s_transitions_F, SIZE_OF(s_transitions_F), F },
|
||||
|
||||
{ s_transitions_G, SIZE_OF(s_transitions_G), G },
|
||||
|
||||
{ s_transitions_H, SIZE_OF(s_transitions_H), H },
|
||||
|
||||
{ s_transitions_I, SIZE_OF(s_transitions_I), I }
|
||||
|
||||
};
|
||||
|
||||
static const fsm_action_t s_out_table[NR_OF_STATES] = {
|
||||
|
||||
{ false, false, false, 0, "Warten" }, // A
|
||||
|
||||
{ false, false, false, 1, "1 Euro" }, // B
|
||||
|
||||
{ false, false, false, 2, "2 Euro" }, // C
|
||||
|
||||
{ false, false, false, 3, "3 Euro" }, // D
|
||||
|
||||
{ true, false, false, 3, "3 Euro + M" }, // E
|
||||
|
||||
{ true, false, false, 4, "4 Euro" }, // F
|
||||
|
||||
{ true, true, false, 4, "4 Euro + M" }, // G
|
||||
|
||||
{ true, false, true, 4, "Kaffee" }, // H
|
||||
|
||||
{ false, false, false, 0, "Kaffee + M" } // I
|
||||
|
||||
};
|
||||
|
||||
void automat_reset(void) {
|
||||
|
||||
s_curstate = A;
|
||||
|
||||
}
|
||||
|
||||
void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert) {
|
||||
|
||||
int input = ((becher & 0x1) << 2) | ((muenze & 0x1) << 1) | (muenz_wert & 0x1);
|
||||
|
||||
const fsm_full_state_t *current = &s_state_table[s_curstate];
|
||||
|
||||
for (int i = 0; i < current->nrOfTransitions; ++i) {
|
||||
|
||||
if ((input & ~current->transitions[i].mask) == (current->transitions[i].input & ~current->transitions[i].mask)) {
|
||||
|
||||
s_curstate = current->transitions[i].nxtstate;
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
s_curstate = current->defaultNxtState;
|
||||
|
||||
}
|
||||
|
||||
fsm_action_t automat_output(void) {
|
||||
|
||||
return s_out_table[s_curstate];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**********************************************************************\
|
||||
* Kurzbeschreibung: automat.h
|
||||
* 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:
|
||||
*
|
||||
*
|
||||
\**********************************************************************/
|
||||
#ifndef AUTOMAT_H
|
||||
#define AUTOMAT_H
|
||||
#define true 1
|
||||
#define false 0
|
||||
typedef int BOOL;
|
||||
typedef struct {
|
||||
BOOL display;
|
||||
BOOL muenz_rueck;
|
||||
BOOL kaffee_los;
|
||||
int guthaben;
|
||||
const char * display_string;
|
||||
} fsm_action_t;
|
||||
extern void automat_reset(void);
|
||||
extern void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert);
|
||||
extern fsm_action_t automat_output(void);
|
||||
#endif /* AUTOMAT_H */
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="kaffee_automat" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/kaffee_automat" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-Llib -lkaffee_automat" />
|
||||
<Add option="-Lbin/Debug -lQt5Core -lQt5Gui -lQt5Widgets -pthread" />
|
||||
<Add option="-lstdc++-6 -lgcc_s_dw2-1" />
|
||||
</Linker>
|
||||
<Unit filename="automat.c">
|
||||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
<Unit filename="automat.h" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<envvars />
|
||||
<debugger />
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
Reference in New Issue
Block a user