forked from DanielOgorchock/ST_Anything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_Servo.cpp
More file actions
113 lines (93 loc) · 3.08 KB
/
EX_Servo.cpp
File metadata and controls
113 lines (93 loc) · 3.08 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//******************************************************************************************
// File: EX_Servo.cpp
// Authors: Dan G Ogorchock
//
// Summary: EX_Servo is a class which implements the SmartThings/Hubitat "Switch Level" device capability.
// It inherits from the st::Executor class.
//
// Create an instance of this class in your sketch's global variable section
// For Example: st::EX_Servo executor1(F("servo1"), PIN_SERVO, INITIAL_ANGLE, false, 1000);
//
// st::EX_Servo() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - byte pin_pwm - REQUIRED - the Arduino Pin to be used as a pwm output
// - int startingAngle - OPTIONAL - the value desired for the initial angle of the servo motor (defaults to 90)
// - bool detachAfterMove - OPTIONAL - determines if servo motor is powered down after move using following timeout (defaults to false)
// - int servoMoveTime - OPTIONAL - determines how long after the servo is moved that the servo is powered down if the above is true (defaults to 1000ms)
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2018-06-23 Dan Ogorchock Original Creation
// 2018-06-24 Dan Ogorchock Since ESP32 does not support SERVO library, exclude all code to prevent compiler error
// 2018-08-19 Dan Ogorchock Added feature to optionally allow servo to be powered down after a move
//
//
//******************************************************************************************
#include "EX_Servo.h"
#include "Constants.h"
#include "Everything.h"
#if not defined(ARDUINO_ARCH_ESP32)
namespace st
{
//private
void EX_Servo::writeAngleToPin()
{
m_nCurrentAngle = map(m_nCurrentLevel, 0, 99, 0, 180);
if (!m_Servo.attached()) {
m_Servo.attach(m_nPinPWM);
}
m_Servo.write(m_nCurrentAngle);
if (st::Executor::debug) {
Serial.print(F("EX_Servo:: Servo motor angle set to "));
Serial.println(m_nCurrentAngle);
}
if (m_bDetachAfterMove) {
delay(m_nServoMoveTime);
m_Servo.detach();
}
}
//public
//constructor
EX_Servo::EX_Servo(const __FlashStringHelper *name, byte pinPWM, int startingAngle, bool detachAfterMove, int servoMoveTime) :
Executor(name),
m_Servo(),
m_nCurrentAngle(startingAngle),
m_bDetachAfterMove(detachAfterMove),
m_nServoMoveTime(servoMoveTime)
{
setPWMPin(pinPWM);
m_nCurrentLevel = map(m_nCurrentAngle, 0, 180, 0, 99);
}
//destructor
EX_Servo::~EX_Servo()
{
}
void EX_Servo::init()
{
writeAngleToPin();
refresh();
}
void EX_Servo::beSmart(const String &str)
{
String s = str.substring(str.indexOf(' ') + 1);
if (st::Executor::debug) {
Serial.print(F("EX_Servo::beSmart s = "));
Serial.println(s);
}
s.trim();
m_nCurrentLevel = int(s.toInt());
writeAngleToPin();
refresh();
}
void EX_Servo::refresh()
{
Everything::sendSmartString(getName() + " " + m_nCurrentLevel);
}
void EX_Servo::setPWMPin(byte pin)
{
m_nPinPWM = pin;
}
}
#endif