-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.cpp
More file actions
279 lines (248 loc) · 6.46 KB
/
params.cpp
File metadata and controls
279 lines (248 loc) · 6.46 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* This file is part of the libopeninv project.
*
* Copyright (C) 2011 Johannes Huebner <dev@johanneshuebner.com>
*
* 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 "params.h"
#include "my_string.h"
namespace Param
{
#define PARAM_ENTRY(category, name, unit, min, max, def, id) { category, #name, unit, min, max, def, id, TYPE_PARAM },
#define TESTP_ENTRY(category, name, unit, min, max, def, id) { category, #name, unit, min, max, def, id, TYPE_TESTPARAM },
#define VALUE_ENTRY(name, unit, id) { 0, #name, unit, 0, 0, 0, id, TYPE_SPOTVALUE },
static const Attributes attribs[] =
{
PARAM_LIST
};
#undef PARAM_ENTRY
#undef TESTP_ENTRY
#undef VALUE_ENTRY
#define PARAM_ENTRY(category, name, unit, min, max, def, id) def,
#define TESTP_ENTRY(category, name, unit, min, max, def, id) def,
#define VALUE_ENTRY(name, unit, id) 0.0f,
static float values[] =
{
PARAM_LIST
};
#undef PARAM_ENTRY
#undef TESTP_ENTRY
#undef VALUE_ENTRY
#define PARAM_ENTRY(category, name, unit, min, max, def, id) FLAG_NONE,
#define TESTP_ENTRY(category, name, unit, min, max, def, id) FLAG_NONE,
#define VALUE_ENTRY(name, unit, id) FLAG_NONE,
static uint8_t flags[] =
{
PARAM_LIST
};
#undef PARAM_ENTRY
#undef TESTP_ENTRY
#undef VALUE_ENTRY
//Duplicate ID check
#define PARAM_ENTRY(category, name, unit, min, max, def, id) ITEM_##id,
#define TESTP_ENTRY(category, name, unit, min, max, def, id) ITEM_##id,
#define VALUE_ENTRY(name, unit, id) ITEM_##id,
enum _dupes
{
PARAM_LIST
};
#undef PARAM_ENTRY
#undef TESTP_ENTRY
#undef VALUE_ENTRY
/**
* Set a parameter (accepts 5-bit fixed-point for CAN SDO compatibility)
*
* @param[in] ParamNum Parameter index
* @param[in] ParamVal New value of parameter (5-bit fixed-point format)
* @return 0 if set ok, -1 if ParamVal outside of allowed range
*/
int Set(PARAM_NUM ParamNum, s32fp ParamVal)
{
char res = -1;
// Convert from 5-bit fixed-point to float
float floatVal = FP_TOFLOAT(ParamVal);
if (floatVal >= attribs[ParamNum].min && floatVal <= attribs[ParamNum].max)
{
values[ParamNum] = floatVal;
Change(ParamNum);
res = 0;
}
return res;
}
/**
* Get a parameters fixed point value (returns 5-bit fixed-point for CAN SDO)
*
* @param[in] ParamNum Parameter index
* @return Parameters value in 5-bit fixed-point format
*/
s32fp Get(PARAM_NUM ParamNum)
{
// Convert from float to 5-bit fixed-point for SDO protocol
return FP_FROMFLT(values[ParamNum]);
}
/**
* Get a parameters integer value
*
* @param[in] ParamNum Parameter index
* @return Parameters value
*/
int GetInt(PARAM_NUM ParamNum)
{
return (int)values[ParamNum];
}
/**
* Get a parameters float value
*
* @param[in] ParamNum Parameter index
* @return Parameters value
*/
float GetFloat(PARAM_NUM ParamNum)
{
return values[ParamNum];
}
/**
* Get a parameters boolean value, 1.00=True
*
* @param[in] ParamNum Parameter index
* @return Parameters value
*/
bool GetBool(PARAM_NUM ParamNum)
{
return (int)values[ParamNum] == 1;
}
/**
* Set a parameters digit value
*
* @param[in] ParamNum Parameter index
* @param[in] ParamVal New value of parameter
*/
void SetInt(PARAM_NUM ParamNum, int ParamVal)
{
values[ParamNum] = (float)ParamVal;
}
/**
* Set a parameters fixed point value without range check and callback
*
* @param[in] ParamNum Parameter index
* @param[in] ParamVal New value of parameter (5-bit fixed-point format)
*/
void SetFixed(PARAM_NUM ParamNum, s32fp ParamVal)
{
values[ParamNum] = FP_TOFLOAT(ParamVal);
}
/**
* Set a parameters floating point value without range check and callback
*
* @param[in] ParamNum Parameter index
* @param[in] ParamVal New value of parameter
*/
void SetFloat(PARAM_NUM ParamNum, float ParamVal)
{
values[ParamNum] = ParamVal;
}
/**
* Get the paramater index from a parameter name
*
* @param[in] name Parameters name
* @return Parameter index if found, PARAM_INVALID otherwise
*/
PARAM_NUM NumFromString(const char *name)
{
PARAM_NUM paramNum = PARAM_INVALID;
const Attributes *pCurAtr = attribs;
for (int i = 0; i < PARAM_LAST; i++, pCurAtr++)
{
if (0 == my_strcmp(pCurAtr->name, name))
{
paramNum = (PARAM_NUM)i;
break;
}
}
return paramNum;
}
/**
* Get the paramater index from a parameters unique id
*
* @param[in] id Parameters unique id
* @return Parameter index if found, PARAM_INVALID otherwise
*/
PARAM_NUM NumFromId(uint32_t id)
{
PARAM_NUM paramNum = PARAM_INVALID;
const Attributes *pCurAtr = attribs;
for (int i = 0; i < PARAM_LAST; i++, pCurAtr++)
{
if (pCurAtr->id == id)
{
paramNum = (PARAM_NUM)i;
break;
}
}
return paramNum;
}
/**
* Get the parameter attributes
*
* @param[in] ParamNum Parameter index
* @return Parameter attributes
*/
const Attributes *GetAttrib(PARAM_NUM ParamNum)
{
return &attribs[ParamNum];
}
/** Load default values for all parameters */
void LoadDefaults()
{
const Attributes *curAtr = attribs;
for (int idx = 0; idx < PARAM_LAST; idx++, curAtr++)
{
if (curAtr->id > 0)
values[idx] = curAtr->def;
}
}
void SetFlagsRaw(PARAM_NUM param, uint8_t rawFlags)
{
flags[param] = rawFlags;
}
void SetFlag(PARAM_NUM param, PARAM_FLAG flag)
{
flags[param] |= (uint8_t)flag;
}
void ClearFlag(PARAM_NUM param, PARAM_FLAG flag)
{
flags[param] &= (uint8_t)~flag;
}
PARAM_FLAG GetFlag(PARAM_NUM param)
{
return (PARAM_FLAG)flags[param];
}
PARAM_TYPE GetType(PARAM_NUM param)
{
return (PARAM_TYPE)attribs[param].type;
}
uint32_t GetIdSum()
{
#ifndef PARAM_ID_SUM_START_OFFSET
#define PARAM_ID_SUM_START_OFFSET 0
#endif // PARAM_ID_SUM_START_OFFSET
#define PARAM_ENTRY(category, name, unit, min, max, def, id) id +
#define TESTP_ENTRY(category, name, unit, min, max, def, id) id +
#define VALUE_ENTRY(name, unit, id) id +
return PARAM_LIST PARAM_ID_SUM_START_OFFSET;
#undef PARAM_ENTRY
#undef TESTP_ENTRY
#undef VALUE_ENTRY
}
}