-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam_json.cpp
More file actions
122 lines (105 loc) · 3 KB
/
param_json.cpp
File metadata and controls
122 lines (105 loc) · 3 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
/*
* This file is part of the libopeninv project.
*
* Copyright (C) 2024
*
* 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 "param_json.h"
#ifdef ARDUINO
#include <ArduinoJson.h>
#include <string.h>
namespace
{
String parameterJson;
size_t streamOffset = 0;
size_t EstimateJsonDocSize()
{
const size_t perParamOverhead = 160;
const size_t baseOverhead = 512;
return baseOverhead + (Param::PARAM_LAST * perParamOverhead);
}
}
namespace ParamJson
{
void Build()
{
DynamicJsonDocument doc(EstimateJsonDocSize());
for (int i = 0; i < Param::PARAM_LAST; i++)
{
const Param::Attributes* attr = Param::GetAttrib((Param::PARAM_NUM)i);
if (!attr) continue;
JsonObject param = doc[attr->name].to<JsonObject>();
param["unit"] = attr->unit;
param["category"] = attr->category;
param["minimum"] = attr->min;
param["maximum"] = attr->max;
param["default"] = attr->def;
param["id"] = attr->id;
param["isparam"] = (Param::GetType((Param::PARAM_NUM)i) == Param::TYPE_PARAM) ? 1 : 0;
if (attr->name != nullptr && strcmp(attr->name, "version") == 0)
{
param["value"] = Param::GetFloat((Param::PARAM_NUM)i);
}
else if (Param::GetType((Param::PARAM_NUM)i) == Param::TYPE_SPOTVALUE)
{
param["value"] = Param::GetFloat((Param::PARAM_NUM)i);
}
}
parameterJson = "";
serializeJson(doc, parameterJson);
streamOffset = 0;
}
const String& Get()
{
return parameterJson;
}
uint32_t GetSize()
{
return parameterJson.length();
}
int GetByte(uint32_t offset)
{
if (offset >= parameterJson.length())
{
return -1;
}
return (int)(uint8_t)parameterJson[offset];
}
void BeginStream()
{
Build();
}
size_t Read(uint8_t* out, size_t maxLen)
{
if (out == nullptr || maxLen == 0)
{
return 0;
}
const size_t totalLen = parameterJson.length();
if (streamOffset >= totalLen)
{
return 0;
}
size_t remaining = totalLen - streamOffset;
size_t toCopy = remaining < maxLen ? remaining : maxLen;
for (size_t i = 0; i < toCopy; i++)
{
out[i] = (uint8_t)parameterJson[streamOffset + i];
}
streamOffset += toCopy;
return toCopy;
}
}
#endif