Skip to content

Commit b316b74

Browse files
committed
Update
1 parent 86ac460 commit b316b74

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

P1AM/P1AM.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
`P1AM`
66
================================================================================
77
8-
Library to interface with P1000 modules using a P1AM-200.
9-
10-
* Author(s): Adam Cummick, Tristan Warder, Michael Scott
8+
Library to interface with Productivity1000 modules.
119
"""
1210

1311
import time
@@ -23,7 +21,7 @@
2321
__repo__ = "https://github.com/facts-engineering/CircuitPython_P1AM.git"
2422

2523
def change_bit(original, bit, state):
26-
"""Change value of sepcific bit position"""
24+
"""Change value of specific bit position"""
2725
if state:
2826
return original | (1 << bit)
2927

@@ -86,19 +84,24 @@ def read_block(length: int, offset: int, block_type: int):
8684
_p1.write(msg)
8785
if _spi_timeout():
8886
with BC_SPI as _p1:
87+
time.sleep(0.00001)
8988
_p1.readinto(data_buf, start=0, end=length)
9089
_data_sync()
9190
data = int.from_bytes(data_buf[0:length], byte_order)
9291
return data
9392

94-
return False # no respsonse
93+
raise RuntimeError(
94+
"Check External Supply Connection\nNo Base Controller Activity"
95+
)
9596

9697

9798
def _data_sync(timeout=0.2):
98-
"""Wait for Base Contorller to update data"""
99+
"""Wait for Base Controller to update data"""
99100
start = time.monotonic()
101+
power_lost = 0
100102
while not ACK.value:
101103
if time.monotonic() - start > timeout:
104+
power_lost += 1
102105
break
103106

104107
start = time.monotonic()
@@ -109,8 +112,14 @@ def _data_sync(timeout=0.2):
109112
start = time.monotonic()
110113
while not ACK.value:
111114
if time.monotonic() - start > timeout:
115+
power_lost += 1
112116
break
113117

118+
if power_lost == 2:
119+
raise RuntimeError(
120+
"Check External Supply Connection\nNo Base Controller Activity"
121+
)
122+
114123

115124
def _spi_timeout(timeout=0.200):
116125
"""Wait for Base Controller to be ready for next transaction"""
@@ -1003,7 +1012,7 @@ def init(self): # pylint: disable=too-many-locals, too-many-statements
10031012

10041013
if _spi_timeout(5) is False:
10051014
raise RuntimeError(
1006-
"Check External Supply Connection\n" "No Base Controller Activity"
1015+
"Check External Supply Connection\nNo Base Controller Activity"
10071016
)
10081017

10091018
while (slots == 0 or slots > 15) and retry < 5:

examples/discrete_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
base = P1AM.Base() # Intializes base. Returns the base object.
3131
module = base[1] # module object for slot 1
3232
output = module[2] # 2nd channel object for our output module.
33-
# output = module.outputs[2] # on combos select inputs with module.outputs
33+
# output = module.outputs[2] # on combos select outputs with module.outputs
3434

3535
while True:
3636
output.value = True

examples/power_cycle.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Example: Power Cycle
3+
4+
5+
This example shows how to re-init the base after power has been cycled.
6+
7+
When powering your device with both USB and external power, this example can
8+
be used to catch and handle errors produced by a temporary loss of external 24V.
9+
10+
This example uses a discrete output module, but can be adapted to other modules.
11+
_____ _____
12+
| P || S |
13+
| 1 || L |
14+
| A || O |
15+
| M || T |
16+
| - || |
17+
| 2 || 0 |
18+
| 0 || 1 |
19+
| 0 || |
20+
¯¯¯¯¯ ¯¯¯¯¯
21+
Written by FACTS Engineering
22+
Copyright (c) 2023 FACTS Engineering, LLC
23+
Licensed under the MIT license.
24+
"""
25+
26+
import time
27+
import P1AM
28+
29+
base = P1AM.Base() # Intializes base. Returns the base object.
30+
module = base[1] # module object for slot 1
31+
output = module[2] # 2nd channel object for our output module.
32+
# output = module.outputs[2] # on combos select outputs with module.outputs
33+
34+
while True:
35+
try:
36+
# removing external 24V here will raise a RuntimeError
37+
output.value = True
38+
time.sleep(1)
39+
output.value = False
40+
time.sleep(1)
41+
except RuntimeError:
42+
while True:
43+
# loop until base is re-initialized
44+
try:
45+
base.init() # must be called if external 24V is interrupted
46+
break
47+
except RuntimeError:
48+
pass

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ readme = "README.md"
1313
authors = [
1414
{name = "Adam Cummick", email = "[email protected]"}
1515
]
16-
urls = {Homepage = "https://github.com/facts-engineering/CircuitPython_P1AM.git"}
16+
urls = {Homepage = "https://github.com/facts-engineering/CircuitPython_P1AM"}
1717
keywords = [
1818
"adafruit",
1919
"circuitpython",

0 commit comments

Comments
 (0)