FotoBox
buzzer.cpp
Go to the documentation of this file.
1 /* buzzer.cpp
2  *
3  * Copyright (c) 2017 Thomas Kais
4  *
5  * This file is subject to the terms and conditions defined in
6  * file 'COPYING', which is part of this source code package.
7  */
8 #include "buzzer.h"
9 
10 #if defined (BUZZER_AVAILABLE)
11 #include <QThread>
12 #include <pigpiod_if2.h>
13 #include "preferenceprovider.h"
14 
15 namespace FotoBox {
16 
18 {
19  //This function resets the used DMA channels, releases memory, and terminates any running threads.
20  pigpio_stop(m_pi);
21 };
22 
23 auto Buzzer::initialise() -> bool
24 {
25  //Connect to the pigpio daemon
26  m_pi = pigpio_start(nullptr, nullptr);
27  if (m_pi < 0) {
28  //pigpio initialisation failed.
29  return false;
30  } else {
31  //pigpio initialised okay, now set mode of the pin
32  if (set_mode(m_pi, PreferenceProvider::instance().outputPin(), PI_OUTPUT) != 0) {
33  return false;
34  }
35  if (set_mode(m_pi, PreferenceProvider::instance().inputPin(), PI_INPUT) != 0) {
36  return false;
37  }
38  }
39  return true;
40 }
41 
42 auto Buzzer::checkDeamon() -> bool
43 {
44  //nullptr -> 'localhost', nullptr -> '8888'
45  auto result = pigpio_start(nullptr, nullptr);
46  if (result < 0) {
47  //pigpio initialisation failed.
48  return false;
49  } else {
50  //deamon reachable -> disconnect
51  pigpio_stop(result);
52  return true;
53  }
54 }
55 
56 void Buzzer::queryPin()
57 {
58  //query pin
59  while (gpio_read(m_pi, PreferenceProvider::instance().inputPin()) != PI_HIGH) {
60  //wait before query pin again
61  QThread::msleep(PreferenceProvider::instance().queryInterval());
62 
63  //if stop is true, stop while loop and return to caller (don't emit triggered)
64  if (m_stop) {
65  return;
66  }
67  }
68 
69  //buzzer was pressed
70  Q_EMIT triggered();
71 }
72 #else
73 
74 namespace FotoBox {
75 
76 Buzzer::~Buzzer(){}; // NOLINT
77 auto Buzzer::initialise() -> bool{return false;}; // NOLINT
78 auto Buzzer::checkDeamon() -> bool{return false;}; // NOLINT
79 void Buzzer::queryPin(){/* stub */}; // NOLINT
80 #endif
81 
82 
83 //don't set parent!
84 Buzzer::Buzzer(QObject* /*parent*/) : QObject(nullptr) { }
85 
87 {
88  //set std::atomic to true
89  m_stop = true;
90 }
91 
92 } // end namespace FotoBox
void stop()
Stop executing.
Definition: buzzer.cpp:86
static auto checkDeamon() -> bool
Check if pigpio daemon is reachable.
Definition: buzzer.cpp:78
~Buzzer() override
Buzzer destructor.
Definition: buzzer.cpp:76
std::atomic< bool > m_stop
Definition: buzzer.h:95
void queryPin()
Query the Raspberry Pi pin.
Definition: buzzer.cpp:79
auto initialise() -> bool
Initialises the library and sets the GPIO mode.
Definition: buzzer.cpp:77
Buzzer(QObject *parent=nullptr)
Buzzer constructor.
Definition: buzzer.cpp:84
void triggered()
Buzzer was pressed.
static PreferenceProvider & instance()
get instance (Meyers Singleton)