Python utime.sleep_ms() Examples

The following are 30 code examples of utime.sleep_ms(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module utime , or try the search function .
Example #1
Source File: alevel.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def main():
    print('alevel test is running.')
    CWriter.set_textpos(ssd, 0, 0)  # In case previous tests have altered it
    wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False)
    wri.set_clip(True, True, False)
    acc = pyb.Accel()
    dial = Dial(wri, 5, 5, height = 75, ticks = 12, bdcolor=None,
                label='Tilt Pyboard', style = Dial.COMPASS, pip=YELLOW)  # Border in fg color
    ptr = Pointer(dial)
    scale = 1/40
    while True:
        x, y, z = acc.filtered_xyz()
        # Depending on relative alignment of display and Pyboard this line may
        # need changing: swap x and y or change signs so arrow points in direction
        # board is tilted.
        ptr.value(-y*scale + 1j*x*scale, YELLOW)
        refresh(ssd)
        utime.sleep_ms(200) 
Example #2
Source File: ssd1331_16bit.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def __init__(self, spi, pincs, pindc, pinrs, height=64, width=96):
        self.spi = spi
        self.rate = 6660000  # Data sheet: 150ns min clock period
        self.pincs = pincs
        self.pindc = pindc  # 1 = data 0 = cmd
        self.height = height  # Required by Writer class
        self.width = width
        # Save color mode for use by writer_gui (blit)
        self.mode = framebuf.RGB565
        gc.collect()
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, self.mode)
        pinrs(0)  # Pulse the reset line
        utime.sleep_ms(1)
        pinrs(1)
        utime.sleep_ms(1)
        self._write(b'\xae\xa0\x72\xa1\x00\xa2\x00\xa4\xa8\x3f\xad\x8e\xb0'\
        b'\x0b\xb1\x31\xb3\xf0\x8a\x64\x8b\x78\x8c\x64\xbb\x3a\xbe\x3e\x87'\
        b'\x06\x81\x91\x82\x50\x83\x7d\xaf', 0)
        gc.collect()
        self.show() 
Example #3
Source File: ssd1331.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def __init__(self, spi, pincs, pindc, pinrs, height=64, width=96):
        self.spi = spi
        self.rate = 6660000  # Data sheet: 150ns min clock period
        self.pincs = pincs
        self.pindc = pindc  # 1 = data 0 = cmd
        self.height = height  # Required by Writer class
        self.width = width
        # Save color mode for use by writer_gui (blit)
        self.mode = framebuf.GS8  # Use 8bit greyscale for 8 bit color.
        gc.collect()
        self.buffer = bytearray(self.height * self.width)
        super().__init__(self.buffer, self.width, self.height, self.mode)
        pinrs(0)  # Pulse the reset line
        utime.sleep_ms(1)
        pinrs(1)
        utime.sleep_ms(1)
        self._write(b'\xae\xa0\x32\xa1\x00\xa2\x00\xa4\xa8\x3f\xad\x8e\xb0'\
        b'\x0b\xb1\x31\xb3\xf0\x8a\x64\x8b\x78\x8c\x64\xbb\x3a\xbe\x3e\x87'\
        b'\x06\x81\x91\x82\x50\x83\x7d\xaf', 0)
        gc.collect()
        self.show() 
Example #4
Source File: color15.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def clock(x):
    print('Clock test.')
    refresh(ssd, True)  # Clear any prior image
    lbl = Label(wri, 5, 85, 'Clock')
    dial = Dial(wri, 5, 5, height = 75, ticks = 12, bdcolor=None, label=50)  # Border in fg color
    hrs = Pointer(dial)
    mins = Pointer(dial)
    hrs.value(0 + 0.7j, RED)
    mins.value(0 + 0.9j, YELLOW)
    dm = cmath.rect(1, -cmath.pi/30)  # Rotate by 1 minute (CW)
    dh = cmath.rect(1, -cmath.pi/1800)  # Rotate hours by 1 minute
    for n in range(x):
        refresh(ssd)
        utime.sleep_ms(200)
        mins.value(mins.value() * dm, YELLOW)
        hrs.value(hrs.value() * dh, RED)
        dial.text('ticks: {}'.format(n))
    lbl.value('Done') 
