Leosac  0.7.0
OpenSourceAccessControl
FLED.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "FLED.hpp"
21 #include <tools/log.hpp>
22 #include <zmqpp/message.hpp>
23 
24 using namespace Leosac::Hardware;
25 
26 FLED::FLED(zmqpp::context &ctx, const std::string &led_name)
27  : backend_(ctx, zmqpp::socket_type::req)
28 {
29  backend_.connect("inproc://" + led_name);
30  poller_.add(backend_);
31 }
32 
33 bool FLED::turnOn(std::chrono::milliseconds duration)
34 {
35  std::string rep;
36  zmqpp::message msg;
37 
38  msg << "ON" << duration.count();
39 
40  backend_.send(msg);
41  poller_.poll(5000);
42  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
43  backend_.receive(rep);
44  if (rep == "OK")
45  return true;
46  return false;
47 }
48 
49 bool FLED::turnOn(int duration)
50 {
51  return turnOn(std::chrono::milliseconds(duration));
52 }
53 
55 {
56  std::string rep;
57 
58  backend_.send("ON");
59  poller_.poll(5000);
60  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
61  backend_.receive(rep);
62  if (rep == "OK")
63  return true;
64  return false;
65 }
66 
68 {
69  std::string rep;
70 
71  backend_.send("OFF");
72  poller_.poll(5000);
73  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
74  backend_.receive(rep);
75  if (rep == "OK")
76  return true;
77  return false;
78 }
79 
81 {
82  std::string rep;
83 
84  backend_.send("TOGGLE");
85  poller_.poll(5000);
86  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
87  backend_.receive(rep);
88  if (rep == "OK")
89  return true;
90  return false;
91 }
92 
94 {
95  std::string rep;
96 
97  backend_.send("BLINK");
98  poller_.poll(5000);
99  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
100  backend_.receive(rep);
101  if (rep == "OK")
102  return true;
103  return false;
104 }
105 
106 bool FLED::blink(std::chrono::milliseconds duration, std::chrono::milliseconds speed)
107 {
108  std::string rep;
109  zmqpp::message msg;
110 
111  msg << "BLINK" << duration.count() << speed.count();
112 
113  backend_.send(msg);
114  poller_.poll(5000);
115  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked.");
116  backend_.receive(rep);
117  if (rep == "OK")
118  return true;
119  return false;
120 }
121 
122 bool FLED::blink(int duration, int speed)
123 {
124  return blink(std::chrono::milliseconds(duration),
125  std::chrono::milliseconds(speed));
126 }
127 
129 {
130  State st = state();
131 
132  if (st.st == State::BLINKING)
133  return st.value;
134  if (st.st == State::OFF)
135  return false;
136  if (st.st == State::ON)
137  return true;
138  ASSERT_LOG(0, "Unkown LED state. Aborting.");
139  exit(-1);
140 }
141 
143 {
144  return !isOn();
145 }
146 
148 {
149  return state().st == State::BLINKING;
150 }
151 
153 {
154  FLED::State led_state;
155 
156  zmqpp::message rep;
157  std::string status_str;
158 
159  backend_.send("STATE");
160  backend_.receive(rep);
161 
162  rep >> status_str;
163  if (status_str == "BLINKING")
164  {
165  assert(rep.parts() == 4);
166  led_state.st = State::BLINKING;
167 
168  // duration
169  rep >> led_state.duration;
170 
171  // speed
172  rep >> led_state.speed;
173 
174  // value
175  rep >> status_str;
176  assert(status_str == "ON" || status_str == "OFF");
177  led_state.value = status_str == "ON";
178  }
179  else
180  {
181  assert(rep.parts() == 1);
182  if (status_str == "ON")
183  led_state.st = State::ON;
184  else if (status_str == "OFF")
185  led_state.st = State::OFF;
186  else
187  assert(0);
188  }
189  return led_state;
190 }
191 
192 zmqpp::socket &FLED::backend()
193 {
194  return backend_;
195 }
int64_t duration
Set only if st is BLINKING, it represents the total duration of blinking.
Definition: FLED.hpp:72
bool blink()
Make the LED blink.
Definition: FLED.cpp:93
enum Leosac::Hardware::FLED::State::@0 st
Internal state of the LED.
bool value
Set only if st is BLINKING : value of the LED (true if ON, false otherwise).
Definition: FLED.hpp:83
FLED(zmqpp::context &ctx, const std::string &led_name)
Definition: FLED.cpp:26
zmqpp::socket backend_
A socket to talk to the backend LED.
Definition: FLED.hpp:174
bool turnOff()
Turn the LED OFF by sending a message to the backend LED impl.
Definition: FLED.cpp:67
bool isOn()
Query the value of the GPIO and returns true if the LED is ON.
Definition: FLED.cpp:128
zmqpp::poller poller_
A poller to not wait for infinity in case something went wrong.
Definition: FLED.hpp:179
State state()
Return the state of the device.
Definition: FLED.cpp:152
Provides facade classes to hardware device implementation.
Definition: Buzzer.cpp:25
bool toggle()
Toggle the LED value by sending a message to the backend LED impl.
Definition: FLED.cpp:80
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
bool isBlinking()
Returns true is the LED is currently blinking.
Definition: FLED.cpp:147
int64_t speed
Set only if st is BLINKING, it represents the speed of blinking.
Definition: FLED.hpp:77
zmqpp::socket & backend()
Access the backend socket (which is connect to the LED device) to send command directly.
Definition: FLED.cpp:192
bool turnOn()
Turn the LED ON by sending a message to the backend LED impl.
Definition: FLED.cpp:54
bool isOff()
Similar to isOn().
Definition: FLED.cpp:142