Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit a5a5385

Browse files
committed
Add color type to GUI
1 parent 419bccd commit a5a5385

File tree

10 files changed

+173
-116
lines changed

10 files changed

+173
-116
lines changed

docs/config-manager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ An example of how this file could look is shown below:
128128
]
129129
```
130130

131-
Supported data types are: bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float and char. The length argument is mandatory for datatype char to indicate the length of the string. Variable length strings are not supported. Also, arrays are not supported for now.
131+
Supported data types are: bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float and char. The length argument is mandatory for datatype char to indicate the length of the string. Variable length strings are not supported. Also, arrays are not supported for now. Finally, the type can also be set to `color`. In this case the field will be shown as a color picker. Define the value of a color in your JSON file as an array with three numbers between 0 and 255 for the rgb values: `"value": [255, 255, 255]`.
132132

133133
Adding the field `"control": "select",` will turn the item into a drop down menu, with the options defined as `"options": [1, 2, 3]`.
134134

docs/dashboard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ An example of how this file could look is shown below:
105105
]
106106
```
107107

108-
Supported data types are: bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float and char. The length argument is mandatory for datatype char to indicate the length of the string. Variable length strings are not supported. Also, arrays are not supported for now.
108+
Supported data types are: bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float and char. The length argument is mandatory for datatype char to indicate the length of the string. Variable length strings are not supported. Also, arrays are not supported for now. Finally, the type can also be set to `color`. In this case the field will be shown as a color picker which will be represented in your code as an array with 3 numbers for the rgb values.
109109

110110
there are a few other specific fields. the `direction` field is mandatory, and `display` will show the items in the live dashboard whereas `control` will put the items as control inputs in the dashboard. For `display` items, `"display": "graph",` will turn them into a live graph, with the x-axis length in seconds defined by `"xaxis": 20`. For displayed floats, the field `digits` can be used to limit the number of digits shown.
111111

