Skip to content

Commit 5e00215

Browse files
authored
Merge pull request #36 from blues/brandon-real-python-samples
feat:add fleshed-out samples across platforms
2 parents 4ea5e11 + 1821639 commit 5e00215

File tree

9 files changed

+337
-53
lines changed

9 files changed

+337
-53
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ With `pip` via PyPi:
1717
pip install note-python
1818
```
1919

20-
or
20+
or
2121

2222

2323
```bash
@@ -102,11 +102,11 @@ The documentation for this library can be found
102102

103103
The [examples](examples/) directory contains examples for using this library with:
104104

105-
- [Serial](examples/serial-example.py)
106-
- [I2C](examples/i2c-example.py)
107-
- [RaspberryPi](examples/rpi-example.py)
108-
- [CircuitPython](examples/cpy-example.py)
109-
- [MicroPython](examples/mpy-example.py)
105+
- [Serial](examples/notecard-basics/serial-example.py)
106+
- [I2C](examples/notecard-basics/i2c-example.py)
107+
- [RaspberryPi](examples/notecard-basics/rpi-example.py)
108+
- [CircuitPython](examples/notecard-basics/cpy-example.py)
109+
- [MicroPython](examples/notecard-basics/mpy-example.py)
110110

111111
## Contributing
112112

examples/cpy-example.py renamed to examples/notecard-basics/cpy-example.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import time
88
import notecard
99

10+
productUID = "com.your-company.your-project"
11+
1012
# Choose either UART or I2C for Notecard
1113
use_uart = True
1214

@@ -31,22 +33,48 @@ def NotecardExceptionInfo(exception):
3133
+ ": " + ' '.join(map(str, exception.args))
3234

3335

34-
def transactionTest(card):
36+
def configure_notecard(card):
3537
"""Submit a simple JSON-based request to the Notecard.
3638
3739
Args:
3840
card (object): An instance of the Notecard class
3941
4042
"""
41-
req = {"req": "card.status"}
43+
req = {"req": "hub.set"}
44+
req["product"] = productUID
45+
req["mode"] = "continuous"
4246

4347
try:
48+
card.Transaction(req)
49+
except Exception as exception:
50+
print("Transaction error: " + NotecardExceptionInfo(exception))
51+
time.sleep(5)
52+
53+
54+
def get_temp_and_voltage(card):
55+
"""Submit a simple JSON-based request to the Notecard.
56+
57+
Args:
58+
card (object): An instance of the Notecard class
59+
60+
"""
61+
temp = 0
62+
voltage = 0
63+
64+
try:
65+
req = {"req": "card.temp"}
66+
rsp = card.Transaction(req)
67+
temp = rsp["value"]
68+
69+
req = {"req": "card.voltage"}
4470
rsp = card.Transaction(req)
45-
print(rsp)
71+
voltage = rsp["value"]
4672
except Exception as exception:
4773
print("Transaction error: " + NotecardExceptionInfo(exception))
4874
time.sleep(5)
4975

76+
return temp, voltage
77+
5078

5179
def main():
5280
"""Connect to Notcard and run a transaction test."""
@@ -63,18 +91,26 @@ def main():
6391
print("Opening Notecard...")
6492
try:
6593
if use_uart:
66-
card = notecard.OpenSerial(port)
94+
card = notecard.OpenSerial(port, debug=True)
6795
else:
68-
card = notecard.OpenI2C(port, 0, 0)
96+
card = notecard.OpenI2C(port, 0, 0, debug=True)
6997
except Exception as exception:
7098
raise Exception("error opening notecard: "
7199
+ NotecardExceptionInfo(exception))
72100

73-
# If success, do a transaction loop
74-
print("Performing Transactions...")
75-
while True:
76-
time.sleep(2)
77-
transactionTest(card)
101+
# If success, configure the Notecard and send some data
102+
configure_notecard(card)
103+
temp, voltage = get_temp_and_voltage(card)
104+
105+
req = {"req": "note.add"}
106+
req["sync"] = True
107+
req["body"] = {"temp": temp, "voltage": voltage}
108+
109+
try:
110+
card.Transaction(req)
111+
except Exception as exception:
112+
print("Transaction error: " + NotecardExceptionInfo(exception))
113+
time.sleep(5)
78114

79115

80116
main()

examples/i2c-example.py renamed to examples/notecard-basics/i2c-example.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import notecard # noqa: E402
1515

16+
productUID = "com.your-company.your-project"
17+
1618
if sys.implementation.name != 'cpython':
1719
raise Exception("Please run this example in a CPython environment.")
1820

@@ -33,22 +35,48 @@ def NotecardExceptionInfo(exception):
3335
return "line " + s1 + ": " + s2 + ": " + ' '.join(map(str, exception.args))
3436

3537

36-
def transactionTest(card):
38+
def configure_notecard(card):
3739
"""Submit a simple JSON-based request to the Notecard.
3840
3941
Args:
4042
card (object): An instance of the Notecard class
4143
4244
"""
43-
req = {"req": "card.status"}
45+
req = {"req": "hub.set"}
46+
req["product"] = productUID
47+
req["mode"] = "continuous"
4448

4549
try:
50+
card.Transaction(req)
51+
except Exception as exception:
52+
print("Transaction error: " + NotecardExceptionInfo(exception))
53+
time.sleep(5)
54+
55+
56+
def get_temp_and_voltage(card):
57+
"""Submit a simple JSON-based request to the Notecard.
58+
59+
Args:
60+
card (object): An instance of the Notecard class
61+
62+
"""
63+
temp = 0
64+
voltage = 0
65+
66+
try:
67+
req = {"req": "card.temp"}
68+
rsp = card.Transaction(req)
69+
temp = rsp["value"]
70+
71+
req = {"req": "card.voltage"}
4672
rsp = card.Transaction(req)
47-
print(rsp)
73+
voltage = rsp["value"]
4874
except Exception as exception:
4975
print("Transaction error: " + NotecardExceptionInfo(exception))
5076
time.sleep(5)
5177

78+
return temp, voltage
79+
5280

5381
def main():
5482
"""Connect to Notcard and run a transaction test."""
@@ -61,16 +89,24 @@ def main():
6189

6290
print("Opening Notecard...")
6391
try:
64-
card = notecard.OpenI2C(port, 0, 0)
92+
card = notecard.OpenI2C(port, 0, 0, debug=True)
6593
except Exception as exception:
6694
raise Exception("error opening notecard: "
6795
+ NotecardExceptionInfo(exception))
6896

69-
# If success, do a transaction loop
70-
print("Performing Transactions...")
71-
while True:
72-
time.sleep(2)
73-
transactionTest(card)
97+
# If success, configure the Notecard and send some data
98+
configure_notecard(card)
99+
temp, voltage = get_temp_and_voltage(card)
100+
101+
req = {"req": "note.add"}
102+
req["sync"] = True
103+
req["body"] = {"temp": temp, "voltage": voltage}
104+
105+
try:
106+
card.Transaction(req)
107+
except Exception as exception:
108+
print("Transaction error: " + NotecardExceptionInfo(exception))
109+
time.sleep(5)
74110

75111

76112
main()

examples/mpy-example.py renamed to examples/notecard-basics/mpy-example.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import time
88
import notecard
99

10+
productUID = "com.your-company.your-project"
11+
1012
# Choose either UART or I2C for Notecard
1113
use_uart = True
1214

@@ -31,22 +33,48 @@ def NotecardExceptionInfo(exception):
3133
+ ' '.join(map(str, exception.args))
3234

3335

34-
def transactionTest(card):
36+
def configure_notecard(card):
3537
"""Submit a simple JSON-based request to the Notecard.
3638
3739
Args:
3840
card (object): An instance of the Notecard class
3941
4042
"""
41-
req = {"req": "card.status"}
43+
req = {"req": "hub.set"}
44+
req["product"] = productUID
45+
req["mode"] = "continuous"
4246

4347
try:
48+
card.Transaction(req)
49+
except Exception as exception:
50+
print("Transaction error: " + NotecardExceptionInfo(exception))
51+
time.sleep(5)
52+
53+
54+
def get_temp_and_voltage(card):
55+
"""Submit a simple JSON-based request to the Notecard.
56+
57+
Args:
58+
card (object): An instance of the Notecard class
59+
60+
"""
61+
temp = 0
62+
voltage = 0
63+
64+
try:
65+
req = {"req": "card.temp"}
66+
rsp = card.Transaction(req)
67+
temp = rsp["value"]
68+
69+
req = {"req": "card.voltage"}
4470
rsp = card.Transaction(req)
45-
print(rsp)
71+
voltage = rsp["value"]
4672
except Exception as exception:
4773
print("Transaction error: " + NotecardExceptionInfo(exception))
4874
time.sleep(5)
4975

76+
return temp, voltage
77+
5078

5179
def main():
5280
"""Connect to Notcard and run a transaction test."""
@@ -65,18 +93,26 @@ def main():
6593
print("Opening Notecard...")
6694
try:
6795
if use_uart:
68-
card = notecard.OpenSerial(port)
96+
card = notecard.OpenSerial(port, debug=True)
6997
else:
70-
card = notecard.OpenI2C(port, 0, 0)
98+
card = notecard.OpenI2C(port, 0, 0, debug=True)
7199
except Exception as exception:
72100
raise Exception("error opening notecard: "
73101
+ NotecardExceptionInfo(exception))
74102

75-
# If success, do a transaction loop
76-
print("Performing Transactions...")
77-
while True:
78-
time.sleep(2)
79-
transactionTest(card)
103+
# If success, configure the Notecard and send some data
104+
configure_notecard(card)
105+
temp, voltage = get_temp_and_voltage(card)
106+
107+
req = {"req": "note.add"}
108+
req["sync"] = True
109+
req["body"] = {"temp": temp, "voltage": voltage}
110+
111+
try:
112+
card.Transaction(req)
113+
except Exception as exception:
114+
print("Transaction error: " + NotecardExceptionInfo(exception))
115+
time.sleep(5)
80116

81117

82118
main()

0 commit comments

Comments
 (0)