Python utime.sleep_us() Examples

The following are 16 code examples of utime.sleep_us(). 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: hx711.py    From terkin-datalogger with GNU Affero General Public License v3.0 6 votes vote down vote up
def power_down(self):
        """When PD_SCK pin changes from low to high and stays at
        high for longer than 60µs, HX711 enters power down mode.


        """
        log.info('HX711 power down')
        state = disable_irq()
        self.pSCK.value(False)
        self.pSCK.value(True)
        utime.sleep_us(80)
        enable_irq(state)

        # Hold level to HIGH, even during deep sleep.
        # https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72
        if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom:
            self.pSCK.hold(True) 
Example #2
Source File: bmp280.py    From UIFlow-Code with GNU General Public License v3.0 5 votes vote down vote up
def read_raw_data(self, result):
        """ Reads the raw (uncompensated) data from the sensor.

            Args:
                result: array of length 3 or alike where the result will be
                stored, in temperature, pressure, humidity order
            Returns:
                None
        """

        self._l1_barray[0] = self._mode_temperature << 5 | self._mode_pressure << 2 | BMP280_POWER_MODE_FORCED
        self.i2c.writeto_mem(self.address, BMP280_REGISTER_CONTROL, self._l1_barray)

        time.sleep_us(self.compute_delay_time())  # Wait the required time

        # burst readout from 0xF7 to 0xFC, recommended by datasheet
        # we read the 6 bytes (3 bytes each)
        self.i2c.readfrom_mem_into(self.address, 0xF7, self._l8_barray)
        readout = self._l8_barray
        # pressure(0xF7): ((msb << 16) | (lsb << 8) | xlsb) >> 4
        raw_press = ((readout[0] << 16) | (readout[1] << 8) | readout[2]) >> 4
        # temperature(0xFA): ((msb << 16) | (lsb << 8) | xlsb) >> 4
        raw_temp = ((readout[3] << 16) | (readout[4] << 8) | readout[5]) >> 4

        result[0] = raw_temp
        result[1] = raw_press 
Example #3
Source File: timed_func.py    From micropython-samples with MIT License 5 votes vote down vote up
def test():
    utime.sleep_us(10000)

# @time_acc_function
# applied to a function causes it to print the number of times it was called
# with the accumulated time used. 
Example #4
Source File: sht1x.py    From upython-aq-monitor with MIT License 5 votes vote down vote up
def __noop(self):
        utime.sleep_us(self.CLOCK_TIME_US) 
Example #5
Source File: pca9685.py    From openmv-projects with MIT License 5 votes vote down vote up
def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        utime.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on 
Example #6
Source File: bluenrg_ms.py    From uble with MIT License 5 votes vote down vote up
def reset(self):
        """
        Reset BlueNRG-MS module
        """
        self._rst_pin.off()
        utime.sleep_us(5)
        self._rst_pin.on()
        utime.sleep_us(5) 
Example #7
Source File: bluenrg_ms.py    From uble with MIT License 5 votes vote down vote up
def read(self, size=HCI_READ_PACKET_SIZE, retry=5):
        """
        Read packet from BlueNRG-MS module
        """
        result = None
        while retry:
            with CSContext(self._nss_pin):
                # Exchange header
                self._spi_bus.write_readinto(
                    _READ_HEADER_MASTER,
                    self._rw_header_slave
                )
                rx_read_bytes = (
                    self._rw_header_slave[4] << 8
                ) | self._rw_header_slave[3]
                if self._rw_header_slave[0] == 0x02 and rx_read_bytes > 0:
                    # SPI is ready
                    # avoid to read more data that size of the buffer
                    if rx_read_bytes > size:
                        rx_read_bytes = size
                    data = b'\xFF' * rx_read_bytes
                    result = bytearray(rx_read_bytes)
                    self._spi_bus.write_readinto(data, result)
                    break
                else:
                    utime.sleep_us(50)
            retry -= 1

        # Add a small delay to give time to the BlueNRG to set the IRQ pin low
        # to avoid a useless SPI read at the end of the transaction
        utime.sleep_us(150)
        return result 
Example #8
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 #9
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 #10
Source File: ina219.py    From pyb_ina219 with MIT License 5 votes vote down vote up
def wake(self):
        """Wake the INA219 from power down mode."""
        configuration = self._read_configuration()
        self._configuration_register(configuration | 0x0007)
        # 40us delay to recover from powerdown (p14 of spec)
        utime.sleep_us(40) 
