本文整理汇总了Python中utime.ticks_ms方法的典型用法代码示例。如果您正苦于以下问题:Python utime.ticks_ms方法的具体用法?Python utime.ticks_ms怎么用?Python utime.ticks_ms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utime
的用法示例。
在下文中一共展示了utime.ticks_ms方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: multi_fields
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def multi_fields(t):
print('Dynamic labels.')
refresh(ssd, True) # Clear any prior image
nfields = []
dy = wri.height + 6
y = 2
col = 15
width = wri.stringlen('99.99')
for txt in ('X:', 'Y:', 'Z:'):
Label(wri, y, 0, txt) # Use wri default colors
nfields.append(Label(wri, y, col, width, bdcolor=None)) # Specify a border, color TBD
y += dy
end = utime.ticks_add(utime.ticks_ms(), t * 1000)
while utime.ticks_diff(end, utime.ticks_ms()) > 0:
for field in nfields:
value = int.from_bytes(uos.urandom(3),'little')/167772
overrange = None if value < 70 else YELLOW if value < 90 else RED
field.value('{:5.2f}'.format(value), fgcolor = overrange, bdcolor = overrange)
refresh(ssd)
utime.sleep(1)
Label(wri, 0, 64, ' OK ', True, fgcolor = RED)
refresh(ssd)
utime.sleep(1)
示例2: multi_fields
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def multi_fields(t):
print('multi_fields')
refresh(ssd, True) # Clear any prior image
nfields = []
dy = wri.height + 6
y = 2
col = 15
width = wri.stringlen('99.99')
for txt in ('X:', 'Y:', 'Z:'):
Label(wri, y, 0, txt) # Use wri default colors
nfields.append(Label(wri, y, col, width, bdcolor=None)) # Specify a border, color TBD
y += dy
end = utime.ticks_add(utime.ticks_ms(), t * 1000)
while utime.ticks_diff(end, utime.ticks_ms()) > 0:
for field in nfields:
value = int.from_bytes(uos.urandom(3),'little')/167772
overrange = None if value < 70 else YELLOW if value < 90 else RED
field.value('{:5.2f}'.format(value), fgcolor = overrange, bdcolor = overrange)
refresh(ssd)
utime.sleep(1)
Label(wri, 0, 64, ' OK ', True, fgcolor = RED)
refresh(ssd)
utime.sleep(1)
示例3: time_since_fix
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def time_since_fix(self):
"""Returns number of millisecond since the last sentence with a valid fix was parsed. Returns 0 if
no fix has been found"""
# Test if a Fix has been found
if self.fix_time == 0:
return -1
# Try calculating fix time using utime; if not running MicroPython
# time.time() returns a floating point value in secs
try:
current = utime.ticks_diff(utime.ticks_ms(), self.fix_time)
except NameError:
current = (time.time() - self.fix_time) * 1000 # ms
return current
示例4: _as_read
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def _as_read(self, n, sock=None): # OSError caught by superclass
if sock is None:
sock = self._sock
data = b''
t = ticks_ms()
while len(data) < n:
if self._timeout(t) or not self.isconnected():
raise OSError(-1)
try:
msg = sock.read(n - len(data))
except OSError as e: # ESP32 issues weird 119 errors here
msg = None
if e.args[0] not in BUSY_ERRORS:
raise
if msg == b'': # Connection closed by host
raise OSError(-1)
if msg is not None: # data received
data = b''.join((data, msg))
t = ticks_ms()
self.last_rx = ticks_ms()
await asyncio.sleep_ms(_SOCKET_POLL_DELAY)
return data
示例5: _as_write
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def _as_write(self, bytes_wr, length=0, sock=None):
if sock is None:
sock = self._sock
if length:
bytes_wr = bytes_wr[:length]
t = ticks_ms()
while bytes_wr:
if self._timeout(t) or not self.isconnected():
raise OSError(-1)
try:
n = sock.write(bytes_wr)
except OSError as e: # ESP32 issues weird 119 errors here
n = 0
if e.args[0] not in BUSY_ERRORS:
raise
if n:
t = ticks_ms()
bytes_wr = bytes_wr[n:]
await asyncio.sleep_ms(_SOCKET_POLL_DELAY)
示例6: broker_up
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def broker_up(self): # Test broker connectivity
if not self.isconnected():
return False
tlast = self.last_rx
if ticks_diff(ticks_ms(), tlast) < 1000:
return True
try:
await self._ping()
except OSError:
return False
t = ticks_ms()
while not self._timeout(t):
await asyncio.sleep_ms(100)
if ticks_diff(self.last_rx, tlast) > 0: # Response received
return True
return False
示例7: run_forever
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def run_forever(self):
assert len(self.intervals)>0
while True:
output_things = self._get_tasks()
start_ts = utime.ticks_ms()
for pub in output_things:
pub._observe()
if not pub.__connections__:
self._remove_task(pub)
if len(self.intervals)==0:
break
end_ts = utime.ticks_ms()
if end_ts > start_ts:
self._advance_time(int(round((end_ts-start_ts)/10)))
sleep = self._get_next_sleep_interval()
utime.sleep_ms(sleep*10)
now = utime.ticks_ms()
self._advance_time(int(round((now-end_ts)/10)) if now>=end_ts else sleep)
示例8: _send
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def _send(self, d): # Write a line to socket.
async with self._s_lock:
start = utime.ticks_ms()
while d:
try:
ns = self._sock.send(d) # OSError if client closes socket
except OSError as e:
err = e.args[0]
if err == errno.EAGAIN: # Would block: await server read
await asyncio.sleep_ms(100)
else:
self._verbose and print('_send fail. Disconnect')
self._evfail.set()
return False # peer disconnect
else:
d = d[ns:]
if d: # Partial write: pause
await asyncio.sleep_ms(20)
if utime.ticks_diff(utime.ticks_ms(), start) > self._to:
self._verbose and print('_send fail. Timeout.')
self._evfail.set()
return False
self._last_wr = utime.ticks_ms()
return True
示例9: readline
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def readline(s, timeout):
line = b''
start = utime.ticks_ms()
while True:
if line.endswith(b'\n'):
if len(line) > 1:
return line
line = b''
start = utime.ticks_ms() # A blank line is just a keepalive
await asyncio.sleep_ms(100) # See note above
d = s.readline()
if d == b'':
raise OSError
if d is not None:
line = b''.join((line, d))
if utime.ticks_diff(utime.ticks_ms(), start) > timeout:
raise OSError
示例10: readline
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def readline(self):
line = b''
start = utime.ticks_ms()
while True:
if line.endswith(b'\n'):
if len(line) > 1:
return line
line = b''
start = utime.ticks_ms() # Blank line is keepalive
self.led(not self.led())
await asyncio.sleep_ms(100) # nonzero wait seems empirically necessary
d = self.sock.readline()
if d == b'':
raise OSError
if d is not None:
line = b''.join((line, d))
if utime.ticks_diff(utime.ticks_ms(), start) > self.timeout:
raise OSError
示例11: perform
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def perform(self) :
current_tick = utime.ticks_ms()
if not self.initial_tick :
self.initial_tick = current_tick
else :
x = utime.ticks_diff(current_tick, self.initial_tick)
if x == 0 :
pass
elif 0 < x :
rgb = self.get_color(x)
if rgb != self.prev_rgb :
if self.verbose :
logging.info("Lamp: setting color to {} from x={}", rgb, x)
self.fill_pixels(rgb)
self.prev_rgb = rgb
else : # wrap around; start over
logging.info("Lamp: tick wrap")
self.initial_tick = current_tick
return True
示例12: adcread
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def adcread(chan): # 16 temp 17 vbat 18 vref
assert chan >= 16 and chan <= 18, 'Invalid ADC channel'
start = utime.ticks_ms()
timeout = 100
stm.mem32[stm.RCC + stm.RCC_APB2ENR] |= 0x100 # enable ADC1 clock.0x4100
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1 # Turn on ADC
stm.mem32[stm.ADC1 + stm.ADC_CR1] = 0 # 12 bit
if chan == 17:
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x200000 # 15 cycles
stm.mem32[stm.ADC + 4] = 1 << 23
elif chan == 18:
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x1000000
stm.mem32[stm.ADC + 4] = 0xc00000
else:
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x40000
stm.mem32[stm.ADC + 4] = 1 << 23
stm.mem32[stm.ADC1 + stm.ADC_SQR3] = chan
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1 | (1 << 30) | (1 << 10) # start conversion
while not stm.mem32[stm.ADC1 + stm.ADC_SR] & 2: # wait for EOC
if utime.ticks_diff(utime.ticks_ms(), start) > timeout:
raise OSError('ADC timout')
data = stm.mem32[stm.ADC1 + stm.ADC_DR] # clear down EOC
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 0 # Turn off ADC
return data
示例13: _as_read
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def _as_read(self, n):
sock = self._sock
data = b''
t = ticks_ms()
while len(data) < n:
esp32_pause() # Necessary on ESP32 or we can time out.
if self._timeout(t) or not self._sta_if.isconnected():
raise OSError(-1)
try:
msg = sock.read(n - len(data))
except OSError as e: # ESP32 issues weird 119 errors here
msg = None
if e.args[0] not in BUSY_ERRORS:
raise
if msg == b'': # Connection closed by host (?)
raise OSError(-1)
if msg is not None: # data received
data = b''.join((data, msg))
t = ticks_ms() # reset timeout
await asyncio.sleep_ms(_SOCKET_POLL_DELAY)
return data
# Write a buffer
示例14: run_cancel_test6
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def run_cancel_test6(loop):
for name in ('complete', 'cancel me'):
loop.create_task(asyn.NamedTask(name, cant60, name)())
loop.create_task(asyn.Cancellable(cant61)())
await asyncio.sleep(4.5)
print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
await asyn.NamedTask.cancel(name)
await asyncio.sleep(7)
name = 'cancel wait'
loop.create_task(asyn.NamedTask(name, cant60, name)())
await asyncio.sleep(0.5)
print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
t = time.ticks_ms()
await asyn.NamedTask.cancel('cancel wait', nowait=False)
print('Was cancelled in {} ms'.format(time.ticks_diff(time.ticks_ms(), t)))
print('Cancelling cant61')
await asyn.Cancellable.cancel_all()
print('Done')
示例15: _gauge
# 需要导入模块: import utime [as 别名]
# 或者: from utime import ticks_ms [as 别名]
def _gauge(self):
now = utime.ticks_ms()
if utime.ticks_diff(now, self._last_read_ts) > self._new_read_ms:
self._last_read_ts = now
r = self._t_os + (self._p_os << 3) + (1 << 6)
self._write(BMX280_REGISTER_CONTROL, r)
utime.sleep_ms(100) # TODO calc sleep
if self._chip_id == 0x58:
d = self._read(BMX280_REGISTER_DATA, 6) # read all data at once (as by spec)
self._p_raw = (d[0] << 12) + (d[1] << 4) + (d[2] >> 4)
self._t_raw = (d[3] << 12) + (d[4] << 4) + (d[5] >> 4)
else:
d = self._read(BMX280_REGISTER_DATA, 8) # read all data at once (as by spec)
self._p_raw = (d[0] << 12) + (d[1] << 4) + (d[2] >> 4)
self._t_raw = (d[3] << 12) + (d[4] << 4) + (d[5] >> 4)
self._h_raw = (d[6] << 8) + d[7]
self._t_fine = 0
self._t = 0
self._h = 0
self._p = 0