|
1 | 1 | # Nuimo Python SDK |
2 | 2 |
|
| 3 | +The **Nuimo** SDK is a single Python source file. It has been tested with Python 2.7 and Python 3.4. |
| 4 | + |
3 | 5 | ## Installation |
4 | 6 | These instructions assume a Debian-based Linux. |
5 | 7 |
|
@@ -51,122 +53,43 @@ sh examples/install.sh scan |
51 | 53 | ``` |
52 | 54 | sh examples/install.sh connect |
53 | 55 | ``` |
54 | | -### 2. Install Pygattlib |
55 | | -[Pygattlib](https://bitbucket.org/OscarAcena/pygattlib) is a Python library to use the GATT Protocol for Bluetooth LE devices. It is a wrapper around the implementation used by gatttool in the bluez package. Unlike some other Python Bluetooth libraries, Pygattlib does not need invoke any external programs. |
56 | | - |
57 | | -**Known Issues** |
58 | | -Pygattlib may not be reliable on your platform. We are investigating these issues at Senic. |
59 | | -1. The library sometimes appears to get 'stuck', especially when executing `discover_characteristics`. |
60 | | - |
61 | | -To install Pygattlib automatically run the following commands. The steps are also described below should you wish to follow them manually. |
62 | | -``` |
63 | | -sh examples/install.sh pygattlib # For Python 2.x |
64 | | -sh examples/install.sh py3gattlib # For Python 3.x |
65 | | -``` |
66 | | -#### Install the dependencies |
67 | | -1. `sudo apt-get install pkg-config libboost-python-dev libboost-thread-dev libbluetooth-dev libglib2.0-dev python-dev python-setuptools` |
68 | | - |
69 | | -#### Installing Pygattlib |
70 | | -1. `hg clone https://bitbucket.org/OscarAcena/pygattlib` |
71 | | -2. `cd pygattlib` |
72 | | -3. `sudo python setup.py install` (Installs **gattlib.so** to **/usr/local/lib/python2.7/dist-packages**) |
73 | | -4. `sudo python3 setup.py install` (Installs **gattlib.cpython-34m.so** and support files to **/usr/local/lib/python3.4/dist-packages/gattlib*.egg**) |
74 | | - |
75 | | -### 3. Install Nuimo Python SDK |
76 | | -1. `cp nuimo.py <your project directory> # The Nuimo SDK is a single file` |
77 | | - |
78 | | -## Usage |
79 | | -The **Nuimo** SDK is a single Python source file. It has been tested with Python 2.7 and Python 3.4. |
80 | | - |
81 | | -#### Testing |
82 | | -To test, run the following command (note that it must be run as root because on Linux, Bluetooth discovery is a restricted operation). |
83 | | -``` |
84 | | -sudo PYTHONPATH=. python examples/test.py |
85 | | -``` |
86 | | -#### Usage |
87 | | -```python |
88 | | - |
89 | | -import time |
90 | | -import sys |
91 | | -from nuimo import NuimoDiscoveryManager |
92 | | - |
93 | | - |
94 | | -def main(): |
95 | | - # Uncomment the next 2 lines to enable detailed logging |
96 | | - # import logging |
97 | | - # logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) |
98 | | - |
99 | | - # Discover Nuimo Controllers |
100 | | - # Note: |
101 | | - # 1) Discovery is a synchronous operation i.e. other Python activity is paused |
102 | | - # 2) Discovery must be run as the root user |
103 | | - # 3) If the Nuimo MAC address is known, the NuimoController can be instantiated directly. |
104 | | - # For example: |
105 | | - # nuimo = NuimoController('D0:DF:D2:8F:49:B6') |
106 | | - |
107 | | - adapter = 'hci0' # Typical bluetooth adapter name |
108 | | - nuimo_manager = NuimoDiscoveryManager(bluetooth_adapter=adapter, delegate=DiscoveryLogger()) |
109 | | - nuimo_manager.start_discovery() |
110 | | - |
111 | | - # Were any Nuimos found? |
112 | | - if len(nuimo_manager.nuimos) == 0: |
113 | | - print('No Nuimos detected') |
114 | | - sys.exit(0) |
115 | | - |
116 | | - # Take the first Nuimo found. |
117 | | - nuimo = nuimo_manager.nuimos[0] |
118 | 56 |
|
119 | | - # Set up handling of Nuimo events. |
120 | | - # In this case just log each incoming event. |
121 | | - # NuimoLogger is defined below. |
122 | | - nuimo_event_delegate = NuimoLogger() |
123 | | - nuimo.set_delegate(nuimo_event_delegate) |
| 57 | +### 2. Install Nuimo Python SDK |
124 | 58 |
|
125 | | - # Attach to the Nuimo. |
126 | | - nuimo.connect() |
| 59 | +#### Install the system dependencies |
127 | 60 |
|
128 | | - # Display an icon for 2 seconds |
129 | | - interval = 2.0 |
130 | | - print("Displaying LED Matrix...") |
131 | | - nuimo.write_matrix(MATRIX_SHUFFLE, interval) |
| 61 | + sudo apt-get install build-essential pkg-config libboost-python-dev libboost-thread-dev libbluetooth-dev libglib2.0-dev python-dev python-setuptools` |
132 | 62 |
|
133 | | - # Nuimo events are dispatched in the background |
134 | | - time.sleep(100000) |
| 63 | +#### Install Nuimo SDK & it's Python dependencies |
135 | 64 |
|
136 | | - nuimo.disconnect() |
| 65 | +If you're using Python 2.X run: |
137 | 66 |
|
138 | | -# Example matrix for the Nuimo display |
139 | | -# Must be 9x9 characters. |
140 | | -MATRIX_SHUFFLE = ( |
141 | | - " " + |
142 | | - " " + |
143 | | - " .. .. " + |
144 | | - " . . " + |
145 | | - " . " + |
146 | | - " . . " + |
147 | | - " .. .. " + |
148 | | - " " + |
149 | | - " ") |
| 67 | + sudo python setup.py install |
150 | 68 |
|
| 69 | +If you're using Python 3.X run: |
151 | 70 |
|
152 | | -class DiscoveryLogger: |
153 | | - """ Handle Nuimo Discovery callbacks. """ |
154 | | - def controller_added(self, nuimo): |
155 | | - print("added Nuimo: {}".format(nuimo)) |
| 71 | + sudo python3 setup.py install |
156 | 72 |
|
| 73 | +This will install Nuimo SDK package and it's dependency Gattlib (see |
| 74 | +note about Gattlib below) to Python package directory. |
157 | 75 |
|
158 | | -class NuimoLogger: |
159 | | - """ Handle Nuimo Controller event callbacks by printing the events. """ |
| 76 | +#### Running the example script |
160 | 77 |
|
161 | | - def received_gesture_event(self, event): |
162 | | - print("received event: name={}, gesture_id={}, value={}".format(event.name, event.gesture, event.value)) |
163 | | - |
164 | | -if __name__ == '__main__': |
165 | | - main() |
| 78 | +To test if your setup is working, run the following command (note that it must be run as root because on Linux, Bluetooth discovery is a restricted operation). |
166 | 79 |
|
167 | 80 | ``` |
| 81 | +sudo python examples/test.py |
| 82 | +``` |
| 83 | + |
| 84 | +You can find the example script here: [examples/test.py](/examples/test.py) |
168 | 85 |
|
169 | 86 | #### Tested on |
170 | 87 | 1. Raspberry Pi Model 3 - Raspbian Jessie Full (raspberrypi 4.1.18) |
171 | 88 | 2. Linux Mint 17.3 Rosa |
172 | 89 |
|
| 90 | +### Note about Pygattlib |
| 91 | +[Pygattlib](https://bitbucket.org/OscarAcena/pygattlib) is a Python library to use the GATT Protocol for Bluetooth LE devices. It is a wrapper around the implementation used by gatttool in the bluez package. Unlike some other Python Bluetooth libraries, Pygattlib does not need invoke any external programs. |
| 92 | + |
| 93 | +**Known Issues** |
| 94 | +Pygattlib may not be reliable on your platform. We are investigating these issues at Senic. |
| 95 | +1. The library sometimes appears to get 'stuck', especially when executing `discover_characteristics`. |
0 commit comments