Home | Projects | Notes > Embedded Systems Design using UML State Machines > Exercise 2: Productivity Timer (ProTimer)
The application that tracks your productive working time.
+
button increments time (minute-level increment).
-
button decrements time (minute-level decrement).
Start/Pause
button starts or pauses the countdown, or shows STAT if pressed together.
When the countdown is paused, time can be modified.
Press the +
and -
button simultaneously to abort the running timer.
Application must beep 20 times when it returns to IDLE mode.
When the application is in IDLE mode, pressing the Start/Pause
button should show the STAT for 1 sec and auto return to IDLE mode.
Arduino Uno board
1, 5mm LED
Jumper wires
IDLE
TIME_SET
PAUSE
COUNTDOWN
STAT
User Activity | Event Generated (SIGNAL) | Parameters | Note |
---|---|---|---|
Press + button | INC_TIME | none | This event gets posted to the state machine whenever the user presses the + button. |
Press - button | DEC_TIME | none | This event gets posted to the state machine whenever the user presses the - button. |
Press S/P button | START_PAUSE | This event gets posted to the state machine whenever the user presses the S/P button. | |
Press + and - button together | ABRT | This event gets posted to the state machine whenever the user presses the + and - buttons together. | |
TIME_TICK | ss (sub second) | This event is system generated for every 100ms. ss parameter value can vary between 1 to 10. |
The variables that appear on the state machine to be used for storing various data and making decisions.
curr_time : uint32_t
elapsed_time : uint32_t
pro_time : uint32_t
xxxxxxxxxx
81/* The main application structure */
2typedef struct protimer_tag
3{
4 uint32_t curr_time; // Stores the time user has set
5 uint32_t elapsed_time; // Number of seconds that has elapsed
6 uint32_t pro_time; // Productive time spent by the user
7 ...
8} protimer_t;
A structure that contains these types of variables is also called as "main application structure".
Define various signals of the application using enum
xxxxxxxxxx
131/* main.h */
2
3/* Signals of the application */
4typedef enum {
5 INC_TIME,
6 DEC_TIME,
7 TIME_TICK,
8 START_PAUSE,
9 ABRT,
10 /* Internal activity signals */
11 ENTRY,
12 EXIT
13} protimer_signal_t;
Define various states of the application using enum
xxxxxxxxxx
101/* main.h */
2
3/* States of the application */
4typedef enum {
5 IDLE,
6 TIME_SET,
7 COUNTDOWN,
8 PAUSE,
9 STAT
10} protimer_state_t;
Define the main application structure
xxxxxxxxxx
91/* main.h */
2
3/* Main application structure */
4typedef struct {
5 uint32_t curr_time;
6 uint32_t elapsed_time;
7 uint32_t pro_time;
8 protimer_state_t active_state;
9} protimer_t
Define structures to represent events
xxxxxxxxxx
121/* main.h */
2
3/* User generated events */
4typedef struct {
5 uint8_t sig;
6} protimer_user_event_t;
7
8/* Tick events */
9typedef struct {
10 uint8_t sig;
11 uint8_t ss; /* Sub-seconds */
12} protimer_tick_event_t;
Or, you could improve this code by introducing the OOP concept. (Using structure embedding)
xxxxxxxxxx
171/* main.h */
2
3/* Generic (supter) event structure */
4typedef struct {
5 uint8_t sig;
6} event_t;
7
8/* User generated events */
9typedef struct {
10 event_t super;
11} protimer_user_event_t;
12
13/* Tick event */
14typedef struct {
15 event_t super;
16 uint8_t ss; /* Sub-seconds */
17} protimer_tick_event_t;