Example #5
Source File: car.py    From 1ZLAB_PyEspCar with GNU General Public License v3.0 6 votes vote down vote up
def go_backward(self, speed_percent=None, delay_ms=None):
        '''
        小车后退
        '''
        if speed_percent is None:
            speed_percent = self.speed_percent
        else:
            speed_percent = float(speed_percent)
        if delay_ms is not None:
            delay_ms = int(delay_ms)

        self.left_motor.speed_percent = -1*speed_percent
        self.right_motor.speed_percent = -1*speed_percent

        if delay_ms is not None:
            delay_ms = int(float(delay_ms))
            utime.sleep_ms(delay_ms)
            self.stop() 
Example #6
Source File: fpt.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def rt_rect():
    print('Simulate realtime data acquisition of discontinuous data.')
    refresh(ssd, True)  # Clear any prior image
    g = CartesianGraph(wri, 2, 2, fgcolor=WHITE, gridcolor=LIGHTGREEN)
    curve = Curve(g, RED)
    x = -1
    for _ in range(40):
        y = 0.1/x if abs(x) > 0.05 else None  # Discontinuity
        curve.point(x, y)
        utime.sleep_ms(100)
        refresh(ssd)
        x += 0.05
    g.clear()
    curve = Curve(g, YELLOW)
    x = -1
    for _ in range(40):
        y = -0.1/x if abs(x) > 0.05 else None  # Discontinuity
        curve.point(x, y)
        utime.sleep_ms(100)
        refresh(ssd)
        x += 0.05 
Example #7
Source File: thingflow.py    From thingflow-python with Apache License 2.0 6 votes vote down vote up
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) 
Example #8
Source File: car.py    From 1ZLAB_PyEspCar with GNU General Public License v3.0 6 votes vote down vote up
def go_forward(self, speed_percent=None, delay_ms=None):
        '''
        小车前进
        '''
        if speed_percent is None:
            speed_percent = self.speed_percent
        else:
            speed_percent = float(speed_percent)
        if delay_ms is not None:
            delay_ms = int(delay_ms)

        self.left_motor.speed_percent = speed_percent
        self.right_motor.speed_percent = speed_percent

        if delay_ms is not None:
            delay_ms = int(float(delay_ms))
            utime.sleep_ms(delay_ms)
            self.stop() 
Example #9
Source File: car.py    From 1ZLAB_PyEspCar with GNU General Public License v3.0 6 votes vote down vote up
def turn_left(self, speed_percent=None, delay_ms=None):
        '''
        小车左转
        '''
        if speed_percent is None:
            speed_percent = self.speed_percent
        else:
            speed_percent = float(speed_percent)            

        self.left_motor.speed_percent = -1* speed_percent
        self.right_motor.speed_percent = speed_percent

        if delay_ms is not None:
            delay_ms = int(float(delay_ms))
            utime.sleep_ms(delay_ms)
            self.stop() 
Example #10
Source File: car.py    From 1ZLAB_PyEspCar with GNU General Public License v3.0 6 votes vote down vote up
def turn_right(self, speed_percent=None, delay_ms=None):
        '''
        小车右转
        '''
        if speed_percent is None:
            speed_percent = self.speed_percent
        else:
            speed_percent = float(speed_percent)
        
        self.left_motor.speed_percent = speed_percent
        self.right_motor.speed_percent = -1* speed_percent
        
        if delay_ms is not None:
            delay_ms = int(float(delay_ms))
            utime.sleep_ms(delay_ms)
            self.stop() 
Example #11
Source File: latency.py    From micropython-async with MIT License 6 votes vote down vote up
def test(use_priority=True):
    global after, after_ms, loop, lp_version
    processing_delay = 2  # Processing time in low priority task (ms)
    if use_priority and not lp_version:
        print('To test priority mechanism you must use fast_io version of uasyncio.')
    else:
        ntasks = max(num_coros) + 10 #4
        if use_priority:
            loop = asyncio.get_event_loop(ntasks, ntasks, 0, ntasks)
            after = asyncio.after
            after_ms = asyncio.after_ms
        else:
            lp_version = False
            after = asyncio.sleep
            after_ms = asyncio.sleep_ms
            loop = asyncio.get_event_loop(ntasks, ntasks)
        s = 'Testing latency of priority task with coros blocking for {}ms.'
        print(s.format(processing_delay))
        if lp_version:
            print('Using priority mechanism.')
        else:
            print('Not using priority mechanism.')
        loop.create_task(run_test(processing_delay))
        loop.run_until_complete(report()) 
