proj test_tc
I took the Jon time_temp.py code and broke out the time tic stuff. The code below runs without errors. I added some comments, hopefully somebody will fill in some of the blank spots.
Trying to get a better handle on how the show_time() function is running at constant one second. I suppose adding some more code this could become a stop watch, but I digress. If this function keeps running at one second , the next step would be to add code the increase the m(minute) value, and start the s(second) back at zero, so on and so on. Not sure if I am going about this in the correct manner.
Ray
# test_tc.py from microbit import * import time EVENT_MS = 1000 # Runs at 1 second # Future reference global h h=0 global m m=0 global s s=0 # starts the tics t_start = time.ticks_ms() runtime = 0 # This the time tics engine def show_time(rt): rt = rt // 1000 # Not sure what this is doing # convert to secs h = rt // 3600 # '' # extract hours rt = rt % 3600 # '' # remove hours m = rt // 60 # '' # extract minutes s = rt % 60 # '' # Show the time elapsing at the command line print("%.2d:%.2d:%.2d " % (h, m, s)) # Starts the show_time engine show_time(0) while True: # loop # Not exactly sure about this t_elapsed = time.ticks_ms() - t_start if t_elapsed >= EVENT_MS: runtime += EVENT_MS show_time(runtime) # This runs show_time @ 0 t_start += EVENT_MS # Time tics
Comments
The run time (rt) is in milliseconds. Dividing by 1000 converts that down to seconds. There 3600 seconds in one hour -- dividing rt (seconds) by 3600 gives hours. The modulus opeator (%s) gives us the remaining seconds. Dividing those by 60 returns minutes; taking the modulus of 60 are the final seconds.
Note: Since h, m, and s are only used in the show_time() function they do not need to be declared as globals -- in fact, what you have now are two sets of variables with the same names. Python is different this way. If you really need a global variable you and need to modify it in a function, you need to specify that you're accessing the global, not a local. This is a function from a cosplay controller I wrote for a friend (@Harukofett on IG). The variable called state_timer is global I need to modify it in some functions.
t_elapsed is the time elapsed since the last marker. The marker is called t_start. Think of time.ticks_ms() as right now.
Think of it this way: You mix up a batch of 5-minute epoxy and look at your watch. The time is 4:55 (we'll call this t_start). A while later you look at your watch again and it's 4:58. 4:58 (now) minus 4:55 (t_start) is three minutes, so you know you have a couple more minutes to work. Once five or more minutes have elapsed you have to mix a new batch and reset the start time.
Since we don't have any delays between events, we can simply add the event duration to the initial t_start to update it.
Once this technique sinks in it will seem very easy and you can create what looks like a multi-tasking system (that said, you have to make sure your tasks don't take a lot of time).
I have no idea why the first line of the quoted text is so large. @Wuerfel_21?
Because you're quoting code outside a codeblock and markdown turns
# Something
into<h1>Something</h1>
Thanks
Do note that control characters can always be bypassed using a backslash
# Something
Thanks Jon. "...and you can create what looks like a multi-tasking system..." I am going to try to separate and run the tics , temperature as separate items. That should give me an indication as to whether this concept is sinking in. Have to get my old brain working a little bit faster and better.
Ray
This my first attempt at running two tasks. It runs without errors, but the temp part values that are shown gets corrupted. Not sure how to slow the showing of the temp values down, it seems to run very quickly. Their must be a better way to do this, I am sure.
Ray
You're trying to handle two different events from the same task timer variable. Even if they run at the same rate, you should have a separate timer variable for each task.
I think this does what you want. Note that even though the display.show() function runs in the background, if you call it before it's finished it will restart.
Update: I made it a little fancier by alternating between F and C.
Thanks Jon. Now, I think I am starting to catch on. I wonder how many tasks can be run this way before the system starts to bog down. Is their a microbit command that shows how much memory is being used on the microbit. Now I will try to add the start/stop of tasks, then add the listdir and system commands. Still not sure how I will handle the delete file command.
Ray
It will depend on how many tasks are running and how long they take. For my friend's cosplay controller I tried to use a 5ms time base but looking at a free output on a 'scope I found there was a lot of wobble (because some tasks states took a little too long). I bumped it up to 10ms (which is easier to deal with mentally, anyway) and all was fine. In her case the project is monitoring, debouncing, and auto-repeating three buttons (I created a simple class for that) and running the current state of the program which can be LED on, LED flashing (two different modes), or fading the LED on and off (which is why my time base needs to be short).
Micropython has a timer class built in, but I'm not sure how it's implemented on the micro:bit. This is why I still favor Spin1 and Spin2 above all other embedded languages: nothing is hidden "under the hood." That said, I can't run Spin on an RP2040 which is what my friend wants to use in her cosplays. Python (which mechanically inspired Spin) is closest.
This is my latest attempt. Almost everything works. I added an ontime, offtime and showtime commands. What I am trying to do is start the time with ontime, in the background, and then use showtime to show the current time. For some reason I cannot get this to work correctly. I got the temp to work fine.
Ray
the os Module contains 3 file functions "listdir()", "remove(filename)" and "size(filename)". If the file does not exist an OSError will occur.
link: https://microbit-micropython.readthedocs.io/en/v1.0.1/os.html
Remove will delete the file "filename"
There is a small app called MicroFS that copies a file from the device or writes a local file to the microbit, I'm not sure if it will be of use to you or just muddy the waters. If you don't think you need it at this time make sure to keep a bookmark for later it's a neat little utility.
link: https://microfs.readthedocs.io/en/latest/_modules/microfs.html
Thanks Unsoundcode. I have the readthedocs pdf, at some point I will print out a paper copy. I am old school, stuff like that I like to read a paper copy.
For the tasking stuff how do I no that the task has actually stopped (quit). I know for the offtemp, the task stops showing, so theoretically the task has quit. Now, when I get the ontime to work, that will be running in the background all the time. When I select offtime, how do I stop(quit) the task.
When I get the bugs worked out for the softRTC, I will start looking into the logging task, that should be interesting.
Ray