-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmicropower.py
68 lines (60 loc) · 2.53 KB
/
micropower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# micropower.py Support for hardware capable of switching off the power
# for Pyboard peripherals
# 28th Aug 2015
# This code is released under the MIT licence
# version 0.45
# Copyright 2015 Peter Hinch
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language
# governing permissions and limitations under the License.
import pyb
class PowerController(object):
def __init__(self, pin_active_high, pin_active_low):
self.upcount = 0
if pin_active_low is not None: # Start with power down
self.al = pyb.Pin(pin_active_low, mode=pyb.Pin.OUT_PP)
self.al.high()
else:
self.al = None
if pin_active_high is not None: # and pullups disabled
self.ah = pyb.Pin(pin_active_high, mode=pyb.Pin.OUT_PP)
self.ah.low()
else:
self.ah = None
def __enter__(self): # use as context manager
self.power_up()
return self
def __exit__(self, *_):
self.power_down()
def power_up(self):
self.upcount += 1 # Cope with nested calls
if self.upcount == 1:
if self.ah is not None:
self.ah.high() # Enable I2C pullups
if self.al is not None:
self.al.low() # Power up
pyb.delay(10) # time for device to settle
def power_down(self):
if self.upcount > 1:
self.upcount -= 1
elif self.upcount == 1:
self.upcount = 0
if self.al is not None:
self.al.high() # Power off
pyb.delay(10) # Avoid glitches on switched
if self.ah is not None: # I2C bus while power decays
self.ah.low() # Disable I2C pullups
for bus in (pyb.SPI(1), pyb.SPI(2), pyb.I2C(1), pyb.I2C(2)):
bus.deinit() # I2C drivers seem to need this
@property
def single_ended(self):
return (self.ah is not None) and (self.al is not None)