|
1 | | -# [Blues Wireless][blues] |
| 1 | +# note-python |
2 | 2 |
|
3 | | -The note-python Python library for communicating with Blues Wireless Notecard via serial or I²C. |
| 3 | +Python library for communicating with the Blues Wireless Notecard over serial or I²C. |
4 | 4 |
|
5 | | -This library allows you to control a Notecard by coding in Python. |
6 | | -Your program may configure Notecard and send Notes to [Notehub.io][notehub]. |
| 5 | + |
7 | 6 |
|
8 | | -See also: |
9 | | -* [note-c][note-c] for C bindings |
10 | | -* [note-go][note-go] for Go bindings |
| 7 | +This library allows you to control a Notecard by coding in Python and works in a desktop setting, on Single-Board Computers like the Raspberry Pi, and on Microcontrollers with MicroPython or CircuitPython support. |
11 | 8 |
|
12 | | -[blues]: https://blues.com |
13 | | -[notehub]: https://notehub.io |
14 | | -[note-arduino]: https://github.com/blues/note-arduino |
15 | | -[note-c]: https://github.com/blues/note-c |
16 | | -[note-go]: https://github.com/blues/note-go |
17 | | -[note-python]: https://github.com/blues/note-python |
| 9 | +## Installation |
| 10 | + |
| 11 | +With pi-py: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install note-python |
| 15 | +``` |
| 16 | + |
| 17 | +For use with MicroPython or CircuitPython, copy the contents of the `notecard` directory into the `lib` directory of your device. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```python |
| 22 | +import notecard |
| 23 | +``` |
| 24 | + |
| 25 | +The `note-python` library requires a pointer to a serial or i2c object that you initialize and pass into the library. This object differs based on platform, so consult the [examples](examples/) directory for platform-specific guidance. |
| 26 | + |
| 27 | +### Serial Configuration |
| 28 | + |
| 29 | +```python |
| 30 | +# Use python-periphery on a desktop or Raspberry Pi |
| 31 | +from periphery import Serial |
| 32 | +port = Serial("/dev/serial0", 9600) |
| 33 | + |
| 34 | +card = notecard.OpenSerial(port) |
| 35 | +``` |
| 36 | + |
| 37 | +### I2C Confgiguration |
| 38 | + |
| 39 | +```python |
| 40 | +# Use python-periphery on a desktop or Raspberry Pi |
| 41 | +from periphery import Serial |
| 42 | +port = I2C("/dev/i2c-1") |
| 43 | +card = notecard.OpenI2C(port, 0, 0) |
| 44 | +``` |
| 45 | + |
| 46 | +### Sending Notecard Requests |
| 47 | + |
| 48 | +Whether using Serial or I2C, sending Notecard requests and reading responses follows the same pattern: |
| 49 | + |
| 50 | +1. Create a JSON object that adheres to the Notecard API |
| 51 | +2. Call `card.Transaction`, `card.Request` or `card.RequestResponse` and pass in the request JSON object. |
| 52 | +3. Make sure the response contains the data you need |
| 53 | + |
| 54 | +```python |
| 55 | +# Construct a JSON Object to add a Note to the Notecard |
| 56 | +req = {"req": "note.add"} |
| 57 | +req["body"] = {"temp": 18.6} |
| 58 | + |
| 59 | +rsp = card.Transaction(req) |
| 60 | +print(rsp) # {"total":1} |
| 61 | +``` |
| 62 | + |
| 63 | +### Using the Library Fluent API |
| 64 | + |
| 65 | +The `notecard` class allows complete access to the Notecard API via manual JSON object construction and the `Transaction`, `Request`, and `RequestResponse` methods. Alternatively, you can import one or more Fluent API helpers to work with common aspects of the Notecard API without having to author JSON objects, by hand. **Note** that not all aspects of the Notecard API are available using these helpers. For a complete list of supported helpers, visit the [API](API.md) doc. |
| 66 | + |
| 67 | +Here's an example that uses the `hub` helper to set the Notecard Product UID in CircuitPython: |
| 68 | + |
| 69 | +```python |
| 70 | +import board |
| 71 | +import busio |
| 72 | + |
| 73 | +import notecard |
| 74 | +from notecard import card, hub, note |
| 75 | + |
| 76 | +port = busio.I2C(board.SCL, board.SDA) |
| 77 | +nCard = notecard.OpenI2C(port, 0, 0, debug=True) |
| 78 | + |
| 79 | +productUID = "com.blues.brandon.tester" |
| 80 | +rsp = hub.set(nCard, productUID, mode="continuous", sync=True) |
| 81 | + |
| 82 | +print(rsp) # {} |
| 83 | +``` |
| 84 | + |
| 85 | +## Documentation |
| 86 | + |
| 87 | +The documentation for this library can be found at the Blues Wireless [wireless.dev](https//wireless.dev/reference/note-python) site. |
| 88 | + |
| 89 | +## Examples |
| 90 | + |
| 91 | +The [examples](examples/) directory contains examples for using this library with: |
| 92 | + |
| 93 | +- [Serial](examples/serial-example.py) |
| 94 | +- [I2C](examples/i2c-example.py) |
| 95 | +- [RaspberryPi](examples/rpi-example.py) |
| 96 | +- [CircuitPython](examples/cpy-example.py) |
| 97 | +- [MicroPython](examples/mpy-example.py) |
| 98 | + |
| 99 | +## Contributing |
| 100 | + |
| 101 | +We love issues, fixes, and pull requests from everyone. By participating in this project, you agree to abide by the Blues Inc [code of conduct]. |
| 102 | + |
| 103 | +For details on contributions we accept and the process for contributing, see our [contribution guide](CONTRIBUTING.md) |
| 104 | + |
| 105 | +## Running the Tests |
| 106 | + |
| 107 | +If you're planning to contribute to this repo, please be sure to run the tests before submitting a PR. First run: |
| 108 | + |
| 109 | +```bash |
| 110 | +pip install -r requirements.txt |
| 111 | +``` |
| 112 | + |
| 113 | +Then, run the linter and tests using the included `MAKEFILE`. |
| 114 | + |
| 115 | +```bash |
| 116 | +make -f MAKEFILE |
| 117 | +``` |
| 118 | + |
| 119 | +Alternatively, you can inspect the contents of the [MAKEFILE](MAKEFILE) and run `flake8` and `pytest` directly. Be aware, however, that the commands in the `MAKEFILE` run against every pull request, so your best bet is to ensure these tests are successful before submitting your PR. |
| 120 | + |
| 121 | +## More Information |
| 122 | + |
| 123 | +For additional Notecard SDKs and Libraries, see: |
| 124 | + |
| 125 | +* [note-c](note-c) for Standard C support |
| 126 | +* [note-go](note-go) for Go |
| 127 | +* [note-arduino](note-arduino) for Arduino |
| 128 | + |
| 129 | +## To learn more about Blues Wireless, the Notecard and Notehub, see: |
| 130 | + |
| 131 | +* [blues.com](https://blues.com) |
| 132 | +* [notehub.io][Notehub] |
| 133 | +* [wireless.dev](https://wireless.dev) |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +Copyright (c) 2019 Blues Inc. Released under the MIT license. See [LICENSE](LICENSE) for details. |
| 138 | + |
| 139 | +[code of conduct]: https://blues.github.io/opensource/code-of-conduct |
| 140 | +[Notehub]: https://notehub.io |
0 commit comments