-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_controller.cpp
146 lines (117 loc) · 5.25 KB
/
event_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
This file is part of the SmartFilamentSensor distribution
Copyright (C) 2022,2023 Slava Zanko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "event_controller.h"
/*** file scope macro definitions ****************************************************************/
#define FILAMENT_SENSOR_INITIAL_LATENCY_MILLISEC 5000
#define ENGINE_SENSOR_INITIAL_LATENCY_MILLISEC 1000
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
static SensorManagement defaultEngineSensor;
static SensorManagement defaultFilamentSensor;
static Timer defaultTimer;
static EventsHandler defaultEventHandler;
/*** global variables ****************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
event_movement_state_t
EventController::getCurrentMovementState()
{
event_movement_state_t movementState;
bool filamentMoved;
bool engineMoved;
filamentMoved = this->eventData.filamentSensor->moved();
engineMoved = this->eventData.engineSensor->moved();
movementState = (filamentMoved && !engineMoved)
? EVENT_MOVEMENT_ONLY_FILAMENT
: (!filamentMoved && engineMoved)
? EVENT_MOVEMENT_ONLY_ENGINE
: (filamentMoved && engineMoved)
? EVENT_MOVEMENT_FILAMENT_AND_ENGINE
: EVENT_MOVEMENT_STOP;
return movementState;
}
/* --------------------------------------------------------------------------------------------- */
void EventController::showDecorations()
{
switch (this->eventData.state)
{
case EVENT_WAITING_IN_ERROR:
this->rgbLed->set(&RgbLed::RED);
break;
case EVENT_MANUAL_FEED:
this->rgbLed->set(&RgbLed::YELLOW);
break;
case EVENT_RETRACTION:
this->rgbLed->set(&RgbLed::BLUE);
break;
default:
this->rgbLed->set(&RgbLed::GREEN);
}
}
/* --------------------------------------------------------------------------------------------- */
void EventController::handleAlarm()
{
switch (this->eventData.alarmState)
{
case ALARM_STATE_ON:
this->alarm->set(true);
break;
case ALARM_STATE_OFF:
this->alarm->set(false);
break;
}
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
EventController::EventController()
{
this->eventData.filamentSensor = &defaultFilamentSensor;
this->eventData.engineSensor = &defaultEngineSensor;
this->eventData.timer = &defaultTimer;
this->eventsHandler = &defaultEventHandler;
}
/* --------------------------------------------------------------------------------------------- */
EventController::EventController(SensorManagement *filamentSensor, SensorManagement *engineSensor, Timer *timer, EventsHandler *eventsHandler)
{
this->eventData.filamentSensor = filamentSensor;
this->eventData.engineSensor = engineSensor;
this->eventData.timer = timer;
this->eventsHandler = eventsHandler;
}
/* --------------------------------------------------------------------------------------------- */
void EventController::init(RgbLed *const rgbLed, PinOutput *const alarm, MovementSensor *const filamentSensor, MovementSensor *const engineSensor)
{
this->alarm = alarm;
this->rgbLed = rgbLed;
this->eventData.engineSensor->init(engineSensor);
this->eventData.filamentSensor->init(filamentSensor);
this->eventData.state = EVENT_NONE;
this->alarm->set(false);
this->eventData.filamentSensor->setReadingDelay(FILAMENT_SENSOR_INITIAL_LATENCY_MILLISEC);
this->eventData.engineSensor->setReadingDelay(ENGINE_SENSOR_INITIAL_LATENCY_MILLISEC);
}
/* --------------------------------------------------------------------------------------------- */
void EventController::heartbeat()
{
this->eventData.timer->heartbeat();
this->eventData.engineSensor->heartbeat();
this->eventData.filamentSensor->heartbeat();
this->eventData.movementState = this->getCurrentMovementState();
this->eventData.state = this->eventsHandler->handle(&this->eventData);
this->handleAlarm();
this->showDecorations();
}
/* --------------------------------------------------------------------------------------------- */