While Loop or setWatch? #7369
Replies: 1 comment
-
Posted at 2020-04-09 by Robin Thr 2020.04.09 Infinite while is problematic. Simple explanation:
Using a watch provides the aditional ability to put the device to sleep and save battery. Posted at 2020-04-09 by Abhigkar RDY Pin which is something set by the sensor and keeps changing by IQS263 sensor. So MCU can request a I2C communication window when this pin goes down. That is the reason why while loop will not be an infinite. What my goal is to perform i2c read/write only when this PIN goes gown, that means I have to put some wait and poll the pin until it goes LOW. I see lots of implementation for this sensor in C/C++ where people set the falling interrupt on RDY pin and perform read/write. Since Espruino does not support interrupt directly [ here ]and setWatch callback only executes in main loop [ here ], I only see only option to setup the IRQ optionin setWatch and provide native calls to perform communication to sensor. Am I making things complicated here? Is there a simple approach available? Posted at 2020-04-09 by Robin
Not at all. Here is what you are after:
Posted at 2020-04-09 by Abhigkar good to know!! @robin Thanks Do you have any idea how to do read or write on I2C bus with this approach, i mean Inline C code makes a read/write calls to I2C ? Posted at 2020-04-09 by @allObjects
'Infinite' at your terms: no - but in Espruino's architecture: infinite enough to qualify... and even more so if you even think remotely to put something like this onto an ESP8266EX chip. For sure Espruino supports interrupts... directly in it's firmware, and puts a marker into the event queue as you define in the In Espruino, everything is event driven... either by a pin signal or a time (which is a timer signal). I'm not sure you really need inline C. But feel free to be its guest and share your experience. As stated - everything in Espruino is event driven / has to be event driven - even a polling is event driven: a time - This - TSO - time sharing option - is one of the oldest architectures... around with the first main frames that gave multiple users so called 'simultaneous terminal access' (vs. console access, which in the early days was the only terminal): a 'pseudo multi-/parallel- tasking' that is done in JS in Espruino as well as in the Browser (in the Browser only until recently...): an application process or flow is divided up into many short code pieces (spaghetti) where one plants the seed of the next(s) one(s) by setting up an timer or pin interrupt (javascript) handler. This way it seems to us 'slow' humans as if things are handled in parallel / multi-tasking, where when closely looked at, each piece is happening so fast that we feel a particular application has the processor's attention at all times. Benefit of this setup is that there is no complicated locking/synchronization required between the apps... it's almost like transactional: a once started piece of code always finishes and what is. done is done without any other application interference... The price pay: you pay upfront by thinking thru the process and identify the pieces and their triggers (interrupts they setup for the next one). Speaking of interference: if you go into async yourself, you are on your own - or to bare metal... and in the end: you have changed nothing: it is still single threaded... and if your thread takes too long to relent, some one else - other application thread - will pay for it. Posted at 2020-04-09 by Robin Thr 2020.04.09 While I'm still trying to track down a forum thread, I believe one uses peek and poke registers.
Not sure what max time duration during CS will provide, but another idea would be to poll using setInterval. See the second to last line, link in post #4 beneath heading 'Interrupts' print(c.get()); but in your case, have the get() function return some flag or data at that point, to activate the read/parse part in a function. I thought I had a forum thread that did what you are after, but it was with a manual clocking of the CS pin with SPI, not I2C. Although this isn't your module, there are some sample snippets to follow. One idea would be to look at the underlying .js code as a template and write your own. Posted at 2020-04-09 by Abhigkar Thanks @allObjects for making me correct on my understanding. I am pretty much new to this type of architecture, and mostly worked on c#/ JavaScript web based application system. Electronics is my hobby and I like Esprunio because of its power and portability on embedded devices. My requirement is very very close to this there where Arduino code needs to be translated in JavaScript.
The only change is the pin, which in my case is not CS but a normal Pin which is controlled by sensor, but still very close to my issue.
I will follow the thread and see if this help me. Posted at 2020-04-10 by Abhigkar this is the Arduino code that I want to convert Posted at 2020-04-10 by @allObjects this is about the mainline I would go: If there is no (Espruino) JS module yet for connecting / talking to Azoteq IQS263 Capacitive Touch sensor, take the Arduino code and put it into a module. How would that go? ...take a peek at other modules talking I2C to sensors. In addition to the Their are variations of this: you may crate a start and stop listening - like You can always let the application do all things. On the other hand, you would like to put as much 'low level' handling into the module, for example: detection of short touch, long touch, etc., and pass that in the callback or make even different callback with this information, including time, touch time, last time, etc. To get some ideas, look at the Espruino event object. For the programming paradigm, do not think server-side JS, like nodeJS, but more browser JS as you are very familiar with and threat the sensor like a html ui action element, for example, as a button, or a canvas where you clicked on and you get tons of info with the event that is delivered to the attached event handler(s). Enjoy the ride! Posted at 2020-04-14 by @gfwilliams
Realistically the vast majority of hardware is absolutely fine waiting a little while after asserting the IRQ line - you don't generally need to jump in and do stuff immediately, and executing in the main loop is fine. Having said that, generally when I'm writing modules, if I'm only expecting to wait for a few milliseconds (eg. ~10ms) then I'll just add a If it's more than 10ms I'd use Posted at 2020-04-14 by Abhigkar Thanks @gfwilliams for your suggestions. I will try it for sure. Since there is already Arduino library available, will it be worth to try to add the C code in Espruino src as a lib and compile it locally? As you are doing with microbit, hexbadge and BangleJS as a same way add my C code in lib folder and compile? Another question, what will happen if there is an un-handled exception in Espruino? A restart/reset?
@allObjects, As I am exploring all available option, I would like to know where I can fine re information about is Espruino event object? Posted at 2020-04-14 by @fanoush
If it is worth to you then go on. However arduino (C++) code and Espruino C code can be quite different so it may not be trivial to port arduino library to espruino. C module is necessity if you cannot do it in JS (no JS api exposed) or for performance reasons. However you lose the modularity. Anyone using same sensor would need to build own espruino version. If you do it as JS module the code can be put here https://www.espruino.com/modules/ and people can use it without custom Espruino build. Posted at 2020-04-14 by Robin Tue 2020.04.14
The reference made in post #10 is to the detail provided once the watch fires. The easiest way to see what is available is to create a new var inside the watch and assign 'e' to that var, then follow that with the 'debugger' command. Fire the watch. Attempting to just write to the console will result in The return property set is described in the second to last full pp beneath the description heading. In the off chance that comment was a reference to Espruinos 'E' class as there are a few events within it: Search 'E Class' or nav on left-hand side 'E'
> 'what will happen if there is an un-handled exception in Espruino' when within the user JS code and not from within the underlying C source
Posted at 2020-04-21 by Abhigkar I am facing a strange behavior that I am not sure if it is expected or my code is wrong. I am doing a simple setWatch on a Pin which is toggling very fast by the IQS263.
I am getting an error in between Prompt not detected - upload failed. Trying to recover..
Am I doing something wrong here? Sometimes I also got the error like:
And
Often device is also gets reset loosing all RAM content Posted at 2020-04-21 by Robin Tue 2020.04.21 Hi @abhigkar I feel somewhat vindicated as I have been experiencing the exact issues, doing nearly the exact task as described. If you check out the last five threads (click on @robin profile and search conversations) I started over six weeks ago, you'll see basically the same. Here is one that will provide some insight. While still trying to track down, just to understand what is going on, I am able to answer some observations I've seen. > 'setWatch on a Pin which is toggling very fast' Within your project, what is: It is likely a buffer is overflowing as the amount of data a setWatch can capture is around 100 bytes. (but I see you are not using the 'data' property) See one of my threads from over six months ago. Memory fills up, then the device locks up with a myriad ~(five or so) of possible errors, the Ctrl+C being one of them. Have been working on an attempt to gate the inbound pulses, but keep getting distracted trying to understand why memory ~400JsVars just vanishes. Also very likely a similar situation to what you are experiencing. That has been quite an undertaking also. My last thread regarding trace() might provide some insight also, and will be updating that later today with another round of discoveries on what might be going on. May I ask where are you located, Time Zone/Country so that I may stay glued to the forum during the same time to exchange ideas quicker? I'm in the midwest USA, CST. (-5 GMT) + DST Posted at 2020-04-21 by Abhigkar Thanks @robin for the response. My Tz is UK BST (GMT+1). I am also available on gitter, if you feel comfortable.. Posted at 2020-04-21 by Robin Okay, six hours apart including DST, just ate lunch, your day is about over. I'll plan on an earlier morning start, should we need to comm back-n-forth quickly. Are weekends better, or are you stuck quarantined as we are for the next few weeks? As this is posted beneath other boards, may I inquire which one? I had basically the same issue(s) using the STM32 and what seems worse with the nRF52. My need is to resolve with the nRF52 for a project requiring BLE, but will work on a tutorial over the next many months for both devices.
Thanks for that, but I'd like to keep things simpler right now, and any detail either of us uncover will be extremely helpful in correcting what is going on, or finding a suitable solution or work around. Posting here will leave a record that is easily accessed. It might make sense to create a new thread for each new issue, although I feel you'll probably encounter every stumbling block ;-) I did over the last few months, and likely in the same order, so continuing here is fine by me. Posted at 2020-04-21 by Abhigkar
Yes, I am, as in lockdown situation, I am sitting home all the time.
Sounds like a plan with only catch that I don't check forum posts regularly. Would miss the notification. Gitter mobile app push a message notification as someone will messaged me. But I am good both week and weekend options.
I am able to flash espruino latest (build on my own, for new board file, and nordic SDK11 libs), a github repository is setup (which is not in a good shape yet) to protect ongoing work. I also have BangleJS to compare the compiler result on need basis. Posted at 2020-04-22 by Robin Tue 2020.04.21 Good Morning @abhigkar, me off to the rack. Wondering, do you have access to a Logic Analyzer? Quite frankly I wasted a year until I had to have one to solve an issue. See images at this post I made a year ago.
They are aournd $20USD on BangGood and well worth the time savings, for stuff like this. While it's unlikely there is a massive amount of data being sent, 'seeing' the data sent makes writing code to detect bits is a lot easier! Posted at 2020-04-22 by @fanoush
if buying from china these are more like $5, e.g. here https://www.aliexpress.com/item/32847322743.html Definitely good thing to have. Oh and it is documented here https://sigrok.org/wiki/Lcsoft_Mini_Board and the 24MHz 8Ch analyzer is e.g. here https://sigrok.org/wiki/MCU123_USBee_AX_Pro_clone |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2020-04-09 by Abhigkar
I am interfacing Azoteq IQS263 Capacitive Touch sensor (Datasheet) with Espruino. The sensor is I2C interface with RDY line.
As per the datasheet the device will response when RDY pin goes low. So I will have to wait and monitor when the RDY pin goes low to start and communication. Well in active this pin is bi-directional and communication windows opens when it is LOW and resumes sensing/calculating when it is HIGH.
What should be the approach for this?
1- While loop??
OR
Beta Was this translation helpful? Give feedback.
All reactions