Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Callbacks should be able to access the port name #14

Open
tigoe opened this issue Oct 14, 2015 · 3 comments
Open

Feature request: Callbacks should be able to access the port name #14

tigoe opened this issue Oct 14, 2015 · 3 comments

Comments

@tigoe
Copy link
Contributor

tigoe commented Oct 14, 2015

I was thinking about how to implement two-port sketches today. In Processing, the port generating the event is returned to serialEvent as a parameter like so:

void serialEvent(port) {

}

With this, you can take different actions depending on which port is sending data. It might be useful to have this option in P5 too. I tried to see if I could access the port using "this" in the callback functions, but it didn't work.

@vanevery
Copy link
Member

What do you think about following the JavaScript Event.target syntax? It might not be a good idea since it isn't a proper JS Event object but it would be familiar to a lot of people.

void serialPort(evt) {
if (evt.target === someport) {
// Do this
} else if (evt.target === someotherport) {
// Do that
}
}

I would also pass the data in via evt.data and get rid of the "raw" callback.

@tigoe
Copy link
Contributor Author

tigoe commented Oct 14, 2015

I like it as a way to transition people to the way JS handles events natively. It's also a nice way to get at the other properties. I particularly like the evt. data bit. I'm not sure why you say it's not a proper event, aren't you generating the event through the libary?

@vanevery
Copy link
Member

There is a standard Event object that is used heavily by DOM elements which uses a standard addEventListener/dispatchEvent syntax. It didn't seem appropriate for this when I started writing it (overkill) but perhaps as the library matures...

Regardless, I'll add an object to the callback that includes the serialport object (target) and the data (data). It

In the mean time, unlike Java you can have different function names for the different port callbacks:
(it's ugly but probably easy to understand)

var firstSerialPort = ...
var secondSerialPort = ...

firstSerialPort.on('data', firstSerialPortDataEvent);
secondSerialPort.on('data', secondSerialPortDataEvent);

function firstSerialPortDataEvent() {

}

function secondSerialPortDataEvent() {

}

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

No branches or pull requests

2 participants