You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/api/platform/ATCmdParser.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
## ATCmdParser
2
2
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.
4
4
5
5
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.
-[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
+
voiddosomething(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
+
classThing {
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
// 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>
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
+
structdosomething_arguments {
124
+
Thing *thing;
125
+
int arg1;
126
+
int arg2;
127
+
};
128
+
129
+
// Create a function that calls “dosomething” with the arguments
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
+
voidcallmemaybe(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
+
<h4id="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