Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
state_hud_indicator.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_STATE_HUD_INDICATOR_HPP_
25#define TAPROOT_STATE_HUD_INDICATOR_HPP_
26
30
31#include "modm/processing/resumable.hpp"
32
33namespace tap
34{
35class Drivers;
36}
37
39{
83template <typename T>
84class StateHUDIndicator : public modm::Resumable<2>
85{
86public:
93 void (*)(T state, tap::communication::serial::RefSerial::Tx::Graphic1Message *graphic);
94
99 static constexpr uint32_t MIN_UPDATE_PERIOD = 500;
100
103 tap::communication::serial::RefSerial::Tx::Graphic1Message *graphic,
104 UpdateHUDIndicatorState updateFunction,
105 T initialState)
106 : refSerialTransmitter(refSerialTransmitter),
107 graphic(graphic),
108 updateFunction(updateFunction),
109 initialState(initialState),
110 indicatorState(initialState)
111 {
112 minUpdatePeriodTimeout.stop();
113 }
114
115 modm::ResumableResult<bool> initialize()
116 {
117 RF_BEGIN(0);
118
119 indicatorState = initialState;
120
121 // Initially add the graphic
122 graphic->graphicData.operation = tap::communication::serial::RefSerial::Tx::GRAPHIC_ADD;
123 RF_CALL(refSerialTransmitter.sendGraphic(graphic));
124 // In future calls to sendGraphic only modify the graphic
125 graphic->graphicData.operation = tap::communication::serial::RefSerial::Tx::GRAPHIC_MODIFY;
126
127 delayTimeout.restart(
129 RF_WAIT_UNTIL(delayTimeout.execute());
130
131 RF_END();
132 }
133
134 modm::ResumableResult<bool> draw()
135 {
136 RF_BEGIN(1);
137 if (indicatorChanged)
138 {
139 // resend graphic if color changed
140 RF_CALL(refSerialTransmitter.sendGraphic(graphic));
141 indicatorChanged = false;
142 delayTimeout.restart(
144 graphic));
145 RF_WAIT_UNTIL(delayTimeout.execute());
146 }
147 RF_END();
148 }
149
150 void setIndicatorState(T newIndicatorState)
151 {
152 if (minUpdatePeriodTimeout.isExpired() || minUpdatePeriodTimeout.isStopped())
153 {
154 if (indicatorState != newIndicatorState)
155 {
156 indicatorState = newIndicatorState;
157 updateFunction(indicatorState, graphic);
158 indicatorChanged = true;
159 minUpdatePeriodTimeout.restart(MIN_UPDATE_PERIOD);
160 }
161 }
162 }
163
164private:
166
167 tap::communication::serial::RefSerial::Tx::Graphic1Message *graphic;
168
169 UpdateHUDIndicatorState updateFunction;
170
171 const T initialState;
172 T indicatorState;
173 bool indicatorChanged = false;
174
175 tap::arch::MilliTimeout delayTimeout;
176
177 tap::arch::MilliTimeout minUpdatePeriodTimeout;
178};
179
181
182} // namespace tap::communication::referee
183
184#endif // TAPROOT_STATE_HUD_INDICATOR_HPP_
Definition timeout.hpp:44
void restart(uint32_t timeout)
Definition timeout.hpp:70
bool isExpired() const
Definition timeout.hpp:95
void stop()
Definition timeout.hpp:80
bool execute()
Definition timeout.hpp:115
bool isStopped() const
Definition timeout.hpp:89
Definition state_hud_indicator.hpp:85
static constexpr uint32_t MIN_UPDATE_PERIOD
Definition state_hud_indicator.hpp:99
void(*)(T state, tap::communication::serial::RefSerial::Tx::Graphic1Message *graphic) UpdateHUDIndicatorState
Definition state_hud_indicator.hpp:93
modm::ResumableResult< bool > draw()
Definition state_hud_indicator.hpp:134
void setIndicatorState(T newIndicatorState)
Definition state_hud_indicator.hpp:150
modm::ResumableResult< bool > initialize()
Definition state_hud_indicator.hpp:115
StateHUDIndicator(tap::communication::serial::RefSerialTransmitter &refSerialTransmitter, tap::communication::serial::RefSerial::Tx::Graphic1Message *graphic, UpdateHUDIndicatorState updateFunction, T initialState)
Definition state_hud_indicator.hpp:101
static constexpr uint32_t getWaitTimeAfterGraphicSendMs(T *)
Definition ref_serial_data.hpp:678
Definition ref_serial_transmitter.hpp:56
mockable modm::ResumableResult< void > sendGraphic(Tx::Graphic1Message *graphicMsg, bool configMsgHeader=true, bool sendMsg=true)
Definition ref_serial_transmitter.cpp:293
Definition state_hud_indicator.hpp:39
Definition ballistics.cpp:29
tap::Drivers Drivers
Definition drivers.hpp:11