From 02172e612a4cb5951d1b911ff0a973adac80e6d6 Mon Sep 17 00:00:00 2001
From: Philippe Coval
Date: Mon, 24 Jun 2019 17:51:10 +0200
Subject: [PATCH] platform: Add PWM support
It was tested on Intel edison on PWM0 but
GPIO12 must be manually configured first
```chip=0
id=0
gpio=12
echo $gpio | sudo tee /sys/class/gpio/export
echo out | sudo tee /sys/class/gpio/gpio$gpio/direction
echo 0 | sudo tee /sys/class/gpio/gpio$gpio/value
sudo cat /sys/kernel/debug/gpio_debug/gpio$gpio/current_pinmux # mode0
echo mode1 | sudo tee /sys/kernel/debug/gpio_debug/gpio${gpio}/current_pinmuxsh
```
More/Better gpio drivers maybe used later,
'pwm' one was used here as base for webthing-iotjs.
Relate-to: https://github.com/rzr/webthing-iotjs/issues/3
Change-Id: I6f587a840e8c429d91ba2491e7cddfdfab66bf4b
Signed-off-by: Philippe Coval
---
example/platform/board/edison.js | 47 ++++++++++++
example/platform/package.json | 3 +-
example/platform/pwm/pwm-property.js | 111 +++++++++++++++++++++++++++
3 files changed, 160 insertions(+), 1 deletion(-)
create mode 100644 example/platform/board/edison.js
create mode 100644 example/platform/pwm/pwm-property.js
diff --git a/example/platform/board/edison.js b/example/platform/board/edison.js
new file mode 100644
index 0000000..80d69fc
--- /dev/null
+++ b/example/platform/board/edison.js
@@ -0,0 +1,47 @@
+// -*- mode: js; js-indent-level:2; -*-
+// SPDX-License-Identifier: MPL-2.0
+
+/**
+ *
+ * Copyright 2018-present Samsung Electronics France SAS, and other contributors
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.*
+ */
+
+const {
+ Thing,
+} = require('webthing');
+
+const PwmProperty = require('../pwm/pwm-property');
+
+class EdisonThing extends Thing {
+ constructor(name, type, description) {
+ super(name || 'Edison',
+ type || [],
+ description || 'A web connected Edison');
+ const self = this;
+ this.pinProperties = [
+ new PwmProperty(this, 'PWM0', 50, {
+ description: 'Analog port of Edison',
+ }),
+ ];
+ this.pinProperties.forEach((property) => {
+ self.addProperty(property);
+ });
+ }
+
+ close() {
+ this.pinProperties.forEach((property) => {
+ property.close && property.close();
+ });
+ }
+}
+
+module.exports = function() {
+ if (!module.exports.instance) {
+ module.exports.instance = new EdisonThing();
+ }
+ return module.exports.instance;
+};
diff --git a/example/platform/package.json b/example/platform/package.json
index 41cf49c..5f60d11 100644
--- a/example/platform/package.json
+++ b/example/platform/package.json
@@ -28,6 +28,7 @@
},
"homepage": "https://github.com/rzr/webthing-iotjs",
"dependencies": {
- "gpio": "^0.2.10"
+ "gpio": "^0.2.10",
+ "pwm": "0.0.3"
}
}
diff --git a/example/platform/pwm/pwm-property.js b/example/platform/pwm/pwm-property.js
new file mode 100644
index 0000000..19984ed
--- /dev/null
+++ b/example/platform/pwm/pwm-property.js
@@ -0,0 +1,111 @@
+// -*- mode: js; js-indent-level:2; -*-
+// SPDX-License-Identifier: MPL-2.0
+
+/**
+ *
+ * Copyright 2018-present Samsung Electronics France SAS, and other contributors
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/
+ */
+const console = require('console');
+
+// Disable logs here by editing to '!console.log'
+const log = console.log || function() {};
+const verbose = !console.log || function() {};
+
+const {
+ Property,
+ Value,
+} = require('webthing');
+
+const pwm = require('pwm');
+
+class PwmOutProperty extends Property {
+ constructor(thing, name, value, metadata, config) {
+ if (typeof config === 'undefined') {
+ config = {};
+ }
+ super(thing, name, new Value(Number(value)),
+ {
+ '@type': 'LevelProperty',
+ title: (metadata && metadata.title) || `PWM: ${name} (dutyCycle)`,
+ type: 'integer',
+ minimum: config.minimum || 0,
+ maximum: config.maximum || 100,
+ readOnly: false,
+ unit: 'percent',
+ description:
+ (metadata && metadata.description) ||
+ (`PWM DutyCycle`),
+ });
+ const self = this;
+ this.config = config;
+ if (!this.config.pwm) {
+ this.config.pwm = {
+ chip: 0,
+ pin: 0,
+ dutyCycle: 0.5, // secs
+ period: 1,
+ };
+ }
+ if (typeof this.config.pwm.dutyCycle == 'undefined') {
+ this.config.pwm.dutyCycle = 0.5;
+ }
+ verbose(`log: opening: ${this.description}`);
+ this.port = pwm.export(
+ this.config.pwm.chip, this.config.pwm.pin,
+ (err) => {
+ verbose(`log: PWM: ${self.getName()}: open: ${err}`);
+ if (err) {
+ console.error(`error: PWM: ${self.getName()}: open: ${err}`);
+ throw err;
+ }
+ self.port.freq = 1 / self.config.pwm.period;
+ // Linux sysfs uses usecs units
+ self.port.setPeriod(
+ self.config.pwm.period * 1000 * 1000,
+ () => {
+ self.port.setDutyCycle(
+ self.config.pwm.dutyCycle / 100 * 1000 * 1000,
+ () => {
+ self.port.setEnable(1, () => {
+ verbose(`log: ${self.getName()}: Enabled`);
+ });
+ });
+ });
+
+ self.value.valueForwarder = function(value) {
+ const usec = Math.floor((self.config.pwm.period * 1000 * 1000) *
+ (Number(value) / 100.0));
+
+ self.port.setDutyCycle(usec, function() {
+ verbose(`log: setDutyCycle: usec=${usec}`);
+ });
+ };
+ });
+ }
+
+ close() {
+ verbose(`log: PWM: ${this.getName()}: close:`);
+ try {
+ this.port && this.port.unexport();
+ } catch (err) {
+ console.error(`error: PWM: ${this.getName()} close:${err}`);
+ return err;
+ }
+ log(`log: PWM: ${this.getName()}: close:`);
+ }
+}
+
+
+module.exports = PwmOutProperty;
+
+
+if (module.parent === null) {
+ new PwmOutProperty;
+ setInterval(function() {
+ console.log(new Date());
+ }, 10000);
+}