gui/js/comp/ControlItem.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export function ControlItem(props) {
4141
}
4242
binDataView.setUint8(target.length, 0);
4343
break;
44+
case "color":
45+
//parse color code
46+
binDataView.setUint8(0, parseInt(target.slice(1,3), 16));
47+
binDataView.setUint8(1, parseInt(target.slice(3,5), 16));
48+
binDataView.setUint8(2, parseInt(target.slice(5,7), 16));
49+
break;
4450
case "bool":
4551
if (target === true) { binDataView.setUint8(0, 1); } else { binDataView.setUint8(0, 0); }
4652
break;
@@ -104,6 +110,8 @@ export function ControlItem(props) {
104110
</select>;
105111
} else if (checkbox) {
106112
return <input onClick={(e) => { setTarget(e.target.checked); save(); }} type={props.type} id={props.name} name={props.name} value={data} {...props.conditionalAttributes} />;
113+
} else if (props.type == "color") {
114+
return <input onChange={(e) => { setTarget(e.target.value); save(); }} type={props.type} id={props.name} name={props.name} value={data} {...props.conditionalAttributes} />;
107115
} else {
108116
return <><input onChange={(e) => { setTarget(e.target.value); }} type={props.type} id={props.name} name={props.name} value={data} {...props.conditionalAttributes} />
109117
<Button onClick={(e) => {

gui/js/comp/DashboardItems.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const DefaultTypeAttributes = {
6262
char: {
6363
type: "text",
6464
},
65+
color: {
66+
type: "color",
67+
},
6568
bool: {
6669
type: "checkbox",
6770
},

gui/js/comp/DisplayItem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export function DisplayItem(props) {
4545
</FlexibleWidthXYPlot>
4646
</div>;
4747

48+
} else if (props.item.type == "color") {
49+
return <input type={props.item.type} id={props.item.name} disabled="disabled" title="This field is read-only" name={props.item,name} style={{cursor: "default"}} value={props.value} />;
4850
} else {
49-
5051
return <span id={props.item.name} name={props.item.name} className={props.item.type == "bool" ? props.value.toString() : ""}>{props.value.toString()}</span>;
5152

5253
}

gui/js/comp/UiComponents.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,28 @@ export const Form = styled.form`
314314
max-width:100%;
315315
}
316316
317+
input[type=color]
318+
{
319+
display:inline-block;
320+
margin-left:-2px;
321+
width: 48px;
322+
height:32px;
323+
border-radius: 3px;
324+
border:none;
325+
outline: none;
326+
-webkit-appearance: none;
327+
cursor:pointer;
328+
background-color:#fff;
329+
vertical-align: middle;
330+
}
331+
332+
input[type=color]::-webkit-color-swatch-wrapper {
333+
padding: 0;
334+
}
335+
input[type=color]::-webkit-color-swatch {
336+
border-radius: 3px;
337+
}
338+
317339
input[type=checkbox] {
318340
width: auto;
319341
margin:12px 0px;

gui/js/functions/configHelpers.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export function obj2bin(obj, binSize, JSON) {
1313
binDataView.setUint8(n, 0);
1414
n += JSON[i].length - obj[JSON[i].name].length;
1515
break;
16+
case "color":
17+
//parse color code
18+
binDataView.setUint8(n, parseInt(obj[JSON[i].name].slice(1,3), 16));
19+
binDataView.setUint8(n + 1, parseInt(obj[JSON[i].name].slice(3,5), 16));
20+
binDataView.setUint8(n + 2, parseInt(obj[JSON[i].name].slice(5,7), 16));
21+
22+
n += 3;
23+
break;
1624
case "bool":
1725
if (obj[JSON[i].name] === true) {binDataView.setUint8(n, 1);} else {binDataView.setUint8(n, 0);}
1826
n++;
@@ -70,6 +78,11 @@ export function bin2obj(rawData, JSON) {
7078
parsedData[JSON[i].name] = utf8decoder.decode(rawData.slice(n, n + JSON[i].length)).split("\0").shift();
7179
n = n + JSON[i].length;
7280
break;
81+
case "color":
82+
//create color code
83+
parsedData[JSON[i].name] = "#" + ("0" + (rawDataView.getUint8(n)).toString(16)).slice(-2) + ("0" + (rawDataView.getUint8(n + 1)).toString(16)).slice(-2) + ("0" + (rawDataView.getUint8(n + 2)).toString(16)).slice(-2);
84+
n = n + 3;
85+
break;
7386
case "bool":
7487
parsedData[JSON[i].name] = !!rawDataView.getUint8(n);
7588
n++;
@@ -124,6 +137,10 @@ export function binsize(name, JSON) {
124137
sizeArray = [n, JSON[i].length];
125138
n = n + JSON[i].length;
126139
break;
140+
case "color":
141+
sizeArray = [n, 3];
142+
n += 3;
143+
break;
127144
case "bool":
128145
sizeArray = [n, 1];
129146
n++;

scripts/preBuildConfig.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def preBuildConfigFun():
3939
if item['type'] == 'char':
4040
cpp.write("\t\"" + item['value'] + "\"")
4141
h.write("\tchar " + item['name'] + "[" + str(item['length']) + "];\n")
42+
elif item['type'] == 'color':
43+
cpp.write("\t{" + str(item['value'][0]) + ',' + str(item['value'][1]) + ',' + str(item['value'][2]) + '}')
44+
h.write("\tuint8_t " + item['name'] +"[3];\n")
4245
elif item['type'] == 'bool':
4346
cpp.write("\t" + str(item['value']).lower())
4447
h.write("\t" + item['type'] + " " + item['name'] +";\n")

scripts/preBuildDash.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def preBuildDashFun():
2323
for item in data:
2424
if item['type'] == 'char':
2525
h.write("\tchar " + item['name'] + "[" + str(item['length']) + "];\n")
26+
elif item['type'] == 'color':
27+
h.write("\tuint8_t " + item['name'] +"[3];\n")
2628
elif item['type'] == 'bool':
2729
h.write("\t" + item['type'] + " " + item['name'] +";\n")
2830
else:

0 commit comments

Comments
 (0)