MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
relay.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "relay.h"
16 
17 RelayAH::RelayAH(int pin) : pin_(pin){};
18 
20 {
21  pinMode(pin_, OUTPUT);
22  digitalWrite(pin_, bool(sw));
23 }
24 
25 void RelayAH::set(Switch state) { digitalWrite(pin_, bool(state)); }
26 
27 int RelayAH::get_state() { return digitalRead(pin_); }
28 
30 {
31  bool state = digitalRead(pin_);
32  delay(10);
33  digitalWrite(pin_, !state); // TODO check this
34 }
35 
36 RelayAL::RelayAL(int pin) : pin_(pin){};
37 
39 {
40  pinMode(pin_, OUTPUT);
41  digitalWrite(pin_, !bool(sw));
42 }
43 
44 void RelayAL::set(Switch state) { digitalWrite(pin_, !bool(state)); }
45 
47 {
48  return !(bool)digitalRead(pin_); // negate the state as this is an active low
49 }
50 
52 {
53  bool state = digitalRead(pin_);
54  delay(10);
55  digitalWrite(pin_, !state);
56 }
int get_state()
Get the current state.
Definition: relay.cpp:27
RelayAH(int pin)
Construct a new active high relay.
Definition: relay.cpp:17
void begin(Switch sw)
initialise pin and set initial relay state
Definition: relay.cpp:19
void set(Switch state)
set relay state
Definition: relay.cpp:25
void toggle()
Toggle the relay.
Definition: relay.cpp:29
void set(Switch state)
set relay state
Definition: relay.cpp:44
int get_state()
Get the current state.
Definition: relay.cpp:46
RelayAL(int pin)
Construct a new active low relay.
Definition: relay.cpp:36
void begin(Switch sw)
initialise pin and set initial relay state
Definition: relay.cpp:38
void toggle()
Toggle the relay.
Definition: relay.cpp:51
Definition: utility.h:39
Switch
Definition: relay.h:22