Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
onesocialinbox
@onesocialinbox
Can Johnny-Five library run on browser DOM? When I browserify/webpack it has some problems running in browser... Any hints??
Lucas Sanz Gorostiaga
@lucas-sg

Hi everyone, I'm desperately looking for help with the Proximity class. I'm trying to read data from a HCRS-04 proximity sensor and then stop after I say so (with the click of a button). The thing is, there is no "disable" method or anything similar to make it stop. So I tried to pass a React state so that when the state changes, the callback function that is associated to the proximity.on("data", callback) method stops processing the data. The Proximity object would still be reading but there would be nothing going on since the state changed.

The problem is that the callback function won't change accordingly. The state is initiated with a "true" value, but when it changes to false the callback function doesn't notice. It's the same as if I had passed a function that said if (true) {...}.
I don't know what to do anymore, I really need this to work for my college thesis.

reconbot
@reconbot:matrix.org
[m]
Got any example code?
Lucas Sanz Gorostiaga
@lucas-sg

Sure, sorry for the delay, but here it is:

const proximityCallback = (centimeters: number) => {
    if (reactStateIsTrue) {
        console.log(centimeters);
    }
};
plotArduinoData(750, proximityCallback);

and the plotArduinoData function being:

export function plotArduinoData(freq: number, callback: (centimeters: number) => void) {
    if (board == null)
        board = new Board({
            repl: false,
            debug: false
        });

    board = board.on("ready", () => {
        const proximity = new Proximity({
            controller: "HCSR04",
            pin: 10,
            freq: freq
        });

        proximity.on("data", (data) => callback(data.cm));
    });
}
Like I said before, the callback function that is being bound to the proximity object in the on method is not changing according to the react state that is being passed on
I got a recommendation to use proximity.removeAllListeners("data"); to make it stop but since I'm using Typescript I don't have the inheritance relationship between
Proximity < Withinable < EventEmitter
reconbot
@reconbot:matrix.org
[m]
How are you getting this to run in the browser?
Lucas Sanz Gorostiaga
@lucas-sg
I'm running it as an Electron application
reconbot
@reconbot:matrix.org
[m]
Ahh
Lucas Sanz Gorostiaga
@lucas-sg
If you have any idea of how to overcome the Typescript problem of not recognizing Proximity's ascendants or if you have another approach to make the callback take the cue from React's state it would be really appreciated :pray:
reconbot
@reconbot:matrix.org
[m]
Is all this in a useEffect?
You probably got to share the react example too as this code looks ok but isn’t the full
Story
Rick Waldron
@rwaldron
I think the best we can do is a "pause/resume", which would "pause" the emitters from emitting until you call "resume"
Lucas Sanz Gorostiaga
@lucas-sg
@rwaldron that would be splendid!
@reconbot:matrix.org no, the first code fragment is part of a functional component that sets the layout for a chart to be plotted out. I didn't put the rest of the component because it's irrelevant to the problem. The component receives the reactStateIsTrue flag as part of props and uses it to tell the Proximity object whether or not to log the cm read (if only the Proximity object would actually listen to the state's changes).
The state (reactStateIsTrue) flag is a boolean that was initiated as:
    const [reactStateIsTrue, stop] = useState(true);
Lucas Sanz Gorostiaga
@lucas-sg

I think the best we can do is a "pause/resume", which would "pause" the emitters from emitting until you call "resume"

Would this be something that could be solved by myself with your advice/guidance or is this something that could be implemented eventually in the project?

Rick Waldron
@rwaldron
I meant, J5 can/should do that
One sec
Rick Waldron
@rwaldron
@lucas-sg when you get a chance, try this: npm install git://github.com/rwaldron/johnny-five.git#suspendable --save
You should now be able to call proximity.pause() to stop events from emitting
and then later proximity.resume() to restart
Lucas Sanz Gorostiaga
@lucas-sg
It says:
zsh: no matches found: git://github.com/rwaldron/johnny-five.git#suspendable
Rick Waldron
@rwaldron
weird. hold tight
Lucas Sanz Gorostiaga
@lucas-sg
I managed to install it by adding quotes before and after but I'm getting a message from the VSCode Linter saying Proximity doesn't have a pause or resume method since I'm using Typescript
And I think it needs to be implemented in DefinetelyTyped as well in order for it to work
Rick Waldron
@rwaldron
@lucas-sg I just ran npx typescript lib/**/*.js --declaration --allowJs --emitDeclarationOnly --outDir types and generated a ./types. It will probably need more attention, but I think that get's you somewhere at least, right?
I should know this, considering I write TS exclusively at work 🤦‍♂️
reconbot
@reconbot:matrix.org
[m]
as any one would
Rick Waldron
@rwaldron
lol
Lucas Sanz Gorostiaga
@lucas-sg
I'm sorry Rick, I don't understand what lib/**/*.js is supposed to represent in my Electron (Node) project. I don't have a lib folder, unless it's well hidden in a specific folder under node_modules, and therefore the command won't run.
I'm sure there is something really silly and basic I'm missing from it but I'm kind of a noob in Typescript and Electron as you can see.
reconbot
@reconbot:matrix.org
[m]
Just use (proximity as any).pause() until he fixes the types
Rick Waldron
@rwaldron
@lucas-sg oh, I'm sorry—that wasn't meant to be for you to do anythng with, that was me experimenting with adding generated .d.ts files to johnny-five
Lucas Sanz Gorostiaga
@lucas-sg
@rwaldron sorry to bother you again with this but what does pause() actually do? I'm trying to plot a real-time plot with Plotly and when I use pause() it resets the plot. It does pause the reading but the unwanted effect is that I now need to save the plot data before hitting pause() so that it doesn't disappear, but stays there frozen instead.
Btw thanks @reconbot:matrix.org the as any cast worked fine :)
Rick Waldron
@rwaldron
@/all johnny-five's default branch has been renamed "main".
@lucas-sg I've published v2.1.0 which now has pause() and resume() on everything that's an event emitter!
Rick Waldron
@rwaldron
@lucas-sg All pause() does is copy the contents of the _events property into a weakmap, then set _events = {}
Your handlers won't be called until you resume(), which takes the object out of the weakmap and sets _events = ...
Can you share your code?
EJ2KDJ
@EJ2KDJ
Hi, does anyone know a place to ask a question about CH341? I’ve posted in the Arduino forum, I also went to the Nodebot Community Forum (URL is dead), the npm forum says it’s archived. I’m a beginner and I'm getting an error message that I can’t figure out.
reconbot
@reconbot:matrix.org
[m]
Do you have a link for it?
Donovan Buck
@dtex
@EJ2KDJ It looks like your not using the right command to load and run your js file. I think you're typing node run. You should instead pass the name of your js file like node myfile.js
EJ2KDJ
@EJ2KDJ
@dtex Thx for your reply, I believe I did what you suggested, however now i’m getting a new error and I posted it in the Arduino forum here:
https://forum.arduino.cc/t/ch341ser-error-when-trying-to-run-led/911832