Import an other JS file in modules directory. #347
Replies: 7 comments
-
Posted at 2015-02-11 by @gfwilliams Hi. You're doing this with the 'projects' option in the Web IDE? The first thing I think might be an issue is that modules aren't just simple files that are 'included'. You've got to define 'exports'. For instance: mymodule.js:
main.js:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-11 by fobus Yes, I'm doing it with projects options in Web IDE. How to import a class that gets parameters on construction like this :
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-11 by @gfwilliams The way we usually do it in modules is:
then:
There's a template on the website here that you might find handy. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-11 by @allObjects never mind ;-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-11 by @allObjects Modules and OOP are not necessarily tied together... if you want to have just one stepper in your system, you do not need to go for a class, module works just fine. If you though want to have multiples, the code has to undergo some changes in order to take advantage of the OOP ideas... AND the abilities of ESPRUINO Javascript implementation. You can start exactly the way @gordon suggests - and you can even nicely test it in the IDE. Put your code plus the 3-liner from @gordon into the file stepper.js and save it in the modules folder of your project sandbox folder. In your runtime code you can then get a stepper the way @gordon shoes in the one-liner.
Do you intend to have more than one stepper in your setup? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-12 by fobus Yes, I'm going to use 7 steppers in my project. It is not only about steppers, OOP is easiest way to build application for me. By taking advantage @gordon's code I have build my OOP like this; The way below is easy to read and understand the code for me.
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-02-12 by @allObjects ;-) somehow, I expected your response... but did not want to make further comment before this had been cleared. I make you now some suggestions that take more advantage of oo and Espruino. Furthermore, you have to know that the odd 'steps' are not stable steps: when hit stop - apply 0 to all pins - at any of these, the motor will either move forward to next even 'step' or ' back to previous even 'step'. If you want to use 'stop' state (all pins 0 / off), you best compensate that internally by making for one external step two internal steps. Since you (may) expose (and use) .stop() you have to make sure that you always make an extra internal step (in the current direction) when stop hit's at an odd internal step. First, use Javascript's 'idea' of oo by using prototype and -second, use Espruino's support for setInterval (or setTimeout). Using prototype saves you memory. Using intervals (or timeouts) makes it possible that more than one stepper can move at one time... (your current piece of code hogs the process until all steps are done). - Times are in milliseconds [ms].
To use this Stepper class you write:
Note that the code executed at every interval - this is the method ._step() - has to be as short as possible. Regarding naming, this may be a helpful rule-of-thumb:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-02-11 by fobus
Hello,
I'm trying to build my application as OOP, that's why I have parted all classes in different files. I have placed those files in modules directory.
Example ;
But at the line var connection=... I'm getting this error
What I am doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions