@@ -148,61 +148,57 @@ Reading characteristic value
148148============================
149149
150150Let's get some data! To read the value of your characteristic, call the ``read() `` method. This method blocks
151- the calling thread until the data has been read from your characteristic and assigned to your variable.
151+ the calling thread until the data has been read from your characteristic and assigned to your variable. The read method
152+ is templated and supports multiple types described in the :ref: `supported-template-types `.
152153
153154.. code :: c++
154155
155- std::vector<std::byte> data = characteristic->read();
156+ std::vector<std::byte> data = characteristic->read<std::byte> ();
156157
157- There are additional read methods available to convert the payload into human-readable datatypes. If your device is sending
158- four bytes representing an integer , you can it directly as an integer instead of doing the conversion yourself :
158+ If you want something more human-readable, and you know your device's characteristic is represented in four bytes in
159+ little-endian order , you just just read your data as an integer:
159160
160161.. code :: c++
161162
162- int data = characteristic->read_int();
163-
163+ int data = characteristic->read<int>();
164164
165+ By default, you should read in your data into ``std::vector<std::byte> `` unless you know for sure your device is outputting
166+ convertible types.
165167
166168
167169Writing to characteristic
168170=========================
169171
170172There are two main options to write to your device. First we can ``write_without_response() `` which writes to your
171173devices asynchronously and does not block your calling thread. If your write fails, you will not get a message
172- telling you that it failed. You must provide this method the data as a `` std::vector<std::byte> ``
174+ telling you that it failed. This method is templated and supports multiple types described in the :ref: ` supported-template-types `.
173175
174176.. code :: c++
175177
176- characteristic->write_without_response(data);
178+ std::vector<std::byte> data = {...};
179+ characteristic->write_without_response<std::byte>(data);
177180
178181
179182And if you write with a response, then the method will block your calling thread and wait until your data has been
180183successfully written to the device.
181184
182185.. code :: c++
183186
184- rotate_char->write_with_response(data);
185-
186- There are convenience overloaded methods to write to your device if you'd rather send a human-readable datatype rather
187- than a vector of bytes. If you use these convenience methods, just make on the other end, your device is configured to handle
188- this information. These convenience methods assume little-endian ordering of bytes.
189-
190- .. code :: c++
187+ std::vector<std::byte> data = {...};
188+ rotate_char->write_with_response<std::byte>(data);
191189
192- std::string data = "Celcius";
193- switch_units_char->write_with_response(data);
190+ Like the read() method, you can write to your device using different formats that will end up being converted to byte arrays.
194191
195192
196193Notifying characteristic
197194========================
198195
199-
200196If your device and characteristic supports notifications, then let's use it. First, just double check that your characteristic
201197has notification support and that it's enabled. So now when your device sends your computer notifications with a data payload you can capture
202198that payload and write your own function to do something with it!
203199
204200Non-member function event handler
205- ----------------------------
201+ ---------------------------------
206202
207203Let's write a callback event handler that takes in a ``std::vector<std::byte> `` and enable notifications, passing the function as a parameter.
208204
@@ -220,7 +216,7 @@ IMPORTANT! All event handlers must follow this signature: ``void (std::vector<st
220216
221217
222218Member function event handler
223- ------------------------
219+ -----------------------------
224220
225221member functions are a little trickier to write, but you just have to bind their class to std::function
226222and add a placeholder parameter, then pass it like normal.
@@ -248,4 +244,12 @@ and add a placeholder parameter, then pass it like normal.
248244Passing member functions is really powerful because you can do things such as update an instance variable when notified.
249245
250246If you're passing the same function to multiple characteristic notifications, then just make sure your function contents are
251- thread-safe, this applies to both member and non-member functions.
247+ thread-safe, this applies to both member and non-member functions.
248+
249+ .. _supported-template-types :
250+
251+ .. include :: ../template_types.rst
252+
253+
254+
255+
0 commit comments