def test(*pin_names, rising=True):
    wups = [
        upower.WakeupPin(pyb.Pin(name), rising=rising) for name in pin_names
    ]
    reason = machine.reset_cause()  # Why have we woken?
    if reason in (machine.PWRON_RESET, machine.HARD_RESET,
                  machine.SOFT_RESET):  # first boot
        rtc.datetime(
            (2020, 8, 6, 4, 13, 0, 0, 0))  # Code to run on 1st boot only
        upower.savetime()
        if upower.bkpram_ok():  # backup RAM holds valid data: battery backed
            light(2)  # Blue only
        else:  # Boot with no backup battery, data is garbage
            light(0, 1, 2)  # White
    elif reason == machine.DEEPSLEEP_RESET:
        reason = upower.why()
        if reason == 'WAKEUP':
            light(0, 1, 2)  # White on timer wakeup
            upower.savetime()
        elif reason == 'X3':  # red
            light(0)
        elif reason == 'X1':  # green
            light(1)
        elif reason == 'C1':
            light(2)  # Blue
        elif reason == 'C13':
            light(0, 1)  # Red and green
        else:
            upower.cprint('Unknown: reset?')  # Prints if a UART is configured

    upower.lpdelay(500)  # ensure LEDs visible before standby turns it off
    while any((p.state() for p in wups)):
        upower.lpdelay(50)  # Wait for wakeup signals to go away
    upower.lpdelay(50)  # Wait out any contact bounce

    # demo of not resetting the wakeup timer after a pin interrupt
    try:  # ms_left can fail in response to various coding errors
        timeleft = upower.ms_left(10000)
    except upower.RTCError:
        timeleft = 10000  # Coding error: uninitialised - just restart the timer
    # Set a minimum sleep duration: too short and it uses less power to stay awake
    # In real apps this might be longer or you might deal with it in other ways.
    timeleft = max(timeleft, 1000)
    rtc.wakeup(timeleft)
    # These calls reconfigure hardware and should be done last, shortly before standby()
    for p in wups:
        p.enable()
    if not upower.usb_connected:
        pyb.standby()  # Set pins hi-z and turn LEDs off
    else:
        light(1)  # Green LED: debugging session.
Example #2
0
                # distance - log distance for day
    def logupdate(self, log):
        return
	    self.log['alive'] = log
        self.save()

    def save(self):
	    return
        bkpram = upower.BkpRAM()
        z = pickle.dumps(self.log).encode('utf8')
        bkpram[0] = len(z)
        bkpram.ba[4: 4+len(z)] = z # Copy into backup RAM

    def load(self):
	    return
        reason = upower.why()                           # Why have we woken?
        if reason == 'BOOT':                            # first boot
            self.log = log_generator()
            self.save()
        elif reason == 'POWERUP':
            bkpram = upower.BkpRAM()
            self.log = pickle.loads(bytes(bkpram.ba[4:4+bkpram[0]]).decode('utf-8')) # retrieve dictionary
            if len(self.log) == 0:
                self.log = log_generator()
        currentdaydata = time.time()
        currentdaytuple = time.localtime(currentdaydata)
        # adjust currentdaytuple to make to midnight on day of interest
        cdt = (currentdaytuple[0], currentdaytuple[1], currentdaytuple[2], 0, 0, 0,
               currentdaytuple[6],currentdaytuple[7])

        date = time.mktime(cdt)
# Copyright Peter Hinch
# V0.4 10th October 2016 Now uses machine.reset_cause()
# Flashes leds at 30 second intervals courtesy of two concurrent timers
# (each times out at one minute intervals).
# Note that the Pyboard flashes the green LED briefly on waking from standby.
import stm, pyb, upower, machine

red, green, yellow = (pyb.LED(x) for x in range(1, 4)
                      )  # LED(3) is blue, not yellow, on D series
