Python源码示例:machine.deepsleep()
示例1
def restart():
machine.deepsleep(1)
示例2
def deep_sleep(self, seconds: Optional[float] = None, **kwargs):
"""
Stops execution in an attempt to enter a low power state.
A deepsleep may not retain RAM or any other state of the system (for example peripherals or network interfaces).
Upon wake execution is resumed from the main script, similar to a hard or power-on reset.
:param seconds: Sleep seconds (default: sleep until there are some PIN/RTC events to process)
:param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
"""
code = '''
import machine
machine.deepsleep({msec})
'''.format(msec=int(seconds * 1000) if seconds else '')
return self.execute(code, wait_response=False, **kwargs).output
示例3
def tear_down(timer, initial_time_remaining):
timer.stop()
elapsed_ms = int(timer.read()*1000)
timer.reset()
time_remaining = initial_time_remaining - elapsed_ms
print('sleeping for {}ms ({})'.format(time_remaining, ertc.get_time()))
# deepsleep_pin = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
# machine.pin_deepsleep_wakeup(pins=[deepsleep_pin], mode=machine.WAKEUP_ALL_LOW, enable_pull=True)
machine.deepsleep(time_remaining)
######################
# External RTC
######################
示例4
def go_deepsleep(t):
"""Deep sleep helper that also powers down the on-board Dotstar."""
set_dotstar_power(False)
machine.deepsleep(t)
示例5
def go_deepsleep(t):
"""Deep sleep helper that also powers down the on-board Dotstar."""
set_dotstar_power(False)
machine.deepsleep(t)
示例6
def go_deepsleep(t):
"""Deep sleep helper that also powers down the on-board Dotstar."""
set_dotstar_power(False)
machine.deepsleep(t)
示例7
def deep_sleep(secs) :
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, secs)
# put the device to sleep
machine.deepsleep()
示例8
def deepsleep(sleeping_time, wait_before_sleep=None, event=None):
if wait_before_sleep is not None:
await asyncio.sleep(wait_before_sleep)
if event is not None:
await event
if platform == "esp32_LoBo":
machine.deepsleep(int(sleeping_time * 1000))
else:
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, int(sleeping_time * 1000))
machine.deepsleep()
示例9
def sleep(self, milliseconds):
# To be able to use this fea
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fire after some milliseconds
rtc.alarm(rtc.ALARM0, milliseconds)
# put the device to sleep
machine.deepsleep()
示例10
def set_wakeup_mode(self):
""" """
# Set wake up parameters.
"""
The arguments are:
- pins: a list or tuple containing the GPIO to setup for deepsleep wakeup.
- mode: selects the way the configured GPIOs can wake up the module.
The possible values are: machine.WAKEUP_ALL_LOW and machine.WAKEUP_ANY_HIGH.
- enable_pull: if set to True keeps the pull up or pull down resistors enabled
during deep sleep. If this variable is set to True, then ULP or capacitive touch
wakeup cannot be used in combination with GPIO wakeup.
-- https://community.hiveeyes.org/t/deep-sleep-with-fipy-esp32-on-micropython/1792/12
This will yield a wake up reason like::
'wakeup_reason': {'code': 1, 'message': 'PIN'}
"""
# Todo: ``enable_pull`` or not?
# From documentation.
# machine.pin_sleep_wakeup(pins=['P8'], mode=machine.WAKEUP_ALL_LOW, enable_pull=True)
# Let's try.
#log.info('Configuring Pin 4 for wakeup from deep sleep')
#machine.pin_sleep_wakeup(pins=['P4'], mode=machine.WAKEUP_ALL_LOW, enable_pull=True)
#machine.pin_sleep_wakeup(pins=['P4'], mode=machine.WAKEUP_ANY_HIGH, enable_pull=True)
pass
示例11
def power_off_lte_modem(self):
"""
We don't use LTE yet.
Important
=========
Once the LTE radio is initialized, it must be de-initialized
before going to deepsleep in order to ensure minimum power consumption.
This is required due to the LTE radio being powered independently and
allowing use cases which require the system to be taken out from
deepsleep by an event from the LTE network (data or SMS received for
instance).
Note
====
When using the expansion board and the FiPy together, the RTS/CTS
jumpers MUST be removed as those pins are being used by the LTE radio.
Keeping those jumpers in place will lead to erratic operation and
higher current consumption specially while in deepsleep.
-- https://forum.pycom.io/topic/3090/fipy-current-consumption-analysis/17
See also
========
- https://community.hiveeyes.org/t/lte-modem-des-pycom-fipy-komplett-stilllegen/2161
- https://forum.pycom.io/topic/4877/deepsleep-on-batteries/10
"""
log.info('Turning off LTE modem')
try:
import pycom
from network import LTE
log.info('Turning off LTE modem on boot')
pycom.lte_modem_en_on_boot(False)
# Disables LTE modem completely. This presumably reduces the power
# consumption to the minimum. Call this before entering deepsleep.
log.info('Invoking LTE.deinit()')
lte = LTE()
lte.deinit(detach=False, reset=True)
except Exception as ex:
log.exc(ex, 'Shutting down LTE modem failed')
示例12
def hibernate(self, interval, lightsleep=False, deepsleep=False):
"""
:param interval:
:param lightsleep: (Default value = False)
:param deepsleep: (Default value = False)
"""
#logging.enable_logging()
if deepsleep:
# Prepare and invoke deep sleep.
# https://docs.micropython.org/en/latest/library/machine.html#machine.deepsleep
log.info('Preparing deep sleep')
# Set wake up mode.
self.set_wakeup_mode()
# Invoke deep sleep.
log.info('Entering deep sleep for {} seconds'.format(interval))
#self.terminal.stop()
machine.deepsleep(int(interval * 1000))
else:
# Adjust watchdog for interval.
self.watchdog.adjust_for_interval(interval)
# Invoke light sleep.
# https://docs.micropython.org/en/latest/library/machine.html#machine.sleep
# https://docs.micropython.org/en/latest/library/machine.html#machine.lightsleep
#
# As "machine.sleep" seems to be a noop on Pycom MicroPython,
# we will just use the regular "time.sleep" here.
# machine.sleep(int(interval * 1000))
machine.idle()
if lightsleep:
log.info('Entering light sleep for {} seconds'.format(interval))
machine.sleep(int(interval * 1000))
else:
# Normal wait.
log.info('Waiting for {} seconds'.format(interval))
time.sleep(interval)