From f92c10d511bdaabba73887fce7106a08acc414c8 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Wed, 1 Aug 2018 09:29:31 +0100 Subject: [PATCH] ssd1306_test I2C uses OD pins. Old code in timed_function replaced. --- SSD1306/ssd1306_test.py | 4 ++-- timed_function/timed_func.py | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/SSD1306/ssd1306_test.py b/SSD1306/ssd1306_test.py index 7b8bf22..5c42fe3 100644 --- a/SSD1306/ssd1306_test.py +++ b/SSD1306/ssd1306_test.py @@ -64,8 +64,8 @@ def test(use_spi=False): # Gnd Gnd # Y9 CLK # Y10 DATA - pscl = machine.Pin('Y9', machine.Pin.OUT_PP) - psda = machine.Pin('Y10', machine.Pin.OUT_PP) + pscl = machine.Pin('Y9', machine.Pin.OPEN_DRAIN) + psda = machine.Pin('Y10', machine.Pin.OPEN_DRAIN) i2c = machine.I2C(scl=pscl, sda=psda) # i2c = machine.I2C(2) ssd = SSD1306_I2C(WIDTH, HEIGHT, i2c) diff --git a/timed_function/timed_func.py b/timed_function/timed_func.py index 1328440..1d9902a 100644 --- a/timed_function/timed_func.py +++ b/timed_function/timed_func.py @@ -1,16 +1,16 @@ # Time a function call by means of a decorator -# On or shortly beore 1st November 2016 the semantics of utime.ticks_diff changed. If using -# older firmware please use the second example below - import utime + +# @timed_function +# Print time taken by a function call + def timed_function(f, *args, **kwargs): - myname = str(f).split(' ')[1] def new_func(*args, **kwargs): t = utime.ticks_us() result = f(*args, **kwargs) - delta = utime.ticks_diff(utime.ticks_us(), t) # Argument order new, old - print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000)) + delta = utime.ticks_diff(utime.ticks_us(), t) + print('Function {} Time = {:6.3f}ms'.format(f.__name__, delta/1000)) return result return new_func @@ -18,15 +18,21 @@ def timed_function(f, *args, **kwargs): def test(): utime.sleep_us(10000) -# Version for use with older firmware -def timed_function(f, *args, **kwargs): - myname = str(f).split(' ')[1] +# @time_acc_function +# applied to a function causes it to print the number of times it was called +# with the accumulated time used. + +def time_acc_function(f, *args, **kwargs): + ncalls = 0 + ttime = 0.0 def new_func(*args, **kwargs): + nonlocal ncalls, ttime t = utime.ticks_us() result = f(*args, **kwargs) - delta = utime.ticks_diff(t, utime.ticks_us()) # Argument order old, new - print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000)) + delta = utime.ticks_diff(utime.ticks_us(), t) + ncalls += 1 + ttime += delta + print('Function: {} Call count = {} Total time = {:6.3f}ms'.format(f.__name__, ncalls, ttime/1000)) return result return new_func -