├── .clang-format
├── .github
└── workflows
│ └── ci.yml
├── .gitignore
├── .vscode
├── extensions.json
└── settings.json
├── LICENSE
├── README.md
├── examples
├── AdafruitPCA9685
│ ├── AdafruitPCA9685.ino
│ ├── README.md
│ ├── ik.h
│ └── platformio.ini
├── MultiplePCA9685
│ ├── MultiplePCA9685.ino
│ ├── README.md
│ ├── ik.h
│ └── platformio.ini
├── MultipleScenes
│ ├── MultipleScenes.ino
│ ├── README.md
│ ├── platformio.ini
│ ├── scene-a.h
│ └── scene-b.h
├── MultipleScenesSD
│ ├── MultipleScenesSD.ino
│ ├── README.md
│ ├── platformio.ini
│ ├── scene-a.bin
│ └── scene-b.bin
├── SDAnimation
│ ├── README.md
│ ├── SDAnimation.ino
│ ├── platformio.ini
│ └── simple.bin
├── SerialLiveMode
│ ├── README.md
│ ├── SerialLiveMode.ino
│ └── platformio.ini
├── StandardServoLib
│ ├── README.md
│ ├── StandardServoLib.ino
│ ├── platformio.ini
│ └── simple.h
├── SwitchModeButton
│ ├── README.md
│ ├── SwitchModeButton.ino
│ ├── platformio.ini
│ └── simple.h
└── WebSocketLiveMode
│ ├── README.md
│ ├── WebSocketLiveMode.ino
│ └── platformio.ini
├── images
├── arduino-nano-with-2-PCA9685.png
├── arduino-nano-with-PCA9685.png
├── arduino-nano-with-sd-module.png
├── arduino-nano-with-servo-and-button.png
├── arduino-nano-with-servo.png
└── esp32-with-servo.png
├── library.properties
├── platformio.ini
├── requirements-dev.txt
├── src
├── AnimationData.cpp
├── AnimationData.h
├── BlenderServoAnimation.cpp
├── BlenderServoAnimation.h
├── Command.cpp
├── Command.h
├── CommonTypes.h
├── Scene.cpp
├── Scene.h
├── Servo.cpp
├── Servo.h
├── ServoManager.cpp
└── ServoManager.h
└── test
├── animation
├── test_live
│ └── test_live.cpp
├── test_loop
│ └── test_loop.cpp
├── test_mode_callback
│ └── test_mode_callback.cpp
├── test_pause
│ └── test_pause.cpp
├── test_play
│ └── test_play.cpp
├── test_play_random
│ └── test_play_random.cpp
├── test_play_single
│ └── test_play_single.cpp
├── test_scene_callback
│ └── test_scene_callback.cpp
└── test_stop
│ └── test_stop.cpp
├── helper.h
├── test_animation_data
└── test_animation_data.cpp
├── test_command
└── test_command.cpp
├── test_scene
└── test_scene.cpp
├── test_servo
└── test_servo.cpp
└── test_servo_manager
└── test_servo_manager.cpp
/.clang-format:
--------------------------------------------------------------------------------
1 | ---
2 | Language: Cpp
3 | # BasedOnStyle: LLVM
4 | AccessModifierOffset: -2
5 | AlignAfterOpenBracket: Align
6 | AlignArrayOfStructures: None
7 | AlignConsecutiveMacros: None
8 | AlignConsecutiveAssignments: None
9 | AlignConsecutiveBitFields: None
10 | AlignConsecutiveDeclarations: None
11 | AlignEscapedNewlines: Right
12 | AlignOperands: Align
13 | AlignTrailingComments: true
14 | AllowAllArgumentsOnNextLine: true
15 | AllowAllConstructorInitializersOnNextLine: true
16 | AllowAllParametersOfDeclarationOnNextLine: true
17 | AllowShortEnumsOnASingleLine: true
18 | AllowShortBlocksOnASingleLine: Never
19 | AllowShortCaseLabelsOnASingleLine: false
20 | AllowShortFunctionsOnASingleLine: false
21 | AllowShortLambdasOnASingleLine: All
22 | AllowShortIfStatementsOnASingleLine: Never
23 | AllowShortLoopsOnASingleLine: false
24 | AlwaysBreakAfterDefinitionReturnType: None
25 | AlwaysBreakAfterReturnType: None
26 | AlwaysBreakBeforeMultilineStrings: false
27 | AlwaysBreakTemplateDeclarations: MultiLine
28 | AttributeMacros:
29 | - __capability
30 | BinPackArguments: true
31 | BinPackParameters: true
32 | BraceWrapping:
33 | AfterCaseLabel: false
34 | AfterClass: false
35 | AfterControlStatement: Never
36 | AfterEnum: false
37 | AfterFunction: false
38 | AfterNamespace: false
39 | AfterObjCDeclaration: false
40 | AfterStruct: false
41 | AfterUnion: false
42 | AfterExternBlock: false
43 | BeforeCatch: false
44 | BeforeElse: false
45 | BeforeLambdaBody: false
46 | BeforeWhile: false
47 | IndentBraces: false
48 | SplitEmptyFunction: true
49 | SplitEmptyRecord: true
50 | SplitEmptyNamespace: true
51 | BreakBeforeBinaryOperators: None
52 | BreakBeforeConceptDeclarations: true
53 | BreakBeforeBraces: Attach
54 | BreakBeforeInheritanceComma: false
55 | BreakInheritanceList: BeforeColon
56 | BreakBeforeTernaryOperators: true
57 | BreakConstructorInitializersBeforeComma: false
58 | BreakConstructorInitializers: BeforeColon
59 | BreakAfterJavaFieldAnnotations: false
60 | BreakStringLiterals: true
61 | ColumnLimit: 100
62 | CommentPragmas: '^ IWYU pragma:'
63 | CompactNamespaces: false
64 | ConstructorInitializerAllOnOneLineOrOnePerLine: false
65 | ConstructorInitializerIndentWidth: 4
66 | ContinuationIndentWidth: 4
67 | Cpp11BracedListStyle: true
68 | DeriveLineEnding: true
69 | DerivePointerAlignment: false
70 | DisableFormat: false
71 | EmptyLineAfterAccessModifier: Never
72 | EmptyLineBeforeAccessModifier: LogicalBlock
73 | ExperimentalAutoDetectBinPacking: false
74 | FixNamespaceComments: true
75 | ForEachMacros:
76 | - foreach
77 | - Q_FOREACH
78 | - BOOST_FOREACH
79 | IfMacros:
80 | - KJ_IF_MAYBE
81 | IncludeBlocks: Preserve
82 | IncludeCategories:
83 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
84 | Priority: 2
85 | SortPriority: 0
86 | CaseSensitive: false
87 | - Regex: '^(<|"(gtest|gmock|isl|json)/)'
88 | Priority: 3
89 | SortPriority: 0
90 | CaseSensitive: false
91 | - Regex: '.*'
92 | Priority: 1
93 | SortPriority: 0
94 | CaseSensitive: false
95 | IncludeIsMainRegex: '(Test)?$'
96 | IncludeIsMainSourceRegex: ''
97 | IndentAccessModifiers: false
98 | IndentCaseLabels: false
99 | IndentCaseBlocks: false
100 | IndentGotoLabels: true
101 | IndentPPDirectives: None
102 | IndentExternBlock: AfterExternBlock
103 | IndentRequires: false
104 | IndentWidth: 2
105 | IndentWrappedFunctionNames: false
106 | InsertTrailingCommas: None
107 | JavaScriptQuotes: Leave
108 | JavaScriptWrapImports: true
109 | KeepEmptyLinesAtTheStartOfBlocks: true
110 | LambdaBodyIndentation: Signature
111 | MacroBlockBegin: ''
112 | MacroBlockEnd: ''
113 | MaxEmptyLinesToKeep: 1
114 | NamespaceIndentation: None
115 | ObjCBinPackProtocolList: Auto
116 | ObjCBlockIndentWidth: 2
117 | ObjCBreakBeforeNestedBlockParam: true
118 | ObjCSpaceAfterProperty: false
119 | ObjCSpaceBeforeProtocolList: true
120 | PenaltyBreakAssignment: 2
121 | PenaltyBreakBeforeFirstCallParameter: 19
122 | PenaltyBreakComment: 300
123 | PenaltyBreakFirstLessLess: 120
124 | PenaltyBreakString: 1000
125 | PenaltyBreakTemplateDeclaration: 10
126 | PenaltyExcessCharacter: 1000000
127 | PenaltyReturnTypeOnItsOwnLine: 60
128 | PenaltyIndentedWhitespace: 0
129 | PointerAlignment: Right
130 | PPIndentWidth: -1
131 | ReferenceAlignment: Pointer
132 | ReflowComments: true
133 | ShortNamespaceLines: 1
134 | SortIncludes: CaseSensitive
135 | SortJavaStaticImport: Before
136 | SortUsingDeclarations: true
137 | SpaceAfterCStyleCast: false
138 | SpaceAfterLogicalNot: false
139 | SpaceAfterTemplateKeyword: true
140 | SpaceBeforeAssignmentOperators: true
141 | SpaceBeforeCaseColon: false
142 | SpaceBeforeCpp11BracedList: false
143 | SpaceBeforeCtorInitializerColon: true
144 | SpaceBeforeInheritanceColon: true
145 | SpaceBeforeParens: ControlStatements
146 | SpaceAroundPointerQualifiers: Default
147 | SpaceBeforeRangeBasedForLoopColon: true
148 | SpaceInEmptyBlock: false
149 | SpaceInEmptyParentheses: false
150 | SpacesBeforeTrailingComments: 1
151 | SpacesInAngles: Never
152 | SpacesInConditionalStatement: false
153 | SpacesInContainerLiterals: true
154 | SpacesInCStyleCastParentheses: false
155 | SpacesInLineCommentPrefix:
156 | Minimum: 1
157 | Maximum: -1
158 | SpacesInParentheses: false
159 | SpacesInSquareBrackets: false
160 | SpaceBeforeSquareBrackets: false
161 | BitFieldColonSpacing: Both
162 | Standard: Latest
163 | StatementAttributeLikeMacros:
164 | - Q_EMIT
165 | StatementMacros:
166 | - Q_UNUSED
167 | - QT_REQUIRE_VERSION
168 | TabWidth: 8
169 | UseCRLF: false
170 | UseTab: Never
171 | WhitespaceSensitiveMacros:
172 | - STRINGIZE
173 | - PP_STRINGIZE
174 | - BOOST_PP_STRINGIZE
175 | - NS_SWIFT_NAME
176 | - CF_SWIFT_NAME
177 | ...
178 |
179 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: Continuous Integration
2 |
3 | on: [push]
4 |
5 | jobs:
6 | build:
7 | name: "Build"
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v4
11 | - name: Create library ZIP
12 | run: |
13 | mkdir BlenderServoAnimation
14 | cp -R src examples library.properties README.md LICENSE BlenderServoAnimation
15 | zip -r blender_servo_animation_arduino_library BlenderServoAnimation
16 | - name: Archive library ZIP
17 | uses: actions/upload-artifact@v4
18 | with:
19 | name: blender_servo_animation_arduino_library.zip
20 | path: |
21 | blender_servo_animation_arduino_library.zip
22 | build-examples:
23 | name: "Build examples"
24 | runs-on: ubuntu-latest
25 | strategy:
26 | fail-fast: false
27 | matrix:
28 | example:
29 | [
30 | AdafruitPCA9685,
31 | MultiplePCA9685,
32 | MultipleScenes,
33 | MultipleScenesSD,
34 | SDAnimation,
35 | SerialLiveMode,
36 | StandardServoLib,
37 | SwitchModeButton,
38 | WebSocketLiveMode,
39 | ]
40 | steps:
41 | - uses: actions/checkout@v4
42 | - uses: actions/cache@v4
43 | with:
44 | path: |
45 | ~/.cache/pip
46 | ~/.platformio/.cache
47 | key: ${{ runner.os }}-pio
48 | - uses: actions/setup-python@v5
49 | with:
50 | python-version: "3.10"
51 | - name: Install PlatformIO Core
52 | run: pip install --upgrade platformio
53 | - name: Build PlatformIO examples
54 | run: |
55 | cd examples/${{ matrix.example }}
56 | pio run
57 | lint:
58 | name: "Lint"
59 | runs-on: ubuntu-22.04
60 | steps:
61 | - uses: actions/checkout@v4
62 | - name: Lint with clang-format
63 | uses: jidicula/clang-format-action@v4.10.2
64 | with:
65 | exclude-regex: 'examples\/.+\/.+\.h'
66 | - name: Lint with arduino-lint
67 | uses: arduino/arduino-lint-action@v1
68 | with:
69 | library-manager: update
70 | test:
71 | name: "Test"
72 | runs-on: ubuntu-latest
73 | steps:
74 | - uses: actions/checkout@v4
75 | - name: Cache PlatformIO directory
76 | uses: actions/cache@v4
77 | with:
78 | path: /home/runner/.platformio
79 | key: platformio-dir
80 | - name: Set up Python 3.10
81 | uses: actions/setup-python@v5
82 | with:
83 | python-version: "3.10"
84 | cache: "pip"
85 | cache-dependency-path: requirements-dev.txt
86 | - name: Install dependencies
87 | run: |
88 | sudo apt-get update
89 | sudo apt-get install --no-install-recommends -y libelf-dev
90 | python -m pip install --upgrade pip
91 | pip install -r requirements-dev.txt
92 | - name: Run tests with platformIO
93 | run: |
94 | pio test -e native
95 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .pio
2 | .vscode/.browse.c_cpp.db*
3 | .vscode/c_cpp_properties.json
4 | .vscode/launch.json
5 | .vscode/ipch
6 | examples/*/.vscode
7 | examples/*/.gitignore
8 |
--------------------------------------------------------------------------------
/.vscode/extensions.json:
--------------------------------------------------------------------------------
1 | {
2 | // See http://go.microsoft.com/fwlink/?LinkId=827846
3 | // for the documentation about the extensions.json format
4 | "recommendations": [
5 | "platformio.platformio-ide"
6 | ],
7 | "unwantedRecommendations": [
8 | "ms-vscode.cpptools-extension-pack"
9 | ]
10 | }
11 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "files.associations": {
3 | "*.tcc": "cpp",
4 | "exception": "cpp",
5 | "memory_resource": "cpp",
6 | "iosfwd": "cpp",
7 | "new": "cpp",
8 | "ostream": "cpp",
9 | "sstream": "cpp",
10 | "stop_token": "cpp",
11 | "streambuf": "cpp",
12 | "__bit_reference": "cpp",
13 | "__string": "cpp",
14 | "algorithm": "cpp",
15 | "deque": "cpp",
16 | "ios": "cpp",
17 | "string": "cpp",
18 | "type_traits": "cpp",
19 | "cstddef": "cpp",
20 | "array": "cpp",
21 | "bitset": "cpp",
22 | "vector": "cpp",
23 | "string_view": "cpp",
24 | "functional": "cpp",
25 | "iomanip": "cpp",
26 | "istream": "cpp",
27 | "limits": "cpp",
28 | "ratio": "cpp",
29 | "tuple": "cpp",
30 | "__config": "cpp",
31 | "__hash_table": "cpp",
32 | "__locale": "cpp",
33 | "iterator": "cpp",
34 | "locale": "cpp",
35 | "thread": "cpp",
36 | "memory": "cpp",
37 | "random": "cpp",
38 | "mutex": "cpp",
39 | "optional": "cpp",
40 | "__tree": "cpp",
41 | "unordered_map": "cpp",
42 | "unordered_set": "cpp",
43 | "numeric": "cpp",
44 | "utility": "cpp"
45 | }
46 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Blender Servo Animation Arduino Library
2 |
3 | This library helps to control servos based on an exported Blender animation. It is specifically designed to work with the [Blender Servo Animation Add-on](https://github.com/timhendriks93/blender-servo-animation).
4 |
5 | [](https://github.com/timhendriks93/blender-servo-animation-arduino/actions/workflows/ci.yml)
6 |
7 | ## First Steps
8 |
9 | ### Installation
10 |
11 | Please refer to the official [Arduino documentation](https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries) to see how you can install this library.
12 |
13 | ### Quick Start
14 |
15 | Take a look at the [StandardServoLib](examples/StandardServoLib) example to get started quickly. It represents the most simple setup and is based on the standard Servo Arduino library with which you might already be familiar.
16 |
17 | ### Usage
18 |
19 | To start using this library, add the following include statement to your script or sketch:
20 |
21 | ```ino
22 | #include
23 | ```
24 |
25 | ## Defining an Animation
26 |
27 | The animation object serves as a control instance to play back the servo movement. Just like in Blender, an animation can consist of multiple scenes and can be triggered to play, pause and stop.
28 |
29 | Start simply by creating a new animation instance at the outer scope of your sketch (outside of `setup` and `loop`):
30 |
31 | ```ino
32 | BlenderServoAnimation animation;
33 | ```
34 |
35 | ### Register a Position Change Callback
36 |
37 | To specify what should happen when a servo needs to be moved to a new position, we have to define a callback function. It receives 2 arguments - the servo ID as `byte` and the new position as `int`:
38 |
39 | ```ino
40 | void move(byte servoID, int position) {
41 | // Custom logic to move the servo
42 | }
43 | ```
44 |
45 | This allows the implementation of any logic to handle the actual servo control. For example, you can make use of the standard Servo library for simple setups or add logic to control servos via PWM control boards such as the PCA9685.
46 |
47 | After defining the callback function, we need to register it via the `onPositionChange` method of the animation instance:
48 |
49 | ```ino
50 | animation.onPositionChange(move);
51 | ```
52 |
53 | ### Adding Scenes and Animation Data
54 |
55 | Before we can play back and control an animation, we first have to add at least one scene to our animation. There are 2 ways to do this depending on the type of animation data you want to provide:
56 |
57 | ```ino
58 | animation.addScene(data, size, fps, frames);
59 | animation.addScene(stream, fps, frames);
60 | ```
61 |
62 | | Parameter | Type | Description |
63 | |-----------|------|-------------|
64 | | data | const byte[] | Exported position data |
65 | | stream | Stream | Stream instance to read positions from |
66 | | size | int | Size of the position data |
67 | | fps | byte | Frames per second as specified in Blender |
68 | | frames | int | Total amount of frames as specified in Blender |
69 |
70 | > Note: make sure the `fps` and `frames` values align exactly with the respective settings of the animation in Blender.
71 |
72 | ### Updating the Animation State
73 |
74 | The animation needs to be triggered regularly in order to update its state and check if any servos have to be moved. We therefore need to call the `run` method during each `loop`:
75 |
76 | ```ino
77 | void loop() {
78 | animation.run();
79 | }
80 | ```
81 |
82 | ## Controlling an Animation
83 |
84 | ### Animation Modes
85 |
86 | 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.
87 |
88 | | Constant | Method | Description |
89 | |----------|--------|-------------|
90 | | MODE_DEFAULT | n/a | Not playing / waiting |
91 | | MODE_PLAY | play() | Start or resume playing all scenes once |
92 | | MODE_PLAY_SINGLE | playSingle(index) | Start or resume playing a single scene once |
93 | | MODE_PLAY_RANDOM | playRandom() | Start or resume randomly playing scenes |
94 | | MODE_PAUSE | pause() | Pausing the scene at the current frame |
95 | | MODE_STOP | stop() | Slowly moving the servos to their starting position |
96 | | MODE_LOOP | loop() | Start or resume playing all scenes in a loop |
97 | | MODE_LIVE | live()/live(stream) | Reading serial commands to move the servos in real-time |
98 |
99 | The modes can be changed or triggered by calling the above methods on the animation object:
100 |
101 | ```ino
102 | animation.play();
103 | animation.playSingle(index);
104 | animation.playRandom();
105 | animation.pause();
106 | animation.loop();
107 | animation.stop();
108 | animation.live(stream);
109 | ```
110 |
111 | > Note: the default mode can not be triggered as it is only handled internally.
112 |
113 | When calling the `stop` method, the threshold values of the animation's servos are considered to control how fast or smooth they are moving towards their neutral position. Keep in mind that the servos will not have a threshold value by default which results in the stop mode to immediately trigger the neutral position of the servos. A slower and safer movement can be achieved by setting the threshold values as low as possible with the actual animation still able to run properly.
114 |
115 | ### Live Mode
116 |
117 | 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:
118 |
119 | ```ino
120 | void setup() {
121 | Serial.begin(115200);
122 | animation.live(Serial);
123 | }
124 | ```
125 |
126 | ### React to Mode Changes
127 |
128 | 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:
129 |
130 | ```ino
131 | byte currentMode = animation.getMode();
132 |
133 | switch (currentMode) {
134 | case BlenderServoAnimation::MODE_DEFAULT:
135 | // Do something
136 | break;
137 | case BlenderServoAnimation::MODE_PLAY:
138 | // Do something else
139 | break;
140 | ...
141 | }
142 | ```
143 |
144 | 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:
145 |
146 | ```ino
147 | void modeChanged(byte prevMode, byte newMode) {
148 | // Do something (e.g. using a switch statement)
149 | }
150 |
151 | void setup() {
152 | animation.onModeChange(modeChanged);
153 | }
154 | ```
155 |
156 | ### React to Scene Changes
157 |
158 | To execute logic whenever there is a transition into a different scene, another callback function can be registered. The function will receive the index of both the previous and the new scene as `byte` values. To register the function, we can call the `onSceneChange` method:
159 |
160 | ```ino
161 | void sceneChanged(byte prevSceneIndex, byte newSceneIndex) {
162 | // Do something (e.g. opening another animation file on an SD card)
163 | }
164 |
165 | void setup() {
166 | animation.onSceneChange(sceneChanged);
167 | }
168 | ```
169 |
170 | ## Examples
171 |
172 | Make sure to also check out the other [examples](examples) to get started more quickly.
173 |
--------------------------------------------------------------------------------
/examples/AdafruitPCA9685/AdafruitPCA9685.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Using an Adafruit PCA9685 module to send servo positions.
3 |
4 | This approach is especially useful when the animation is based on multiple servos. We assume the
5 | servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be
6 | connected to channel 0 etc.
7 | */
8 |
9 | #include "ik.h"
10 | #include
11 | #include
12 |
13 | // Animation object to control the animation
14 | BlenderServoAnimation animation;
15 |
16 | // PWM driver instance to set PWM output
17 | Adafruit_PWMServoDriver pwm;
18 |
19 | // Callback function which is called whenever a servo needs to be moved
20 | void move(byte servoID, int position) {
21 | // We assume the servoID is equal to the used channel on the PCA9685
22 | pwm.setPWM(servoID, 0, position);
23 | }
24 |
25 | void setup() {
26 | // Set the position callback
27 | animation.onPositionChange(move);
28 |
29 | // Add a scene based on PROGMEM data
30 | animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);
31 |
32 | // Trigger the animation loop mode
33 | animation.loop();
34 |
35 | // Initialize servo driver
36 | pwm.begin();
37 | pwm.setPWMFreq(60);
38 |
39 | delay(10);
40 | }
41 |
42 | void loop() {
43 | // Update the animation state on each loop
44 | animation.run();
45 | }
46 |
--------------------------------------------------------------------------------
/examples/AdafruitPCA9685/README.md:
--------------------------------------------------------------------------------
1 | # Adafruit PCA9685
2 |
3 | Using an Adafruit PCA9685 module to send servo positions.
4 |
5 | This approach is especially useful when the animation is based on multiple servos. We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be connected to channel 0 etc.
6 |
7 | ## Library Dependencies
8 |
9 | - [Adafruit-PWM-Servo-Driver-Library](https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library)
10 |
11 | ## Wiring Diagram
12 |
13 | 
14 |
--------------------------------------------------------------------------------
/examples/AdafruitPCA9685/ik.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 30
5 | Frames: 100
6 | Seconds: 3
7 | Bones: 2
8 | Armature: Armature
9 | Scene: Scene
10 | File: ik.blend
11 | */
12 |
13 | #include
14 |
15 | const byte FPS = 30;
16 | const int FRAMES = 100;
17 | const int LENGTH = 1040;
18 |
19 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
20 | 0x3c, 0x00, 0x01, 0x77, 0x3e, 0x3c, 0x01, 0x01, 0x77, 0x3e, 0x0a, 0x3c,
21 | 0x00, 0x01, 0x78, 0x3e, 0x3c, 0x01, 0x01, 0x78, 0x3e, 0x0a, 0x3c, 0x00,
22 | 0x01, 0x79, 0x3e, 0x3c, 0x01, 0x01, 0x7b, 0x3e, 0x0a, 0x3c, 0x00, 0x01,
23 | 0x7c, 0x3e, 0x3c, 0x01, 0x01, 0x7f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0x3c, 0x01, 0x01, 0x61, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x8a, 0x3e, 0x3c,
97 | 0x01, 0x01, 0x64, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x87, 0x3e, 0x3c, 0x01,
98 | 0x01, 0x67, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x84, 0x3e, 0x3c, 0x01, 0x01,
99 | 0x6a, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x82, 0x3e, 0x3c, 0x01, 0x01, 0x6c,
100 | 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x80, 0x3e, 0x3c, 0x01, 0x01, 0x6e, 0x3e,
101 | 0x0a, 0x3c, 0x00, 0x01, 0x7d, 0x3e, 0x3c, 0x01, 0x01, 0x71, 0x3e, 0x0a,
102 | 0x3c, 0x00, 0x01, 0x7c, 0x3e, 0x3c, 0x01, 0x01, 0x72, 0x3e, 0x0a, 0x3c,
103 | 0x00, 0x01, 0x7a, 0x3e, 0x3c, 0x01, 0x01, 0x74, 0x3e, 0x0a, 0x3c, 0x00,
104 | 0x01, 0x79, 0x3e, 0x3c, 0x01, 0x01, 0x75, 0x3e, 0x0a, 0x3c, 0x00, 0x01,
105 | 0x78, 0x3e, 0x3c, 0x01, 0x01, 0x76, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x77,
106 | 0x3e, 0x3c, 0x01, 0x01, 0x77, 0x3e, 0x0a, 0x0a,
107 | };
108 |
--------------------------------------------------------------------------------
/examples/AdafruitPCA9685/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | lib_deps =
6 | adafruit/Adafruit PWM Servo Driver Library@^2.4.1
7 | BlenderServoAnimation=symlink://../../
8 |
9 | [env:uno]
10 | board = uno
11 | platform = atmelavr
12 | framework = arduino
13 |
14 | [env:ATmega2560]
15 | board = ATmega2560
16 | platform = atmelavr
17 | framework = arduino
18 |
19 | [env:nanoatmega328]
20 | board = nanoatmega328
21 | platform = atmelavr
22 | framework = arduino
23 |
24 | [env:esp32dev]
25 | board = esp32dev
26 | platform = espressif32
27 | framework = arduino
28 |
--------------------------------------------------------------------------------
/examples/MultiplePCA9685/MultiplePCA9685.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Using two PCA9685 PWM servo drivers to animate 2 servos.
3 |
4 | Note that the A0 address jumper has to be soldered on the second driver board. This setup can
5 | easily be extended to animate up to 32 servos. If even more servos are needed, you can also add
6 | more driver boards to the chain.
7 |
8 | We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0
9 | has to be connected to channel 0 etc.
10 | */
11 |
12 | #include "ik.h"
13 | #include
14 | #include
15 |
16 | #define SERVO_AMOUNT 2
17 |
18 | // Animation object to control the animation
19 | BlenderServoAnimation animation;
20 |
21 | // PWM driver instances to set PWM output
22 | Adafruit_PWMServoDriver pwmA(0x40);
23 | Adafruit_PWMServoDriver pwmB(0x41);
24 |
25 | // We use a struct to map a servo to a PCA9685 board and channel
26 | struct servoMapping {
27 | byte id;
28 | Adafruit_PWMServoDriver pwm;
29 | byte channel;
30 | };
31 |
32 | // Define an array of servo maps
33 | servoMapping servoMappings[SERVO_AMOUNT] = {
34 | // Servo 0 attached to board A on channel 0
35 | {0, pwmA, 0},
36 |
37 | // Servo 1 attached to board B on channel 0
38 | {1, pwmB, 0},
39 | };
40 |
41 | // Callback function which is called whenever a servo needs to be moved
42 | void setPWM(byte servoID, int position) {
43 | // Iterate through the available servos
44 | for (int i = 0; i < SERVO_AMOUNT; i++) {
45 | // Continue if the current servo ID doesn't match the target servo ID
46 | if (servoMappings[i].id != servoID) {
47 | continue;
48 | }
49 |
50 | // Get the PWM driver instance and channel from the mapping
51 | Adafruit_PWMServoDriver pwm = servoMappings[i].pwm;
52 | byte channel = servoMappings[i].channel;
53 |
54 | // Set the current position as PWM output
55 | pwm.setPWM(channel, 0, position);
56 |
57 | // Break the for loop as we finsihed handling the servo movement
58 | break;
59 | }
60 | }
61 |
62 | void setup() {
63 | // Set the position callback
64 | animation.onPositionChange(setPWM);
65 |
66 | // Add a scene based on PROGMEM data
67 | animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);
68 |
69 | // Trigger the animation loop mode
70 | animation.loop();
71 |
72 | // Initialize servo drivers
73 | pwmA.begin();
74 | pwmA.setPWMFreq(60);
75 | pwmB.begin();
76 | pwmB.setPWMFreq(60);
77 |
78 | delay(10);
79 | }
80 |
81 | void loop() {
82 | // Update the animation state on each loop
83 | animation.run();
84 | }
85 |
--------------------------------------------------------------------------------
/examples/MultiplePCA9685/README.md:
--------------------------------------------------------------------------------
1 | # Multiple PCA9685
2 |
3 | Using two PCA9685 PWM servo drivers to animate 2 servos.
4 |
5 | Note that the A0 address jumper has to be soldered on the second driver board. This setup can easily be extended to animate up to 32 servos. If even more servos are needed, you can also add more driver boards to the chain.
6 |
7 | We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be connected to channel 0 etc.
8 |
9 | ## Library Dependencies
10 |
11 | - [Adafruit-PWM-Servo-Driver-Library](https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library)
12 |
13 | ## Wiring Diagram
14 |
15 | 
16 |
--------------------------------------------------------------------------------
/examples/MultiplePCA9685/ik.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 30
5 | Frames: 100
6 | Seconds: 3
7 | Bones: 2
8 | Armature: Armature
9 | Scene: Scene
10 | File: ik.blend
11 | */
12 |
13 | #include
14 |
15 | const byte FPS = 30;
16 | const int FRAMES = 100;
17 | const int LENGTH = 1040;
18 |
19 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
20 | 0x3c, 0x00, 0x01, 0x77, 0x3e, 0x3c, 0x01, 0x01, 0x77, 0x3e, 0x0a, 0x3c,
21 | 0x00, 0x01, 0x78, 0x3e, 0x3c, 0x01, 0x01, 0x78, 0x3e, 0x0a, 0x3c, 0x00,
22 | 0x01, 0x79, 0x3e, 0x3c, 0x01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
95 | 0x3e, 0x3c, 0x01, 0x01, 0x5e, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x8d, 0x3e,
96 | 0x3c, 0x01, 0x01, 0x61, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x8a, 0x3e, 0x3c,
97 | 0x01, 0x01, 0x64, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x87, 0x3e, 0x3c, 0x01,
98 | 0x01, 0x67, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x84, 0x3e, 0x3c, 0x01, 0x01,
99 | 0x6a, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x82, 0x3e, 0x3c, 0x01, 0x01, 0x6c,
100 | 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x80, 0x3e, 0x3c, 0x01, 0x01, 0x6e, 0x3e,
101 | 0x0a, 0x3c, 0x00, 0x01, 0x7d, 0x3e, 0x3c, 0x01, 0x01, 0x71, 0x3e, 0x0a,
102 | 0x3c, 0x00, 0x01, 0x7c, 0x3e, 0x3c, 0x01, 0x01, 0x72, 0x3e, 0x0a, 0x3c,
103 | 0x00, 0x01, 0x7a, 0x3e, 0x3c, 0x01, 0x01, 0x74, 0x3e, 0x0a, 0x3c, 0x00,
104 | 0x01, 0x79, 0x3e, 0x3c, 0x01, 0x01, 0x75, 0x3e, 0x0a, 0x3c, 0x00, 0x01,
105 | 0x78, 0x3e, 0x3c, 0x01, 0x01, 0x76, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x77,
106 | 0x3e, 0x3c, 0x01, 0x01, 0x77, 0x3e, 0x0a, 0x0a,
107 | };
108 |
--------------------------------------------------------------------------------
/examples/MultiplePCA9685/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | lib_deps =
6 | adafruit/Adafruit PWM Servo Driver Library@^2.4.1
7 | BlenderServoAnimation=symlink://../../
8 |
9 | [env:uno]
10 | board = uno
11 | platform = atmelavr
12 | framework = arduino
13 |
14 | [env:ATmega2560]
15 | board = ATmega2560
16 | platform = atmelavr
17 | framework = arduino
18 |
19 | [env:nanoatmega328]
20 | board = nanoatmega328
21 | platform = atmelavr
22 | framework = arduino
23 |
24 | [env:esp32dev]
25 | board = esp32dev
26 | platform = espressif32
27 | framework = arduino
28 |
--------------------------------------------------------------------------------
/examples/MultipleScenes/MultipleScenes.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Setting up an animation consisting of 2 scenes.
3 |
4 | The 2 scenes will be played synchronously in a loop. It's even possible to have different playback
5 | rates (fps) and frames per animation.
6 | */
7 |
8 | #include "scene-a.h"
9 | #include "scene-b.h"
10 | #include
11 |
12 | #ifdef ARDUINO_ARCH_ESP32
13 | #include
14 | #else
15 | #include
16 | #endif
17 |
18 | #define SERVO_PIN 3
19 |
20 | // Animation object to control the animation
21 | BlenderServoAnimation animation;
22 |
23 | // Servo object to send positions
24 | Servo myServo;
25 |
26 | // Callback function which is called whenever a servo needs to be moved
27 | void move(byte servoID, int position) {
28 | // Ignore the servoID (there is only one servo) and write the current position
29 | myServo.writeMicroseconds(position);
30 | }
31 |
32 | void setup() {
33 | // Attach the servo to the defined servo pin
34 | myServo.attach(SERVO_PIN);
35 |
36 | // Set the position callback
37 | animation.onPositionChange(move);
38 |
39 | // Add multiple scenes based on PROGMEM data
40 | animation.addScene(SceneA::ANIMATION_DATA, SceneA::LENGTH, SceneA::FPS, SceneA::FRAMES);
41 | animation.addScene(SceneB::ANIMATION_DATA, SceneB::LENGTH, SceneB::FPS, SceneB::FRAMES);
42 |
43 | // Trigger the animation loop mode
44 | animation.loop();
45 |
46 | // There are also other playback options
47 | // show.play(); // Plays all scenes once in the order they have been added
48 | // show.playRandom(); // Randomly plays scenes in a loop
49 | // show.playSingle(1); // Play the scene at the given index once
50 | }
51 |
52 | void loop() {
53 | // Update the animation state on each loop
54 | animation.run();
55 | }
56 |
--------------------------------------------------------------------------------
/examples/MultipleScenes/README.md:
--------------------------------------------------------------------------------
1 | # Multiple Scenes
2 |
3 | Setting up an animation consisting of 2 scenes.
4 |
5 | The 2 scenes will be played synchronously in a loop. It's even possible to have different playback rates (fps) and frames per animation.
6 |
7 | ## Library Dependencies
8 |
9 | - [Servo](https://github.com/arduino-libraries/Servo)
10 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
11 |
12 | ## Wiring Diagram
13 |
14 | 
15 |
--------------------------------------------------------------------------------
/examples/MultipleScenes/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | BlenderServoAnimation=symlink://../../
12 |
13 | [env:uno]
14 | extends = atmelavr_base
15 | board = uno
16 |
17 | [env:ATmega2560]
18 | extends = atmelavr_base
19 | board = ATmega2560
20 |
21 | [env:nanoatmega328]
22 | extends = atmelavr_base
23 | board = nanoatmega328
24 |
25 | [env:esp32dev]
26 | platform = espressif32
27 | board = esp32dev
28 | lib_deps =
29 | madhephaestus/ESP32Servo@^0.13.0
30 | BlenderServoAnimation=symlink://../../
31 |
--------------------------------------------------------------------------------
/examples/MultipleScenes/scene-a.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 30
5 | Frames: 100
6 | Seconds: 3
7 | Bones: 1
8 | Armature: Armature
9 | Scene: SceneA
10 | File: scenes.blend
11 | */
12 |
13 | #include
14 |
15 | namespace SceneA {
16 |
17 | const byte FPS = 30;
18 | const int FRAMES = 100;
19 | const int LENGTH = 600;
20 |
21 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
22 | 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc1, 0x3e, 0x0a,
23 | 0x3c, 0x00, 0x05, 0xc5, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xcb, 0x3e, 0x0a,
24 | 0x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3e, 0x0a, 0x3c, 0x00, 0x05, 0x7f, 0x3e, 0x0a,
68 | 0x3c, 0x00, 0x05, 0x8d, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x9a, 0x3e, 0x0a,
69 | 0x3c, 0x00, 0x05, 0xa5, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xae, 0x3e, 0x0a,
70 | 0x3c, 0x00, 0x05, 0xb6, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xbb, 0x3e, 0x0a,
71 | 0x3c, 0x00, 0x05, 0xbf, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a,
72 | };
73 |
74 | } // namespace SceneA
75 |
--------------------------------------------------------------------------------
/examples/MultipleScenes/scene-b.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 60
5 | Frames: 200
6 | Seconds: 3
7 | Bones: 1
8 | Armature: Armature.001
9 | Scene: SceneB
10 | File: scenes.blend
11 | */
12 |
13 | #include
14 |
15 | namespace SceneB {
16 |
17 | const byte FPS = 60;
18 | const int FRAMES = 200;
19 | const int LENGTH = 1160;
20 |
21 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
22 | 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a, 0x0a, 0x3c, 0x00, 0x05, 0xc1, 0x3e,
23 | 0x0a, 0x0a, 0x3c, 0x00, 0x05, 0xc2, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc3,
24 | 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc5, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc7,
25 | 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc9, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xcb,
26 | 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xcd, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xd0,
27 | 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xd3,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x0a,
114 | 0x3c, 0x00, 0x05, 0xcd, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xcb, 0x3e, 0x0a,
115 | 0x3c, 0x00, 0x05, 0xc8, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc6, 0x3e, 0x0a,
116 | 0x3c, 0x00, 0x05, 0xc5, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc3, 0x3e, 0x0a,
117 | 0x3c, 0x00, 0x05, 0xc2, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc1, 0x3e, 0x0a,
118 | 0x0a, 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a, 0x0a,
119 | };
120 |
121 | } // namespace SceneB
122 |
--------------------------------------------------------------------------------
/examples/MultipleScenesSD/MultipleScenesSD.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Setting up an animation consisting of 2 scenes with their animation data stored on an SD card.
3 |
4 | The 2 scenes will be played synchronously in a loop. It's even possible to have different playback
5 | rates (fps) and frames per animation.
6 | */
7 |
8 | #include
9 | #include
10 |
11 | #ifdef ARDUINO_ARCH_ESP32
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #define SERVO_PIN 3
18 | #define CS_PIN 4
19 | #define SCENE_AMOUNT 2
20 |
21 | // Servo object to send positions
22 | Servo myServo;
23 |
24 | // File object to read animation data
25 | File animationFile;
26 |
27 | // We use a struct to map a scene to a filename
28 | struct sceneMapping {
29 | byte fps;
30 | int frames;
31 | String filename;
32 | };
33 |
34 | // Define an array of scene maps
35 | sceneMapping sceneMappings[SCENE_AMOUNT] = {
36 | // Scene 0 = Scene A
37 | {30, 100, "scene-a.bin"},
38 |
39 | // Scene 1 = Scene B
40 | {60, 200, "scene-b.bin"},
41 | };
42 |
43 | // Callback function which is called whenever a servo needs to be moved
44 | void move(byte servoID, int position) {
45 | // Ignore the servoID (there is only one servo) and write the current position
46 | myServo.writeMicroseconds(position);
47 | }
48 |
49 | // Callback function which is called whenever a scene is being changed
50 | void changeSceneFile(byte prevSceneIndex, byte nextSceneIndex) {
51 | // Get the filename of the next scene
52 | String filename = sceneMappings[nextSceneIndex].filename;
53 |
54 | // Close the current animation file which points to the SD
55 | animationFile.close();
56 |
57 | // Open the new file on the SD and set it as the new animation file
58 | animationFile = SD.open(filename);
59 |
60 | if (!animationFile) {
61 | Serial.println("Opening file failed.");
62 | }
63 | }
64 |
65 | // Animation object to control the animation
66 | BlenderServoAnimation animation;
67 |
68 | void setup() {
69 | Serial.begin(9600);
70 | while (!Serial) {
71 | };
72 |
73 | Serial.println("Initializing SD card...");
74 |
75 | if (!SD.begin(CS_PIN)) {
76 | Serial.println("Initialization failed!");
77 | while (true) {
78 | };
79 | }
80 | Serial.println("Initialization done.");
81 |
82 | // Attach the servo to pin 12
83 | myServo.attach(SERVO_PIN);
84 |
85 | // Set the position and scene change callback
86 | animation.onPositionChange(move);
87 | animation.onSceneChange(changeSceneFile);
88 |
89 | // Add multiple scenes with the same File stream
90 | for (byte i = 0; i < SCENE_AMOUNT; i++) {
91 | byte fps = sceneMappings[i].fps;
92 | int frames = sceneMappings[i].frames;
93 |
94 | animation.addScene(animationFile, fps, frames);
95 | }
96 |
97 | // Trigger the show loop mode
98 | animation.loop();
99 |
100 | // There are also other playback options
101 | // show.play(); // Plays all scenes once in the order they have been added
102 | // show.playRandom(); // Randomly plays scenes in a loop
103 | // show.playSingle(1); // Play the scene at the given index once
104 | }
105 |
106 | void loop() {
107 | // Update the animation state on each loop
108 | animation.run();
109 | }
110 |
--------------------------------------------------------------------------------
/examples/MultipleScenesSD/README.md:
--------------------------------------------------------------------------------
1 | # Multiple Scenes with SD Card Animation Data
2 |
3 | Setting up an animation consisting of 2 scenes with their animation data being read from an SD card.
4 |
5 | The 2 scenes will be played synchronously in a loop. It's even possible to have different playback rates (fps) and frames per animation.
6 |
7 | ## Library Dependencies
8 |
9 | - [SD](https://github.com/arduino-libraries/SD)
10 | - [Servo](https://github.com/arduino-libraries/Servo)
11 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
12 |
13 | ## Wiring Diagram
14 |
15 | 
16 |
--------------------------------------------------------------------------------
/examples/MultipleScenesSD/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | arduino-libraries/SD@^1.2.4
12 | BlenderServoAnimation=symlink://../../
13 |
14 | [env:uno]
15 | extends = atmelavr_base
16 | board = uno
17 |
18 | [env:ATmega2560]
19 | extends = atmelavr_base
20 | board = ATmega2560
21 |
22 | [env:nanoatmega328]
23 | extends = atmelavr_base
24 | board = nanoatmega328
25 |
26 | [env:esp32dev]
27 | platform = espressif32
28 | board = esp32dev
29 | lib_deps =
30 | madhephaestus/ESP32Servo@^0.13.0
31 | BlenderServoAnimation=symlink://../../
32 |
--------------------------------------------------------------------------------
/examples/MultipleScenesSD/scene-a.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/examples/MultipleScenesSD/scene-a.bin
--------------------------------------------------------------------------------
/examples/MultipleScenesSD/scene-b.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/examples/MultipleScenesSD/scene-b.bin
--------------------------------------------------------------------------------
/examples/SDAnimation/README.md:
--------------------------------------------------------------------------------
1 | # SD Card Animation Data
2 |
3 | Reading animation data from an SD card.
4 |
5 | The setup requires nothing but a micro controller and a single servo. It uses the standard Arduino servo library to send servo positions.
6 |
7 | ## Library Dependencies
8 |
9 | - [SD](https://github.com/arduino-libraries/SD)
10 | - [Servo](https://github.com/arduino-libraries/Servo)
11 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
12 |
13 | ## Wiring Diagram
14 |
15 | 
16 |
--------------------------------------------------------------------------------
/examples/SDAnimation/SDAnimation.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Reading animation data from an SD card.
3 |
4 | The setup requires nothing but a micro controller and a single servo. It uses the standard Arduino
5 | servo library to send servo positions.
6 | */
7 |
8 | #include
9 | #include
10 |
11 | #ifdef ARDUINO_ARCH_ESP32
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #define SERVO_PIN 3
18 | #define CS_PIN 4
19 | #define FPS 30
20 | #define FRAMES 100
21 |
22 | // Servo object to send positions
23 | Servo myServo;
24 |
25 | // File object to read animation data
26 | File animationFile;
27 |
28 | // Callback function which is called whenever a servo needs to be moved
29 | void move(byte servoID, int position) {
30 | // Ignore the servoID (there is only one servo) and write the current position
31 | myServo.writeMicroseconds(position);
32 | }
33 |
34 | // Callback function which is called whenever a scene is being changed
35 | void resetFile(byte prevSceneIndex, byte nextSceneIndex) {
36 | // Seek back to the first position in the file
37 | animationFile.seek(0);
38 | }
39 |
40 | // Animation object to control the animation
41 | BlenderServoAnimation animation;
42 |
43 | void setup() {
44 | Serial.begin(9600);
45 | while (!Serial) {
46 | };
47 |
48 | Serial.println("Initializing SD card...");
49 |
50 | if (!SD.begin(CS_PIN)) {
51 | Serial.println("Initialization failed!");
52 | while (true) {
53 | };
54 | }
55 | Serial.println("Initialization done.");
56 |
57 | animationFile = SD.open("simple.bin");
58 |
59 | if (animationFile) {
60 | Serial.println("File opened successfully.");
61 | } else {
62 | Serial.println("Opening file failed.");
63 | }
64 |
65 | // Attach the servo to the defined servo pin
66 | myServo.attach(SERVO_PIN);
67 |
68 | // Set the position and scene change callback
69 | animation.onPositionChange(move);
70 | animation.onSceneChange(resetFile);
71 |
72 | // Add a scene with the File stream
73 | animation.addScene(animationFile, FPS, FRAMES);
74 |
75 | // Trigger the animation loop mode
76 | animation.loop();
77 | }
78 |
79 | void loop() {
80 | // Update the animation state on each loop
81 | animation.run();
82 | }
83 |
--------------------------------------------------------------------------------
/examples/SDAnimation/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | arduino-libraries/SD@^1.2.4
12 | BlenderServoAnimation=symlink://../../
13 |
14 | [env:uno]
15 | extends = atmelavr_base
16 | board = uno
17 |
18 | [env:ATmega2560]
19 | extends = atmelavr_base
20 | board = ATmega2560
21 |
22 | [env:nanoatmega328]
23 | extends = atmelavr_base
24 | board = nanoatmega328
25 |
26 | [env:esp32dev]
27 | platform = espressif32
28 | board = esp32dev
29 | lib_deps =
30 | madhephaestus/ESP32Servo@^0.13.0
31 | BlenderServoAnimation=symlink://../../
32 |
--------------------------------------------------------------------------------
/examples/SDAnimation/simple.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/examples/SDAnimation/simple.bin
--------------------------------------------------------------------------------
/examples/SerialLiveMode/README.md:
--------------------------------------------------------------------------------
1 | # Serial Live Mode
2 |
3 | Sending live servo positions via serial commands.
4 |
5 | This example requires a USB connection to your PC and a running Blender instance with the Blender Servo Animation Add-on. After starting the live mode by connecting to the micro controller via serial, you can move the servo in real time via Blender.
6 |
7 | ## Library Dependencies
8 |
9 | - [Servo](https://github.com/arduino-libraries/Servo)
10 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
11 |
12 | ## Wiring Diagram
13 |
14 | 
15 |
--------------------------------------------------------------------------------
/examples/SerialLiveMode/SerialLiveMode.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Sending live servo positions via serial commands.
3 |
4 | This example requires a USB connection to your PC and a running Blender instance with the Blender
5 | Servo Animation Add-on. After starting the live mode by connecting to the micro controller via
6 | serial, you can move the servo in real time via Blender.
7 | */
8 |
9 | #include
10 |
11 | #ifdef ARDUINO_ARCH_ESP32
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #define SERVO_PIN 3
18 | #define BAUD_RATE 115200
19 |
20 | // Animation object to control the animation
21 | BlenderServoAnimation animation;
22 |
23 | // Servo object to send positions
24 | Servo myServo;
25 |
26 | // Callback function which is called whenever a servo needs to be moved
27 | void move(byte servoID, int position) {
28 | // Ignore the servoID (there is only one servo) and write the current position
29 | myServo.writeMicroseconds(position);
30 | }
31 |
32 | void setup() {
33 | // Initialize serial communication
34 | Serial.begin(BAUD_RATE);
35 |
36 | // Attach the servo to the defined servo pin
37 | myServo.attach(SERVO_PIN);
38 |
39 | // Set the position callback
40 | animation.onPositionChange(move);
41 |
42 | // Trigger the animation live mode by passing the Serial instance
43 | animation.live(Serial);
44 | }
45 |
46 | void loop() {
47 | // Update the animation state on each loop
48 | animation.run();
49 | }
50 |
--------------------------------------------------------------------------------
/examples/SerialLiveMode/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | BlenderServoAnimation=symlink://../../
12 |
13 | [env:uno]
14 | extends = atmelavr_base
15 | board = uno
16 |
17 | [env:ATmega2560]
18 | extends = atmelavr_base
19 | board = ATmega2560
20 |
21 | [env:nanoatmega328]
22 | extends = atmelavr_base
23 | board = nanoatmega328
24 |
25 | [env:esp32dev]
26 | platform = espressif32
27 | board = esp32dev
28 | lib_deps =
29 | madhephaestus/ESP32Servo@^0.13.0
30 | BlenderServoAnimation=symlink://../../
31 |
--------------------------------------------------------------------------------
/examples/StandardServoLib/README.md:
--------------------------------------------------------------------------------
1 | # Standard Servo Library
2 |
3 | Using the standard Arduino servo library to send servo positions.
4 |
5 | The setup requires nothing but a micro controller and a single servo. The animation is played in a loop.
6 |
7 | ## Library Dependencies
8 |
9 | - [Servo](https://github.com/arduino-libraries/Servo)
10 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
11 |
12 | ## Wiring Diagram
13 |
14 | 
15 |
--------------------------------------------------------------------------------
/examples/StandardServoLib/StandardServoLib.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Using the standard Arduino servo library to send servo positions.
3 |
4 | The setup requires nothing but a micro controller and a single servo. The animation is played in a
5 | loop.
6 | */
7 |
8 | #include "simple.h"
9 | #include
10 |
11 | #ifdef ARDUINO_ARCH_ESP32
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #define SERVO_PIN 3
18 |
19 | // Servo object to send positions
20 | Servo myServo;
21 |
22 | // Callback function which is called whenever a servo needs to be moved
23 | void move(byte servoID, int position) {
24 | // Ignore the servoID (there is only one servo) and write the current position
25 | myServo.writeMicroseconds(position);
26 | }
27 |
28 | // Animation object to control the animation
29 | BlenderServoAnimation animation;
30 |
31 | void setup() {
32 | // Attach the servo to the defined servo pin
33 | myServo.attach(SERVO_PIN);
34 |
35 | // Set the position callback
36 | animation.onPositionChange(move);
37 |
38 | // Add a scene based on PROGMEM data
39 | animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);
40 |
41 | // Trigger the animation loop mode
42 | animation.loop();
43 | }
44 |
45 | void loop() {
46 | // Update the animation state on each loop
47 | animation.run();
48 | }
49 |
--------------------------------------------------------------------------------
/examples/StandardServoLib/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | BlenderServoAnimation=symlink://../../
12 |
13 | [env:uno]
14 | extends = atmelavr_base
15 | board = uno
16 |
17 | [env:ATmega2560]
18 | extends = atmelavr_base
19 | board = ATmega2560
20 |
21 | [env:nanoatmega328]
22 | extends = atmelavr_base
23 | board = nanoatmega328
24 |
25 | [env:esp32dev]
26 | platform = espressif32
27 | board = esp32dev
28 | lib_deps =
29 | madhephaestus/ESP32Servo@^0.13.0
30 | BlenderServoAnimation=symlink://../../
31 |
--------------------------------------------------------------------------------
/examples/StandardServoLib/simple.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 30
5 | Frames: 100
6 | Seconds: 3
7 | Bones: 1
8 | Armature: Armature
9 | Scene: Scene
10 | File: simple.blend
11 | */
12 |
13 | #include
14 |
15 | const byte FPS = 30;
16 | const int FRAMES = 100;
17 | const int LENGTH = 600;
18 |
19 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
20 | 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc3, 0x3e, 0x0a,
21 | 0x3c, 0x00, 0x05, 0xca, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xd7, 0x3e, 0x0a,
22 | 0x3c, 0x00, 0x05, 0xe8, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xfd, 0x3e, 0x0a,
23 | 0x3c, 0x00, 0x06, 0x16, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0x32, 0x3e, 0x0a,
24 | 0x3c, 0x00, 0x06, 0x51, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0x73, 0x3e, 0x0a,
25 | 0x3c, 0x00, 0x06, 0x97, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0xbe, 0x3e, 0x0a,
26 | 0x3c, 0x00, 0x06, 0xe6, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0x0f, 0x3e, 0x0a,
27 | 0x3c, 0x00, 0x07, 0x39, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0x65, 0x3e, 0x0a,
28 | 0x3c, 0x00, 0x07, 0x90, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0xbb, 0x3e, 0x0a,
29 | 0x3c, 0x00, 0x07, 0xe7, 0x3e, 0x0a, 0x3c, 0x00, 0x08, 0x11, 0x3e, 0x0a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, 0x0a,
59 | 0x3c, 0x00, 0x03, 0x4f, 0x3e, 0x0a, 0x3c, 0x00, 0x03, 0x76, 0x3e, 0x0a,
60 | 0x3c, 0x00, 0x03, 0x9f, 0x3e, 0x0a, 0x3c, 0x00, 0x03, 0xc7, 0x3e, 0x0a,
61 | 0x3c, 0x00, 0x03, 0xf0, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0x19, 0x3e, 0x0a,
62 | 0x3c, 0x00, 0x04, 0x41, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0x6a, 0x3e, 0x0a,
63 | 0x3c, 0x00, 0x04, 0x91, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0xb7, 0x3e, 0x0a,
64 | 0x3c, 0x00, 0x04, 0xdb, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0xfe, 0x3e, 0x0a,
65 | 0x3c, 0x00, 0x05, 0x1f, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x3e, 0x3e, 0x0a,
66 | 0x3c, 0x00, 0x05, 0x5a, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x73, 0x3e, 0x0a,
67 | 0x3c, 0x00, 0x05, 0x8a, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x9d, 0x3e, 0x0a,
68 | 0x3c, 0x00, 0x05, 0xac, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xb7, 0x3e, 0x0a,
69 | 0x3c, 0x00, 0x05, 0xbe, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a,
70 | };
71 |
--------------------------------------------------------------------------------
/examples/SwitchModeButton/README.md:
--------------------------------------------------------------------------------
1 | # Switch Mode Button
2 |
3 | Using a button to switch between different modes and control the animation.
4 |
5 | We are using a button to handle both short and long presses. Note that the servo will not move until the button is shortly pressed to start the animation.
6 |
7 | Starting the animation will only play it once, so another button press is required to play it again. A long press will stop the animation and slowly move the servo to its neutral position.
8 |
9 | ## Library Dependencies
10 |
11 | - [OneButton](https://github.com/mathertel/OneButton)
12 | - [Servo](https://github.com/arduino-libraries/Servo)
13 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)
14 |
15 | ## Wiring Diagram
16 |
17 | 
18 |
--------------------------------------------------------------------------------
/examples/SwitchModeButton/SwitchModeButton.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Using a button to switch between different modes and control the animation.
3 |
4 | We are using a button to handle both short and long presses. Note that the servo will not move
5 | until the button is shortly pressed to start the animation.
6 |
7 | Starting the animation will only play it once, so another button press is required to play it
8 | again. A long press will stop the animation and slowly move the servo to its neutral position.
9 | */
10 |
11 | #include "simple.h"
12 | #include
13 | #include
14 |
15 | #ifdef ARDUINO_ARCH_ESP32
16 | #include
17 | #else
18 | #include
19 | #endif
20 |
21 | #define BUTTON_PIN 2
22 | #define SERVO_PIN 3
23 |
24 | // The button instance for switching animation modes
25 | OneButton modeButton(BUTTON_PIN, true, true);
26 |
27 | // Servo object to send positions
28 | Servo myServo;
29 |
30 | // Callback function which is called whenever a servo needs to be moved
31 | void move(byte servoID, int position) {
32 | // Ignore the servoID (there is only one servo) and write the current position
33 | myServo.writeMicroseconds(position);
34 | }
35 |
36 | // Callback function which is called whenever the animation mode changes
37 | void modeChanged(byte prevMode, byte newMode) {
38 | switch (newMode) {
39 | case BlenderServoAnimation::MODE_PLAY:
40 | if (prevMode == BlenderServoAnimation::MODE_PAUSE) {
41 | // E.g. resume audio
42 | } else {
43 | // E.g. start audio
44 | }
45 | break;
46 | case BlenderServoAnimation::MODE_PAUSE:
47 | // E.g. pause audio
48 | break;
49 | case BlenderServoAnimation::MODE_STOP:
50 | // E.g. stop audio
51 | break;
52 | }
53 | }
54 |
55 | // Animation object to control the animation
56 | BlenderServoAnimation animation;
57 |
58 | // Callback to be triggered on a short button press
59 | void onPressed() {
60 | // Get the current mode, act accordingly and trigger another mode
61 | switch (animation.getMode()) {
62 | // On short press in default or pause mode, we want to start or resume the
63 | // animation
64 | case BlenderServoAnimation::MODE_DEFAULT:
65 | case BlenderServoAnimation::MODE_PAUSE:
66 | animation.play();
67 | break;
68 | // On short press in play mode, we want to pause the animation
69 | case BlenderServoAnimation::MODE_PLAY:
70 | animation.pause();
71 | break;
72 | }
73 | }
74 |
75 | // Callback to be triggered on a long button press
76 | void onLongPressed() {
77 | // Get the current mode, act accordingly and trigger another mode
78 | switch (animation.getMode()) {
79 | // On long press in play, pause or live mode, we want to stop the animation
80 | case BlenderServoAnimation::MODE_PLAY:
81 | case BlenderServoAnimation::MODE_PAUSE:
82 | animation.stop();
83 | break;
84 | }
85 | }
86 |
87 | void setup() {
88 | // Attach the servo to the defined servo pin
89 | myServo.attach(SERVO_PIN);
90 |
91 | // Attach the callbacks to the mode button
92 | modeButton.attachClick(onPressed);
93 | modeButton.attachLongPressStart(onLongPressed);
94 |
95 | // Set the position callback
96 | animation.onPositionChange(move);
97 |
98 | // Add a scene based on PROGMEM data
99 | animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);
100 |
101 | // Register the mode change callback function
102 | animation.onModeChange(modeChanged);
103 | }
104 |
105 | void loop() {
106 | // Update the mode button state on each loop
107 | modeButton.tick();
108 |
109 | // Update the animation state on each loop
110 | animation.run();
111 | }
112 |
--------------------------------------------------------------------------------
/examples/SwitchModeButton/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | framework = arduino
6 |
7 | [atmelavr_base]
8 | platform = atmelavr
9 | lib_deps =
10 | arduino-libraries/Servo@^1.1.8
11 | mathertel/OneButton@^2.0.3
12 | BlenderServoAnimation=symlink://../../
13 |
14 | [env:uno]
15 | extends = atmelavr_base
16 | board = uno
17 |
18 | [env:ATmega2560]
19 | extends = atmelavr_base
20 | board = ATmega2560
21 |
22 | [env:nanoatmega328]
23 | extends = atmelavr_base
24 | board = nanoatmega328
25 |
26 | [env:esp32dev]
27 | platform = espressif32
28 | board = esp32dev
29 | lib_deps =
30 | madhephaestus/ESP32Servo@^0.13.0
31 | mathertel/OneButton@^2.0.3
32 | BlenderServoAnimation=symlink://../../
33 |
--------------------------------------------------------------------------------
/examples/SwitchModeButton/simple.h:
--------------------------------------------------------------------------------
1 | /*
2 | Blender Servo Animation Positions
3 |
4 | FPS: 30
5 | Frames: 100
6 | Seconds: 3
7 | Bones: 1
8 | Armature: Armature
9 | Scene: Scene
10 | File: simple.blend
11 | */
12 |
13 | #include
14 |
15 | const byte FPS = 30;
16 | const int FRAMES = 100;
17 | const int LENGTH = 600;
18 |
19 | const byte PROGMEM ANIMATION_DATA[LENGTH] = {
20 | 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc3, 0x3e, 0x0a,
21 | 0x3c, 0x00, 0x05, 0xca, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xd7, 0x3e, 0x0a,
22 | 0x3c, 0x00, 0x05, 0xe8, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xfd, 0x3e, 0x0a,
23 | 0x3c, 0x00, 0x06, 0x16, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0x32, 0x3e, 0x0a,
24 | 0x3c, 0x00, 0x06, 0x51, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0x73, 0x3e, 0x0a,
25 | 0x3c, 0x00, 0x06, 0x97, 0x3e, 0x0a, 0x3c, 0x00, 0x06, 0xbe, 0x3e, 0x0a,
26 | 0x3c, 0x00, 0x06, 0xe6, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0x0f, 0x3e, 0x0a,
27 | 0x3c, 0x00, 0x07, 0x39, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0x65, 0x3e, 0x0a,
28 | 0x3c, 0x00, 0x07, 0x90, 0x3e, 0x0a, 0x3c, 0x00, 0x07, 0xbb, 0x3e, 0x0a,
29 | 0x3c, 0x00, 0x07, 0xe7, 0x3e, 0x0a, 0x3c, 0x00, 0x08, 0x11, 0x3e, 0x0a,
30 | 0x3c, 0x00,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, 0x00, 0x03, 0x4f, 0x3e, 0x0a, 0x3c, 0x00, 0x03, 0x76, 0x3e, 0x0a,
60 | 0x3c, 0x00, 0x03, 0x9f, 0x3e, 0x0a, 0x3c, 0x00, 0x03, 0xc7, 0x3e, 0x0a,
61 | 0x3c, 0x00, 0x03, 0xf0, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0x19, 0x3e, 0x0a,
62 | 0x3c, 0x00, 0x04, 0x41, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0x6a, 0x3e, 0x0a,
63 | 0x3c, 0x00, 0x04, 0x91, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0xb7, 0x3e, 0x0a,
64 | 0x3c, 0x00, 0x04, 0xdb, 0x3e, 0x0a, 0x3c, 0x00, 0x04, 0xfe, 0x3e, 0x0a,
65 | 0x3c, 0x00, 0x05, 0x1f, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x3e, 0x3e, 0x0a,
66 | 0x3c, 0x00, 0x05, 0x5a, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x73, 0x3e, 0x0a,
67 | 0x3c, 0x00, 0x05, 0x8a, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0x9d, 0x3e, 0x0a,
68 | 0x3c, 0x00, 0x05, 0xac, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xb7, 0x3e, 0x0a,
69 | 0x3c, 0x00, 0x05, 0xbe, 0x3e, 0x0a, 0x3c, 0x00, 0x05, 0xc0, 0x3e, 0x0a,
70 | };
71 |
--------------------------------------------------------------------------------
/examples/WebSocketLiveMode/README.md:
--------------------------------------------------------------------------------
1 | # Web Socket Live Mode
2 |
3 | Sending live servo positions via web socket commands.
4 |
5 | This example requires an ESP32 and a running Blender instance with the Blender Servo Animation Add-on. We create an web socket server to receive live position values via Blender and move a single servo.
6 |
7 | ## Library Dependencies
8 |
9 | - [ESP32Servo](https://github.com/madhephaestus/ESP32Servo)
10 | - [ESPAsyncWebServer](https://github.com/esphome/ESPAsyncWebServer)
11 |
12 | ## Wiring Diagram
13 |
14 | 
15 |
--------------------------------------------------------------------------------
/examples/WebSocketLiveMode/WebSocketLiveMode.ino:
--------------------------------------------------------------------------------
1 | /*
2 | Sending live servo positions via web socket commands.
3 |
4 | This example requires an ESP32 and a running Blender instance with the Blender Servo Animation
5 | Add-on. We create a web socket server to receive live position values via Blender and move a
6 | single servo.
7 | */
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #define SERVO_PIN 12
15 | #define PORT 80
16 | #define PATH "/"
17 |
18 | // Change to your SSID and password
19 | const char *ssid = "SSID";
20 | const char *password = "PASSWORD";
21 |
22 | // Create an asynchronous web socket server
23 | AsyncWebServer server(PORT);
24 | AsyncWebSocket ws(PATH);
25 |
26 | // Servo object to send positions
27 | Servo myServo;
28 |
29 | // Callback function which is called whenever a servo needs to be moved
30 | void move(byte servoID, int position) {
31 | // Ignore the servoID (there is only one servo) and write the current position
32 | myServo.writeMicroseconds(position);
33 | }
34 |
35 | // Animation object to control the animation
36 | BlenderServoAnimation animation;
37 |
38 | // Callback function writing live stream data to the animation
39 | void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
40 | void *arg, uint8_t *data, size_t len) {
41 | if (type != WS_EVT_DATA) {
42 | return;
43 | }
44 |
45 | for (size_t i = 0; i < len; i++) {
46 | animation.writeLiveStream(data[i]);
47 | }
48 | }
49 |
50 | void setup() {
51 | Serial.begin(9600);
52 | while (!Serial) {
53 | };
54 |
55 | WiFi.begin(ssid, password);
56 | while (WiFi.status() != WL_CONNECTED) {
57 | delay(1000);
58 | Serial.println("Connecting to WiFi...");
59 | }
60 | Serial.println("Connected to WiFi");
61 | Serial.println(WiFi.localIP());
62 |
63 | // Register the web socket callback function
64 | ws.onEvent(onWebSocketEvent);
65 |
66 | // Add the web socket handler and start the server
67 | server.addHandler(&ws);
68 | server.begin();
69 |
70 | // Attach the servo to the defined servo pin
71 | myServo.attach(SERVO_PIN);
72 |
73 | // Set the position callback
74 | animation.onPositionChange(move);
75 |
76 | // Trigger the animation live mode
77 | animation.live();
78 | }
79 |
80 | void loop() {
81 | // Update the animation state on each loop
82 | animation.run();
83 | }
84 |
--------------------------------------------------------------------------------
/examples/WebSocketLiveMode/platformio.ini:
--------------------------------------------------------------------------------
1 | [platformio]
2 | src_dir = ./
3 |
4 | [env]
5 | lib_deps =
6 | madhephaestus/ESP32Servo@^0.13.0
7 | esphome/AsyncTCP-esphome@^1.2.2
8 | esphome/ESPAsyncWebServer-esphome@^2.1.0
9 | BlenderServoAnimation=symlink://../../
10 |
11 | [env:esp32dev]
12 | board = esp32dev
13 | platform = espressif32
14 | framework = arduino
15 |
--------------------------------------------------------------------------------
/images/arduino-nano-with-2-PCA9685.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/arduino-nano-with-2-PCA9685.png
--------------------------------------------------------------------------------
/images/arduino-nano-with-PCA9685.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/arduino-nano-with-PCA9685.png
--------------------------------------------------------------------------------
/images/arduino-nano-with-sd-module.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/arduino-nano-with-sd-module.png
--------------------------------------------------------------------------------
/images/arduino-nano-with-servo-and-button.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/arduino-nano-with-servo-and-button.png
--------------------------------------------------------------------------------
/images/arduino-nano-with-servo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/arduino-nano-with-servo.png
--------------------------------------------------------------------------------
/images/esp32-with-servo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timhendriks93/blender-servo-animation-arduino/b5a86175b10dcda3f35e7f0b8c45a2b9edc9efcf/images/esp32-with-servo.png
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=BlenderServoAnimation
2 | version=2.0.0
3 | author=Tim Hendriks
4 | maintainer=Tim Hendriks
5 | sentence=Library to control servos based on an exported Blender animation.
6 | paragraph=This library helps to control servos based on an exported Blender animation.
It is specifically designed to work with the Blender Servo Animation Add-on.
7 | category=Device Control
8 | url=https://github.com/timhendriks93/blender-servo-animation-arduino
9 | architectures=*
--------------------------------------------------------------------------------
/platformio.ini:
--------------------------------------------------------------------------------
1 | [env:native]
2 | platform = native
3 | test_build_src = yes
4 | build_flags =
5 | -Wno-deprecated-declarations
6 | debug_test = test_servo
7 | lib_deps =
8 | ArduinoFake
9 |
--------------------------------------------------------------------------------
/requirements-dev.txt:
--------------------------------------------------------------------------------
1 | platformio>=6.1
2 |
--------------------------------------------------------------------------------
/src/AnimationData.cpp:
--------------------------------------------------------------------------------
1 | #include "AnimationData.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::AnimationData;
5 |
6 | AnimationData::AnimationData() {
7 | }
8 |
9 | AnimationData::AnimationData(const byte *data, int dataSize) {
10 | this->data = data;
11 | this->dataSize = dataSize;
12 | }
13 |
14 | AnimationData::AnimationData(Stream *stream) {
15 | this->stream = stream;
16 | }
17 |
18 | bool AnimationData::isAvailable() {
19 | if (this->stream) {
20 | return this->stream->available() > 0;
21 | } else if (this->data) {
22 | return this->dataSize - this->dataPosition > 0;
23 | } else {
24 | return this->readIndex != this->writeIndex;
25 | }
26 | }
27 |
28 | byte AnimationData::getNextByte() {
29 | if (this->stream) {
30 | return this->stream->read();
31 | } else if (this->data) {
32 | return this->readProgmem();
33 | } else {
34 | return this->readBuffer();
35 | }
36 | }
37 |
38 | byte AnimationData::readProgmem() {
39 | if (this->dataPosition < this->dataSize) {
40 | return pgm_read_byte(this->data + this->dataPosition++);
41 | } else {
42 | return -1;
43 | }
44 | }
45 |
46 | byte AnimationData::readBuffer() {
47 | byte value = this->buffer[this->readIndex++];
48 |
49 | if (this->readIndex >= BUFFER_SIZE) {
50 | this->readIndex = 0;
51 | }
52 |
53 | return value;
54 | }
55 |
56 | void AnimationData::writeByte(byte value) {
57 | this->buffer[this->writeIndex++] = value;
58 |
59 | if (this->writeIndex >= BUFFER_SIZE) {
60 | this->writeIndex = 0;
61 | }
62 | }
63 |
64 | void AnimationData::reset() {
65 | if (this->data) {
66 | this->dataPosition = 0;
67 | } else {
68 | this->readIndex = 0;
69 | this->writeIndex = 0;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/AnimationData.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #ifndef BlenderServoAnimationLibrary_AnimationData_H
4 | #define BlenderServoAnimationLibrary_AnimationData_H
5 |
6 | namespace BlenderServoAnimationLibrary {
7 |
8 | class AnimationData {
9 |
10 | public:
11 | static const byte LINE_BREAK = 0xA;
12 |
13 | AnimationData();
14 | AnimationData(const byte *data, int dataSize);
15 | AnimationData(Stream *stream);
16 |
17 | bool isAvailable();
18 |
19 | byte getNextByte();
20 |
21 | void writeByte(byte value);
22 | void reset();
23 |
24 | private:
25 | static const byte BUFFER_SIZE = 64;
26 |
27 | int dataSize = 0;
28 | int dataPosition = 0;
29 |
30 | const byte *data = nullptr;
31 |
32 | Stream *stream = nullptr;
33 |
34 | byte buffer[BUFFER_SIZE] = {0};
35 | byte writeIndex = 0;
36 | byte readIndex = 0;
37 |
38 | byte readProgmem();
39 | byte readBuffer();
40 | };
41 |
42 | } // namespace BlenderServoAnimationLibrary
43 |
44 | #endif
--------------------------------------------------------------------------------
/src/BlenderServoAnimation.cpp:
--------------------------------------------------------------------------------
1 | #include "BlenderServoAnimation.h"
2 | #include
3 |
4 | BlenderServoAnimation::~BlenderServoAnimation() {
5 | if (this->scenes) {
6 | delete[] this->scenes;
7 | }
8 |
9 | if (this->liveStream != nullptr) {
10 | delete this->liveStream;
11 | }
12 |
13 | if (this->playedIndexes != nullptr) {
14 | delete[] this->playedIndexes;
15 | }
16 | }
17 |
18 | bool BlenderServoAnimation::hasFinished() {
19 | return this->playIndex + 1 >= this->addIndex;
20 | }
21 |
22 | bool BlenderServoAnimation::hasScenes() {
23 | return this->addIndex > 0;
24 | }
25 |
26 | void BlenderServoAnimation::addScene(const byte *data, int size, byte fps, int frames) {
27 | AnimationData *animationData = new AnimationData(data, size);
28 | Scene *scene = new Scene(&this->servoManager, animationData, fps, frames);
29 | this->registerScene(scene);
30 | }
31 |
32 | void BlenderServoAnimation::addScene(Stream &stream, byte fps, int frames) {
33 | AnimationData *animationData = new AnimationData(&stream);
34 | Scene *scene = new Scene(&this->servoManager, animationData, fps, frames);
35 | this->registerScene(scene);
36 | }
37 |
38 | void BlenderServoAnimation::registerScene(Scene *scene) {
39 | Scene **newScenes = new Scene *[this->addIndex + 1];
40 | bool *newPlayedIndexes = new bool[this->addIndex + 1];
41 |
42 | for (int i = 0; i < this->addIndex; i++) {
43 | newScenes[i] = this->scenes[i];
44 | newPlayedIndexes[i] = this->playedIndexes[i];
45 | }
46 |
47 | newScenes[this->addIndex] = scene;
48 | newPlayedIndexes[this->addIndex] = false;
49 |
50 | delete[] this->scenes;
51 | delete[] this->playedIndexes;
52 |
53 | this->scenes = newScenes;
54 | this->playedIndexes = newPlayedIndexes;
55 | this->addIndex++;
56 | }
57 |
58 | void BlenderServoAnimation::setDefaultServoThreshold(byte value) {
59 | this->servoManager.setDefaultThreshold(value);
60 | }
61 |
62 | void BlenderServoAnimation::setServoThreshold(byte id, byte value) {
63 | this->servoManager.setThreshold(id, value);
64 | }
65 |
66 | void BlenderServoAnimation::setServoOffset(byte id, int offset) {
67 | this->servoManager.setOffset(id, offset);
68 | }
69 |
70 | void BlenderServoAnimation::setScene(byte index) {
71 | if (!this->scenes || index >= this->addIndex) {
72 | return;
73 | }
74 |
75 | byte prevIndex = this->playIndex;
76 |
77 | this->playIndex = index;
78 | this->playedIndexes[index] = true;
79 | this->scene = this->scenes[index];
80 | this->scene->reset();
81 |
82 | if (this->allScenesPlayed()) {
83 | this->resetPlayedScenes();
84 | }
85 |
86 | if (this->sceneCallback) {
87 | this->sceneCallback(prevIndex, index);
88 | }
89 | }
90 |
91 | void BlenderServoAnimation::setRandomScene() {
92 | byte randomIndex = 0;
93 |
94 | if (this->addIndex > 1) {
95 | do {
96 | randomIndex = random(this->addIndex);
97 | } while (this->playedIndexes[randomIndex]);
98 | }
99 |
100 | this->setScene(randomIndex);
101 | }
102 |
103 | void BlenderServoAnimation::resetScene() {
104 | byte prevIndex = this->playIndex;
105 |
106 | this->scene = nullptr;
107 | this->playIndex = 0;
108 |
109 | if (this->sceneCallback) {
110 | this->sceneCallback(prevIndex, 0);
111 | }
112 | }
113 |
114 | void BlenderServoAnimation::resetPlayedScenes() {
115 | for (int i = 0; i < this->addIndex; i++) {
116 | this->playedIndexes[i] = false;
117 | }
118 | }
119 |
120 | void BlenderServoAnimation::play() {
121 | if (!this->hasScenes() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
122 | return;
123 | }
124 |
125 | if (!this->scene) {
126 | this->setScene(this->playIndex);
127 | }
128 |
129 | this->changeMode(MODE_PLAY);
130 | }
131 |
132 | void BlenderServoAnimation::playSingle(byte index) {
133 | bool indexExists = this->scenes && index < this->addIndex;
134 | bool modeIsAllowed = this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE);
135 | bool pausedOtherScene = this->mode == MODE_PAUSE && this->playIndex != index;
136 |
137 | if (!indexExists || !modeIsAllowed || pausedOtherScene) {
138 | return;
139 | }
140 |
141 | if (!this->scene || this->scene->getFrame() == 0) {
142 | this->setScene(index);
143 | }
144 |
145 | this->changeMode(MODE_PLAY_SINGLE);
146 | }
147 |
148 | void BlenderServoAnimation::playRandom() {
149 | if (!this->hasScenes() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
150 | return;
151 | }
152 |
153 | if (!this->scene || this->scene->getFrame() == 0) {
154 | this->setRandomScene();
155 | }
156 |
157 | this->changeMode(MODE_PLAY_RANDOM);
158 | }
159 |
160 | void BlenderServoAnimation::loop() {
161 | if (!this->hasScenes() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
162 | return;
163 | }
164 |
165 | if (!this->scene) {
166 | this->setScene(this->playIndex);
167 | }
168 |
169 | this->changeMode(MODE_LOOP);
170 | }
171 |
172 | void BlenderServoAnimation::pause() {
173 | if (!this->scene || this->modeIsIn(4, MODE_DEFAULT, MODE_PAUSE, MODE_STOP, MODE_LIVE)) {
174 | return;
175 | }
176 |
177 | this->changeMode(MODE_PAUSE);
178 | }
179 |
180 | void BlenderServoAnimation::stop() {
181 | if (this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
182 | return;
183 | }
184 |
185 | this->changeMode(MODE_STOP);
186 | }
187 |
188 | void BlenderServoAnimation::live() {
189 | if (this->mode != MODE_DEFAULT) {
190 | return;
191 | }
192 |
193 | if (this->liveStream != nullptr) {
194 | delete this->liveStream;
195 | }
196 |
197 | this->liveStream = new AnimationData();
198 | this->changeMode(MODE_LIVE);
199 | }
200 |
201 | void BlenderServoAnimation::live(Stream &stream) {
202 | if (this->mode != MODE_DEFAULT) {
203 | return;
204 | }
205 |
206 | if (this->liveStream != nullptr) {
207 | delete this->liveStream;
208 | }
209 |
210 | this->liveStream = new AnimationData(&stream);
211 | this->changeMode(MODE_LIVE);
212 | }
213 |
214 | void BlenderServoAnimation::writeLiveStream(byte value) {
215 | if (this->liveStream != nullptr) {
216 | this->liveStream->writeByte(value);
217 | }
218 | }
219 |
220 | byte BlenderServoAnimation::getMode() {
221 | return this->mode;
222 | }
223 |
224 | byte BlenderServoAnimation::getPlayIndex() {
225 | return this->playIndex;
226 | }
227 |
228 | bool BlenderServoAnimation::scenePlayed(int index) {
229 | return this->playedIndexes[index];
230 | }
231 |
232 | bool BlenderServoAnimation::allScenesPlayed() {
233 | for (int i = 0; i < this->addIndex; i++) {
234 | if (!this->playedIndexes[i]) {
235 | return false;
236 | }
237 | }
238 |
239 | return true;
240 | }
241 |
242 | void BlenderServoAnimation::run(unsigned long currentMicros) {
243 | switch (this->mode) {
244 | case MODE_PLAY:
245 | case MODE_PLAY_SINGLE:
246 | case MODE_PLAY_RANDOM:
247 | case MODE_LOOP:
248 | this->handlePlayMode(currentMicros);
249 | break;
250 | case MODE_STOP:
251 | this->handleStopMode(currentMicros);
252 | break;
253 | case MODE_LIVE:
254 | this->handleLiveMode();
255 | break;
256 | }
257 | }
258 |
259 | void BlenderServoAnimation::handlePlayMode(unsigned long currentMicros) {
260 | if (!this->scene) {
261 | this->changeMode(MODE_DEFAULT);
262 | return;
263 | }
264 |
265 | this->scene->play(currentMicros);
266 |
267 | if (!this->scene->hasFinished()) {
268 | return;
269 | }
270 |
271 | switch (this->mode) {
272 | case MODE_PLAY:
273 | if (this->hasFinished()) {
274 | this->resetScene();
275 | } else {
276 | this->setScene(this->playIndex + 1);
277 | }
278 | break;
279 | case MODE_PLAY_SINGLE:
280 | this->resetScene();
281 | break;
282 | case MODE_PLAY_RANDOM:
283 | this->setRandomScene();
284 | this->changeMode(MODE_PLAY_RANDOM);
285 | break;
286 | case MODE_LOOP:
287 | if (this->hasFinished()) {
288 | this->setScene(0);
289 | } else {
290 | this->setScene(this->playIndex + 1);
291 | }
292 | this->changeMode(MODE_LOOP);
293 | break;
294 | }
295 |
296 | if (!this->scene) {
297 | this->changeMode(MODE_DEFAULT);
298 | }
299 | }
300 |
301 | void BlenderServoAnimation::handleStopMode(unsigned long currentMicros) {
302 | if (!this->scene) {
303 | this->changeMode(MODE_DEFAULT);
304 | return;
305 | }
306 |
307 | this->scene->stop(currentMicros);
308 |
309 | if (this->servoManager.servosAreAllNeutral()) {
310 | this->changeMode(MODE_DEFAULT);
311 | }
312 | }
313 |
314 | void BlenderServoAnimation::handleLiveMode() {
315 | this->servoManager.parseData(this->liveStream, false);
316 | }
317 |
318 | Scene *BlenderServoAnimation::getCurrentScene() {
319 | return this->scene;
320 | }
321 |
322 | void BlenderServoAnimation::onPositionChange(PositionCallback callback) {
323 | this->servoManager.setPositionCallback(callback);
324 | }
325 |
326 | void BlenderServoAnimation::onModeChange(ModeCallback callback) {
327 | this->modeCallback = callback;
328 | }
329 |
330 | void BlenderServoAnimation::onSceneChange(SceneCallback callback) {
331 | this->sceneCallback = callback;
332 | }
333 |
334 | void BlenderServoAnimation::changeMode(byte mode) {
335 | byte prevMode = this->mode;
336 | this->mode = mode;
337 |
338 | if (prevMode == MODE_LIVE) {
339 | delete this->liveStream;
340 | this->liveStream = nullptr;
341 | }
342 |
343 | if (this->modeCallback) {
344 | this->modeCallback(prevMode, mode);
345 | }
346 | }
347 |
348 | bool BlenderServoAnimation::modeIsIn(byte modeAmount, ...) {
349 | bool match = false;
350 |
351 | va_list modes;
352 | va_start(modes, modeAmount);
353 |
354 | for (int i = 0; i < modeAmount; i++) {
355 | if (this->mode == va_arg(modes, int)) {
356 | match = true;
357 | }
358 | }
359 |
360 | va_end(modes);
361 |
362 | return match;
363 | }
364 |
--------------------------------------------------------------------------------
/src/BlenderServoAnimation.h:
--------------------------------------------------------------------------------
1 | #include "AnimationData.h"
2 | #include "CommonTypes.h"
3 | #include "Scene.h"
4 | #include "ServoManager.h"
5 | #include
6 | #include
7 |
8 | #ifndef BlenderServoAnimation_H
9 | #define BlenderServoAnimation_H
10 |
11 | using BlenderServoAnimationLibrary::AnimationData;
12 | using BlenderServoAnimationLibrary::ModeCallback;
13 | using BlenderServoAnimationLibrary::PositionCallback;
14 | using BlenderServoAnimationLibrary::Scene;
15 | using BlenderServoAnimationLibrary::SceneCallback;
16 | using BlenderServoAnimationLibrary::ServoManager;
17 |
18 | class BlenderServoAnimation {
19 |
20 | public:
21 | static const byte MODE_DEFAULT = 0;
22 | static const byte MODE_PLAY = 1;
23 | static const byte MODE_PLAY_SINGLE = 2;
24 | static const byte MODE_PLAY_RANDOM = 3;
25 | static const byte MODE_PAUSE = 4;
26 | static const byte MODE_LOOP = 5;
27 | static const byte MODE_STOP = 6;
28 | static const byte MODE_LIVE = 7;
29 |
30 | ~BlenderServoAnimation();
31 |
32 | byte getMode();
33 | byte getPlayIndex();
34 |
35 | void addScene(const byte *data, int size, byte fps, int frames);
36 | void addScene(Stream &stream, byte fps, int frame);
37 | void onPositionChange(PositionCallback callback);
38 | void onModeChange(ModeCallback callback);
39 | void onSceneChange(SceneCallback callback);
40 | void run(unsigned long currentMicros = micros());
41 | void play();
42 | void playSingle(byte index);
43 | void playRandom();
44 | void loop();
45 | void pause();
46 | void stop();
47 | void live();
48 | void live(Stream &stream);
49 | void writeLiveStream(byte value);
50 | void setDefaultServoThreshold(byte value);
51 | void setServoThreshold(byte id, byte value);
52 | void setServoOffset(byte id, int offset);
53 |
54 | bool hasFinished();
55 | bool hasScenes();
56 | bool scenePlayed(int index);
57 | bool allScenesPlayed();
58 |
59 | Scene *getCurrentScene();
60 |
61 | private:
62 | ServoManager servoManager;
63 |
64 | Scene **scenes = nullptr;
65 | Scene *scene = nullptr;
66 |
67 | AnimationData *liveStream = nullptr;
68 |
69 | bool *playedIndexes = nullptr;
70 |
71 | ModeCallback modeCallback = nullptr;
72 | SceneCallback sceneCallback = nullptr;
73 |
74 | byte mode = MODE_DEFAULT;
75 |
76 | int addIndex = 0;
77 | int playIndex = 0;
78 |
79 | void registerScene(Scene *scene);
80 | void changeMode(byte mode);
81 | void handlePlayMode(unsigned long currentMicros);
82 | void handleStopMode(unsigned long currentMicros);
83 | void handleLiveMode();
84 | void setScene(byte index);
85 | void setRandomScene();
86 | void resetScene();
87 | void resetPlayedScenes();
88 |
89 | bool modeIsIn(byte modeAmount, ...);
90 | };
91 |
92 | #endif
--------------------------------------------------------------------------------
/src/Command.cpp:
--------------------------------------------------------------------------------
1 | #include "Command.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::Command;
5 |
6 | void Command::write(byte value) {
7 | if (value == START_MARKER) {
8 | this->reset();
9 | }
10 |
11 | if (this->index >= LENGTH) {
12 | return;
13 | }
14 |
15 | this->values[this->index] = value;
16 | this->index++;
17 | }
18 |
19 | bool Command::isValid() {
20 | return this->values[INDEX_START_MARKER] == START_MARKER &&
21 | this->values[INDEX_END_MARKER] == END_MARKER;
22 | }
23 |
24 | byte Command::getServoID() {
25 | return this->values[INDEX_SERVO_ID];
26 | }
27 |
28 | int Command::getServoPosition() {
29 | return this->values[INDEX_POSITION_BYTE] | this->values[INDEX_POSITION_SIG_BYTE] << BITS;
30 | }
31 |
32 | void Command::reset() {
33 | for (byte i = 0; i < LENGTH; i++) {
34 | this->values[i] = 0;
35 | }
36 |
37 | this->index = 0;
38 | }
--------------------------------------------------------------------------------
/src/Command.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #ifndef BlenderServoAnimationLibrary_Command_H
4 | #define BlenderServoAnimationLibrary_Command_H
5 |
6 | namespace BlenderServoAnimationLibrary {
7 |
8 | class Command {
9 |
10 | public:
11 | static const byte LINE_BREAK = 0xA;
12 |
13 | void write(byte value);
14 |
15 | bool isValid();
16 |
17 | byte getServoID();
18 |
19 | int getServoPosition();
20 |
21 | private:
22 | static const byte START_MARKER = 0x3C;
23 | static const byte END_MARKER = 0x3E;
24 |
25 | static const byte LENGTH = 5;
26 | static const byte BITS = 8;
27 |
28 | static const byte INDEX_START_MARKER = 0;
29 | static const byte INDEX_SERVO_ID = 1;
30 | static const byte INDEX_POSITION_SIG_BYTE = 2;
31 | static const byte INDEX_POSITION_BYTE = 3;
32 | static const byte INDEX_END_MARKER = 4;
33 |
34 | byte values[LENGTH] = {0};
35 | byte index = 0;
36 |
37 | void reset();
38 | };
39 |
40 | } // namespace BlenderServoAnimationLibrary
41 |
42 | #endif
--------------------------------------------------------------------------------
/src/CommonTypes.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #ifndef BlenderServoAnimationLibrary_CommonTypes_H
4 | #define BlenderServoAnimationLibrary_CommonTypes_H
5 |
6 | namespace BlenderServoAnimationLibrary {
7 |
8 | typedef void (*ModeCallback)(byte, byte);
9 | typedef void (*PositionCallback)(byte, int);
10 | typedef void (*SceneCallback)(byte, byte);
11 |
12 | } // namespace BlenderServoAnimationLibrary
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/src/Scene.cpp:
--------------------------------------------------------------------------------
1 | #include "Scene.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::Scene;
5 |
6 | Scene::Scene(ServoManager *servoManager, AnimationData *data, byte fps, int frames) {
7 | this->servoManager = servoManager;
8 | this->data = data;
9 | this->fps = fps;
10 | this->frames = frames;
11 | this->frameMicros = round((float)Scene::SECOND_IN_MICROS / (float)fps);
12 | this->diffPerSecond = Scene::SECOND_IN_MICROS - (this->frameMicros * fps);
13 | }
14 |
15 | Scene::~Scene() {
16 | if (this->data) {
17 | delete this->data;
18 | }
19 | }
20 |
21 | void Scene::play(unsigned long currentMicros) {
22 | if (this->frames == 0) {
23 | this->servoManager->parseData(this->data);
24 | return;
25 | }
26 |
27 | if (!this->isNewFrame(currentMicros)) {
28 | return;
29 | }
30 |
31 | this->lastMicros = currentMicros;
32 | this->frame++;
33 |
34 | if (this->frame % this->fps == 0) {
35 | this->lastMicros += this->diffPerSecond;
36 | }
37 |
38 | this->servoManager->parseData(this->data);
39 | }
40 |
41 | void Scene::stop(unsigned long currentMicros) {
42 | if (this->getMicrosDiff(currentMicros) < STOP_DELAY_IN_MICROS) {
43 | return;
44 | }
45 |
46 | this->lastMicros = currentMicros;
47 | this->servoManager->moveAllTowardsNeutral();
48 |
49 | if (this->servoManager->servosAreAllNeutral()) {
50 | this->reset();
51 | }
52 | }
53 |
54 | void Scene::reset() {
55 | this->frame = 0;
56 | this->data->reset();
57 | }
58 |
59 | unsigned int Scene::getMicrosDiff(unsigned long currentMicros) {
60 | if (currentMicros >= this->lastMicros) {
61 | return currentMicros - this->lastMicros;
62 | }
63 |
64 | return MAX_MICROS - this->lastMicros + currentMicros;
65 | }
66 |
67 | bool Scene::isNewFrame(unsigned long currentMicros) {
68 | return this->lastMicros == 0 || this->getMicrosDiff(currentMicros) >= this->frameMicros;
69 | }
70 |
71 | bool Scene::hasFinished() {
72 | return this->frame == this->frames;
73 | }
74 |
75 | byte Scene::getFPS() {
76 | return this->fps;
77 | }
78 |
79 | int Scene::getFrame() {
80 | return this->frame;
81 | }
82 |
83 | int Scene::getFrames() {
84 | return this->frames;
85 | }
86 |
--------------------------------------------------------------------------------
/src/Scene.h:
--------------------------------------------------------------------------------
1 | #include "AnimationData.h"
2 | #include "ServoManager.h"
3 | #include
4 |
5 | #ifndef BlenderServoAnimationLibrary_Scene_H
6 | #define BlenderServoAnimationLibrary_Scene_H
7 |
8 | namespace BlenderServoAnimationLibrary {
9 |
10 | class Scene {
11 |
12 | public:
13 | Scene(ServoManager *servoManager, AnimationData *data, byte fps, int frames);
14 | ~Scene();
15 |
16 | void play(unsigned long currentMicros);
17 | void stop(unsigned long currentMicros);
18 | void reset();
19 |
20 | bool hasFinished();
21 |
22 | byte getFPS();
23 |
24 | int getFrame();
25 | int getFrames();
26 |
27 | private:
28 | static const unsigned long MAX_MICROS = 4294967295;
29 | static const unsigned long SECOND_IN_MICROS = 1000000;
30 | static const unsigned int STOP_DELAY_IN_MICROS = 10000;
31 |
32 | byte fps = 0;
33 |
34 | int diffPerSecond = 0;
35 |
36 | unsigned int frame = 0;
37 | unsigned int frames = 0;
38 | unsigned int frameMicros = 0;
39 |
40 | unsigned long lastMicros = 0;
41 |
42 | ServoManager *servoManager = nullptr;
43 |
44 | AnimationData *data = nullptr;
45 |
46 | bool isNewFrame(unsigned long currentMicros);
47 |
48 | unsigned int getMicrosDiff(unsigned long currentMicros);
49 | };
50 |
51 | } // namespace BlenderServoAnimationLibrary
52 |
53 | #endif
--------------------------------------------------------------------------------
/src/Servo.cpp:
--------------------------------------------------------------------------------
1 | #include "Servo.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::Servo;
5 |
6 | Servo::Servo(byte id, PositionCallback callback, byte threshold) {
7 | this->id = id;
8 | this->positionCallback = callback;
9 | this->setThreshold(threshold);
10 | }
11 |
12 | void Servo::move(int position, bool useOffset) {
13 | if (useOffset && this->offset != 0) {
14 | position += this->offset;
15 | }
16 |
17 | if (position == this->currentPosition || this->positionExceedsThreshold(position)) {
18 | return;
19 | }
20 |
21 | this->positionCallback(this->id, position);
22 | this->currentPosition = position;
23 |
24 | if (this->neutralPosition == -1) {
25 | this->neutralPosition = this->currentPosition;
26 | }
27 | }
28 |
29 | void Servo::moveTowardsNeutral() {
30 | if (this->neutralPosition == -1) {
31 | return;
32 | }
33 |
34 | if (this->threshold == 0) {
35 | return this->move(this->neutralPosition, false);
36 | }
37 |
38 | int newPosition = this->currentPosition;
39 |
40 | if (abs(this->neutralPosition - newPosition) < this->step) {
41 | newPosition = this->neutralPosition;
42 | } else if (this->currentPosition > this->neutralPosition) {
43 | newPosition -= this->step;
44 | } else if (this->currentPosition < this->neutralPosition) {
45 | newPosition += this->step;
46 | }
47 |
48 | this->move(newPosition, false);
49 | }
50 |
51 | bool Servo::isNeutral() {
52 | return this->currentPosition == this->neutralPosition;
53 | }
54 |
55 | void Servo::setPositionCallback(PositionCallback callback) {
56 | this->positionCallback = callback;
57 | }
58 |
59 | void Servo::setThreshold(byte value) {
60 | this->threshold = value;
61 | this->step = round(value / STEP_DIVIDER);
62 | }
63 |
64 | void Servo::setOffset(int offset) {
65 | this->offset = offset;
66 | }
67 |
68 | bool Servo::positionExceedsThreshold(int position) {
69 | if (this->currentPosition == -1) {
70 | return false;
71 | }
72 |
73 | int positionDiff = abs(position - this->currentPosition);
74 |
75 | return this->threshold && positionDiff > this->threshold;
76 | }
77 |
78 | byte Servo::getId() {
79 | return this->id;
80 | }
81 |
--------------------------------------------------------------------------------
/src/Servo.h:
--------------------------------------------------------------------------------
1 | #include "CommonTypes.h"
2 | #include
3 |
4 | #ifndef BlenderServoAnimationLibrary_Servo_H
5 | #define BlenderServoAnimationLibrary_Servo_H
6 |
7 | namespace BlenderServoAnimationLibrary {
8 |
9 | class Servo {
10 |
11 | public:
12 | Servo(byte id, PositionCallback callback, byte threshold = 0);
13 |
14 | void move(int position, bool useOffset = true);
15 | void moveTowardsNeutral();
16 | void setThreshold(byte value);
17 | void setOffset(int offset);
18 | void setPositionCallback(PositionCallback callback);
19 |
20 | bool isNeutral();
21 |
22 | byte getId();
23 |
24 | private:
25 | const byte STEP_DIVIDER = 10;
26 |
27 | byte id = 0;
28 | byte threshold = 0;
29 | byte step = 0;
30 |
31 | int neutralPosition = -1;
32 | int currentPosition = -1;
33 | int offset = 0;
34 |
35 | PositionCallback positionCallback = nullptr;
36 |
37 | bool positionExceedsThreshold(int position);
38 | };
39 |
40 | } // namespace BlenderServoAnimationLibrary
41 |
42 | #endif
--------------------------------------------------------------------------------
/src/ServoManager.cpp:
--------------------------------------------------------------------------------
1 | #include "ServoManager.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::Servo;
5 | using BlenderServoAnimationLibrary::ServoManager;
6 |
7 | ServoManager::~ServoManager() {
8 | if (this->servos) {
9 | delete[] this->servos;
10 | }
11 | }
12 |
13 | void ServoManager::setPositionCallback(PositionCallback callback) {
14 | this->positionCallback = callback;
15 |
16 | for (byte i = 0; i < this->servoAmount; i++) {
17 | this->servos[i]->setPositionCallback(callback);
18 | }
19 | }
20 |
21 | void ServoManager::setDefaultThreshold(byte value) {
22 | this->defaultThreshold = value;
23 | }
24 |
25 | void ServoManager::setThreshold(byte servoId, byte value) {
26 | Servo *servo = this->getServo(servoId);
27 |
28 | servo->setThreshold(value);
29 | }
30 |
31 | void ServoManager::setOffset(byte servoId, int offset) {
32 | Servo *servo = this->getServo(servoId);
33 |
34 | servo->setOffset(offset);
35 | }
36 |
37 | void ServoManager::parseData(AnimationData *data, bool considerLineBreaks) {
38 | if (!data || !this->positionCallback) {
39 | return;
40 | }
41 |
42 | while (data->isAvailable()) {
43 | byte value = data->getNextByte();
44 |
45 | if (considerLineBreaks && value == Command::LINE_BREAK) {
46 | break;
47 | }
48 |
49 | this->command.write(value);
50 |
51 | this->handleCommand();
52 | }
53 | }
54 |
55 | void ServoManager::handleCommand() {
56 | if (!this->command.isValid()) {
57 | return;
58 | }
59 |
60 | byte id = this->command.getServoID();
61 | int position = this->command.getServoPosition();
62 | Servo *servo = this->getServo(id);
63 |
64 | servo->move(position);
65 | }
66 |
67 | void ServoManager::moveAllTowardsNeutral() {
68 | for (byte i = 0; i < this->servoAmount; i++) {
69 | Servo *servo = this->servos[i];
70 |
71 | if (!servo->isNeutral()) {
72 | servo->moveTowardsNeutral();
73 | }
74 | }
75 | }
76 |
77 | bool ServoManager::servosAreAllNeutral() {
78 | for (byte i = 0; i < this->servoAmount; i++) {
79 | Servo *servo = this->servos[i];
80 |
81 | if (!servo->isNeutral()) {
82 | return false;
83 | }
84 | }
85 |
86 | return true;
87 | }
88 |
89 | Servo *ServoManager::addServo(byte id) {
90 | Servo *servo = new Servo(id, this->positionCallback, this->defaultThreshold);
91 | Servo **newServos = new Servo *[this->servoAmount + 1];
92 |
93 | for (byte i = 0; i < this->servoAmount; i++) {
94 | newServos[i] = this->servos[i];
95 | }
96 |
97 | newServos[this->servoAmount] = servo;
98 |
99 | delete[] this->servos;
100 |
101 | this->servos = newServos;
102 | this->servoAmount++;
103 |
104 | return servo;
105 | }
106 |
107 | Servo *ServoManager::getServo(byte id) {
108 | for (byte i = 0; i < this->servoAmount; i++) {
109 | Servo *servo = this->servos[i];
110 |
111 | if (servo->getId() == id) {
112 | return servo;
113 | }
114 | }
115 |
116 | return this->addServo(id);
117 | }
118 |
--------------------------------------------------------------------------------
/src/ServoManager.h:
--------------------------------------------------------------------------------
1 | #include "AnimationData.h"
2 | #include "Command.h"
3 | #include "CommonTypes.h"
4 | #include "Servo.h"
5 | #include
6 |
7 | #ifndef BlenderServoAnimationLibrary_ServoManager_H
8 | #define BlenderServoAnimationLibrary_ServoManager_H
9 |
10 | namespace BlenderServoAnimationLibrary {
11 |
12 | class ServoManager {
13 |
14 | public:
15 | ~ServoManager();
16 |
17 | void setPositionCallback(PositionCallback callback);
18 | void setDefaultThreshold(byte value);
19 | void setThreshold(byte servoId, byte value);
20 | void setOffset(byte servoId, int offset);
21 | void parseData(AnimationData *data, bool considerLineBreaks = true);
22 | void moveAllTowardsNeutral();
23 |
24 | bool servosAreAllNeutral();
25 |
26 | private:
27 | Servo **servos = nullptr;
28 |
29 | Command command;
30 |
31 | PositionCallback positionCallback = nullptr;
32 |
33 | byte servoAmount = 0;
34 | byte defaultThreshold = 0;
35 |
36 | void handleCommand();
37 |
38 | Servo *addServo(byte id);
39 | Servo *getServo(byte id);
40 | };
41 |
42 | } // namespace BlenderServoAnimationLibrary
43 |
44 | #endif
45 |
--------------------------------------------------------------------------------
/test/animation/test_live/test_live.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | }
10 |
11 | void test_prevented(void) {
12 | BlenderServoAnimation animation;
13 | StreamMock mock;
14 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
15 |
16 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0);
17 |
18 | animation.loop();
19 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
20 | animation.live(mock);
21 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
22 | animation.pause();
23 | animation.play();
24 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
25 | animation.live(mock);
26 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
27 | animation.pause();
28 | animation.playSingle(0);
29 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
30 | animation.live(mock);
31 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
32 | animation.pause();
33 | animation.playRandom();
34 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
35 | animation.live(mock);
36 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
37 | animation.pause();
38 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
39 | animation.live(mock);
40 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
41 | animation.stop();
42 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
43 | animation.live(mock);
44 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
45 | }
46 |
47 | void test_allowed(void) {
48 | StreamMock mock;
49 | BlenderServoAnimation animation;
50 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
51 |
52 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
53 | animation.live(mock);
54 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
55 | }
56 |
57 | int main(int argc, char **argv) {
58 | UNITY_BEGIN();
59 | RUN_TEST(test_prevented);
60 | RUN_TEST(test_allowed);
61 | UNITY_END();
62 | }
--------------------------------------------------------------------------------
/test/animation/test_loop/test_loop.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | resetPositionLog();
10 | }
11 |
12 | void test_loop(void) {
13 | BlenderServoAnimation animation;
14 | animation.onPositionChange(move);
15 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
16 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
17 |
18 | TEST_ASSERT_TRUE(animation.hasScenes());
19 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
20 |
21 | animation.loop();
22 |
23 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
24 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
25 |
26 | for (int i = 0; i < FRAME_MICROS * 5; i += FRAME_MICROS) {
27 | animation.run(i);
28 | }
29 |
30 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
31 | TEST_ASSERT_EQUAL(1, animation.getPlayIndex());
32 |
33 | for (int i = FRAME_MICROS * 5; i < FRAME_MICROS * 10; i += FRAME_MICROS) {
34 | animation.run(i);
35 | }
36 |
37 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
38 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
39 | TEST_ASSERT_EQUAL(20, logIndex);
40 | }
41 |
42 | void test_without_scenes(void) {
43 | BlenderServoAnimation animation;
44 | animation.onPositionChange(move);
45 | animation.loop();
46 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
47 | }
48 |
49 | void test_prevented(void) {
50 | StreamMock mock;
51 | BlenderServoAnimation animation;
52 | animation.onPositionChange(move);
53 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
54 |
55 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0);
56 |
57 | animation.play();
58 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
59 | animation.loop();
60 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
61 | animation.pause();
62 | animation.playSingle(0);
63 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
64 | animation.loop();
65 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
66 | animation.pause();
67 | animation.playRandom();
68 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
69 | animation.loop();
70 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
71 | animation.stop();
72 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
73 | animation.loop();
74 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
75 | animation.run(10000);
76 | animation.live(mock);
77 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
78 | animation.loop();
79 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
80 | }
81 |
82 | void test_allowed(void) {
83 | BlenderServoAnimation animation;
84 | animation.onPositionChange(move);
85 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
86 |
87 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
88 | animation.loop();
89 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
90 | animation.pause();
91 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
92 | animation.loop();
93 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
94 | }
95 |
96 | int main(int argc, char **argv) {
97 | UNITY_BEGIN();
98 | RUN_TEST(test_loop);
99 | RUN_TEST(test_without_scenes);
100 | RUN_TEST(test_prevented);
101 | RUN_TEST(test_allowed);
102 | UNITY_END();
103 | }
104 |
--------------------------------------------------------------------------------
/test/animation/test_mode_callback/test_mode_callback.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 |
4 | #include
5 |
6 | int prevMode = -1;
7 | int newMode = -1;
8 |
9 | void setUp(void) {
10 | prevMode = -1;
11 | newMode = -1;
12 | }
13 |
14 | void onModeChange(byte prevArg, byte newArg) {
15 | prevMode = prevArg;
16 | newMode = newArg;
17 | }
18 |
19 | void test_different_mode(void) {
20 | BlenderServoAnimation animation;
21 | animation.onPositionChange(move);
22 | animation.onModeChange(onModeChange);
23 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
24 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
25 |
26 | TEST_ASSERT_EQUAL(-1, prevMode);
27 | TEST_ASSERT_EQUAL(-1, newMode);
28 | animation.playSingle(1);
29 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, prevMode);
30 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, newMode);
31 | TEST_ASSERT_EQUAL(1, animation.getPlayIndex());
32 | animation.pause();
33 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, prevMode);
34 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, newMode);
35 | }
36 |
37 | void test_same_mode(void) {
38 | BlenderServoAnimation animation;
39 | animation.onPositionChange(move);
40 | animation.onModeChange(onModeChange);
41 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
42 |
43 | animation.loop();
44 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, prevMode);
45 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, newMode);
46 | animation.loop();
47 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, prevMode);
48 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, newMode);
49 | }
50 |
51 | void test_all_modes(void) {
52 | StreamMock mock;
53 | BlenderServoAnimation animation;
54 | animation.onPositionChange(move);
55 | animation.onModeChange(onModeChange);
56 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
57 |
58 | animation.play();
59 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, prevMode);
60 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, newMode);
61 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
62 | animation.pause();
63 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, prevMode);
64 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, newMode);
65 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
66 | animation.playRandom();
67 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, prevMode);
68 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, newMode);
69 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
70 | animation.pause();
71 | animation.playSingle(0);
72 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, prevMode);
73 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, newMode);
74 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
75 | animation.pause();
76 | animation.loop();
77 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, prevMode);
78 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, newMode);
79 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
80 | animation.stop();
81 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, prevMode);
82 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, newMode);
83 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
84 | animation.run(10000);
85 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, prevMode);
86 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, newMode);
87 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
88 | animation.live(mock);
89 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, prevMode);
90 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, newMode);
91 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
92 | }
93 |
94 | int main(int argc, char **argv) {
95 | UNITY_BEGIN();
96 | RUN_TEST(test_different_mode);
97 | RUN_TEST(test_same_mode);
98 | RUN_TEST(test_all_modes);
99 | UNITY_END();
100 | }
101 |
--------------------------------------------------------------------------------
/test/animation/test_pause/test_pause.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | resetPositionLog();
10 | }
11 |
12 | void test_pause(byte mode) {
13 | BlenderServoAnimation animation;
14 | animation.onPositionChange(move);
15 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
16 |
17 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
18 |
19 | switch (mode) {
20 | case BlenderServoAnimation::MODE_PLAY:
21 | animation.play();
22 | break;
23 | case BlenderServoAnimation::MODE_PLAY_SINGLE:
24 | animation.playSingle(0);
25 | break;
26 | case BlenderServoAnimation::MODE_PLAY_RANDOM:
27 | animation.playRandom();
28 | break;
29 | case BlenderServoAnimation::MODE_LOOP:
30 | animation.loop();
31 | break;
32 | }
33 |
34 | TEST_ASSERT_EQUAL(mode, animation.getMode());
35 | TEST_ASSERT_EQUAL(0, animation.getCurrentScene()->getFrame());
36 |
37 | for (int i = 0; i < FRAME_MICROS * 3; i += FRAME_MICROS) {
38 | animation.run(i);
39 | }
40 |
41 | TEST_ASSERT_EQUAL(3, animation.getCurrentScene()->getFrame());
42 |
43 | animation.pause();
44 |
45 | for (int i = FRAME_MICROS * 3; i < FRAME_MICROS * 6; i += FRAME_MICROS) {
46 | animation.run(i);
47 | }
48 |
49 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
50 | TEST_ASSERT_EQUAL(3, animation.getCurrentScene()->getFrame());
51 |
52 | switch (mode) {
53 | case BlenderServoAnimation::MODE_PLAY:
54 | animation.play();
55 | break;
56 | case BlenderServoAnimation::MODE_PLAY_SINGLE:
57 | animation.playSingle(0);
58 | break;
59 | case BlenderServoAnimation::MODE_PLAY_RANDOM:
60 | animation.playRandom();
61 | break;
62 | case BlenderServoAnimation::MODE_LOOP:
63 | animation.loop();
64 | break;
65 | }
66 |
67 | animation.run(FRAME_MICROS * 7);
68 |
69 | TEST_ASSERT_EQUAL(mode, animation.getMode());
70 | TEST_ASSERT_EQUAL(4, animation.getCurrentScene()->getFrame());
71 | TEST_ASSERT_EQUAL(8, logIndex);
72 | }
73 |
74 | void test_pause_play(void) {
75 | test_pause(BlenderServoAnimation::MODE_PLAY);
76 | }
77 |
78 | void test_pause_loop(void) {
79 | test_pause(BlenderServoAnimation::MODE_LOOP);
80 | }
81 |
82 | void test_pause_play_single(void) {
83 | test_pause(BlenderServoAnimation::MODE_PLAY_SINGLE);
84 | }
85 |
86 | void test_pause_play_random(void) {
87 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(1, 0);
88 | test_pause(BlenderServoAnimation::MODE_PLAY_RANDOM);
89 | }
90 |
91 | void test_prevented(void) {
92 | StreamMock mock;
93 | BlenderServoAnimation animation;
94 | animation.onPositionChange(move);
95 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
96 |
97 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
98 | animation.pause();
99 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
100 | animation.play();
101 | animation.stop();
102 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
103 | animation.pause();
104 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
105 | animation.run(10000);
106 | animation.live(mock);
107 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
108 | animation.pause();
109 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
110 | }
111 |
112 | void test_allowed(void) {
113 | BlenderServoAnimation animation;
114 | animation.onPositionChange(move);
115 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
116 |
117 | animation.play();
118 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
119 | animation.pause();
120 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
121 | animation.loop();
122 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
123 | animation.pause();
124 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
125 | }
126 |
127 | int main(int argc, char **argv) {
128 | UNITY_BEGIN();
129 | RUN_TEST(test_pause_play);
130 | RUN_TEST(test_pause_loop);
131 | RUN_TEST(test_pause_play_single);
132 | RUN_TEST(test_pause_play_random);
133 | RUN_TEST(test_prevented);
134 | RUN_TEST(test_allowed);
135 | UNITY_END();
136 | }
137 |
--------------------------------------------------------------------------------
/test/animation/test_play/test_play.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | resetPositionLog();
10 | }
11 |
12 | void test_play(void) {
13 | BlenderServoAnimation animation;
14 | animation.onPositionChange(move);
15 |
16 | TEST_ASSERT_FALSE(animation.hasScenes());
17 | TEST_ASSERT_EQUAL(nullptr, animation.getCurrentScene());
18 |
19 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
20 |
21 | TEST_ASSERT_TRUE(animation.hasScenes());
22 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
23 |
24 | animation.play();
25 |
26 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
27 | TEST_ASSERT_NOT_EQUAL(nullptr, animation.getCurrentScene());
28 | TEST_ASSERT_EQUAL(5, animation.getCurrentScene()->getFrames());
29 |
30 | for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
31 | animation.run(i);
32 | }
33 |
34 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
35 | TEST_ASSERT_EQUAL(nullptr, animation.getCurrentScene());
36 | TEST_ASSERT_EQUAL(10, logIndex);
37 | }
38 |
39 | void test_without_scenes(void) {
40 | BlenderServoAnimation animation;
41 | animation.onPositionChange(move);
42 | animation.play();
43 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
44 | }
45 |
46 | void test_prevented(void) {
47 | StreamMock mock;
48 | BlenderServoAnimation animation;
49 | animation.onPositionChange(move);
50 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
51 |
52 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0);
53 |
54 | animation.loop();
55 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
56 | animation.play();
57 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
58 | animation.pause();
59 | animation.playSingle(0);
60 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
61 | animation.play();
62 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
63 | animation.pause();
64 | animation.playRandom();
65 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
66 | animation.play();
67 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
68 | animation.stop();
69 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
70 | animation.play();
71 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
72 | animation.run(10000);
73 | animation.live(mock);
74 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
75 | animation.play();
76 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
77 | }
78 |
79 | void test_allowed(void) {
80 | BlenderServoAnimation animation;
81 | animation.onPositionChange(move);
82 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
83 |
84 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
85 | animation.play();
86 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
87 | animation.pause();
88 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
89 | animation.play();
90 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
91 | }
92 |
93 | int main(int argc, char **argv) {
94 | UNITY_BEGIN();
95 | RUN_TEST(test_play);
96 | RUN_TEST(test_without_scenes);
97 | RUN_TEST(test_prevented);
98 | RUN_TEST(test_allowed);
99 | UNITY_END();
100 | }
101 |
--------------------------------------------------------------------------------
/test/animation/test_play_random/test_play_random.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | resetPositionLog();
10 | }
11 |
12 | void test_play_random(void) {
13 | BlenderServoAnimation animation;
14 | animation.onPositionChange(move);
15 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
16 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
17 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
18 |
19 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
20 | TEST_ASSERT_FALSE(animation.scenePlayed(0));
21 | TEST_ASSERT_FALSE(animation.scenePlayed(1));
22 | TEST_ASSERT_FALSE(animation.scenePlayed(2));
23 |
24 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(1, 0, 0, 2);
25 |
26 | animation.playRandom();
27 |
28 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
29 | TEST_ASSERT_NOT_EQUAL(nullptr, animation.getCurrentScene());
30 | TEST_ASSERT_EQUAL(1, animation.getPlayIndex());
31 | TEST_ASSERT_FALSE(animation.scenePlayed(0));
32 | TEST_ASSERT_TRUE(animation.scenePlayed(1));
33 | TEST_ASSERT_FALSE(animation.scenePlayed(2));
34 |
35 | for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
36 | animation.run(i);
37 | }
38 |
39 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
40 | TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
41 | TEST_ASSERT_TRUE(animation.scenePlayed(0));
42 | TEST_ASSERT_TRUE(animation.scenePlayed(1));
43 | TEST_ASSERT_FALSE(animation.scenePlayed(2));
44 |
45 | for (long i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
46 | animation.run(i);
47 | }
48 |
49 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
50 | TEST_ASSERT_EQUAL(2, animation.getPlayIndex());
51 | TEST_ASSERT_EQUAL(20, logIndex);
52 | TEST_ASSERT_FALSE(animation.scenePlayed(0));
53 | TEST_ASSERT_FALSE(animation.scenePlayed(1));
54 | TEST_ASSERT_FALSE(animation.scenePlayed(2));
55 | }
56 |
57 | void test_without_scenes(void) {
58 | BlenderServoAnimation animation;
59 | animation.onPositionChange(move);
60 | animation.playRandom();
61 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
62 | }
63 |
64 | void test_prevented(void) {
65 | StreamMock mock;
66 | BlenderServoAnimation animation;
67 | animation.onPositionChange(move);
68 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
69 |
70 | animation.loop();
71 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
72 | animation.playRandom();
73 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
74 | animation.pause();
75 | animation.play();
76 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
77 | animation.playRandom();
78 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
79 | animation.pause();
80 | animation.playSingle(0);
81 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
82 | animation.playRandom();
83 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
84 | animation.stop();
85 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
86 | animation.playRandom();
87 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
88 | animation.run(10000);
89 | animation.live(mock);
90 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
91 | animation.playRandom();
92 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
93 | }
94 |
95 | void test_allowed(void) {
96 | BlenderServoAnimation animation;
97 | animation.onPositionChange(move);
98 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
99 |
100 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
101 | animation.playRandom();
102 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
103 | animation.pause();
104 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
105 | animation.playRandom();
106 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
107 | }
108 |
109 | int main(int argc, char **argv) {
110 | UNITY_BEGIN();
111 | RUN_TEST(test_play_random);
112 | RUN_TEST(test_without_scenes);
113 | RUN_TEST(test_prevented);
114 | RUN_TEST(test_allowed);
115 | UNITY_END();
116 | }
117 |
--------------------------------------------------------------------------------
/test/animation/test_play_single/test_play_single.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include
4 |
5 | using namespace fakeit;
6 |
7 | void setUp(void) {
8 | ArduinoFakeReset();
9 | resetPositionLog();
10 | }
11 |
12 | void test_play_single(void) {
13 | BlenderServoAnimation animation;
14 | animation.onPositionChange(move);
15 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
16 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
17 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
18 |
19 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
20 |
21 | animation.playSingle(2);
22 |
23 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
24 | TEST_ASSERT_EQUAL(2, animation.getPlayIndex());
25 |
26 | for (long i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
27 | animation.run(i);
28 | }
29 |
30 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
31 | TEST_ASSERT_EQUAL(10, logIndex);
32 | }
33 |
34 | void test_without_scenes(void) {
35 | BlenderServoAnimation animation;
36 | animation.onPositionChange(move);
37 | animation.playSingle(0);
38 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
39 | }
40 |
41 | void test_prevented(void) {
42 | StreamMock mock;
43 | BlenderServoAnimation animation;
44 | animation.onPositionChange(move);
45 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
46 |
47 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0);
48 |
49 | animation.loop();
50 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
51 | animation.playSingle(0);
52 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
53 | animation.pause();
54 | animation.play();
55 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
56 | animation.playSingle(0);
57 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
58 | animation.pause();
59 | animation.playRandom();
60 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
61 | animation.playSingle(0);
62 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
63 | animation.stop();
64 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
65 | animation.playSingle(0);
66 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
67 | animation.run(10000);
68 | animation.live(mock);
69 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
70 | animation.playSingle(0);
71 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
72 | }
73 |
74 | void test_allowed(void) {
75 | BlenderServoAnimation animation;
76 | animation.onPositionChange(move);
77 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
78 |
79 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
80 | animation.playSingle(0);
81 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
82 | animation.pause();
83 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
84 | animation.playSingle(0);
85 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
86 | }
87 |
88 | void test_prevent_sudden_index_change(void) {
89 | BlenderServoAnimation animation;
90 | animation.onPositionChange(move);
91 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
92 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
93 |
94 | animation.play();
95 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
96 | animation.run(FRAME_MICROS);
97 | animation.pause();
98 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
99 | animation.playSingle(1);
100 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PAUSE, animation.getMode());
101 | TEST_ASSERT_EQUAL(2, logIndex);
102 | }
103 |
104 | int main(int argc, char **argv) {
105 | UNITY_BEGIN();
106 | RUN_TEST(test_play_single);
107 | RUN_TEST(test_without_scenes);
108 | RUN_TEST(test_prevented);
109 | RUN_TEST(test_allowed);
110 | RUN_TEST(test_prevent_sudden_index_change);
111 | UNITY_END();
112 | }
113 |
--------------------------------------------------------------------------------
/test/animation/test_scene_callback/test_scene_callback.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 |
4 | #include
5 |
6 | int prevSceneIndex = -1;
7 | int nextSceneIndex = -1;
8 |
9 | void setUp(void) {
10 | prevSceneIndex = -1;
11 | nextSceneIndex = -1;
12 | }
13 |
14 | void onSceneChange(byte prevArg, byte newArg) {
15 | prevSceneIndex = prevArg;
16 | nextSceneIndex = newArg;
17 | }
18 |
19 | void test_play(void) {
20 | BlenderServoAnimation animation;
21 | animation.onSceneChange(onSceneChange);
22 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
23 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
24 |
25 | TEST_ASSERT_EQUAL(-1, prevSceneIndex);
26 | TEST_ASSERT_EQUAL(-1, nextSceneIndex);
27 |
28 | animation.play();
29 |
30 | TEST_ASSERT_EQUAL(0, prevSceneIndex);
31 | TEST_ASSERT_EQUAL(0, nextSceneIndex);
32 |
33 | for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
34 | animation.run(i);
35 | }
36 |
37 | TEST_ASSERT_EQUAL(0, prevSceneIndex);
38 | TEST_ASSERT_EQUAL(1, nextSceneIndex);
39 | }
40 |
41 | void test_play_single(void) {
42 | BlenderServoAnimation animation;
43 | animation.onSceneChange(onSceneChange);
44 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
45 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
46 |
47 | TEST_ASSERT_EQUAL(-1, prevSceneIndex);
48 | TEST_ASSERT_EQUAL(-1, nextSceneIndex);
49 |
50 | animation.playSingle(1);
51 |
52 | TEST_ASSERT_EQUAL(0, prevSceneIndex);
53 | TEST_ASSERT_EQUAL(1, nextSceneIndex);
54 |
55 | for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
56 | animation.run(i);
57 | }
58 |
59 | TEST_ASSERT_EQUAL(1, prevSceneIndex);
60 | TEST_ASSERT_EQUAL(0, nextSceneIndex);
61 | }
62 |
63 | int main(int argc, char **argv) {
64 | UNITY_BEGIN();
65 | RUN_TEST(test_play);
66 | RUN_TEST(test_play_single);
67 | UNITY_END();
68 | }
69 |
--------------------------------------------------------------------------------
/test/animation/test_stop/test_stop.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "BlenderServoAnimation.h"
3 | #include "Scene.h"
4 | #include
5 |
6 | using namespace fakeit;
7 |
8 | void setUp(void) {
9 | ArduinoFakeReset();
10 | resetPositionLog();
11 | }
12 |
13 | void test_stop(byte mode) {
14 | StreamMock mock;
15 | mock.availableValue = 0;
16 | BlenderServoAnimation animation;
17 | animation.onPositionChange(move);
18 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
19 |
20 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
21 |
22 | switch (mode) {
23 | case BlenderServoAnimation::MODE_PLAY:
24 | animation.play();
25 | break;
26 | case BlenderServoAnimation::MODE_PLAY_SINGLE:
27 | animation.playSingle(0);
28 | break;
29 | case BlenderServoAnimation::MODE_PLAY_RANDOM:
30 | animation.playRandom();
31 | break;
32 | case BlenderServoAnimation::MODE_LOOP:
33 | animation.loop();
34 | break;
35 | case BlenderServoAnimation::MODE_LIVE:
36 | animation.live(mock);
37 | break;
38 | }
39 |
40 | TEST_ASSERT_EQUAL(mode, animation.getMode());
41 |
42 | for (int i = 0; i < FRAME_MICROS * 2; i += FRAME_MICROS) {
43 | animation.run(i);
44 | }
45 |
46 | if (mode != BlenderServoAnimation::MODE_LIVE) {
47 | TEST_ASSERT_EQUAL(2, animation.getCurrentScene()->getFrame());
48 | }
49 |
50 | animation.stop();
51 |
52 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
53 |
54 | for (int i = FRAME_MICROS * 2; i < FRAME_MICROS * 4; i += FRAME_MICROS) {
55 | animation.run(i);
56 | }
57 |
58 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
59 | }
60 |
61 | void test_stop_play(void) {
62 | test_stop(BlenderServoAnimation::MODE_PLAY);
63 | }
64 |
65 | void test_stop_loop(void) {
66 | test_stop(BlenderServoAnimation::MODE_LOOP);
67 | }
68 |
69 | void test_stop_play_single(void) {
70 | test_stop(BlenderServoAnimation::MODE_PLAY_SINGLE);
71 | }
72 |
73 | void test_stop_play_random(void) {
74 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(1);
75 | test_stop(BlenderServoAnimation::MODE_PLAY_RANDOM);
76 | }
77 |
78 | void test_stop_live(void) {
79 | test_stop(BlenderServoAnimation::MODE_LIVE);
80 | }
81 |
82 | void test_prevented(void) {
83 | BlenderServoAnimation animation;
84 | animation.onPositionChange(move);
85 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
86 |
87 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
88 | animation.stop();
89 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_DEFAULT, animation.getMode());
90 | }
91 |
92 | void test_allowed(void) {
93 | StreamMock mock;
94 | BlenderServoAnimation animation;
95 | animation.onPositionChange(move);
96 | animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
97 |
98 | animation.play();
99 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY, animation.getMode());
100 | animation.stop();
101 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
102 | animation.run(10000);
103 | animation.playSingle(0);
104 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_SINGLE, animation.getMode());
105 | animation.stop();
106 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
107 | animation.run(10000);
108 | animation.playRandom();
109 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_PLAY_RANDOM, animation.getMode());
110 | animation.stop();
111 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
112 | animation.run(10000);
113 | animation.loop();
114 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LOOP, animation.getMode());
115 | animation.stop();
116 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
117 | animation.run(10000);
118 | animation.live(mock);
119 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_LIVE, animation.getMode());
120 | animation.stop();
121 | TEST_ASSERT_EQUAL(BlenderServoAnimation::MODE_STOP, animation.getMode());
122 | }
123 |
124 | int main(int argc, char **argv) {
125 | UNITY_BEGIN();
126 | RUN_TEST(test_stop_play);
127 | RUN_TEST(test_stop_play_single);
128 | RUN_TEST(test_stop_play_random);
129 | RUN_TEST(test_stop_loop);
130 | RUN_TEST(test_stop_live);
131 | RUN_TEST(test_prevented);
132 | RUN_TEST(test_allowed);
133 | UNITY_END();
134 | }
135 |
--------------------------------------------------------------------------------
/test/helper.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define FPS 30
4 | #define FRAMES 5
5 | #define FRAME_MICROS 33333
6 | #define ANIMATION_MICROS 166665
7 | #define STOP_MICROS 10000
8 | #define LOG_SIZE 20
9 | #define DATA_SIZE 55
10 |
11 | struct positionLog {
12 | byte servoId = 0;
13 | int position = 0;
14 | };
15 |
16 | positionLog positions[LOG_SIZE];
17 |
18 | byte logIndex = 0;
19 |
20 | void resetPositionLog(void) {
21 | for (int i = 0; i < LOG_SIZE; i++) {
22 | positions[i].servoId = 0;
23 | positions[i].position = 0;
24 | }
25 |
26 | logIndex = 0;
27 | }
28 |
29 | void move(byte servoID, int position) {
30 | positions[logIndex].servoId = servoID;
31 | positions[logIndex].position = position;
32 | logIndex++;
33 | }
34 |
35 | const byte PROGMEM PROGMEM_DATA[DATA_SIZE] = {
36 | 0x3c, 0x00, 0x01, 0x77, 0x3e, 0x3c, 0x01, 0x01, 0x77, 0x3e, 0x0a, 0x3c, 0x00, 0x01,
37 | 0x78, 0x3e, 0x3c, 0x01, 0x01, 0x78, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x79, 0x3e, 0x3c,
38 | 0x01, 0x01, 0x7b, 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x7c, 0x3e, 0x3c, 0x01, 0x01, 0x7f,
39 | 0x3e, 0x0a, 0x3c, 0x00, 0x01, 0x80, 0x3e, 0x3c, 0x01, 0x01, 0x84, 0x3e, 0x0a,
40 | };
41 |
42 | class StreamMock : public Stream {
43 | public:
44 | int availableValue = 1;
45 |
46 | int available() {
47 | return this->availableValue;
48 | }
49 |
50 | int read() {
51 | return 123;
52 | }
53 |
54 | int peek() {
55 | return 1;
56 | }
57 |
58 | void flush() {
59 | }
60 |
61 | size_t write(uint8_t) {
62 | return 1;
63 | }
64 | };
65 |
--------------------------------------------------------------------------------
/test/test_animation_data/test_animation_data.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "AnimationData.h"
3 | #include
4 |
5 | using BlenderServoAnimationLibrary::AnimationData;
6 |
7 | const byte PROGMEM values[5] = {60, 3, 1, 119, 62};
8 |
9 | void test_progmem_data(void) {
10 | AnimationData data(values, 5);
11 | TEST_ASSERT_TRUE(data.isAvailable());
12 |
13 | for (byte i = 0; i < 5; i++) {
14 | TEST_ASSERT_EQUAL(values[i], data.getNextByte());
15 | }
16 |
17 | TEST_ASSERT_FALSE(data.isAvailable());
18 | }
19 |
20 | void test_stream_data(void) {
21 | StreamMock stream;
22 | AnimationData data(&stream);
23 | TEST_ASSERT_TRUE(data.isAvailable());
24 | TEST_ASSERT_EQUAL(123, data.getNextByte());
25 | }
26 |
27 | void test_buffer_data(void) {
28 | AnimationData data;
29 | TEST_ASSERT_FALSE(data.isAvailable());
30 |
31 | for (byte i = 0; i < 5; i++) {
32 | data.writeByte(values[i]);
33 | }
34 |
35 | TEST_ASSERT_TRUE(data.isAvailable());
36 |
37 | for (byte i = 0; i < 5; i++) {
38 | TEST_ASSERT_EQUAL(values[i], data.getNextByte());
39 | }
40 |
41 | TEST_ASSERT_FALSE(data.isAvailable());
42 | }
43 |
44 | int main(int argc, char **argv) {
45 | UNITY_BEGIN();
46 | RUN_TEST(test_progmem_data);
47 | RUN_TEST(test_stream_data);
48 | RUN_TEST(test_buffer_data);
49 | UNITY_END();
50 | }
51 |
--------------------------------------------------------------------------------
/test/test_command/test_command.cpp:
--------------------------------------------------------------------------------
1 | #include "Command.h"
2 | #include
3 |
4 | using BlenderServoAnimationLibrary::Command;
5 |
6 | void test_valid(void) {
7 | byte values[5] = {60, 3, 1, 119, 62};
8 | Command command;
9 |
10 | for (int i = 0; i < 5; i++) {
11 | command.write(values[i]);
12 |
13 | if (i == 4) {
14 | TEST_ASSERT_TRUE(command.isValid());
15 | TEST_ASSERT_EQUAL(3, command.getServoID());
16 | TEST_ASSERT_EQUAL(375, command.getServoPosition());
17 | } else {
18 | TEST_ASSERT_FALSE(command.isValid());
19 | }
20 | }
21 | }
22 |
23 | void test_invalid(void) {
24 | byte values[5] = {60, 3, 1, 119, 0};
25 | Command command;
26 |
27 | for (int i = 0; i < 5; i++) {
28 | command.write(values[i]);
29 |
30 | TEST_ASSERT_FALSE(command.isValid());
31 | }
32 | }
33 |
34 | void test_incomplete(void) {
35 | byte values[13] = {60, 3, 1, 60, 3, 1, 119, 62, 60, 3, 1, 144, 62};
36 | Command command;
37 |
38 | for (int i = 0; i < 13; i++) {
39 | command.write(values[i]);
40 |
41 | if (i == 7 || i == 12) {
42 | TEST_ASSERT_TRUE(command.isValid());
43 | } else {
44 | TEST_ASSERT_FALSE(command.isValid());
45 | }
46 | }
47 | }
48 |
49 | int main(int argc, char **argv) {
50 | UNITY_BEGIN();
51 | RUN_TEST(test_valid);
52 | RUN_TEST(test_invalid);
53 | RUN_TEST(test_incomplete);
54 | UNITY_END();
55 | }
--------------------------------------------------------------------------------
/test/test_scene/test_scene.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "AnimationData.h"
3 | #include "Scene.h"
4 | #include
5 |
6 | using BlenderServoAnimationLibrary::AnimationData;
7 | using BlenderServoAnimationLibrary::Scene;
8 | using BlenderServoAnimationLibrary::ServoManager;
9 |
10 | void setUp(void) {
11 | resetPositionLog();
12 | }
13 |
14 | void test_play_with_frames(void) {
15 | ServoManager servoManager;
16 | servoManager.setPositionCallback(move);
17 | AnimationData *data = new AnimationData(PROGMEM_DATA, DATA_SIZE);
18 | Scene scene(&servoManager, data, FPS, FRAMES);
19 |
20 | positionLog exp[10] = {
21 | {0, 375}, {1, 375}, {0, 376}, {1, 376}, {0, 377},
22 | {1, 379}, {0, 380}, {1, 383}, {0, 384}, {1, 388},
23 | };
24 |
25 | for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
26 | scene.play(i);
27 | }
28 |
29 | TEST_ASSERT_EQUAL(10, logIndex);
30 |
31 | for (byte i = 0; i < 10; i++) {
32 | TEST_ASSERT_EQUAL(exp[i].servoId, positions[i].servoId);
33 | TEST_ASSERT_EQUAL(exp[i].position, positions[i].position);
34 | }
35 | }
36 |
37 | void test_stop(void) {
38 | ServoManager servoManager;
39 | servoManager.setPositionCallback(move);
40 | servoManager.setDefaultThreshold(20);
41 | AnimationData *data = new AnimationData(PROGMEM_DATA, DATA_SIZE);
42 | Scene scene(&servoManager, data, FPS, FRAMES);
43 |
44 | positionLog exp[15] = {
45 | {0, 375}, {1, 375}, {0, 376}, {1, 376}, {0, 377}, {1, 379}, {0, 380}, {1, 383},
46 | {0, 378}, {1, 381}, {0, 376}, {1, 379}, {0, 375}, {1, 377}, {1, 375},
47 | };
48 |
49 | for (int i = 0; i < FRAME_MICROS * 4; i += FRAME_MICROS) {
50 | scene.play(i);
51 | }
52 |
53 | TEST_ASSERT_FALSE(scene.hasFinished());
54 | TEST_ASSERT_EQUAL(4, scene.getFrame());
55 | TEST_ASSERT_EQUAL(8, logIndex);
56 |
57 | for (int i = 0; i < STOP_MICROS * 10; i += STOP_MICROS) {
58 | scene.stop(i);
59 | }
60 |
61 | TEST_ASSERT_EQUAL(0, scene.getFrame());
62 | TEST_ASSERT_EQUAL(15, logIndex);
63 |
64 | for (byte i = 0; i < 15; i++) {
65 | TEST_ASSERT_EQUAL(exp[i].servoId, positions[i].servoId);
66 | TEST_ASSERT_EQUAL(exp[i].position, positions[i].position);
67 | }
68 | }
69 |
70 | void test_has_finished(void) {
71 | ServoManager servoManager;
72 | AnimationData *data = new AnimationData(PROGMEM_DATA, DATA_SIZE);
73 | Scene scene(&servoManager, data, FPS, FRAMES);
74 |
75 | TEST_ASSERT_FALSE(scene.hasFinished());
76 |
77 | for (byte i = 0; i < FRAMES; i++) {
78 | scene.play(i * FRAME_MICROS);
79 |
80 | if (i == FRAMES - 1) {
81 | TEST_ASSERT_TRUE(scene.hasFinished());
82 | } else {
83 | TEST_ASSERT_FALSE(scene.hasFinished());
84 | }
85 | }
86 | }
87 |
88 | void test_get_frame(void) {
89 | ServoManager servoManager;
90 | AnimationData *data = new AnimationData(PROGMEM_DATA, DATA_SIZE);
91 | Scene scene(&servoManager, data, FPS, FRAMES);
92 |
93 | TEST_ASSERT_EQUAL(0, scene.getFrame());
94 |
95 | for (int i = 0; i < FRAMES; i++) {
96 | scene.play(i * FRAME_MICROS);
97 |
98 | TEST_ASSERT_EQUAL(i + 1, scene.getFrame());
99 | }
100 | }
101 |
102 | int main(int argc, char **argv) {
103 | UNITY_BEGIN();
104 | RUN_TEST(test_play_with_frames);
105 | RUN_TEST(test_stop);
106 | RUN_TEST(test_has_finished);
107 | RUN_TEST(test_get_frame);
108 | UNITY_END();
109 | }
--------------------------------------------------------------------------------
/test/test_servo/test_servo.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "Servo.h"
3 | #include
4 |
5 | using BlenderServoAnimationLibrary::Servo;
6 |
7 | void setUp(void) {
8 | resetPositionLog();
9 | }
10 |
11 | void test_move(void) {
12 | byte servoId = 2;
13 | Servo servo(servoId, move);
14 |
15 | TEST_ASSERT_TRUE(servo.isNeutral());
16 |
17 | servo.move(340);
18 |
19 | TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
20 | TEST_ASSERT_EQUAL(340, positions[0].position);
21 |
22 | servo.move(330);
23 |
24 | TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
25 | TEST_ASSERT_EQUAL(330, positions[1].position);
26 |
27 | servo.move(340);
28 |
29 | TEST_ASSERT_EQUAL(servoId, positions[2].servoId);
30 | TEST_ASSERT_EQUAL(340, positions[2].position);
31 |
32 | TEST_ASSERT_EQUAL(3, logIndex);
33 | }
34 |
35 | void test_move_towards_neutral(void) {
36 | byte servoId = 123;
37 | Servo servo(servoId, move, 10);
38 |
39 | servo.move(350);
40 |
41 | TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
42 | TEST_ASSERT_EQUAL(350, positions[0].position);
43 | TEST_ASSERT_TRUE(servo.isNeutral());
44 |
45 | servo.move(340);
46 |
47 | TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
48 | TEST_ASSERT_EQUAL(340, positions[1].position);
49 | TEST_ASSERT_FALSE(servo.isNeutral());
50 |
51 | for (int i = 0; i < 10; i++) {
52 | servo.moveTowardsNeutral();
53 |
54 | TEST_ASSERT_EQUAL(servoId, positions[2 + i].servoId);
55 | TEST_ASSERT_EQUAL(341 + i, positions[2 + i].position);
56 | }
57 |
58 | TEST_ASSERT_TRUE(servo.isNeutral());
59 | TEST_ASSERT_EQUAL(12, logIndex);
60 | }
61 |
62 | void test_threshold(void) {
63 | byte servoId = 3;
64 | Servo servo(servoId, move, 15);
65 |
66 | TEST_ASSERT_TRUE(servo.isNeutral());
67 |
68 | servo.move(340);
69 |
70 | TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
71 | TEST_ASSERT_EQUAL(340, positions[0].position);
72 |
73 | servo.move(330);
74 |
75 | TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
76 | TEST_ASSERT_EQUAL(330, positions[1].position);
77 |
78 | servo.move(310);
79 |
80 | TEST_ASSERT_EQUAL(0, positions[2].servoId);
81 | TEST_ASSERT_EQUAL(0, positions[2].position);
82 |
83 | TEST_ASSERT_EQUAL(2, logIndex);
84 | }
85 |
86 | void test_offset(void) {
87 | byte servoId = 3;
88 | Servo servo(servoId, move);
89 | servo.setOffset(10);
90 |
91 | TEST_ASSERT_TRUE(servo.isNeutral());
92 |
93 | servo.move(340);
94 |
95 | TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
96 | TEST_ASSERT_EQUAL(350, positions[0].position);
97 |
98 | servo.move(330);
99 |
100 | TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
101 | TEST_ASSERT_EQUAL(340, positions[1].position);
102 |
103 | servo.setOffset(-10);
104 | servo.move(320);
105 |
106 | TEST_ASSERT_EQUAL(servoId, positions[2].servoId);
107 | TEST_ASSERT_EQUAL(310, positions[2].position);
108 |
109 | TEST_ASSERT_EQUAL(3, logIndex);
110 | }
111 |
112 | void test_move_towards_neutral_with_offset(void) {
113 | byte servoId = 123;
114 | Servo servo(servoId, move, 10);
115 | servo.setOffset(10);
116 |
117 | servo.move(350);
118 |
119 | TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
120 | TEST_ASSERT_EQUAL(360, positions[0].position);
121 | TEST_ASSERT_TRUE(servo.isNeutral());
122 |
123 | servo.move(340);
124 |
125 | TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
126 | TEST_ASSERT_EQUAL(350, positions[1].position);
127 | TEST_ASSERT_FALSE(servo.isNeutral());
128 |
129 | for (int i = 0; i < 10; i++) {
130 | servo.moveTowardsNeutral();
131 |
132 | TEST_ASSERT_EQUAL(servoId, positions[2 + i].servoId);
133 | TEST_ASSERT_EQUAL(351 + i, positions[2 + i].position);
134 | }
135 |
136 | TEST_ASSERT_TRUE(servo.isNeutral());
137 | TEST_ASSERT_EQUAL(12, logIndex);
138 | }
139 |
140 | int main(int argc, char **argv) {
141 | UNITY_BEGIN();
142 | RUN_TEST(test_move);
143 | RUN_TEST(test_move_towards_neutral);
144 | RUN_TEST(test_threshold);
145 | RUN_TEST(test_offset);
146 | RUN_TEST(test_move_towards_neutral_with_offset);
147 | UNITY_END();
148 | }
--------------------------------------------------------------------------------
/test/test_servo_manager/test_servo_manager.cpp:
--------------------------------------------------------------------------------
1 | #include "../test/helper.h"
2 | #include "AnimationData.h"
3 | #include "ServoManager.h"
4 | #include
5 |
6 | using namespace BlenderServoAnimationLibrary;
7 |
8 | void setUp(void) {
9 | resetPositionLog();
10 | }
11 |
12 | void test_parse_data(void) {
13 | AnimationData data(PROGMEM_DATA, DATA_SIZE);
14 | ServoManager servoManager;
15 | servoManager.setPositionCallback(move);
16 |
17 | TEST_ASSERT_EQUAL(0, logIndex);
18 |
19 | servoManager.parseData(&data);
20 |
21 | TEST_ASSERT_EQUAL(2, logIndex);
22 | TEST_ASSERT_EQUAL(0, positions[0].servoId);
23 | TEST_ASSERT_EQUAL(375, positions[0].position);
24 | TEST_ASSERT_EQUAL(1, positions[1].servoId);
25 | TEST_ASSERT_EQUAL(375, positions[1].position);
26 | }
27 |
28 | void test_parse_data_without_line_breaks(void) {
29 | AnimationData data(PROGMEM_DATA, DATA_SIZE);
30 | ServoManager servoManager;
31 | servoManager.setPositionCallback(move);
32 |
33 | TEST_ASSERT_EQUAL(0, logIndex);
34 |
35 | servoManager.parseData(&data, false);
36 |
37 | TEST_ASSERT_EQUAL(10, logIndex);
38 | }
39 |
40 | void test_move_all_towards_neutral(void) {
41 | AnimationData data(PROGMEM_DATA, DATA_SIZE);
42 | ServoManager servoManager;
43 | servoManager.setPositionCallback(move);
44 | servoManager.setDefaultThreshold(20);
45 | servoManager.setThreshold(0, 10);
46 |
47 | TEST_ASSERT_EQUAL(0, logIndex);
48 |
49 | servoManager.parseData(&data);
50 | servoManager.parseData(&data);
51 | servoManager.parseData(&data);
52 |
53 | TEST_ASSERT_EQUAL(6, logIndex);
54 | TEST_ASSERT_FALSE(servoManager.servosAreAllNeutral());
55 |
56 | servoManager.moveAllTowardsNeutral();
57 |
58 | TEST_ASSERT_EQUAL(8, logIndex);
59 | TEST_ASSERT_FALSE(servoManager.servosAreAllNeutral());
60 |
61 | servoManager.moveAllTowardsNeutral();
62 |
63 | TEST_ASSERT_EQUAL(10, logIndex);
64 | TEST_ASSERT_TRUE(servoManager.servosAreAllNeutral());
65 | }
66 |
67 | int main(int argc, char **argv) {
68 | UNITY_BEGIN();
69 | RUN_TEST(test_parse_data);
70 | RUN_TEST(test_parse_data_without_line_breaks);
71 | RUN_TEST(test_move_all_towards_neutral);
72 | UNITY_END();
73 | }
--------------------------------------------------------------------------------