Skip to content

Commit

Permalink
put common types into namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jul 27, 2024
1 parent 490e7f3 commit 76b828f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 43 deletions.
2 changes: 1 addition & 1 deletion examples/MultipleScenesSD/MultipleScenesSD.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/BlenderServoAnimation.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "BlenderServoAnimation.h"
#include "AnimationData.h"
#include <Arduino.h>

using BlenderServoAnimationLibrary::Scene;

BlenderServoAnimation::~BlenderServoAnimation() {
if (this->scenes) {
delete[] this->scenes;
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 9 additions & 6 deletions src/BlenderServoAnimation.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AnimationData.h"
#include "Scene.h"
#include "ServoManager.h"
#include "typedefs.h"
#include "CommonTypes.h"
#include <Arduino.h>
#include <stdarg.h>

Expand All @@ -11,6 +11,9 @@
using BlenderServoAnimationLibrary::AnimationData;
using BlenderServoAnimationLibrary::Scene;
using BlenderServoAnimationLibrary::ServoManager;
using BlenderServoAnimationLibrary::ModeCallback;
using BlenderServoAnimationLibrary::PositionCallback;
using BlenderServoAnimationLibrary::SceneCallback;

class BlenderServoAnimation {

Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions src/CommonTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <Arduino.h>

#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
2 changes: 0 additions & 2 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "Scene.h"
#include "Command.h"
#include "Servo.h"
#include <Arduino.h>

using BlenderServoAnimationLibrary::Scene;
Expand Down
9 changes: 4 additions & 5 deletions src/Servo.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "Servo.h"
#include "typedefs.h"
#include <Arduino.h>

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);
}

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/Servo.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "typedefs.h"
#include "CommonTypes.h"
#include <Arduino.h>

#ifndef BlenderServoAnimationLibrary_Servo_H
Expand All @@ -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();

Expand All @@ -32,7 +32,7 @@ class Servo {
int currentPosition = -1;
int offset = 0;

pcb positionCallback = nullptr;
PositionCallback positionCallback = nullptr;

bool positionExceedsThreshold(int position);
};
Expand Down
7 changes: 3 additions & 4 deletions src/ServoManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ServoManager.h"
#include "Servo.h"
#include <Arduino.h>

using BlenderServoAnimationLibrary::Servo;
Expand All @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ServoManager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AnimationData.h"
#include "Command.h"
#include "Servo.h"
#include "typedefs.h"
#include "CommonTypes.h"
#include <Arduino.h>

#ifndef BlenderServoAnimationLibrary_ServoManager_H
Expand All @@ -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);
Expand All @@ -28,7 +28,7 @@ class ServoManager {

Command command;

pcb positionCallback = nullptr;
PositionCallback positionCallback = nullptr;

byte servoAmount = 0;
byte defaultThreshold = 0;
Expand Down
5 changes: 0 additions & 5 deletions src/typedefs.h

This file was deleted.

8 changes: 4 additions & 4 deletions test/test_servo/test_servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 76b828f

Please sign in to comment.