Module development - using example of Temperature Nodes with 1-Wire PT100 / DS18B20 Sensor at multiple Locations #2624
Replies: 8 comments
-
Posted at 2017-09-04 by @allObjects 1. For a first step, we develop the module code and usage in single file TemperatureDevTest.js: The code below is working and covers basically the above requirements for the module:
The output - in the console - is as expected (code unrelated: unusually hot... a heat wave... usually it is around 50 F or even less this early in the morning...)
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-05 by @allObjects 2. For the second step, we want to use the code like a module. *** EDIT No 'hands-on' for this 2nd step as outlined in posts #3 thru #7. Following it in thoughts and understanding the concepts is sufficient. Hands-on is again asked for post #8 and beyond. *** To use the code like a module, in other words, use the Usually, the upload retrieves the module from either the local sandbox, the official espruino.com/modules library of the Web, or any other place of the Web by supplying the full url as 'moduleName'. How code and modules are loaded is touched on in the conversation about simple explanation how to save code that espruino run on start?. The same means that upload uses to upload modules to the board are available to us in either the console or the already uploaded to the board. The To make the code work like a module (on retrieval or add and retrieval), we must add Looking at some existing modules, you find that export does this - for example, for the DS18B20 Temperature Sensor:
The
With the module code now complete, we have to feed it into the
Making the source code lines all string and concatenate them is though quite some work and will need more work when further down the development process the will be stored as separate module file. Therefore we will use the ES 6 multi-line string to achieve the change very quickly:
...where 'backTick' is the backwards-looking single quote (`), also called (in French) 'accent-grave'. Putting backTicks around multiple lines makes JavaScript to look at it these a single string ignoring the line ends (CR / CRLF / LF). Using this string in the Note: When the module code is very large and so the source as string will be too, you may ran out of memory... But to that, there is a solution as shown in the next post. Last change is in the application part where we now have to retrieve the the Since Even though we are done with 'modularizing', it is not good enough: Since we do not go through the usual upload process that parses any required module for nested 'requireds', our module will fail on usage. Why? Answer: From where should Espruino know that it should have retrieved the module(s) referenced in the module as well? Solution to the missing nested module 'DS18B20' is to upload the referenced module(s) 'upfront' - if known at that time - or retrieved and added to the cache 'now' dynamically as well, either from included code or code read from 'somewhere', such as SD Card... or the Web. For our simple example - and because we know what will be used, we add this The final code looks like below... where the
It works as expected and shows in the console:
Pretty hot...is it! - I mean also literally... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-05 by @allObjects As mentioned in the previous post, the string containing a complete module can be pretty big and could lead to run out of memory. Therefore the question: How can a module be broken up into small(er) pieces and composed at runtime from such pieces. In order to have the a piece of code working as a module, it is good enough to have a 'hock object' as the first or core component in the Since the constructor function is such a first and core component, let's try to attache a the Intentionally, the module was retrieved into a temporary variable The split up module code into core component - lines 4..34 - and other component(s) - lines 49/41..48 - and attaching the other component(s), such as a method method - line 40 - represents composing a complete module from components component by component. The code for it looks like this:
There is one comment to make: if the to-attach module component is retrieved over the network or read from a storage it is retrieved as a string and not as an object... but that is no issue what so ever... we just 'ab-use' the
The code with all module components as strings looks then like:
...and works - and we get two (2) warnings about module not found on upload as expected... of course it runs... no magic... just JavaScript brilliantly and to it's true nature implemented by @gfwilliams. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-05 by @allObjects Two final challenges remain to resolve - even though they are not part of the overall topic of this conversation:
But what we have shown so far is how we can develop even large module(s) within the same source file as the application through the module(s)'s early stages. Next post returns back to the topic and focus of this conversion: Module development(process). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-05 by @gfwilliams Looks good - I'd say if you're running into out of memory errors, you should probably just do what is described in the first step - without using Pulling modules over the internet is very possible - it's actually quite easy (download as a string, and use For replacing a module, you'd have to have a carefully designed module that could fully unload itself - and that you kept no references to. However if that is done then yes, replacing it on the fly could definitely be done. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-05 by @allObjects
...with that comment we come full circle with #4 and #5 of the conversation about How to create a module that is a 'Class' - aka a function usable with new - or: How to make require() return a function and not an object. A nice historical event... At that time you posted '...That one's definitely not in my plans at all I'm afraid!'. With promises now implemented, I'm not so sure this statement of your's still stands firm... ;-) --- *** Espruino is now closer to the require.js / AMD as it ever was... and kind of an M2M Browser Eco System vs. a Web Browser... *** (Just a little bit food for thought: enabling require to detect the invocation pattern/method signature - single string vs. array - would allow the async approach, of course it also requires some flanking support actions, like a way to define how and where to pull the modules asynchronously. This food for thought is though a rabbit trail of this conversation and will have to be in its own conversation). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-06 by @allObjects In the related conversation about Module creation at runtime I learned about a superior solution for step 2:
Lines 2..11 represent the exact code that will become the 'outfactored' module. Keeping the module code as 'active' code in the wrapping, anonymous function passed into The final code using this more useful approach looks then like this:
Lines 3..42 will become the - un-minified - source of the module file placed into the modules folder of the sandbox (or any other module repository). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-06 by @allObjects 3. For the third step, we tidy up the module code with some (extra) documentation. It is very useful to include in the module code - as comment - some terse documentation about the usage of the module, see example in DS18B20 temperature sensor module. The embedded module documentation/information is at the beginning in block comment. The usage example is taken from current code. The code to be and to be used as a module -
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-09-04 by @allObjects
While developing a module, it is convenient in early stages to have all code in one and the same file, that is:
For a module example, I want to have a module that
This is good enough for now, because:
The focus of this conversation is less on the module and its functions but more so on the development process within the Espruino Eco System - IDE, Project, Sandbox, Module Compiler (js minifier), etc.
Subsequent post will walk you through the steps to build a module for yourself... and may be for others too.
Beta Was this translation helpful? Give feedback.
All reactions