Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

The RGB.js sample run fails #92

Closed
yhe39 opened this issue Aug 8, 2016 · 6 comments
Closed

The RGB.js sample run fails #92

yhe39 opened this issue Aug 8, 2016 · 6 comments

Comments

@yhe39
Copy link

yhe39 commented Aug 8, 2016

The RGB.js sample doesn't match the new promise-based gpio API.
We change the code:

var red = gpio.open({pin: pins.IO2, direction: 'out'});
to
var red = null;
gipo.open({pin: pins.IO2, direction: 'out'}).then(function(pin){
red = pin;
});

And the same change of the red, green and blue
Then move the setInterval() to the blue gpio.open statements. Then the test pass.

@kenchris
Copy link
Contributor

kenchris commented Aug 8, 2016

As this is using promises, maybe the code should be restructured instead to not require var red = null outside

@kenchris
Copy link
Contributor

kenchris commented Aug 8, 2016

You can always do stuff like (not sure now we are not using real promises)

Promise p1 = gpio.open ...
Promise p2 = gpio.open ...

Promise.all([p1, p2]).then(function(values) { }) 

@kenchris
Copy link
Contributor

kenchris commented Aug 8, 2016

Btw, ES2017 would make this quite similar as before promises

async function run() {
  let led = await gpio.open({pin: pins.LED0, direction: 'out'});

  let red = await gpio.open({pin: pins.IO2, direction: 'out'});
  let green = await gpio.open({pin: pins.IO7, direction: 'out'});
  let blue = await gpio.open({pin: pins.IO8, direction: 'out'});
}

Not sure if we could use babel as a tool for converting this into ES5.1

@kenchris
Copy link
Contributor

kenchris commented Aug 8, 2016

It might make sense, so make an example to see if we babel can be used for transpiling newer ES features onto JerryScript.

Example

require("babel-polyfill");

async function run() {
  let red = await gpio.open();
  let blue = await gpio.open();
  gen();
}

run();

becomes

"use strict";

var run = function () {
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
    var red, blue;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return gpio.open();
          case 2:
            red = _context.sent;
            _context.next = 5;
            return gpio.open();
          case 5:
            blue = _context.sent;

            gen();

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function run() {
    return _ref.apply(this, arguments);
  };
}();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

require("babel-polyfill");

run();

Using .babelrc:

{
  "presets": ["es2015"],
  "plugins": ["syntax-async-generators", "syntax-async-functions", "transform-async-to-generator"]
}

and ./node_modules/.bin/babel test.js

@grgustaf
Copy link
Contributor

Ken's suggestions are still interesting to try out, but the original RGB.js now works again because I've added back a synchronous version of gpio.open.

@yhe39
Copy link
Author

yhe39 commented Aug 10, 2016

Verified. this issue doesn't exist. close this issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants