-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtransaction_manager.py
More file actions
81 lines (61 loc) · 2.58 KB
/
transaction_manager.py
File metadata and controls
81 lines (61 loc) · 2.58 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Classes for managing Notecard transactions.
This module provides transaction management for Notecard communications,
implementing the RTX/CTX protocol for reliable data exchange between
the host and Notecard.
"""
import sys
import time
from notecard.timeout import start_timeout, has_timed_out
from notecard.gpio import GPIO
class TransactionManager:
"""Class for managing the start and end of Notecard transactions.
Some Notecards need to be signaled via GPIO when a transaction is about to
start. When the Notecard sees a particular GPIO, called RTX (ready to
transact), go high, it responds with a high pulse on another GPIO, CTX
(clear to transact). At this point, the transaction can proceed. This class
implements this protocol in its start method.
"""
def __init__(self, rtx_pin, ctx_pin):
"""Initialize the TransactionManager.
Even though RTX is an output, we set it as an input here to conserve
power until we need to use it.
"""
self.rtx_pin = GPIO.setup(rtx_pin, GPIO.IN)
self.ctx_pin = GPIO.setup(ctx_pin, GPIO.IN)
def start(self, timeout_secs):
"""Prepare the Notecard for a transaction."""
start = start_timeout()
self.rtx_pin.direction(GPIO.OUT)
self.rtx_pin.value(1)
# If the Notecard supports RTX/CTX, it'll pull CTX low. If the Notecard
# doesn't support RTX/CTX, this pull up will make sure we get the clear
# to transact immediately.
self.ctx_pin.pull(GPIO.PULL_UP)
# Wait for the Notecard to signal clear to transact (i.e. drive the CTX
# pin HIGH). Time out after timeout_secs seconds.
while True:
if self.ctx_pin.value():
break
if (has_timed_out(start, timeout_secs)):
# Abandon request on timeout.
self.stop()
raise Exception(
"Timed out waiting for Notecard to give clear to transact."
)
time.sleep(.001)
self.ctx_pin.pull(GPIO.PULL_NONE)
def stop(self):
"""Make RTX an input to conserve power and remove the pull up on CTX."""
self.rtx_pin.direction(GPIO.IN)
self.ctx_pin.pull(GPIO.PULL_NONE)
class NoOpTransactionManager:
"""Class for transaction start/stop when no transaction pins are set.
If the transaction pins aren't set, the start and stop operations should be
no-ops.
"""
def start(self, timeout_secs):
"""No-op start function."""
pass
def stop(self):
"""No-op stop function."""
pass