Skip to content

Commit c6ceecf

Browse files
committed
WIP platform docs cleanup
- Break platform into 3 major sections - Introcude relevent concetps in those sections - Move detailed exampes into Callback docs
1 parent d939a5a commit c6ceecf

File tree

3 files changed

+181
-215
lines changed

3 files changed

+181
-215
lines changed

docs/reference/api/platform/ATCmdParser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## ATCmdParser
22

3-
ATCmdParser is an Mbed OS compatible AT command parser. AT commands are instructions used to communicate with a communication device such as a modem, phone or Wi-Fi module. Each command is a text string in ASCII format, and every command starts with "AT" characters followed by a command specifying the operation to be carried out.
3+
ATCmdParser is an Mbed OS compatible AT command parser and serializer. AT commands are instructions used to communicate with a communication device such as a modem, phone or Wi-Fi module. Each command is a text string in ASCII format, and every command starts with "AT" characters followed by a command specifying the operation to be carried out.
44

55
The ATCmdParser class in Mbed OS implements functionality to send and receive AT commands to devices capable of communicating using AT commands. The ATCmdParser internally uses the driver for the communication channel to talk to the device. It expects the driver to implement the FileHandle interface to invoke the functions on the driver.
66

docs/reference/api/platform/Callback.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,156 @@ Here is an example that uses everything discussed in the [introduction to callba
2727

2828
- [Platform overview](/docs/development/reference/platform.html).
2929
- [Introduction to callbacks](/docs/development/reference/platform.html#callbacks).
30+
31+
#### Create callbacks
32+
33+
First, you need to understand the syntax of the Callback type. The Callback type is a templated type parameterized by a C++ function declaration:
34+
35+
``` c++
36+
// Callback</*return type*/(/*parameters*/)> cb;
37+
Callback<int(float)> cb; // A callback that takes in a float and returns an int
38+
Callback<void(float)> cb; // A callback that takes in a float and returns nothing
39+
Callback<int()> cb; // A callback that takes in nothing and returns an int
40+
Callback<void(float, float)> cb; // A callback that takes in two floats and returns nothing
41+
```
42+
43+
You can create a Callback directly from a C function or function pointer with the same type:
44+
45+
``` c++
46+
void dosomething(int) {
47+
// do something
48+
}
49+
50+
Callback<void(int)> cb(dosomething);
51+
```
52+
53+
If an API provides a function that takes in a callback, you can pass in a C function or function pointer with the same type:
54+
55+
``` c++
56+
class ADC {
57+
// ADC can pass an analog value to the callback
58+
void attach(Callback<void(float)> cb);
59+
};
60+
61+
void dosomething(float f) {
62+
// do something
63+
}
64+
65+
ADC adc;
66+
adc.attach(dosomething);
67+
```
68+
69+
But what about state? The Callback type also supports passing a state pointer for a function. This state can be either a pointer to an object that is passed to a member function, or a pointer passed to a C-style function.
70+
71+
Because this form of creating Callbacks requires two arguments, you need to create the Callback explicitly using the Callback constructor. The Callback also comes with the lowercase callback function, which creates callbacks based on the arguments type and avoids the need to repeat the template type.
72+
73+
You can create a callback with a member function.
74+
75+
``` c++
76+
class Thing {
77+
int state;
78+
void catinthehat(int i) {
79+
state = // do something
80+
}
81+
}
82+
83+
// We can create a Callback with the Callback constructor
84+
Thing thing1;
85+
adc.attach(Callback<void(int)>(&thing1, &Thing::catinthehat));
86+
87+
// Or we can create a Callback with the callback function to avoid repeating ourselves
88+
Thing thing2;
89+
adc.attach(callback(&thing2, &Thing::catinthehat));
90+
```
91+
92+
Or you can pass the state to a C-style function.
93+
94+
``` c++
95+
struct thing_t {
96+
int state;
97+
}
98+
99+
void catinthehat(thing_t *thing, float f) {
100+
thing->state = // do something
101+
}
102+
103+
// We can create a Callback with the Callback constructor
104+
thing_t thing1;
105+
adc.attach(Callback<void(int)>(catinthehat, &thing1));
106+
107+
// Or we can create a Callback with the callback function to avoid repeating ourselves
108+
thing_t thing2;
109+
adc.attach(callback(catinthehat, &thing2));
110+
```
111+
112+
<span class="notes">**Note:** This state is restricted to a single pointer. This means you can’t bind both an object and argument to a callback.</span>
113+
114+
``` c++
115+
// Does not work
116+
adc.attach(callback(&thing, &Thing::dosomething, &arg));
117+
```
118+
119+
If you need to pass multiple arguments to a callback and you can’t store the arguments in the class, you can create a struct that contains all of the arguments and pass a pointer to that. However, you need to handle the memory allocation yourself.
120+
121+
``` c++
122+
// Create a struct that contains all of the state needed for “dosomething”
123+
struct dosomething_arguments {
124+
Thing *thing;
125+
int arg1;
126+
int arg2;
127+
};
128+
129+
// Create a function that calls “dosomething” with the arguments
130+
void dosomething_with_arguments(struct dosomething_arguments *args) {
131+
args->thing->dosomething(args->arg1, args->arg2);
132+
}
133+
134+
135+
// Allocate arguments and pass to callback
136+
struct dosomething_arguments args = { &thing, arg1, arg2 };
137+
adc.attach(callback(dosomething_with_arguments, &args)); // yes
138+
```
139+
140+
#### Call callbacks
141+
142+
Callbacks overload the function call operator, so you can call a Callback like you would a normal function:
143+
144+
```c++
145+
void callme(Callback<void(float)> cb) {
146+
cb(1.0f);
147+
}
148+
```
149+
150+
The only thing to watch out for is that the Callback type has a null Callback, just like a null function pointer. Uninitialized callbacks are null and assert if you call them. If you want a call to always succeed, you need to check if it is null first.
151+
152+
``` c++
153+
void callmemaybe(Callback<void(float)> cb) {
154+
if (cb) {
155+
cb(1.0f);
156+
}
157+
}
158+
```
159+
160+
The Callback class is what’s known in C++ as a “Concrete Type”. That is, the Callback class is lightweight enough to be passed around like an int, pointer or other primitive type.
161+
162+
```c++
163+
class Thing {
164+
private:
165+
Callback<void(int)> _cb;
166+
167+
public:
168+
void attach(Callback<void(int)> cb) {
169+
_cb = cb
170+
}
171+
172+
void dothething(int arg) {
173+
If (_cb) {
174+
_cb(arg);
175+
}
176+
}
177+
}
178+
```
179+
180+
<h4 id="the-importance-of-state">The importance of state</h4>
181+
182+
A callback is a user provided function that a user may pass to an API. The callback allows the API to execute the user’s code in its own context. You can find more information on how to use callbacks in the [technical callback documentation](/docs/development/reference/callback.html).

0 commit comments

Comments
 (0)