Example #12
Source File: bmx280.py    From uPySensors with Apache License 2.0 6 votes vote down vote up
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 
Example #13
Source File: button.py    From 1ZLAB_MicroPython_ESP32_Tutorial with GNU General Public License v3.0 6 votes vote down vote up
def irq_handler(self, irq_pin):
        '''
        外部中断的相应函数
        '''
        # 如果当前正在处理中断,则忽略
        if not self.flag:
            return
        # 添加软件滤波
        utime.sleep_ms(50)
        if self.pin.value() == self.BUTTON_PRESS:
            # 判断当前按键状态是不是按下,如果是,则执行回调函数
            if self.flag and self.callback is not None:
                self.flag = False
                # 执行回调函数
                self.callback(self.pin)
                self.flag = True 
Example #14
Source File: button.py    From 1ZLAB_MicroPython_ESP32_Tutorial with GNU General Public License v3.0 6 votes vote down vote up
def irq_handler(self, irq_pin):
        '''
        外部中断的相应函数
        '''
        # 如果当前正在处理中断,则忽略
        if not self.flag:
            return
        # 添加软件滤波
        utime.sleep_ms(50)
        if self.pin.value() == self.BUTTON_PRESS:
            # 判断当前按键状态是不是按下,如果是,则执行回调函数
            if self.flag and self.callback is not None:
                self.flag = False
                # 执行回调函数
                self.callback(self.pin)
                self.flag = True 
Example #15
Source File: blynklib_mp.py    From lib-python with MIT License 6 votes vote down vote up
def connect(self, timeout=_CONNECT_TIMEOUT):
        end_time = time.time() + timeout
        while not self.connected():
            if self._state == self.DISCONNECTED:
                try:
                    self._get_socket()
                    self._authenticate()
                    self._set_heartbeat()
                    self._last_rcv_time = ticks_ms()
                    self.log('Registered events: {}\n'.format(list(self._events.keys())))
                    self.call_handler(self._CONNECT)
                    return True
                except BlynkError as b_err:
                    self.disconnect(b_err)
                    sleep_ms(self.TASK_PERIOD_RES)
                except RedirectError as r_err:
                    self.disconnect()
                    self.server = r_err.server
                    self.port = r_err.port
                    sleep_ms(self.TASK_PERIOD_RES)
            if time.time() >= end_time:
                return False 
Example #16
Source File: baud.py    From micropython-async with MIT License 5 votes vote down vote up
def gps_test():
    print('Initialising')
    uart = pyb.UART(4, BAUDRATE, read_buf_len=400)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    maxlen = 0
    minlen = 100
    while True:
        res = await sreader.readline()
        l = len(res)
        maxlen = max(maxlen, l)
        minlen = min(minlen, l)
        print(l, maxlen, minlen, res)
        red.toggle()
        utime.sleep_ms(10) 
Example #17
Source File: core.py    From pysmartnode with MIT License 5 votes vote down vote up
def sleep(secs):
    yield int(secs * 1000)


# Implementation of sleep_ms awaitable with zero heap memory usage 
Example #18
Source File: alcd.py    From micropython-async with MIT License 5 votes vote down vote up
def lcd_nybble(self, bits):  # send the LS 4 bits
        for pin in self.datapins:
            pin.value(bits & 0x01)
            bits >>= 1
        time.sleep_us(LCD.E_DELAY)  # 50μs
        self.LCD_E.value(True)  # Toggle the enable pin
        time.sleep_us(LCD.E_PULSE)
        self.LCD_E.value(False)
        if self.initialising:
            time.sleep_ms(5)
        else:
            time.sleep_us(LCD.E_DELAY)  # 50μs 
Example #19
Source File: epd1in54b.py    From micropython-waveshare-epd with MIT License 5 votes vote down vote up
def delay_ms(self, delaytime):
        utime.sleep_ms(delaytime) 
