Skip to content

Commit 0639ea5

Browse files
committed
gpio: Refactor to align upcoming adc
Reorder in before out to minimise diff Use self instead of _this generated by babel.js for IoT.js Change-Id: I53a9d97f645be63d33e942706105ffbc7ddc0d46 Signed-off-by: Philippe Coval <[email protected]>
1 parent 7ab89dd commit 0639ea5

File tree

1 file changed

+46
-49
lines changed

1 file changed

+46
-49
lines changed

example/platform/gpio/gpio-property.js

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,93 +22,90 @@ const {
2222

2323
const gpio = require('gpio');
2424

25-
class GpioOutProperty extends Property {
25+
class GpioInProperty extends Property {
2626
constructor(thing, name, value, metadata, config) {
27-
const valueObject = new Value(value, (value) => {
28-
this.handleValueChanged && this.handleValueChanged(value);
27+
const valueObject = new Value(Boolean(value), () => {
2928
});
3029
super(thing, name, valueObject,
3130
{
32-
'@type': 'OnOffProperty',
31+
'@type': 'BooleanProperty',
3332
label: (metadata && metadata.label) || `On/Off: ${name}`,
3433
type: 'boolean',
35-
description: (metadata && metadata.description) ||
36-
(`GPIO Actuator on pin=${config.pin}`),
34+
readOnly: true,
35+
description:
36+
(metadata && metadata.description) ||
37+
(`GPIO Sensor on pin=${config.pin}`),
3738
});
38-
const _this = this;
39+
const self = this;
3940
this.config = config;
40-
41+
const callback = () => {
42+
log(`log: GPIO: ${self.getName()}: open:`);
43+
self.port.on('change', (value) => {
44+
value = Boolean(value);
45+
log(`log: GPIO: ${self.getName()}: change: ${value}`);
46+
self.value.notifyOfExternalUpdate(value);
47+
});
48+
};
4149
this.port = gpio.export(config.pin,
42-
{direction: 'out',
43-
ready: () => {
44-
log(`log: GPIO: ${_this.getName()}: open:`);
45-
_this.handleValueChanged = (value) => {
46-
try {
47-
log(`log: GPIO: ${_this.getName()}: \
48-
writing: ${value}`);
49-
_this.port.set(value);
50-
} catch (err) {
51-
console.error(`error: GPIO:
52-
${_this.getName()}: Fail to write: ${err}`);
53-
return err;
54-
}
55-
};
56-
}});
50+
{direction: 'in', ready: callback});
5751
}
5852

5953
close() {
60-
const _this = this;
6154
try {
6255
this.port && this.port.unexport(this.config.pin);
6356
} catch (err) {
64-
console.error(`error: GPIO: ${this.getName()}: Fail to close: ${err}`);
57+
console.error(`error: GPIO: ${this.getName()} close:${err}`);
6558
return err;
6659
}
67-
log(`log: GPIO: ${_this.getName()}: close:`);
60+
log(`log: GPIO: ${this.getName()}: close:`);
6861
}
6962
}
7063

71-
72-
class GpioInProperty extends Property {
64+
class GpioOutProperty extends Property {
7365
constructor(thing, name, value, metadata, config) {
74-
super(thing, name, new Value(Boolean(value)),
66+
const valueObject = new Value(value, (value) => {
67+
this.handleValueChanged && this.handleValueChanged(value);
68+
});
69+
super(thing, name, valueObject,
7570
{
76-
'@type': 'BooleanProperty',
71+
'@type': 'OnOffProperty',
7772
label: (metadata && metadata.label) || `On/Off: ${name}`,
7873
type: 'boolean',
79-
readOnly: true,
80-
description:
81-
(metadata && metadata.description) ||
82-
(`GPIO Sensor on pin=${config.pin}`),
74+
description: (metadata && metadata.description) ||
75+
(`GPIO Actuator on pin=${config.pin}`),
8376
});
84-
const _this = this;
77+
const self = this;
8578
this.config = config;
86-
const callback = () => {
87-
log(`log: GPIO: ${_this.getName()}: open:`);
88-
_this.port.on('change', (value) => {
89-
value = Boolean(value);
90-
log(`log: GPIO: ${_this.getName()}: change: ${value}`);
91-
_this.value.notifyOfExternalUpdate(value);
92-
});
93-
};
79+
9480
this.port = gpio.export(config.pin,
95-
{direction: 'in', ready: callback});
81+
{direction: 'out',
82+
ready: () => {
83+
log(`log: GPIO: ${self.getName()}: open:`);
84+
self.handleValueChanged = (value) => {
85+
try {
86+
log(`log: GPIO: ${self.getName()}: \
87+
writing: ${value}`);
88+
self.port.set(value);
89+
} catch (err) {
90+
console.error(`error: GPIO:
91+
${self.getName()}: Fail to write: ${err}`);
92+
return err;
93+
}
94+
};
95+
}});
9696
}
9797

9898
close() {
99-
const _this = this;
10099
try {
101-
this.inverval && clearInterval(this.inverval);
102100
this.port && this.port.unexport(this.config.pin);
103101
} catch (err) {
104-
console.error(`error: GPIO: ${this.getName()} close:${err}`);
102+
console.error(`error: GPIO: ${this.getName()}: Fail to close: ${err}`);
105103
return err;
106104
}
107-
log(`log: GPIO: ${_this.getName()}: close:`);
105+
log(`log: GPIO: ${self.getName()}: close:`);
108106
}
109107
}
110108

111-
112109
function GpioProperty(thing, name, value, metadata, config) {
113110
if (config.direction === 'out') {
114111
return new GpioOutProperty(thing, name, value, metadata, config);

0 commit comments

Comments
 (0)