Python time.sleep_ms() Examples
The following are 30 code examples for showing how to use time.sleep_ms(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
time
, or try the search function
.
Example 1
Project: UIFlow-Code Author: m5stack File: speak.py License: GNU General Public License v3.0 | 6 votes |
def tone(self, freq=1800, duration=200, volume=None, timer=True): duration = min(max(30, duration), 2000) freq = min(max(20, freq), 20000) self.checkInit() if volume == None: self.pwm.init(freq=freq, duty=self._volume) else: self.pwm.init(freq=freq, duty=volume) if timer: # if self._timer.isrunning(): # self._timer.period(duration) # else: self._timer.init(period=duration, mode=self._timer.ONE_SHOT, callback=self._timeout_cb) time.sleep_ms(duration-15) else: time.sleep_ms(duration) self.pwm.duty(0) time.sleep_ms(1) self.pwm.freq(1)
Example 2
Project: developer-badge-2018-apps Author: IBM-Developer-Korea File: buzzer.py License: Apache License 2.0 | 6 votes |
def playnotes(self, title, length=150, duty=64): if title not in self.notes: print('unknown title: {}'.format(title)) return melody = self.notes[title] print('Play', title) for i in melody: if i == 0: self.pwm.duty(0) else: self.pwm.freq(i) self.pwm.duty(duty) time.sleep_ms(length) self.pwm.duty(0)
Example 3
Project: developer-badge-2018-apps Author: IBM-Developer-Korea File: buzzer.py License: Apache License 2.0 | 6 votes |
def playnotes(self, title, length=150, duty=64): if title not in self.notes: print('unknown title: {}'.format(title)) return melody = self.notes[title] print('Play', title) for i in melody: if i == 0: self.pwm.duty(0) else: self.pwm.freq(i) self.pwm.duty(duty) time.sleep_ms(length) self.pwm.duty(0)
Example 4
Project: developer-badge-2018-apps Author: IBM-Developer-Korea File: iot_client.py License: Apache License 2.0 | 6 votes |
def main(): c.set_callback(sub_cb) if not wait_network(): print('Cannot connect WiFi') raise Exception('Cannot connect WiFi') c.connect() if conf.data['orgID'] != 'quickstart': c.subscribe(ledCommandTopic) c.subscribe(irCommandTopic) print('Connected, waiting for event ({})'.format(conf.data['deviceID'])) status = {'d': {'sine':{}}} count = 0 try: while True: status['d']['sine'] = sineVal(-1.0, 1.0, 16, count) count += 1 c.publish(statusTopic, json.dumps(status)) time.sleep_ms(10000) #c.wait_msg() c.check_msg() finally: c.disconnect() print('Disonnected')
Example 5
Project: developer-badge-2018-apps Author: IBM-Developer-Korea File: buzzer.py License: Apache License 2.0 | 6 votes |
def playnotes(self, title, length=150, duty=64): # Init p = Pin(27, Pin.OUT) self.pwm = PWM(p) self.pwm.duty(0) if title not in self.notes: print('unknown title: {}'.format(title)) return melody = self.notes[title] print('Play', title) for i in melody: if i == 0: self.pwm.duty(0) else: self.pwm.freq(i) self.pwm.duty(duty) time.sleep_ms(length) # deinit self.pwm.deinit()
Example 6
Project: MicroPython-ESP8266-DHT-Nokia-5110 Author: mcauser File: am2320.py License: MIT License | 6 votes |
def measure(self): buf = self.buf address = self.address # wake sensor try: self.i2c.writeto(address, b'') except OSError: pass # read 4 registers starting at offset 0x00 self.i2c.writeto(address, b'\x03\x00\x04') # wait at least 1.5ms time.sleep_ms(2) # read data self.i2c.readfrom_mem_into(address, 0, buf) # debug print print(ustruct.unpack('BBBBBBBB', buf)) crc = ustruct.unpack('<H', bytearray(buf[-2:]))[0] if (crc != self.crc16(buf[:-2])): raise Exception("checksum error")
Example 7
Project: tinypico-micropython Author: tinypico File: ssd1306.py License: MIT License | 6 votes |
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): self.rate = 10 * 1024 * 1024 dc.init(dc.OUT, value=0) res.init(res.OUT, value=0) cs.init(cs.OUT, value=1) self.spi = spi self.dc = dc self.res = res self.cs = cs import time self.res(1) time.sleep_ms(1) self.res(0) time.sleep_ms(10) self.res(1) super().__init__(width, height, external_vcc)
Example 8
Project: micropython-sht30 Author: rsc1975 File: sht30.py License: Apache License 2.0 | 6 votes |
def send_cmd(self, cmd_request, response_size=6, read_delay_ms=100): """ Send a command to the sensor and read (optionally) the response The responsed data is validated by CRC """ try: self.i2c.start(); self.i2c.writeto(self.i2c_addr, cmd_request); if not response_size: self.i2c.stop(); return time.sleep_ms(read_delay_ms) data = self.i2c.readfrom(self.i2c_addr, response_size) self.i2c.stop(); for i in range(response_size//3): if not self._check_crc(data[i*3:(i+1)*3]): # pos 2 and 5 are CRC raise SHT30Error(SHT30Error.CRC_ERROR) if data == bytearray(response_size): raise SHT30Error(SHT30Error.DATA_ERROR) return data except OSError as ex: if 'I2C' in ex.args[0]: raise SHT30Error(SHT30Error.BUS_ERROR) raise ex
Example 9
Project: ulnoiot-upy Author: ulno File: weather.py License: MIT License | 6 votes |
def loop(): show_temp = True while True: g = get() display.fill(0) display.text("Weather in", 0, 0) display.text(g['name'], 0, 8) if left_button.value() == 0 or right_button.value() == 0: show_temp = not show_temp if show_temp: celsius = g['main']['temp'] - 273.15 display.text("Temp/C: %.1f" % celsius, 0, 24) display.text("Temp/F: %.1f" % (celsius * 1.8 + 32), 0, 32) else: # humidity display.text("Humidity: %d%%" % g['main']['humidity'], 0, 24) if lower_button.value() == 0: break display.show() time.sleep_ms(500)
Example 10
Project: py-mpu6050 Author: larsks File: mpu6050.py License: GNU General Public License v3.0 | 6 votes |
def get_sensor_avg(self, samples, softstart=100): '''Return the average readings from the sensors over the given number of samples. Discard the first softstart samples to give things time to settle.''' sample = self.read_sensors() counters = [0] * 7 for i in range(samples + softstart): # the sleep here is to ensure we read a new sample # each time time.sleep_ms(2) sample = self.read_sensors() if i < softstart: continue for j, val in enumerate(sample): counters[j] += val return [x//samples for x in counters]
Example 11
Project: MaixPy_scripts Author: sipeed File: demo_http.py License: MIT License | 6 votes |
def wifi_reset(): global uart wifi_enable(0) time.sleep_ms(200) wifi_enable(1) time.sleep(2) uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096) tmp = uart.read() uart.write("AT+UART_CUR=921600,8,1,0,0\r\n") print(uart.read()) uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data uart.write("AT\r\n") tmp = uart.read() print(tmp) if not tmp.endswith("OK\r\n"): print("reset fail") return None try: nic = network.ESP8285(uart) except Exception: return None return nic
Example 12
Project: MaixPy_scripts Author: sipeed File: upgrade_at_firmware.py License: MIT License | 6 votes |
def cmd_join_ap_and_wait(self): print("[cmd join ap]") recv = self.uart.read() print(recv) if self.passwd: self.uart.write(b'AT+CWJAP_DEF="{}","{}"\r\n'.format(self.ssid, self.passwd)) else: self.uart.write(b'AT+CWJAP_DEF="{}"\r\n'.format(self.ssid)) time.sleep_ms(200) print("[wait join ap -- 0]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 10000: return False recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"GOT IP" in read: return True
Example 13
Project: MaixPy_scripts Author: sipeed File: upgrade_at_firmware.py License: MIT License | 6 votes |
def wait_join_ap(self): print("[wait join ap]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 10000: raise Exception("wait for join AP timeout") recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"GOT IP" in read: break time.sleep_ms(1000) read = self.uart.read() print(read)
Example 14
Project: MaixPy_scripts Author: sipeed File: upgrade_at_firmware.py License: MIT License | 6 votes |
def wait_upgrade(self): print("[wait upgrade process]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 80000: raise Exception("wait for update timeout") recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if self.update_step != 4 and b"+CIPUPDATE:4" in read: self.update_step = 4 if self.update_step == 4 and b"OK" in read: break time.sleep_ms(200)
Example 15
Project: MaixPy_scripts Author: sipeed File: demo_net_socket.py License: MIT License | 6 votes |
def wifi_reset(): global uart wifi_enable(0) time.sleep_ms(200) wifi_enable(1) time.sleep(2) uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096) tmp = uart.read() uart.write("AT+UART_CUR=921600,8,1,0,0\r\n") print(uart.read()) uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data uart.write("AT\r\n") tmp = uart.read() print(tmp) if not tmp.endswith("OK\r\n"): print("reset fail") return None try: nic = network.ESP8285(uart) except Exception: return None return nic
Example 16
Project: MaixPy_scripts Author: sipeed File: demo_send_pic.py License: MIT License | 6 votes |
def wifi_reset(): global uart wifi_enable(0) time.sleep_ms(200) wifi_enable(1) time.sleep(2) uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096) tmp = uart.read() uart.write("AT+UART_CUR=921600,8,1,0,0\r\n") print(uart.read()) uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=4096) uart.write("AT\r\n") tmp = uart.read() print(tmp) if not tmp.endswith("OK\r\n"): print("reset fail") return None try: nic = network.ESP8285(uart) except Exception: return None return nic
Example 17
Project: microhomie Author: microhomie File: main.py License: MIT License | 6 votes |
def read_temp(self, fahrenheit=True): """ Reads temperature from a single DS18X20 :param fahrenheit: Whether or not to return value in Fahrenheit :type fahrenheit: bool :return: Temperature :rtype: float """ self.ds18b20.convert_temp() time.sleep_ms(750) temp = self.ds18b20.read_temp(self.addr) if fahrenheit: ntemp = temp print("Temp: " + str(self.c_to_f(ntemp))) return self.c_to_f(ntemp) return temp
Example 18
Project: terkin-datalogger Author: hiveeyes File: backup.py License: GNU Affero General Public License v3.0 | 6 votes |
def rename_file(self, oldfile, newfile): """ Rename fila :param oldfile: old file name :param newfile: new file name """ #log.info('Renaming backup file {} to {}'.format(oldfile, newfile)) try: os.rename(oldfile, newfile) except OSError: pass time.sleep_ms(5) uos.sync() time.sleep_ms(5)
Example 19
Project: terkin-datalogger Author: hiveeyes File: ds18x20_sensor.py License: GNU Affero General Public License v3.0 | 6 votes |
def read(self): """ - Start conversion on all DS18x20 sensors. - Wait for conversion. - Read all DS18x20 sensors. """ if self.bus is None or self.driver is None: return self.SENSOR_NOT_INITIALIZED log.info('Acquire readings from all DS18x20 sensors attached to bus "{}"'.format(self.bus.name)) # Start conversion on all DS18x20 sensors. self.driver.convert_temp() time.sleep_ms(750) # Read scratch memory of each sensor. data = self.read_devices() if not data: log.warning('No data from any DS18x20 devices on bus "{}"'.format(self.bus.name)) log.debug('Data from 1-Wire bus "{}" is "{}"'.format(self.bus.name, data)) return data
Example 20
Project: SX127x_driver_for_MicroPython_on_ESP8266 Author: Wei1234c File: ssd1306.py License: GNU General Public License v3.0 | 5 votes |
def poweron(self): self.res(1) time.sleep_ms(1) self.res(0) time.sleep_ms(10) self.res(1)
Example 21
Project: specter-diy Author: cryptoadvance File: qr.py License: MIT License | 5 votes |
def query(self, data, timeout=100): """Blocking query""" self.uart.write(data) t0 = time.time() while self.uart.any() < 7: time.sleep_ms(10) t = time.time() if t > t0+timeout/1000: return None res = self.uart.read(7) return res
Example 22
Project: specter-diy Author: cryptoadvance File: qr.py License: MIT License | 5 votes |
def scan(self): self.clean_uart() if self.trigger is not None: self.trigger.off() else: self.set_setting(SETTINGS_ADDR, SETTINGS_CONT_MODE) self.data = b"" self.scanning = True self.animated = False while self.scanning: await asyncio.sleep_ms(10) # we will exit this loop from update() # or manual cancel from GUI return self.data
Example 23
Project: specter-diy Author: cryptoadvance File: qr.py License: MIT License | 5 votes |
def update(self): if not self.scanning: self.clean_uart() return # read all available data if self.uart.any() > 0: d = self.uart.read() self.data += d # we got a full scan if self.data.endswith(self.EOL): # maybe two chunks = self.data.split(self.EOL) self.data = b"" try: for chunk in chunks[:-1]: if self.process_chunk(chunk): self.stop_scanning() break # animated in trigger mode elif self.trigger is not None: self.trigger.on() await asyncio.sleep_ms(30) self.trigger.off() except Exception as e: print(e) self.stop_scanning() raise e
Example 24
Project: specter-diy Author: cryptoadvance File: core.py License: MIT License | 5 votes |
def ioloop(dt:int=30): while True: time.sleep_ms(dt) update(dt)
Example 25
Project: micropython-bmp180 Author: micropython-IMU File: bmp180.py License: MIT License | 5 votes |
def __init__(self, i2c_bus): # create i2c obect _bmp_addr = self._bmp_addr self._bmp_i2c = i2c_bus self._bmp_i2c.start() self.chip_id = self._bmp_i2c.readfrom_mem(_bmp_addr, 0xD0, 2) # read calibration data from EEPROM self._AC1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAA, 2))[0] self._AC2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAC, 2))[0] self._AC3 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAE, 2))[0] self._AC4 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB0, 2))[0] self._AC5 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB2, 2))[0] self._AC6 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB4, 2))[0] self._B1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB6, 2))[0] self._B2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB8, 2))[0] self._MB = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBA, 2))[0] self._MC = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBC, 2))[0] self._MD = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBE, 2))[0] # settings to be adjusted by user self.oversample_setting = 3 self.baseline = 101325.0 # output raw self.UT_raw = None self.B5_raw = None self.MSB_raw = None self.LSB_raw = None self.XLSB_raw = None self.gauge = self.makegauge() # Generator instance for _ in range(128): next(self.gauge) time.sleep_ms(1)
Example 26
Project: micropython-adafruit-ssd1306 Author: adafruit File: ssd1306.py License: MIT License | 5 votes |
def poweron(self): self.res.high() time.sleep_ms(1) self.res.low() time.sleep_ms(10) self.res.high()
Example 27
Project: UIFlow-Code Author: m5stack File: _finger.py License: GNU General Public License v3.0 | 5 votes |
def __init__(self): self.uart = machine.UART(1, tx=17, rx=16) self.uart.init(19600, bits=0, parity=None, stop=1) self._timer = timEx.addTimer(100, timEx.PERIODIC, self._monitor) self._times = 0 self.cb = None self.unknownCb = None self.access_add = 0 self.user_id_add = 0 self.state = '' time.sleep_ms(100) self.readUser()
Example 28
Project: UIFlow-Code Author: m5stack File: _encode.py License: GNU General Public License v3.0 | 5 votes |
def setLed(self, pos, color): pos = max(min(pos, 11), 0) try: self.i2c.writeto_mem(self._addr, pos, color.to_bytes(3, 'big')) time.sleep_ms(1) except: pass
Example 29
Project: UIFlow-Code Author: m5stack File: speak.py License: GNU General Public License v3.0 | 5 votes |
def _timeout_cb(self, timer): self.checkInit() self.pwm.duty(0) time.sleep_ms(1) self.pwm.freq(1)
Example 30
Project: UIFlow-Code Author: m5stack File: time_ex.py License: GNU General Public License v3.0 | 5 votes |
def timeCb(self): global delete_num while True: for i in self.timerList: if i.dead: delete_num.append(i) else: i.update() if delete_num: for i in delete_num: self.timerList.remove(i) delete_num = [] time.sleep_ms(10)