Dec-06-2020, 09:43 PM
Hi everyone,
I have quite some experience with Delphi, Parallax SPIN and Arduino C++
NOw I started to use CircuitPython with a Teensy 4.0-microcontrollerBoard.
The following question is about programming in python in general
On micorcontrollers non-blocking timing is essential. Do multiple things
at the (almost) same time.
From C++ you can pass variables by pointer to be able to modify the variables value
that is used to pass the parameter
So in c++ it would look like this
inside of the variable that is used by the functioncall
example
what makes the call very compact
I tried to realise this in python I have this working solution
So can it be done more compact without the need of another variable like TPOver?
best regards Stefan
I have quite some experience with Delphi, Parallax SPIN and Arduino C++
NOw I started to use CircuitPython with a Teensy 4.0-microcontrollerBoard.
The following question is about programming in python in general
On micorcontrollers non-blocking timing is essential. Do multiple things
at the (almost) same time.
From C++ you can pass variables by pointer to be able to modify the variables value
that is used to pass the parameter
So in c++ it would look like this
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) { unsigned long currentMillis = millis(); if ( currentMillis - expireTime >= TimePeriod ) { expireTime = currentMillis; // set new expireTime return true; // more time than TimePeriod) has elapsed since last time if-condition was true } else return false; // not expired }the parameter expiretime is passed by pointer so the modifying of the value is stored
inside of the variable that is used by the functioncall
example
unsigned long MyTimer if ( TimePeriodIsOver (MyTimer, 500) )With this call the variable MyTimer gets updated automatically
what makes the call very compact
I tried to realise this in python I have this working solution
import board import digitalio import time def TimePeriodIsOver(p_TimerVar, Period): now = time.monotonic() Diff = now - p_TimerVar if Diff >= (Period / 1000): p_TimerVar = now + Period / 1000 return True , p_TimerVar else: return False, p_TimerVar led = digitalio.DigitalInOut(board.D13) led.direction = digitalio.Direction.OUTPUT duration = 0.1 print('Hello 4 World!') now = time.monotonic() now_ns = time.monotonic_ns() print ('Now:',now, ' nanosec:', now_ns) TimerVar = time.monotonic() print('before while true TimerVar',TimerVar) while True: #print('TimerVar',TimerVar) #time.sleep(0.25) TPOver, TimerVar, = TimePeriodIsOver(TimerVar, 500) if TPOver == True: if led.value == True: led.value = False else: led.value = TrueBut this is not as compact as the c++-Version
So can it be done more compact without the need of another variable like TPOver?
best regards Stefan