How to run NRF.setRSSIHandler function in a infinite loop with a wait time? #5278
Replies: 1 comment
-
Posted at 2022-09-29 by @fanoush what about counting 10 calls inside handler with help of global variable and doing Javascript/espruino is single threaded and event based so until your code ends nothing else runs. BTW what is I'd use Posted at 2022-09-29 by user148837 wait is a function that I added: function wait(ms){ even after giving the loop inside the variable creates issue as I am running an infinite while loop. And for that, JS is first trying to finish the infinite loop ( which is not happening) keeping the calling of the handler in queue. Does Espruino supports async/ await? This would have easily solved the issue. Or is there any way to restart the whole code after every complete run? Posted at 2022-09-30 by @fanoush no async/await (and it would not help you with correctly timing setRSSIHandler anyway) but there is still easy solution, just read again my post. in javascript/Espruino it is bad idea to write long running loops in C/arduino style. In Espruino you have setTimeout,setInterval,setWatch, promises and events. Posted at 2022-09-30 by @fanoush as for the rssi handler to be called 10 times and compute average I'd do something like
to do this every 5 seconds you just put it into setIterval
Posted at 2022-09-30 by @gfwilliams The code @fanoush posted is the best way. Hard-coding a 'wait' in Espruino (and JS in general) really doesn't work very well.
Posted at 2022-10-04 by user148837 Thank you very much. I am new at this platform. And this has teach me not to go with hard code version. Thanks again. Posted at 2022-10-04 by user148837 Yes I understand the issue now. Thank you. Posted at 2022-10-05 by @allObjects Using @fanoush 's code imbedded in your app, the convenience singleton object for handling all RSSI stuff could look like the code below. Working thru the example you will notice that Espruino / JavaScript is completely event driven... and you can take advantage of that even in application components you write. No need to burn cycles like famous (NOP) wait loops do... ;) NB: Things - such as
HTML document running the same Espruino code:
The
...or simply:
The objet even emits
This event can be listen to in the application in a similar way as
With `lon=0' (logging turned off) you get only outputs like this in the console (most recent average rssi value, most recent history event with date and rssi average, most recent raw rssi sample value, and history count):
Setting
Attachments: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2022-09-28 by user148837
I am trying to run NRF.setRSSIHandler 10 times and print the average value. After that wait for 5 second and rerun the loop again for infinite time. But for JavaScript's callback function issue, it is not giving the expected output. I've tried using several closure solutions but none of them work. Is there any way to get the correct output?
Code:
Beta Was this translation helpful? Give feedback.
All reactions