-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwb-mqtt-dac.js
94 lines (84 loc) · 2.87 KB
/
wb-mqtt-dac.js
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
(function () {
//don't touch this line
// prepare iio device list
var iioChannelOfNodeMap = {};
function init() {
var virt_device_params = {
cells: {},
};
var config = {};
try {
config = readConfig('/var/lib/wb-mqtt-dac/conf.d/system.conf', { logErrorOnNoFile: false });
} catch (err) {
try {
config = readConfig('/etc/wb-mqtt-dac.conf', { logErrorOnNoFile: false });
} catch (err) {
log.warning('DAC: no config file');
return;
}
}
var channels_by_id = {};
virt_device_params.title = config.device_name;
for (var i = 0; i < config.channels.length; ++i) {
var channel = config.channels[i];
if (channel.iio_of_name) {
var keys = Object.keys(iioChannelOfNodeMap);
for (var j = 0; j < keys.length; j++) {
var ofNodeName = iioChannelOfNodeMap[keys[j]];
if (ofNodeName == channel.iio_of_name) {
channel.iio_device = keys[j];
}
}
}
if (channel.iio_device) {
virt_device_params.cells[channel.id] = { type: 'range', value: 0 };
virt_device_params.cells[channel.id].max = channel.max_value_mv;
channels_by_id[channel.id] = channel;
} else {
log.warning('DAC: {}: IIO device not found, skipping'.format(channel.id));
}
}
// virtual device with controls has to be created before calling defineRule
if (Object.keys(virt_device_params.cells).length > 0) {
defineVirtualDevice('wb-dac', virt_device_params);
}
var channelIds = Object.keys(channels_by_id);
for (var i = 0; i < channelIds.length; ++i) {
var channel = channels_by_id[channelIds[i]];
defineRule('_dac_change_' + channel.id, {
whenChanged: 'wb-dac/' + channel.id,
then: function (newValue, devName, cellName) {
var channel = channels_by_id[cellName];
var value_raw = Math.round(newValue / channel.multiplier);
runShellCommand(
'echo {} > /sys/bus/iio/devices/iio:device{}/out_voltage{}_raw'.format(
value_raw,
channel.iio_device,
channel.iio_channel
)
);
},
});
}
}
/* Get of_node name for each IIO device */
runShellCommand(
"for i in /sys/bus/iio/devices/iio:device*/of_node/name; do echo -n $i; echo ' '`cat $i`; done",
{
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
var strList = capturedOutput.split('\n');
for (var i = 0; i < strList.length; ++i) {
var strParts = strList[i].split(' ', 2);
if (strParts.length == 2) {
var path = strParts[0];
var ofNodeName = strParts[1];
var num = path.match(/iio:device(\d+)/)[1];
iioChannelOfNodeMap[num] = ofNodeName;
}
}
init();
},
}
);
})();