From 76b828fe8c6410ad612435a68bbfdf07d55187a9 Mon Sep 17 00:00:00 2001 From: Tim Hendriks Date: Sat, 27 Jul 2024 17:13:40 +0200 Subject: [PATCH] put common types into namespace --- examples/MultipleScenesSD/MultipleScenesSD.ino | 2 +- src/BlenderServoAnimation.cpp | 15 ++++++--------- src/BlenderServoAnimation.h | 15 +++++++++------ src/CommonTypes.h | 14 ++++++++++++++ src/Scene.cpp | 2 -- src/Servo.cpp | 9 ++++----- src/Servo.h | 8 ++++---- src/ServoManager.cpp | 7 +++---- src/ServoManager.h | 6 +++--- src/typedefs.h | 5 ----- test/test_servo/test_servo.cpp | 8 ++++---- 11 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 src/CommonTypes.h delete mode 100644 src/typedefs.h diff --git a/examples/MultipleScenesSD/MultipleScenesSD.ino b/examples/MultipleScenesSD/MultipleScenesSD.ino index c38e803..861f969 100644 --- a/examples/MultipleScenesSD/MultipleScenesSD.ino +++ b/examples/MultipleScenesSD/MultipleScenesSD.ino @@ -16,7 +16,7 @@ #define SERVO_PIN 3 #define CS_PIN 4 -#define SCENE_AMOUNT = 2 +#define SCENE_AMOUNT 2 // Servo object to send positions Servo myServo; diff --git a/src/BlenderServoAnimation.cpp b/src/BlenderServoAnimation.cpp index cfdb090..ab3a468 100644 --- a/src/BlenderServoAnimation.cpp +++ b/src/BlenderServoAnimation.cpp @@ -1,9 +1,6 @@ #include "BlenderServoAnimation.h" -#include "AnimationData.h" #include -using BlenderServoAnimationLibrary::Scene; - BlenderServoAnimation::~BlenderServoAnimation() { if (this->scenes) { delete[] this->scenes; @@ -322,16 +319,16 @@ Scene *BlenderServoAnimation::getCurrentScene() { return this->scene; } -void BlenderServoAnimation::onPositionChange(pcb positionCallback) { - this->servoManager.setPositionCallback(positionCallback); +void BlenderServoAnimation::onPositionChange(PositionCallback callback) { + this->servoManager.setPositionCallback(callback); } -void BlenderServoAnimation::onModeChange(mcb modeCallback) { - this->modeCallback = modeCallback; +void BlenderServoAnimation::onModeChange(ModeCallback callback) { + this->modeCallback = callback; } -void BlenderServoAnimation::onSceneChange(scb sceneCallback) { - this->sceneCallback = sceneCallback; +void BlenderServoAnimation::onSceneChange(SceneCallback callback) { + this->sceneCallback = callback; } void BlenderServoAnimation::changeMode(byte mode) { diff --git a/src/BlenderServoAnimation.h b/src/BlenderServoAnimation.h index 5524443..8316932 100644 --- a/src/BlenderServoAnimation.h +++ b/src/BlenderServoAnimation.h @@ -1,7 +1,7 @@ #include "AnimationData.h" #include "Scene.h" #include "ServoManager.h" -#include "typedefs.h" +#include "CommonTypes.h" #include #include @@ -11,6 +11,9 @@ using BlenderServoAnimationLibrary::AnimationData; using BlenderServoAnimationLibrary::Scene; using BlenderServoAnimationLibrary::ServoManager; +using BlenderServoAnimationLibrary::ModeCallback; +using BlenderServoAnimationLibrary::PositionCallback; +using BlenderServoAnimationLibrary::SceneCallback; class BlenderServoAnimation { @@ -31,9 +34,9 @@ class BlenderServoAnimation { void addScene(const byte *data, int size, byte fps, int frames); void addScene(Stream &stream, byte fps, int frame); - void onPositionChange(pcb positionCallback); - void onModeChange(mcb modeCallback); - void onSceneChange(scb sceneCallback); + void onPositionChange(PositionCallback callback); + void onModeChange(ModeCallback callback); + void onSceneChange(SceneCallback callback); void run(unsigned long currentMicros = micros()); void play(); void playSingle(byte index); @@ -65,8 +68,8 @@ class BlenderServoAnimation { bool *playedIndexes = nullptr; - mcb modeCallback = nullptr; - scb sceneCallback = nullptr; + ModeCallback modeCallback = nullptr; + SceneCallback sceneCallback = nullptr; byte mode = MODE_DEFAULT; diff --git a/src/CommonTypes.h b/src/CommonTypes.h new file mode 100644 index 0000000..bc7672e --- /dev/null +++ b/src/CommonTypes.h @@ -0,0 +1,14 @@ +#include + +#ifndef BlenderServoAnimationLibrary_CommonTypes_H +#define BlenderServoAnimationLibrary_CommonTypes_H + +namespace BlenderServoAnimationLibrary { + +typedef void (*ModeCallback)(byte, byte); // Mode callback +typedef void (*PositionCallback)(byte, int); // Position callback +typedef void (*SceneCallback)(byte, byte); // Scene callback + +} // BlenderServoAnimationLibrary + +#endif diff --git a/src/Scene.cpp b/src/Scene.cpp index a96e379..404ae36 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -1,6 +1,4 @@ #include "Scene.h" -#include "Command.h" -#include "Servo.h" #include using BlenderServoAnimationLibrary::Scene; diff --git a/src/Servo.cpp b/src/Servo.cpp index 6d86537..03465f5 100644 --- a/src/Servo.cpp +++ b/src/Servo.cpp @@ -1,12 +1,11 @@ #include "Servo.h" -#include "typedefs.h" #include using BlenderServoAnimationLibrary::Servo; -Servo::Servo(byte id, pcb positionCallback, byte threshold) { +Servo::Servo(byte id, PositionCallback callback, byte threshold) { this->id = id; - this->positionCallback = positionCallback; + this->positionCallback = callback; this->setThreshold(threshold); } @@ -53,8 +52,8 @@ bool Servo::isNeutral() { return this->currentPosition == this->neutralPosition; } -void Servo::setPositionCallback(pcb positionCallback) { - this->positionCallback = positionCallback; +void Servo::setPositionCallback(PositionCallback callback) { + this->positionCallback = callback; } void Servo::setThreshold(byte value) { diff --git a/src/Servo.h b/src/Servo.h index 1470cf3..cd64150 100644 --- a/src/Servo.h +++ b/src/Servo.h @@ -1,4 +1,4 @@ -#include "typedefs.h" +#include "CommonTypes.h" #include #ifndef BlenderServoAnimationLibrary_Servo_H @@ -9,13 +9,13 @@ namespace BlenderServoAnimationLibrary { class Servo { public: - Servo(byte id, pcb positionCallback, byte threshold = 0); + Servo(byte id, PositionCallback callback, byte threshold = 0); void move(int position, bool useOffset = true); void moveTowardsNeutral(); void setThreshold(byte value); void setOffset(int offset); - void setPositionCallback(pcb positionCallback); + void setPositionCallback(PositionCallback callback); bool isNeutral(); @@ -32,7 +32,7 @@ class Servo { int currentPosition = -1; int offset = 0; - pcb positionCallback = nullptr; + PositionCallback positionCallback = nullptr; bool positionExceedsThreshold(int position); }; diff --git a/src/ServoManager.cpp b/src/ServoManager.cpp index d9eba8f..6d7df29 100644 --- a/src/ServoManager.cpp +++ b/src/ServoManager.cpp @@ -1,5 +1,4 @@ #include "ServoManager.h" -#include "Servo.h" #include using BlenderServoAnimationLibrary::Servo; @@ -11,11 +10,11 @@ ServoManager::~ServoManager() { } } -void ServoManager::setPositionCallback(pcb positionCallback) { - this->positionCallback = positionCallback; +void ServoManager::setPositionCallback(PositionCallback callback) { + this->positionCallback = callback; for (byte i = 0; i < this->servoAmount; i++) { - this->servos[i]->setPositionCallback(positionCallback); + this->servos[i]->setPositionCallback(callback); } } diff --git a/src/ServoManager.h b/src/ServoManager.h index 11f8b98..3f67bb3 100644 --- a/src/ServoManager.h +++ b/src/ServoManager.h @@ -1,7 +1,7 @@ #include "AnimationData.h" #include "Command.h" #include "Servo.h" -#include "typedefs.h" +#include "CommonTypes.h" #include #ifndef BlenderServoAnimationLibrary_ServoManager_H @@ -14,7 +14,7 @@ class ServoManager { public: ~ServoManager(); - void setPositionCallback(pcb positionCallback); + void setPositionCallback(PositionCallback callback); void setDefaultThreshold(byte value); void setThreshold(byte servoId, byte value); void setOffset(byte servoId, int offset); @@ -28,7 +28,7 @@ class ServoManager { Command command; - pcb positionCallback = nullptr; + PositionCallback positionCallback = nullptr; byte servoAmount = 0; byte defaultThreshold = 0; diff --git a/src/typedefs.h b/src/typedefs.h deleted file mode 100644 index d9e464a..0000000 --- a/src/typedefs.h +++ /dev/null @@ -1,5 +0,0 @@ -#include - -typedef void (*mcb)(byte, byte); // Mode callback -typedef void (*pcb)(byte, int); // Position callback -typedef void (*scb)(byte, byte); // Scene callback diff --git a/test/test_servo/test_servo.cpp b/test/test_servo/test_servo.cpp index 45c5fe4..66b45ff 100644 --- a/test/test_servo/test_servo.cpp +++ b/test/test_servo/test_servo.cpp @@ -139,10 +139,10 @@ void test_move_towards_neutral_with_offset(void) { int main(int argc, char **argv) { UNITY_BEGIN(); - // RUN_TEST(test_move); - // RUN_TEST(test_move_towards_neutral); - // RUN_TEST(test_threshold); - // RUN_TEST(test_offset); + RUN_TEST(test_move); + RUN_TEST(test_move_towards_neutral); + RUN_TEST(test_threshold); + RUN_TEST(test_offset); RUN_TEST(test_move_towards_neutral_with_offset); UNITY_END(); } \ No newline at end of file