Vuo
2.4.4
Loading...
Searching...
No Matches
node
vuo.serial
VuoSerialDevice.c
Go to the documentation of this file.
1
10
#include "
VuoSerialDevice.h
"
11
#include "
VuoSerial.h
"
12
14
#ifdef VUO_COMPILER
15
VuoModuleMetadata
({
16
"title"
:
"Serial Device"
,
17
"description"
:
"Information about a serial I/O device."
,
18
"keywords"
: [ ],
19
"version"
:
"1.0.0"
,
20
"dependencies"
: [
21
"VuoSerialDevices"
,
22
"VuoText"
23
]
24
});
25
#endif
27
31
static
VuoSerialDevice_MatchType
VuoSerialDevice_MatchType_makeFromJson
(json_object *js)
32
{
33
if
(strcmp(json_object_get_string(js),
"path"
) == 0)
34
return
VuoSerialDevice_MatchPath;
35
36
return
VuoSerialDevice_MatchName;
37
}
38
42
static
const
char
*
VuoSerialDevice_getStringForMatchType
(
VuoSerialDevice_MatchType
type)
43
{
44
if
(type == VuoSerialDevice_MatchPath)
45
return
"path"
;
46
47
return
"name"
;
48
}
49
61
VuoSerialDevice
VuoSerialDevice_makeFromJson
(json_object *js)
62
{
63
return
(
VuoSerialDevice
){
64
VuoJson_getObjectValue
(
VuoSerialDevice_MatchType
, js,
"matchType"
, VuoSerialDevice_MatchName),
65
VuoJson_getObjectValue
(
VuoText
, js,
"name"
, NULL),
66
VuoJson_getObjectValue
(
VuoText
, js,
"path"
, NULL)
67
};
68
}
69
73
json_object *
VuoSerialDevice_getJson
(
const
VuoSerialDevice
value)
74
{
75
json_object *js = json_object_new_object();
76
77
json_object *matchTypeObject = json_object_new_string(
VuoSerialDevice_getStringForMatchType
(value.
matchType
));
78
json_object_object_add(js,
"matchType"
, matchTypeObject);
79
80
if
(value.
name
)
81
{
82
json_object *o =
VuoText_getJson
(value.
name
);
83
json_object_object_add(js,
"name"
, o);
84
}
85
86
if
(value.
path
)
87
{
88
json_object *o =
VuoText_getJson
(value.
path
);
89
json_object_object_add(js,
"path"
, o);
90
}
91
92
return
js;
93
}
94
98
bool
VuoSerialDevice_areEqual
(
const
VuoSerialDevice
valueA,
const
VuoSerialDevice
valueB)
99
{
100
if
(valueA.
matchType
!= valueB.
matchType
)
101
return
false
;
102
103
if
(!
VuoText_areEqual
(valueA.
name
, valueB.
name
))
104
return
false
;
105
106
if
(!
VuoText_areEqual
(valueA.
path
, valueB.
path
))
107
return
false
;
108
109
return
true
;
110
}
111
115
bool
VuoSerialDevice_isLessThan
(
const
VuoSerialDevice
valueA,
const
VuoSerialDevice
valueB)
116
{
117
return
VuoText_isLessThan
(valueA.
name
, valueB.
name
);
118
}
119
129
bool
VuoSerialDevice_realize
(
VuoSerialDevice
device,
VuoSerialDevice
*realizedDevice)
130
{
131
// Already have a path; nothing to do.
132
if
(device.
matchType
== VuoSerialDevice_MatchPath)
133
{
134
realizedDevice->
matchType
= device.
matchType
;
135
realizedDevice->
name
=
VuoText_make
(device.
name
);
136
realizedDevice->
path
=
VuoText_make
(device.
path
);
137
return
true
;
138
}
139
140
// Otherwise, try to find a matching name.
141
142
VuoList_VuoSerialDevice
devices =
VuoSerial_getDeviceList
();
143
VuoRetain
(devices);
144
145
unsigned
long
deviceCount =
VuoListGetCount_VuoSerialDevice
(devices);
146
if
(deviceCount == 0)
147
{
148
VUserLog
(
"Warning: No serial devices found."
);
149
VuoRelease
(devices);
150
return
false
;
151
}
152
153
VuoText
nameToMatch =
""
;
154
if
(device.
name
)
155
nameToMatch = device.
name
;
156
157
VuoText
foundName = NULL;
158
VuoText
foundPath = NULL;
159
for
(
unsigned
long
i = 1; i <= deviceCount; ++i)
160
{
161
VuoSerialDevice
d =
VuoListGetValue_VuoSerialDevice
(devices, i);
162
if
(strstr(d.
name
, nameToMatch))
163
{
164
foundName = d.
name
;
165
foundPath = d.
path
;
166
break
;
167
}
168
}
169
170
if
(foundPath)
171
{
172
realizedDevice->
matchType
= VuoSerialDevice_MatchPath;
173
realizedDevice->
name
=
VuoText_make
(foundName);
174
realizedDevice->
path
=
VuoText_make
(foundPath);
175
VuoRelease
(devices);
176
return
true
;
177
}
178
179
VUserLog
(
"Warning: Didn't find a serial device matching '%s'."
, device.
name
);
180
VuoRelease
(devices);
181
return
false
;
182
}
183
187
char
*
VuoSerialDevice_getSummary
(
const
VuoSerialDevice
value)
188
{
189
VuoSerialDevice
realizedDevice;
190
if
(
VuoSerialDevice_realize
(value, &realizedDevice))
191
{
192
VuoSerialDevice_retain
(realizedDevice);
193
char
*outputText =
VuoText_format
(
"<b>Name:</b> <code>%s</code><br><b>URL:</b> <code>%s</code>"
, realizedDevice.
name
, realizedDevice.
path
);
194
VuoSerialDevice_release
(realizedDevice);
195
return
outputText;
196
}
197
return
strdup(
"Unknown device"
);
198
}
Generated on Thu Nov 9 2023 16:59:30 for Vuo by
1.9.8