Example #11
Source File: hx711.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def power_up(self):
        """When PD_SCK Input is low, chip is in normal working mode."""

        # Unfreeze pin hold when coming from deep sleep.
        # https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72
        if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom:
            self.pSCK.hold(False)

        log.info('HX711 power up')
        self.pSCK.value(False)
        #utime.sleep_us(80)

        #self.initialize() 
Example #12
Source File: asi2c.py    From micropython-iot with MIT License 4 votes vote down vote up
def _handler(self, _, sn=bytearray(2), txnull=bytearray(2)):
        addr = Responder.addr
        self.rem.irq(handler=None)
        utime.sleep_us(_DELAY)  # Ensure Initiator has set up to write.
        self.i2c.readfrom_into(addr, sn)
        self.own(1)
        self.waitfor(0)
        self.own(0)
        n = sn[0] + ((sn[1] & 0x7f) << 8)  # no of bytes to receive
        if n > self.rxbufsize:
            raise ValueError('Receive data too large for buffer.')
        self.cantx = not bool(sn[1] & 0x80)  # Can Initiator accept a payload?
        if n:
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            mv = memoryview(self.rx_mv[0: n])  # allocates
            self.i2c.readfrom_into(addr, mv)
            self.own(1)
            self.waitfor(0)
            self.own(0)
            self._handle_rxd(mv)

        self.own(1)  # Request to send
        self.waitfor(1)
        utime.sleep_us(_DELAY)
        dtx = self.txbyt != b'' and self.cantx  # Data to send
        siz = self.txsiz if dtx else txnull
        if self.rxbyt:
            siz[1] |= 0x80  # Hold off Initiator TX
        else:
            siz[1] &= 0x7f
        self.i2c.writeto(addr, siz)  # Was getting ENODEV occasionally on Pyboard
        self.own(0)
        self.waitfor(0)
        if dtx:
            self.own(1)
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            self.i2c.writeto(addr, self.txbyt)
            self.own(0)
            self.waitfor(0)
            self._txdone()  # Invalidate source
        self.rem.irq(handler=self._handler, trigger=machine.Pin.IRQ_RISING) 
Example #13
Source File: asi2c.py    From micropython-iot with MIT License 4 votes vote down vote up
def _handler(self, _, sn=bytearray(2), txnull=bytearray(2)):
        addr = Responder.addr
        self.rem.irq(handler=None)
        utime.sleep_us(_DELAY)  # Ensure Initiator has set up to write.
        self.i2c.readfrom_into(addr, sn)
        self.own(1)
        self.waitfor(0)
        self.own(0)
        n = sn[0] + ((sn[1] & 0x7f) << 8)  # no of bytes to receive
        if n > self.rxbufsize:
            raise ValueError('Receive data too large for buffer.')
        self.cantx = not bool(sn[1] & 0x80)  # Can Initiator accept a payload?
        if n:
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            mv = memoryview(self.rx_mv[0: n])  # allocates
            self.i2c.readfrom_into(addr, mv)
            self.own(1)
            self.waitfor(0)
            self.own(0)
            self._handle_rxd(mv)

        self.own(1)  # Request to send
        self.waitfor(1)
        utime.sleep_us(_DELAY)
        dtx = self.txbyt != b'' and self.cantx  # Data to send
        siz = self.txsiz if dtx else txnull
        if self.rxbyt:
            siz[1] |= 0x80  # Hold off Initiator TX
        else:
            siz[1] &= 0x7f
        self.i2c.writeto(addr, siz)  # Was getting ENODEV occasionally on Pyboard
        self.own(0)
        self.waitfor(0)
        if dtx:
            self.own(1)
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            self.i2c.writeto(addr, self.txbyt)
            self.own(0)
            self.waitfor(0)
            self._txdone()  # Invalidate source
        self.rem.irq(handler=self._handler, trigger=machine.Pin.IRQ_RISING) 
