forked from raspilc/ioBroker.mcp3xxx-analog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·167 lines (140 loc) · 4.84 KB
/
main.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
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
"use strict";
/*
* Created with @iobroker/create-adapter v1.16.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const mcpadc = require('spi-device');
// Load your modules here, e.g.:
// const fs = require("fs");
class Mcp3xxxAnalog extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "mcp3xxx-analog",
});
this.on("ready", this.onReady.bind(this));
this.on("objectChange", this.onObjectChange.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
var requiredchannels = (Number(this.config.SelectIC[6]));
var n = 0;
while (n < requiredchannels) {
await this.setObjectNotExists('Channel' + n, {
type: 'state',
common: {
name: 'Kanal' + n,
type: 'number',
role: 'value',
read: true,
write: true
},
native: {}
});
this.log.info("Channel " + n + " initialized");
n++;
};
readanalog(this, Number(this.config.Bus), Number(this.config.device), requiredchannels, Number(this.config.Interval), Number(this.config.SelectIC[4]));
this.subscribeStates("*");
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info("cleaned everything up...");
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Mcp3xxxAnalog(options);
} else {
// otherwise start the instance directly
new Mcp3xxxAnalog();
}
function readanalog(self, busNum, devNum, channels, readtime, resolution) {
self.log.info("Start reading analog values at bus " + busNum + " device " + devNum + " on " + channels + " channels @ " + readtime + " ms interval" + " with resolution 1" + resolution + " Bit");
const mcp = require('spi-device');
try{
const mcp3204 = mcp.open(busNum, devNum, (err) => {
var i; var cmd;
for (i = 0; i < channels; i++) {
(function(i) {
setInterval(() => {
if (resolution == 2) {
if (channels == 2) {
cmd = [0x01, ((0x05 + 2 * i) << 5), 0x00]; // MCP3202
} else {
cmd = [0x05 + i[2] * 2, ((0x00 + i) << 6), 0x00]; // MCP3204/3208
};
} else {
if (channels == 2) {
cmd = [((0x0d + 2 * i) << 3), 0x00]; // MCP3002
} else {
cmd = [0x01, ((0x08 + i) << 4), 0x00]; // MCP3004/3008
};
};
/*self.log.info("ch" + i + " cmd: " + cmd);*/
const message = [{
sendBuffer: Buffer.from(cmd), // Sent to read
receiveBuffer: Buffer.alloc(3), // received raw data
byteLength: 3,
speedHz: 20000
}];
if (err) throw err;
mcp3204.transfer(message, (err, message) => {
if (err) {
throw err;
self.log.warn("Error reading analog values");
} else {
if (resolution == 0) {
if (channels == 2) {
self.setStateAsync(('Channel' + i), (((message[0].receiveBuffer[0] & 0x03) << 8) + message[0].receiveBuffer[1]), true); /* MCP3002 */
} else {
self.setStateAsync(('Channel' + i), (((message[0].receiveBuffer[1] & 0x03) << 8) + message[0].receiveBuffer[2]), true); /* MCP3004/3008 */
};
} else if (resolution == 2) {
self.setStateAsync(('Channel' + i), (((message[0].receiveBuffer[1] & 0x0f) << 8) + message[0].receiveBuffer[2]), true); /* MCP320X */
};
};
});
}, readtime);
})(i);
}
});
} catch (err) {
self.log.warn("Error reading analog values. " + err)};
};