-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttention.cpp
More file actions
191 lines (182 loc) · 5.69 KB
/
Copy pathAttention.cpp
File metadata and controls
191 lines (182 loc) · 5.69 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* This file is part of the Eclipse2017 project. It is subject to the GPLv3
* license terms in the LICENSE file found in the top-level directory of this
* distribution and at
* https://github.com/jjackowski/eclipse2017/blob/master/LICENSE.
* No part of the Eclipse2017 project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Copyright (C) 2017 Jeff Jackowski
*/
#include "Attention.hpp"
#include <duds/hardware/devices/clocks/LinuxClockDriver.hpp>
#include <duds/time/planetary/Planetary.hpp>
#include <system_error>
#include <iostream>
#include <csignal>
#include <boost/exception/diagnostic_information.hpp>
extern std::sig_atomic_t quit;
Attention::Record::Record(int t, int pri, int p, Audible aud) :
time(t), priority(pri), page(p), sound(aud) { }
Attention::Attention(duds::hardware::interface::DigitalPin &buz, int testTimeOffset) :
buzzer(buz), page(-1) {
running = std::thread(&Attention::run, this, testTimeOffset);
}
Attention::~Attention() {
{
std::lock_guard<std::mutex> lock(block);
records.clear();
page = 42;
}
change.notify_one();
try {
running.join();
} catch (const std::system_error& e) {
// invalid_argument may occur if thread has terminated
if (e.code() != std::errc::invalid_argument) {
// something else
throw;
}
}
}
// in milliseconds
static int buzlen[Attention::Total] = {
0,
450,
3100,
400
};
void Attention::run(int testTimeOffset)
try {
duds::hardware::interface::DigitalPinAccess buz;
buzzer.access(&buz);
// start by waiting
std::unique_lock<std::mutex> lock(block);
while (records.empty() && !quit) {
change.wait(lock);
}
if (quit) {
return;
}
duds::hardware::devices::clocks::LinuxClockDriver lcd;
duds::hardware::devices::clocks::LinuxClockDriver::Measurement::TimeSample ts;
boost::posix_time::time_duration time;
// begin operating loop
do {
// get the current time
lcd.sampleTime(ts);
// find seconds since midnight UTC
time = duds::time::planetary::earth->posix(ts.value).time_of_day();
if (testTimeOffset) {
time += boost::posix_time::seconds(testTimeOffset);
}
// find next item time-wise
RecordContainer::index<index_time>::type &timeIdx =
records.get<index_time>();
RecordContainer::index<index_time>::type::iterator iter = timeIdx.begin();
// it may be well in the past
while ((iter != timeIdx.end()) && (iter->time - time.total_seconds()) < -4) {
iter = timeIdx.erase(iter);
}
// that may have eliminated everything
if (iter == timeIdx.end()) {
// wait until something happens
change.wait(lock);
} else {
// find highest priority item; there may be a better way
RecordContainer::index<index_time>::type::iterator next = iter;
for (++next; (next != timeIdx.end()) && (next->time == iter->time); ++next) {
if (next->priority < iter->priority) {
iter = next;
}
}
// work out time to wait
std::chrono::milliseconds delay(
iter->time * 1000 - time.total_milliseconds() - buzlen[iter->sound]
);
// time to make noise?
if (delay.count() < 16) {
page = iter->page;
int snd = iter->sound;
// remove all records for this time
auto range = timeIdx.equal_range(iter->time);
timeIdx.erase(range.first, range.second);
// after unlocking, member variables must not be used
lock.unlock();
try {
switch (snd) {
case NoSound:
break;
case Notice:
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(150));
buz.output(false);
std::this_thread::sleep_for(std::chrono::milliseconds(150));
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(150));
buz.output(false);
break;
case Time:
// one beep
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
buz.output(false);
std::this_thread::sleep_for(std::chrono::milliseconds(800));
// two beeps
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
buz.output(false);
std::this_thread::sleep_for(std::chrono::milliseconds(800));
// three beeps
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
buz.output(false);
std::this_thread::sleep_for(std::chrono::milliseconds(800));
// long tone
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
buz.output(false);
break;
case Warning:
buz.output(true);
std::this_thread::sleep_for(std::chrono::milliseconds(400));
buz.output(false);
}
} catch (...) {
std::cerr << "Attention thread error during buzzer output:\n" <<
boost::current_exception_diagnostic_information()
<< std::endl;
}
lock.lock();
} else {
// wait
change.wait_for(lock, delay);
}
}
} while ((page != 42) && !quit);
} catch (...) {
std::cerr << "Program failed in attention thread:\n" <<
boost::current_exception_diagnostic_information()
<< std::endl;
}
void Attention::add(int time, int priority, int page, Audible sound) {
std::lock_guard<std::mutex> lock(block);
records.emplace(time, priority, page, sound);
change.notify_one();
}
void Attention::remove(int page) {
std::lock_guard<std::mutex> lock(block);
RecordContainer::index<index_page>::type &pageIdx = records.get<index_page>();
//std::pair<RecordContainer::index<index_page>::type::iterator
auto range = pageIdx.equal_range(page);
pageIdx.erase(range.first, range.second);
change.notify_one();
}
int Attention::changeToPage() {
std::lock_guard<std::mutex> lock(block);
int ret = page;
// reset attention page
page = -1;
return ret;
}