rtc = pyb.RTC()
rtc.wakeup(
    None
)  # If we have a backup battery clear down any setting from a previously running program
reason = machine.reset_cause()  # Why have we woken?
if reason == machine.PWRON_RESET or reason == machine.HARD_RESET:  # first boot
    rtc.datetime((2020, 8, 6, 4, 13, 0, 0, 0))  # Code to run on 1st boot only
    aa = upower.Alarm('a')
    aa.timeset(second=39)
    ab = upower.Alarm('b')
    ab.timeset(second=9)
    red.on()
elif reason == machine.DEEPSLEEP_RESET:
    reason = upower.why()
    if reason == 'ALARM_A':
        green.on()
    elif reason == 'ALARM_B':
        yellow.on()

upower.lpdelay(1000)  # Let LED's be seen!
pyb.standby()
leds = tuple(pyb.LED(x) for x in range(1, 4))  # rgy
for led in leds:
    led.off()

reason = machine.reset_cause()  # Why have we woken?
if reason == machine.PWRON_RESET or reason == machine.HARD_RESET:  # first boot
    rtc.datetime((2016, 8, 6, 4, 13, 0, 0, 0))  # Code to run on 1st boot only
    upower.savetime()
    if upower.bkpram_ok():  # backup RAM holds valid data
        leds[2].on()  # Y only
    else:  # No backup battery, data is garbage
        for led in leds:
            led.on()  # RGY == 1st boot
elif reason == machine.DEEPSLEEP_RESET:
    reason = upower.why()
    if reason == "WAKEUP":  # green and yellow on timer wakeup
        leds[1].on()
        leds[2].on()
        upower.savetime()
    elif reason == "TAMPER":  # red
        leds[0].on()
    elif reason == "X1":  # red and green on X1 rising edge
        leds[0].on()
        leds[1].on()
    else:
        upower.cprint("Unknown: reset?")  # Prints if a UART is configured

t = rtc.datetime()[4:7]
upower.cprint("{:02d}.{:02d}.{:02d}".format(t[0], t[1], t[2]))
    return pkl

def gestione_power_on():

    print("Power On")
    uart.write("Power On.")

 
#imposto setting seriale - set MCU serial port1  
uart = UART(1, 9600)                         
uart.init(9600, bits=8, parity=None, stop=1)
 
test=0

reason=upower.why()   # motivo dell'uscita da low power mode.
                      # see upower.py module documentation.

uart.write(str(reason) +'\n')

#reason='ALARM_B'     # solo per debug
try:
    if reason=='X1':
       verde.on()
       pyb.delay(3)
       verde.off()
       uart.write('ready'+'\n') # uscito da standby - standby exit.
       while test==0:
          inBuffer_rx=""
          if uart.any(): 
             inBuffer_rx=uart.readline()
Example #6
0
# lead to some odd effects in time dependent applications.
# Note also that pyboard firmware briefly flashes the green LED on all wakeups.

import pyb, upower
tamper = upower.Tamper()
wkup = upower.wakeup_X1()
tamper.setup(level=0, freq=16, samples=2,
             edge=False)  # A level of zero triggers
#upower.tamper.setup(level = 1, edge = True) # Falling edge trigger. You must supply a pullup resistor.
rtc = pyb.RTC()

leds = tuple(pyb.LED(x) for x in range(1, 4))  # rgy
for led in leds:
    led.off()

reason = upower.why()  # Why have we woken?
if reason == 'BOOT':  # first boot or reset: yellow LED
    rtc.datetime((2015, 8, 6, 4, 13, 0, 0, 0))  # Code to run on 1st boot only
    upower.savetime()
    for led in leds:
        led.on()  # RGY == 1st boot
elif reason == 'POWERUP':  # with backup battery in place
    leds[2].on()  # Y only == subsequent boots (with battery)
elif reason == 'WAKEUP':  # green and yellow on timer wakeup
    leds[1].on()
    leds[2].on()
    upower.savetime()
elif reason == 'TAMPER':  # red
    leds[0].on()
elif reason == 'X1':  # red and green on X1 rising edge
    leds[0].on()