Example #14
Source File: bluenrg_ms.py    From uble with MIT License 4 votes vote down vote up
def write(self, header, param, retry=5):
        """
        Write packet to BlueNRG-MS module
        """
        result = None
        while retry:
            with CSContext(self._nss_pin):
                # Exchange header
                self._spi_bus.write_readinto(
                    _WRITE_HEADER_MASTER,
                    self._rw_header_slave
                )
                rx_write_bytes = self._rw_header_slave[1]
                rx_read_bytes = (
                    self._rw_header_slave[4] << 8
                ) | self._rw_header_slave[3]
                if self._rw_header_slave[0] == 0x02 and (
                        rx_write_bytes > 0 or rx_read_bytes > 0):
                    # SPI is ready
                    if header:
                        # avoid to write more data that size of the buffer
                        if rx_write_bytes >= len(header):
                            result = bytearray(len(header))
                            self._spi_bus.write_readinto(header, result)
                            if param:
                                rx_write_bytes -= len(header)
                                # avoid to read more data that size of the
                                # buffer
                                if len(param) > rx_write_bytes:
                                    tx_bytes = rx_write_bytes
                                else:
                                    tx_bytes = len(param)
                                result = bytearray(tx_bytes)
                                self._spi_bus.write_readinto(param, result)
                                break
                            else:
                                break
                        else:
                            break
                    else:
                        break
                else:
                    utime.sleep_us(50)
            retry -= 1

        return result 
Example #15
Source File: asi2c.py    From micropython-async with MIT License 4 votes vote down vote up
def _handler(self, _, sn=bytearray(2), txnull=bytearray(2)):
        addr = Responder.addr
        self.rem.irq(handler=None, trigger=machine.Pin.IRQ_RISING)
        utime.sleep_us(_DELAY)  # Ensure Initiator has set up to write.
        self.i2c.readfrom_into(addr, sn)
        self.own(1)
        self.waitfor(0)
        self.own(0)
        n = sn[0] + ((sn[1] & 0x7f) << 8)  # no of bytes to receive
        if n > self.rxbufsize:
            raise ValueError('Receive data too large for buffer.')
        self.cantx = not bool(sn[1] & 0x80)  # Can Initiator accept a payload?
        if n:
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            mv = memoryview(self.rx_mv[0: n])  # allocates
            self.i2c.readfrom_into(addr, mv)
            self.own(1)
            self.waitfor(0)
            self.own(0)
            self._handle_rxd(mv)

        self.own(1)  # Request to send
        self.waitfor(1)
        utime.sleep_us(_DELAY)
        dtx = self.txbyt != b'' and self.cantx  # Data to send
        siz = self.txsiz if dtx else txnull
        if self.rxbyt:
            siz[1] |= 0x80  # Hold off Initiator TX
        else:
            siz[1] &= 0x7f
        self.i2c.writeto(addr, siz)  # Was getting ENODEV occasionally on Pyboard
        self.own(0)
        self.waitfor(0)
        if dtx:
            self.own(1)
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            self.i2c.writeto(addr, self.txbyt)
            self.own(0)
            self.waitfor(0)
            self._txdone()  # Invalidate source
        self.rem.irq(handler=self._handler, trigger=machine.Pin.IRQ_RISING) 
Example #16
Source File: asi2c.py    From micropython-async with MIT License 4 votes vote down vote up
def _handler(self, _, sn=bytearray(2), txnull=bytearray(2)):
        addr = Responder.addr
        self.rem.irq(handler=None)
        utime.sleep_us(_DELAY)  # Ensure Initiator has set up to write.
        self.i2c.readfrom_into(addr, sn)
        self.own(1)
        self.waitfor(0)
        self.own(0)
        n = sn[0] + ((sn[1] & 0x7f) << 8)  # no of bytes to receive
        if n > self.rxbufsize:
            raise ValueError('Receive data too large for buffer.')
        self.cantx = not bool(sn[1] & 0x80)  # Can Initiator accept a payload?
        if n:
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            mv = memoryview(self.rx_mv[0: n])  # allocates
            self.i2c.readfrom_into(addr, mv)
            self.own(1)
            self.waitfor(0)
            self.own(0)
            self._handle_rxd(mv)

        self.own(1)  # Request to send
        self.waitfor(1)
        utime.sleep_us(_DELAY)
        dtx = self.txbyt != b'' and self.cantx  # Data to send
        siz = self.txsiz if dtx else txnull
        if self.rxbyt:
            siz[1] |= 0x80  # Hold off Initiator TX
        else:
            siz[1] &= 0x7f
        self.i2c.writeto(addr, siz)  # Was getting ENODEV occasionally on Pyboard
        self.own(0)
        self.waitfor(0)
        if dtx:
            self.own(1)
            self.waitfor(1)
            utime.sleep_us(_DELAY)
            self.i2c.writeto(addr, self.txbyt)
            self.own(0)
            self.waitfor(0)
            self._txdone()  # Invalidate source
        self.rem.irq(handler=self._handler, trigger=machine.Pin.IRQ_RISING)