In this election, we'll show you how to split the original single final version of this many projects
script into two separate files and that we will want to revise the script to make it easier to write
and to maintain.
Let's have a look at the main script first to call it Modula here, and then the one that holds the
individual functions in some of the variables will call it modules.
So the main part of the script looks like this.
I've got only the necessary modules, external modules included, and of course, necessary variables
here, including the got I could see object to the display object and the being the object and they'll
be parsing whenever necessary.
I'll be passing those objects across to the functions in the other file.
I also have the settings file here and I create the wireless object.
Now, just before I continue, just to know that there are many different ways by which you can do that.
For example, some of those variables, you could choose to put them into a second supporting file or
even a third file and distribute them in different ways.
But this was just the first iteration, the first version of the modular version of the script.
And it's a possible approach, not the only approach.
The only function in the main script is this one here at time.
So I could have moved that to the other side as well.
But it is just simpler to keep the time ISO function in the main part of my script in the main file
just makes it easier to use and everything is identical here, as you've seen in the previous version
of the program.
So here's a program as you saw it in the previous lecture.
Here's a time I said function.
And here in this version of that, it is this addition into the if statement that decides whether to
call the post to efficient that function and just edit the event equals one option here, because what
I want to do is to make sure that the post to efficient that is called the very first time that I'm
explicitly calling time ISO when I'm doing the test.
So if this line of code down here, one of four is uncommented, then I'm passing number one to time
ISO and that will be picked up right here.
And that will allow me to definitely test posta if this isn't that what I'm doing, debugging, regardless
of what the temperature is, there's just a debugging improvement here that I've made compared to the
previous version.
Nothing fancy, really.
The rest is all the same.
Now, what's more interesting to talk about here is what changes in the code, because obviously when
I split a single file into two files is not just moving things around to the way that I'm calling those
things also needs to be adjusted.
So let's cut to the top again and have a look at the inputs.
Now, all of the inputs here you recognize from the original version, version of the one, but a new
one is this.
So now I'm importing this supporting secondary file code or it'd be a meta edit if this is that and
this module P y and I'm taking the name of this file without extension and importing it as a module,
you can see that it's stored right here in the file system.
It's called the modules, part of my two scripts software side of the project.
And because this is a very long name to be using in my script, I'm just renaming it into modules.
So now you can see that, for example, down here, if I want to call the do connect function that is
inside this file, I need to prepare it with more same modules.
DOT do connect and then I have to pass the necessary parameters in this case.
Do connect and she can see requires access to the wireless network object and the settings object because
it's referenced here to grab the Wi-Fi network name and password.
So I'm passing those as parameters now where originally I didn't have to because everything was part
of the same global set of parameters.
So here it's got to connect parentheses, first parameter wassmann, landslided parameter settings,
and then this function is called and it's executed.
You can see that this test a little earlier for the recording and you can see that the output of this
function appears in the shell and it's the connecting network right here.
Another thing that I've done, obviously, in both versions of the program is to have lots and lots
of shell printouts to help me with the so you can see it every opportunity, of course, to print out
telling me what is going on so I can then have a look at the output and figure out whether things are
working or if they're not working.
Gives me clues about what it is that I need to fix.
So that's what's happening here.
With Do Connect are the functions that need to pass parameters to things such as, for example, the
function that draws the display.
So this one is electrostatic screen and I'm passing the display object because this function creates
or printed out the left part of the display of the image and the display.
That doesn't change.
So I needed to pass the display object so that then I can write a text to it.
OK, so that's about it.
There's nothing else really here worth talking about.
I do invite you to carefully study these two files and see how they interact and how they work.
But I'm just going to go ahead and run it now.
I mean, debugging mode, so I'm only going to run the timer.
So once instead of going through the actual timer, interrupt, set up and running it and calling it
every one second to make sure that things are working first.
No, I had an issue here and it didn't.
So you can see here that he was unable to connect to Wi-Fi.
For some reason it's given me and already surprising error to zero, to which I don't really know what
it means.
I did some search, but this information is not very well, not very helpful, to say the least.
So.
At this point, I know that my script of the Wi-Fi connection does work, but mainly to try out to to
run the script repeatedly until it gets it into the connection happens.
So going to clear and run the script again.
OK, and now second time.
We did get medical configuration, so we did have a connection and you can see we've got a time, which
is a good thing, and we got a if they sent that notification that appears right here, just arrived
in Russia, it seems like everything is working except for that intermediate refusal to connect to my
Wi-Fi network originally.
So let's try now to run Descript.
Properly, which means setting up the timeline as well.
OK.
All right, so you may notice that in the beginning you stop this, actually see if I can go back.
Right here.
So notice that in the beginning, the DP 32 was not able, for some reason to connect to Wi-Fi, so
it failed at this point.
But next time that the connection function was called, he was able to to do connect and then he was
able to acquire the Internet.
Time to get home connected.
Yes.
That means that it went to the Internet time server who was able to get the time.
And after that, we were able to synchronize and sync the ATC with the Internet time.
And from that point on which the script will not need to rethink ATC with at Internet time, I'll just
get that time from the real time clock and speed on the screen every one second.
All right, so it seems like it's working properly, I'm going to run this one more time.
And there you go, the script works.
Updates every one second.
According to the.
Period of the timeline of the hardware timer and the interrupt service routine.
And those are generated.
So that's an example of how you can split a large micro Python script into.
And one fires.