-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBlindsServo.h
87 lines (67 loc) · 2.26 KB
/
BlindsServo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef BLINDSSERVO_H
#define BLINDSSERVO_H
#include <Servo.h>
#include <functional>
#define DEBUG_PRINT_CALLBACK_SIGNATURE std::function<void(String debugPrint)> debugPrintCallback
#define STATUS_CHANGED_CALLBACK_SIGNATURE std::function<void(int servoId)> statusChangedCallback
#define POSITION_CHANGED_CALLBACK_SIGNATURE std::function<void(int servoId)> positionChangedCallback
class BlindsServo {
public:
enum blindsStatus { OPEN, CLOSED, OPENING, CLOSING };
BlindsServo(int id, int servoPin, int minPulseValue, int maxPulseValue, int maxDegree, boolean reversed = false, boolean debug = false);
BlindsServo();
~BlindsServo();
// Main loop
void loop(); // Main loop
// Debug
void setDebug(bool debug);
// Callbacks
void setDebugPrintCallback(DEBUG_PRINT_CALLBACK_SIGNATURE);
void setStatusChangedCallback(STATUS_CHANGED_CALLBACK_SIGNATURE);
void setPositionChangedCallback(POSITION_CHANGED_CALLBACK_SIGNATURE);
public:
// Getters
boolean isOpening();
boolean isClosing();
int currentAngleInPercent();
int getId();
int getAngle();
boolean isClosed();
boolean isMoving();
blindsStatus getStatus();
// Setters
void setStop(); // Stops the movement
void setOpen();
void setClose();
void goToPosition(int position); // 0-100
private:
void init(int id, int servoPin, int minPulseValue, int maxPulseValue, int maxDegree, boolean reversed, boolean debug);
void attach();
void detach();
void goToAngle(int angle); // Goes to specific angle
int angleToServo(int angle);
void debugPrint(String text);
private:
Servo servo;
boolean attached;
int id = 0; // id of the servo
// Previous status
blindsStatus previousStatus;
// Position management
int target = 0;
int currentPosition = -1; // -1 = unknown value (initial state)
int previousPosition = -1;
// Servo configuration
int servoMinPulse;
int servoMaxPulse;
int servoPin;
int servoMaxDegree;
boolean isReversed = false;
// Debugging options
boolean isDebug = false;
// Callbacks
DEBUG_PRINT_CALLBACK_SIGNATURE {nullptr};
STATUS_CHANGED_CALLBACK_SIGNATURE { nullptr };
POSITION_CHANGED_CALLBACK_SIGNATURE { nullptr };
};
#endif