You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-create some low-level Arduino-like millis() (milliseconds) and micros()
4
+
(microseconds) timing functions for Python
5
+
By Gabriel Staples
6
+
http://www.ElectricRCAircraftGuy.com
7
+
-click "Contact me" at the top of my website to find my email address
8
+
Started: 11 July 2016
9
+
Updated: 7 Sept 2016
10
+
11
+
History (newest on top):
12
+
* 20160907 - v0.2.1 created - updated delay functions to use modulus operator to guarantee proper C uint32_t-like underflow subtraction behavior when the timer rolls over
13
+
* 20160813 - v0.2.0 created - added Linux capability
14
+
* 20160711 - v0.1.0 created - functions for Windows *only* (via the QPC timer)
15
+
16
+
References:
17
+
WINDOWS:
18
+
-personal (C++ code): GS_PCArduino.h
19
+
1) Acquiring high-resolution time stamps (Windows)
CLOCK_MONOTONIC_RAW=4# see <linux/time.h> here: https://github.com/torvalds/linux/blob/master/include/uapi/linux/time.h
79
+
80
+
#prepare ctype timespec structure of {long, long}
81
+
#-NB: use c_long (generally signed 32-bit) variables within the timespec C struct, per the definition here: https://github.com/torvalds/linux/blob/master/include/uapi/linux/time.h
82
+
classtimespec(ctypes.Structure):
83
+
_fields_=\
84
+
[
85
+
('tv_sec', ctypes.c_long),
86
+
('tv_nsec', ctypes.c_long)
87
+
]
88
+
89
+
#Configure Python access to the clock_gettime C library, via ctypes:
#-see here for use of underscore to make "module private": http://stackoverflow.com/questions/1547145/defining-private-module-functions-in-python/1547160#1547160
122
+
#-see here for example of constrain function: http://stackoverflow.com/questions/34837677/a-pythonic-way-to-write-a-constrain-function/34837691
123
+
def_constrain(val, min_val, max_val):
124
+
"constrain a number to be >= min_val and <= max_val"
125
+
if (val<min_val):
126
+
val=min_val
127
+
elif (val>max_val):
128
+
val=max_val
129
+
returnval
130
+
131
+
#Other timing functions:
132
+
defdelay(delay_ms):
133
+
"delay for delay_ms milliseconds (ms)"
134
+
#constrain the commanded delay time to be within valid C type uint32_t limits
135
+
delay_ms=_constrain(delay_ms, 0, (1<<32)-1)
136
+
t_start=millis()
137
+
while ((millis() -t_start)%(1<<32) <delay_ms): #use modulus to force C uint32_t-like underflow behavior
138
+
pass#do nothing
139
+
return
140
+
141
+
defdelayMicroseconds(delay_us):
142
+
"delay for delay_us microseconds (us)"
143
+
#constrain the commanded delay time to be within valid C type uint32_t limits
144
+
delay_us=_constrain(delay_us, 0, (1<<32)-1)
145
+
t_start=micros()
146
+
while ((micros() -t_start)%(1<<32) <delay_us): #use modulus to force C uint32_t-like underflow behavior
Create some low-level Arduino-like millis() & delay() (for milliseconds) and micros() and delayMicroseconds() (for microseconds) timing functions for Python
3
+
Create some low-level Arduino-like millis() & delay() (for milliseconds) and micros() and delayMicroseconds() (for microseconds) timing functions for Python.
4
+
5
+
Compatible with Python in both Windows *and* Linux. Has ultra-great resolution timestamps (sub-microsecond), even in older versions of Python 3 which don't natively support (in the [time](https://docs.python.org/2.7/library/time.html) module, for instance) high resolutions like this.
* 22 July 2016: This module works for Python in Windows only so far. If anyone would like to expand it to support Linux too, please do.
10
-
11
-
Thanks,
10
+
## History:
11
+
(newest on top)
12
+
* 20160907 - v0.2.1 created - updated delay functions to use modulus operator to guarantee proper C uint32_t-like underflow subtraction behavior when the timer rolls over
13
+
* 20160813 - v0.2.0 created - added Linux capability
14
+
* 20160711 - v0.1.0 created - functions for Windows *only* (via the QPC timer)
15
+
16
+
## Author:
12
17
Gabriel Staples
13
-
www.ElectricRCAircraftGuy.com
18
+
www.ElectricRCAircraftGuy.com
19
+
-click "Contact me" at the top of my website to find my email address
0 commit comments