utime.ticks_diff() between pycom and upython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Frida
Posts: 38
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

utime.ticks_diff() between pycom and upython

Post by Frida » Thu May 23, 2019 6:13 pm

In upython, utime.ticks_diff (new, old) gives positive results.
In pycom, utime.ticks_diff (old, new) gives positive results.
Old and new have been switched, I wonder why, there is no
change in the underlying code. It must be somewhere,
between the call and the underlying code.

ps. I prefer the pycom way.

Code: Select all

import time
time.time()
start = time.ticks_ms()

while 1:
	#if time.ticks_diff(time.ticks_ms(), start) > 10000: # esp32 pico upython
	if time.ticks_diff(start, time.ticks_ms()) > 10000: # pycom wipy
		break        
time.time()

deadline = time.ticks_add(time.ticks_ms(), 100)

#while time.ticks_diff(deadline, time.ticks_ms()) > 0:	# esp32 pico upython
#	print(time.ticks_diff(deadline, time.ticks_ms()))	# esp32 pico upython
while time.ticks_diff(time.ticks_ms(), deadline) > 0:	# pycom wipy
	print(time.ticks_diff(time.ticks_ms(), deadline))	# pycom wipy

Yes Frida is my watchdog!

Christian Walther
Posts: 47
Joined: Fri Aug 19, 2016 11:55 am

Re: utime.ticks_diff() between pycom and upython

Post by Christian Walther » Thu May 23, 2019 7:08 pm

It was changed at some point after a lengthy discussion: https://github.com/micropython/micropython/issues/2217

User avatar
Roberthh
Posts: 1455
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: utime.ticks_diff() between pycom and upython

Post by Roberthh » Fri May 24, 2019 5:44 am

Obviously there is a difference in the underlying code, and yes, this change by Pycom was definitely not necessary, and they may switch back. Besides that, you can make you code for that, by introducing a factor:

diff = ticks_diff(old, new) * ticks_diff(1, 2)

That will give you the same result on both systems. If you pre-calculate the factor, you'll save the second call.

User avatar
dhylands
Posts: 3195
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: utime.ticks_diff() between pycom and upython

Post by dhylands » Fri May 24, 2019 1:41 pm

Presumably, the second call should be ticks_diff(1, 2)

User avatar
Roberthh
Posts: 1455
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: utime.ticks_diff() between pycom and upython

Post by Roberthh » Fri May 24, 2019 1:50 pm

Sure

Post Reply