Skip to content

Commit 9beb4ef

Browse files
committed
add mode callback to readme
1 parent 2b51617 commit 9beb4ef

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

README.md

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The callback function is used to specify what should happen when a servo needs t
6969
7070
```ino
7171
void myServoCallback(byte servoID, int position) {
72-
// Do something
72+
// Do something
7373
}
7474
```
7575

@@ -82,7 +82,7 @@ This allows to implement any kind of logic to handle the actual servo control. W
8282
using namespace BlenderServoAnimation;
8383

8484
void move(byte servoID, int position) {
85-
// Do something
85+
// Do something
8686
}
8787

8888
Servo myBlenderServo(0, Bone, move);
@@ -126,16 +126,16 @@ Alternatively, we can also create an array of servos and call the `addServos` me
126126

127127
```ino
128128
Servo myBlenderServos[] = {
129-
Servo(0, BoneA, move),
130-
Servo(1, BoneB, move),
131-
Servo(2, BoneC, move),
132-
...
129+
Servo(0, BoneA, move),
130+
Servo(1, BoneB, move),
131+
Servo(2, BoneC, move),
132+
...
133133
}
134134

135135
Animation myBlenderAnimation(30, 1000);
136136

137137
void setup() {
138-
myBlenderAnimation.addServos(myBlenderServos);
138+
myBlenderAnimation.addServos(myBlenderServos);
139139
}
140140
```
141141
@@ -145,21 +145,21 @@ The animation needs to be triggered regularly in order to update its state and c
145145
146146
```ino
147147
void loop() {
148-
myBlenderAnimation.run();
148+
myBlenderAnimation.run();
149149
}
150150
```
151151

152152
### Animation Modes
153153

154-
At first, an animation will be in the default mode. In this mode, the animation is simply not playing and waiting until the mode is changed.
154+
At first, an animation will be in the default mode. In this mode, the animation is simply not doing anything and waits until the mode has changed.
155155

156-
| Mode | Description |
157-
|------|-------------|
158-
| default | Not playing / waiting |
159-
| play | Playing back the animation |
160-
| pause | Pausing the animation at the current frame |
161-
| stop | Slowly moving the servos to their neutral position |
162-
| live | Reading serial commands to move the servos in real-time |
156+
| Mode | Constant | Description |
157+
|------|----------|-------------|
158+
| default | MODE_DEFAULT | Not playing / waiting |
159+
| play | MODE_PLAY | Playing back the animation |
160+
| pause | MODE_PAUSE | Pausing the animation at the current frame |
161+
| stop | MODE_STOP | Slowly moving the servos to their neutral position |
162+
| live | MODE_LIVE | Reading serial commands to move the servos in real-time |
163163

164164
The modes can be changed or triggered via the following methods:
165165

@@ -174,13 +174,46 @@ myBlenderAnimation.live(stream);
174174
175175
When calling the `stop` method, it is possible to pass a delay in milliseconds. This delay will be used when the servos are moved to their neutral position during the stop mode. To get to the neutral position, the current position will either be increased or decreased by 1. The delay therefore controls how fast or smooth this movement will take place. The default value for this parameter is `20`.
176176

177+
```ino
178+
myBlenderAnimation.stop(); // Default reset speed applied
179+
myBlenderAnimation.stop(30); // Servos will reset slower
180+
```
181+
177182
To use the `live` method, we have to pass a stream instance which will be used for reading serial commands. For example, we can pass `Serial` if we want to use the standard USB connection of an Arduino compatible board:
178183

179184
```ino
180185
void setup() {
181-
Serial.begin(115200);
182-
myBlenderAnimation.live(Serial);
186+
Serial.begin(115200);
187+
myBlenderAnimation.live(Serial);
188+
}
189+
```
190+
191+
To get the current animation mode, we can simply call the `getMode` method. This will return a `byte` representing one of the mode constants mentioned in the table above. We can then compare the return value to those constants to act according to the current mode:
192+
193+
```ino
194+
byte currentMode = myBlenderAnimation.getMode();
195+
196+
switch (currentMode) {
197+
case Animation::MODE_DEFAULT:
198+
// Do something
199+
break;
200+
case Animation::MODE_PLAY:
201+
// Do something else
202+
break;
203+
...
204+
}
205+
```
206+
207+
On top of manually checking the animation mode, we can also register a callback function which is triggered as soon as the animation mode has changed. The function will receive both the previous mode and the new mode as `byte` values. To register the function, we can call the `onModeChange` method:
208+
209+
```ino
210+
void modeChanged(byte prevMode, byte newMode) {
211+
// Do something (e.g. using another switch)
212+
}
213+
214+
void setup() {
215+
myBlenderAnimation.onModeChange(modeChanged);
183216
}
184217
```
185218
186-
Make sure to check out the [examples](examples) to get started quickly and learn more about using this library with your Arduino sketches / programs.
219+
The [SwitchModeButton example](examples/SwitchModeButton) shows how to combine all mode methods to control an animation based on a single button. Make sure to also check out the other [examples](examples) to get started quickly.

0 commit comments

Comments
 (0)