-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathComponentTelemetry.scala
More file actions
192 lines (182 loc) · 5.94 KB
/
ComponentTelemetry.scala
File metadata and controls
192 lines (182 loc) · 5.94 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package fpp.compiler.codegen
import fpp.compiler.analysis._
import fpp.compiler.ast._
import fpp.compiler.codegen._
/** Writes out C++ for component telemetry channels */
case class ComponentTelemetry (
s: CppWriterState,
aNode: Ast.Annotated[AstNode[Ast.DefComponent]]
) extends ComponentCppWriterUtils(s, aNode) {
def getConstantMembers: List[CppDoc.Class.Member] = {
if !hasChannels then Nil
else List(
linesClassMember(
List(
Line.blank :: lines(s"//! Channel IDs"),
wrapInEnum(
sortedChannels.flatMap((id, channel) =>
writeEnumConstant(
channelIdConstantName(channel.getName),
id,
Some(s"Channel ID for ${channel.getName}"),
CppWriterUtils.Hex
)
)
)
).flatten
)
)
}
def getFunctionMembers: List[CppDoc.Class.Member] = {
getWriteFunctions
}
def getVariableMembers: List[CppDoc.Class.Member] = {
List(
addAccessTagAndComment(
"PRIVATE",
"First update flags for telemetry channels",
updateOnChangeChannels.map((_, channel) =>
linesClassMember(
lines(
s"""|
|//! Initialized to true; cleared when channel ${channel.getName} is first updated
|bool ${channelUpdateFlagName(channel.getName)};
|"""
)
)
),
CppDoc.Lines.Hpp
),
addAccessTagAndComment(
"PRIVATE",
"Last value storage for telemetry channels",
updateOnChangeChannels.map((_, channel) => {
val channelName = channel.getName
val channelType = writeChannelType(channel.channelType, "Fw::TlmString")
val channelStoreName = channelStorageName(channel.getName)
linesClassMember(
lines(
s"""|
|//! Records the last emitted value for channel $channelName
|$channelType $channelStoreName;
|"""
)
)
}),
CppDoc.Lines.Hpp
)
).flatten
}
private def getWriteFunctions: List[CppDoc.Class.Member] = {
def writeBody(channel: TlmChannel) = intersperseBlankLines(
List(
channel.update match {
case Ast.SpecTlmChannel.OnChange => lines(
s"""|// Check to see if it is the first time
|if (not this->${channelUpdateFlagName(channel.getName)}) {
| // Check to see if value has changed. If not, don't write it.
| if (arg == this->${channelStorageName(channel.getName)}) {
| return;
| }
| else {
| this->${channelStorageName(channel.getName)} = arg;
| }
|}
|else {
| this->${channelUpdateFlagName(channel.getName)} = false;
| this->${channelStorageName(channel.getName)} = arg;
|}
|"""
)
case _ => Nil
},
wrapInIf(
s"this->${portVariableName(tlmPort.get)}[0].isConnected()",
List.concat(
lines(
s"""|if (
| this->${portVariableName(timeGetPort.get)}[0].isConnected() &&
| (_tlmTime == Fw::ZERO_TIME)
|) {
| this->${portVariableName(timeGetPort.get)}[0].invoke(_tlmTime);
|}
|
|Fw::TlmBuffer _tlmBuff;
|"""
),
lines(
channel.channelType.getUnderlyingType match {
case t: Type.String =>
val serialSize = writeStringSize(s, t)
s"Fw::SerializeStatus _stat = arg.serialize(_tlmBuff, FW_MIN(FW_TLM_STRING_MAX_SIZE, $serialSize));"
case _ =>
"Fw::SerializeStatus _stat = _tlmBuff.serialize(arg);"
}
),
lines(
s"""|FW_ASSERT(
| _stat == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_stat)
|);
|
|FwChanIdType _id;
|
|_id = this->getIdBase() + ${channelIdConstantName(channel.getName)};
|
|this->${portVariableName(tlmPort.get)}[0].invoke(
| _id,
| _tlmTime,
| _tlmBuff
|);
|"""
)
)
)
)
)
addAccessTagAndComment(
"PROTECTED",
"Telemetry write functions",
sortedChannels.map((_, channel) =>
functionClassMember(
Some(
addSeparatedString(
s"Write telemetry channel ${channel.getName}",
AnnotationCppWriter.asStringOpt(channel.aNode)
)
),
channelWriteFunctionName(channel.getName),
List(
CppDoc.Function.Param(
CppDoc.Type(writeChannelParam(channel.channelType)),
"arg",
Some("The telemetry value")
),
CppDoc.Function.Param(
CppDoc.Type("Fw::Time"),
"_tlmTime",
Some("Timestamp. Default: unspecified, request from getTime port"),
Some("Fw::Time()")
)
),
CppDoc.Type("void"),
writeBody(channel),
CppDoc.Function.NonSV,
channel.update match {
case Ast.SpecTlmChannel.OnChange => CppDoc.Function.NonConst
case _ => CppDoc.Function.Const
}
)
)
)
}
private def writeChannelParam(t: Type) = {
val typeName = writeChannelType(t)
t match {
case t if s.isPrimitive(t, typeName) => typeName
case _ => s"const $typeName&"
}
}
private def channelWriteFunctionName(name: String) =
s"tlmWrite_$name"
}