Example #20
Source File: bluenrg_ms.py    From uble with MIT License 5 votes vote down vote up
def hw_bootloader(self):
        """hw_bootloader"""
        self.set_spi_irq_as_output()
        self.reset()
        utime.sleep_ms(4)
        self.set_spi_irq_as_input() 
Example #21
Source File: core.py    From microdot with MIT License 5 votes vote down vote up
def sleep(secs):
    yield int(secs * 1000)

# Implementation of sleep_ms awaitable with zero heap memory usage 
Example #22
Source File: core.py    From pysmartnode with MIT License 5 votes vote down vote up
def wait(self, delay):
        # Default wait implementation, to be overriden in subclasses
        # with IO scheduling
        if __debug__ and DEBUG:
            log.debug("Sleeping for: %s", delay)
        time.sleep_ms(delay) 
Example #23
Source File: alcd.py    From micropython-async with MIT License 5 votes vote down vote up
def runlcd(self):  # Periodically check for changed text and update LCD if so
        while(True):
            for row in range(self.rows):
                if self.dirty[row]:
                    msg = self[row]
                    self.lcd_byte(LCD.LCD_LINES[row], LCD.CMD)
                    for thisbyte in msg:
                        self.lcd_byte(ord(thisbyte), LCD.CHR)
                        await asyncio.sleep_ms(0)  # Reshedule ASAP
                    self.dirty[row] = False
            await asyncio.sleep_ms(20)  # Give other coros a look-in 
Example #24
Source File: alcd.py    From micropython-async with MIT License 5 votes vote down vote up
def lcd_nybble(self, bits):                 # send the LS 4 bits
        for pin in self.datapins:
            pin.value(bits & 0x01)
            bits >>= 1
        time.sleep_us(LCD.E_DELAY)  # 50μs
        self.LCD_E.value(True)                  # Toggle the enable pin
        time.sleep_us(LCD.E_PULSE)
        self.LCD_E.value(False)
        if self.initialising:
            time.sleep_ms(5)
        else:
            time.sleep_us(LCD.E_DELAY)  # 50μs 
Example #25
Source File: alcd.py    From micropython-async with MIT License 5 votes vote down vote up
def runlcd(self):                     # Periodically check for changed text and update LCD if so
        while(True):
            for row in range(self.rows):
                if self.dirty[row]:
                    msg = self[row]
                    self.lcd_byte(LCD.LCD_LINES[row], LCD.CMD)
                    for thisbyte in msg:
                        self.lcd_byte(ord(thisbyte), LCD.CHR)
                        await asyncio.sleep_ms(0)  # Reshedule ASAP
                    self.dirty[row] = False
            await asyncio.sleep_ms(20)          # Give other coros a look-in 
Example #26
Source File: core.py    From micropython-async with MIT License 5 votes vote down vote up
def sleep(secs):
    yield int(secs * 1000)

# Implementation of sleep_ms awaitable with zero heap memory usage 
Example #27
Source File: ms_timer_test.py    From micropython-async with MIT License 5 votes vote down vote up
def foo():
    while True:
        await asyncio.sleep(0)
        utime.sleep_ms(10)  # Emulate slow processing 
Example #28
Source File: pin_cb_test.py    From micropython-async with MIT License 5 votes vote down vote up
def dummy():
    while True:
        await asyncio.sleep(0)
        utime.sleep_ms(5)  # Emulate slow processing 
Example #29
Source File: vl53l0x.py    From uPySensors with Apache License 2.0 5 votes vote down vote up
def _calibrate(self, vhv_init_byte):
        self._register(_SYSRANGE_START, 0x01 | vhv_init_byte)
        for timeout in range(_IO_TIMEOUT):
            if self._register(_RESULT_INTERRUPT_STATUS) & 0x07:
                break
            utime.sleep_ms(1)
        else:
            raise TimeoutError()
        self._register(_INTERRUPT_CLEAR, 0x01)
        self._register(_SYSRANGE_START, 0x00) 
Example #30
Source File: latency.py    From micropython-async with MIT License 5 votes vote down vote up
def lp_task(delay):
    await after_ms(0)  # If running low priority get on LP queue ASAP
    while True:
        time.sleep_ms(delay)  # Simulate processing
        await after_ms(0)