forked from DanielOgorchock/ST_Anything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_Switch_Dim.cpp
More file actions
133 lines (119 loc) · 3.75 KB
/
EX_Switch_Dim.cpp
File metadata and controls
133 lines (119 loc) · 3.75 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//******************************************************************************************
// File: EX_Switch_Dim.cpp
// Authors: Dan G Ogorchock
//
// Summary: EX_Switch_Dim is a class which implements the SmartThings "Switch" and "Switch Level" device capabilities.
// It inherits from the st::Executor class.
//
// Create an instance of this class in your sketch's global variable section
// For Example: st::EX_Switch_Dim executor1("dimmerSwitch1", PIN_SWITCH, PIN_LEVEL, LOW, true);
//
// st::EX_Switch_Dim() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - byte pin - REQUIRED - the Arduino Pin to be used as a digital output
// - byte pin_pwm - REQUIRED - the Arduino Pin to be used as a pwm output
// - bool startingState - OPTIONAL - the value desired for the initial state of the switch. LOW = "off", HIGH = "on"
// - bool invertLogic - OPTIONAL - determines whether the Arduino Digital Output should use inverted logic
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2016-04-30 Dan Ogorchock Original Creation
// 2018-08-14 Dan Ogorchock Modified to avoid compiler errors on ESP32 since it currently does not support "analogWrite()"
// 2018-08-30 Dan Ogorchock Modified comment section above to comply with new Parent/Child Device Handler requirements
// 2018-08-30 Dan Ogorchock Adding reporting of 'level'
//
//
//******************************************************************************************
#include "EX_Switch_Dim.h"
#include "Constants.h"
#include "Everything.h"
namespace st
{
//private
void EX_Switch_Dim::writeStateToPin()
{
digitalWrite(m_nPinSwitch, m_bInvertLogic ? !m_bCurrentState : m_bCurrentState);
}
void EX_Switch_Dim::writeLevelToPin()
{
#if defined(ARDUINO_ARCH_ESP32)
if (st::Executor::debug) {
Serial.println(F("EX_Switch_Dim:: analogWrite not currently supported on ESP32!"));
}
#else
analogWrite(m_nPinPWM, map(m_nCurrentLevel, 0, 100, 0, 255));
#endif
}
//public
//constructor
EX_Switch_Dim::EX_Switch_Dim(const __FlashStringHelper *name, byte pinSwitch, byte pinPWM, bool startingState, bool invertLogic) :
Executor(name),
m_bCurrentState(startingState),
m_bInvertLogic(invertLogic)
{
m_nCurrentLevel = startingState == HIGH ? 100 : 0;
setSwitchPin(pinSwitch);
setPWMPin(pinPWM);
}
//destructor
EX_Switch_Dim::~EX_Switch_Dim()
{
}
void EX_Switch_Dim::init()
{
//Everything::sendSmartString(getName() + " " + (m_bCurrentState == HIGH ? F("on") : F("off")));
refresh();
}
void EX_Switch_Dim::beSmart(const String &str)
{
String s = str.substring(str.indexOf(' ') + 1);
if (st::Executor::debug) {
Serial.print(F("EX_Switch_Dim::beSmart s = "));
Serial.println(s);
}
if (s == F("on"))
{
m_bCurrentState = HIGH;
}
else if (s == F("off"))
{
m_bCurrentState = LOW;
}
else //must be a set level command
{
s.trim();
m_nCurrentLevel = byte(s.toInt());
if (m_nCurrentLevel == 0)
{
m_bCurrentState = LOW;
}
else
{
m_bCurrentState = HIGH;
}
}
writeStateToPin();
writeLevelToPin();
//Everything::sendSmartString(getName() + " " + (m_bCurrentState == HIGH ? F("on") : F("off")));
refresh();
}
void EX_Switch_Dim::refresh()
{
Everything::sendSmartString(getName() + " " + (m_bCurrentState == HIGH ? F("on") : F("off")));
Everything::sendSmartString(getName() + " " + m_nCurrentLevel);
}
void EX_Switch_Dim::setSwitchPin(byte pin)
{
m_nPinSwitch = pin;
pinMode(m_nPinSwitch, OUTPUT);
writeStateToPin();
}
void EX_Switch_Dim::setPWMPin(byte pin)
{
m_nPinPWM = pin;
pinMode(m_nPinPWM, OUTPUT);
writeLevelToPin();
}
}