Skip to content

Commit 9682464

Browse files
author
Geoffrey Hunter
committed
Fixed compilation error with include of main.h. Added comment blocks to top of source files. Added build script.
1 parent bbcd22b commit 9682464

File tree

8 files changed

+105
-43
lines changed

8 files changed

+105
-43
lines changed

build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
mkdir -p build
4+
cd build
5+
cmake ..
6+
make
7+
./src/FunctionPointerStateMachineExample

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
project(NewtonRaphson)
21

32
set(HEADER_FILES
43
main.h

src/Led.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
///
2+
/// \file Led.c
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the Led module.
8+
/// \details
9+
/// See README.md in root dir for more info.
110

211
#include <stdio.h>
312

src/Led.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
///
2+
/// \file Led.h
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the Led module.
8+
/// \details
9+
/// See README.md in root dir for more info.
10+
111
#ifndef LED_H
212
#define LED_H
313

src/StateMachine.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,58 @@
1+
///
2+
/// \file StateMachine.c
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the StateMachine module.
8+
/// \details
9+
/// See README.md in root dir for more info.
10+
11+
// System includes
112
#include <stdio.h>
213

3-
#include "StateMachine.h"
4-
5-
#include "Main.h"
14+
// Local includes
615
#include "Led.h"
16+
#include "main.h"
17+
#include "StateMachine.h"
718

819

920
typedef struct {
1021
const char * name;
1122
void (*func)(void);
1223
} stateFunctionRow_t;
1324

14-
/// @brief Maps a state to it's state transition function, which should be called
25+
/// \brief Maps a state to it's state transition function, which should be called
1526
/// when the state transitions into this state.
16-
/// @warning This has to stay in sync with the state_t enum!
27+
/// \warning This has to stay in sync with the state_t enum!
1728
static stateFunctionRow_t stateFunctionA[] = {
1829
// NAME // FUNC
19-
{ "ST_INIT", &Led_Init }, // ST_INIT
2030
{ "ST_IDLE", &Led_Idle }, // ST_IDLE
2131
{ "ST_LED_ON", &Led_On }, // ST_LED_ON
2232
{ "ST_LED_OFF", &Led_Off }, // ST_LED_OFF
2333
};
2434

25-
/// @brief All the possible events that can occur for this state machine.
26-
/// @details Unlike states_t, these do not need to be kept in a special order.
27-
typedef enum {
28-
EV_ANY,
29-
EV_NONE,
30-
EV_BUTTON_PUSHED,
31-
EV_TIME_OUT,
32-
} event_t;
33-
3435
typedef struct {
3536
state_t currState;
3637
event_t event;
3738
state_t nextState;
3839
} stateTransMatrixRow_t;
3940

4041
static stateTransMatrixRow_t stateTransMatrix[] = {
41-
// CURR STATE // EVENT // NEXT STATE
42-
{ ST_INIT, EV_ANY, ST_IDLE },
42+
// CURR STATE // EVENT // NEXT STATE
4343
{ ST_IDLE, EV_BUTTON_PUSHED, ST_LED_ON },
4444
{ ST_LED_ON, EV_TIME_OUT, ST_LED_OFF },
4545
{ ST_LED_ON, EV_BUTTON_PUSHED, ST_IDLE },
4646
{ ST_LED_OFF, EV_TIME_OUT, ST_LED_ON },
4747
{ ST_LED_OFF, EV_BUTTON_PUSHED, ST_IDLE }
4848
};
4949

50-
event_t event;
51-
5250
void StateMachine_Init(stateMachine_t * stateMachine) {
5351
printf("Initialising state machine.\r\n");
54-
stateMachine->currState = ST_INIT;
55-
}
56-
57-
event_t StateMachine_GetEvent() {
58-
if(buttonPushed) {
59-
buttonPushed = false;
60-
return EV_BUTTON_PUSHED;
61-
}
62-
63-
// No event
64-
return EV_NONE;
52+
stateMachine->currState = ST_IDLE;
6553
}
6654

67-
void StateMachine_RunIteration(stateMachine_t *stateMachine) {
68-
// Get an event
69-
event = StateMachine_GetEvent();
55+
void StateMachine_RunIteration(stateMachine_t *stateMachine, event_t event) {
7056

7157
// Iterate through the state transition matrix, checking if there is both a match with the current state
7258
// and the event

src/StateMachine.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
///
2+
/// \file StateMachine.h
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the StateMachine module.
8+
/// \details
9+
/// See README.md in root dir for more info.
10+
111
#ifndef STATE_MACHINE_H
212
#define STATE_MACHINE_H
313

414
typedef enum {
5-
ST_INIT,
615
ST_IDLE,
716
ST_LED_ON,
817
ST_LED_OFF
@@ -12,8 +21,17 @@ typedef struct {
1221
state_t currState;
1322
} stateMachine_t;
1423

24+
/// \brief All the possible events that can occur for this state machine.
25+
/// \details Unlike states_t, these do not need to be kept in a special order.
26+
typedef enum {
27+
EV_ANY,
28+
EV_NONE,
29+
EV_BUTTON_PUSHED,
30+
EV_TIME_OUT,
31+
} event_t;
32+
1533
void StateMachine_Init(stateMachine_t * stateMachine);
16-
void StateMachine_RunIteration(stateMachine_t *stateMachine);
34+
void StateMachine_RunIteration(stateMachine_t *stateMachine, event_t event);
1735
const char * StateMachine_GetStateName(state_t state);
1836

1937
#endif

src/main.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1+
///
2+
/// \file main.c
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the main entry point to the state machine example.
8+
/// \details
9+
/// See README.md in root dir for more info.
110

211
#include <stdio.h>
312
#include <stdbool.h>
413

514
#include "StateMachine.h"
615

7-
bool buttonPushed = false;
8-
916
int main() {
1017
printf("main() called.\r\n");
1118

19+
// Create new state machine object
1220
stateMachine_t stateMachine;
1321

1422
StateMachine_Init(&stateMachine);
1523
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
1624

17-
// Run first iteration
18-
StateMachine_RunIteration(&stateMachine);
25+
// Push button to start flasher
26+
printf("Button pushed.\r\n");
27+
StateMachine_RunIteration(&stateMachine, EV_BUTTON_PUSHED);
28+
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
29+
30+
// Timeout
31+
printf("Timeout.\r\n");
32+
StateMachine_RunIteration(&stateMachine, EV_TIME_OUT);
33+
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
34+
35+
// Timeout
36+
printf("Timeout.\r\n");
37+
StateMachine_RunIteration(&stateMachine, EV_TIME_OUT);
38+
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
39+
40+
// Timeout
41+
printf("Timeout.\r\n");
42+
StateMachine_RunIteration(&stateMachine, EV_TIME_OUT);
1943
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
2044

21-
// Let's pretend a button was pushed
22-
buttonPushed = true;
45+
// Push button again to stop flasher
2346
printf("Button pushed.\r\n");
24-
StateMachine_RunIteration(&stateMachine);
47+
StateMachine_RunIteration(&stateMachine, EV_BUTTON_PUSHED);
2548
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
2649

2750
return 0;

src/main.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
///
2+
/// \file main.h
3+
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
4+
/// \edited n/a
5+
/// \created 2017-04-15
6+
/// \last-modified 2018-03-19
7+
/// \brief Contains the main entry point to the state machine example.
8+
/// \details
9+
/// See README.md in root dir for more info.
10+
111
#ifndef MAIN_H
212
#define MAIN_H
313

0 commit comments

Comments
 (0)