Skip to content

Commit

Permalink
platform: Add PWM support
Browse files Browse the repository at this point in the history
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: rzr/webthing-iotjs#3
Change-Id: I6f587a840e8c429d91ba2491e7cddfdfab66bf4b
Signed-off-by: Philippe Coval <p.coval@samsung.com>
  • Loading branch information
rzr committed Jun 27, 2019
1 parent 8ce4c0b commit 8875578
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
14 changes: 13 additions & 1 deletion example/platform/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ tmp_dir ?= tmp
NODE_PATH := ${topreldir}:${NODE_PATH}
export NODE_PATH

edison_gpio ?= 12


%: %/${runtime}
echo "# $@: $^"
Expand Down Expand Up @@ -75,10 +77,20 @@ check/%: ${lib_srcs}

check: check/${runtime}


board/%: ${main_src} board/%.js /sys/class/gpio/export setup
${runtime} $< ${@F}

/sys/class/gpio/gpio${edison_gpio}:
echo ${edison_gpio} | sudo tee /sys/class/gpio/export
ls -l $@

edison/%: sys/class/gpio/gpio${edison_gpio} ${main_src}
echo out | sudo tee ${<}/direction
echo 0 | sudo tee ${<}/value
sudo cat /sys/kernel/debug/gpio_debug/gpio${edison_gpio}/current_pinmux # mode0
echo mode1 | sudo tee /sys/kernel/debug/gpio_debug/gpio${edison_gpio}/current_pinmuxsh
${MAKE} run/${@F} run_args="${@D}"

flex-phat/%: ${main_src} /sys/class/gpio/export
-gpio -v || sudo apt-get install gpio
gpio -g mode 11 up
Expand Down
48 changes: 48 additions & 0 deletions example/platform/board/edison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// -*- 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('urn:dev:ops:my-edison-1234',
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 = () => {
if (!module.exports.instance) {
module.exports.instance = new EdisonThing();
}
return module.exports.instance;
};
3 changes: 2 additions & 1 deletion example/platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/rzr/webthing-iotjs",
"dependencies": {
"gpio": "^0.2.10"
"gpio": "^0.2.10",
"pwm": "0.0.3"
}
}
115 changes: 115 additions & 0 deletions example/platform/pwm/pwm-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// -*- 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;

if (typeof this.config.pwm == 'undefined') {
this.config.pwm = {};
}

if (typeof this.config.pwm.pin == 'undefined') {
this.config.pwm.pin = 0;
}

if (typeof this.config.pwm.period == 'undefined') {
this.config.pwm.period = 0.1;
}

if (typeof this.config.pwm.dutyCycle == 'undefined') {
this.config.pwm.dutyCycle = 0.5;
}
verbose(`log: opening: ${this.getName()}`);
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 = (value) => {
const usec = Math.floor((self.config.pwm.period * 1000 * 1000) *
(Number(value) / 100.0));

self.port.setDutyCycle(usec, () => {
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(() => {
console.log(new Date());
}, 10000);
}

0 comments on commit 8875578

Please sign in to comment.