Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (96 loc) · 2.99 KB
/
Copy pathmain.cpp
File metadata and controls
118 lines (96 loc) · 2.99 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
// Demolition Man Verbal Morality Statute Monitor
// Created by Tony DiCola (tony@tonydicola.com)
// Released under an MIT license (http://opensource.org/licenses/MIT).
// Main application.
#include <algorithm>
#include <csignal>
#include <cstdint>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <wiringPi.h>
#include "Adafruit_Thermal.h"
#include "AlsaError.h"
#include "AlsaSink.h"
#include "AlsaSource.h"
#include "DemoManMonitor.h"
#include "PocketSphinxKWS.h"
using namespace std;
#define ALARM_FILE "alarm_movie_padded.raw"
#define RECORD_HW "plughw:0,0"
#define PLAYBACK_HW "plughw:1,0"
#define KEYWORD_FILE "keywords.txt"
#define PRINTER_PORT "/dev/ttyAMA0"
#define QUIET_PIN 0
#define LED_PIN 4
bool shouldRun = true;
void setQuietMode(DemoManMonitor& monitor, bool quietMode) {
monitor.setQuietMode(quietMode);
if (quietMode) {
cout << "Quiet mode enabled." << endl;
}
else {
cout << "Quiet mode disabled." << endl;
}
}
void light(bool enable) {
digitalWrite(LED_PIN, enable ? HIGH : LOW);
}
int main(int argc, char* argv[]) {
try {
cout << "Demolition Man Verbal Morality Statute Monitor" << endl;
cout << "Loading..." << endl;
// Signal handler to catch ctrl-c in the main loop and shut down gracefully (i.e. call destructors).
signal(SIGINT, [](int param){ shouldRun = false; });
// Initialize wiringPi library and quiet switch input.
wiringPiSetup () ;
pinMode(QUIET_PIN, INPUT);
bool quietSwitch = (digitalRead(QUIET_PIN) == HIGH);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Initialize printer.
Adafruit_Thermal printer(PRINTER_PORT);
printer.begin();
// Load alarm raw audio.
ifstream input(ALARM_FILE, ios::in | ios::binary);
input.seekg (0, input.end);
size_t length = input.tellg();
input.seekg (0, input.beg);
vector<uint8_t> alarm(length);
input.read((char*)alarm.data(), length);
// Initialize audio sink and source.
AlsaSink sink;
sink.open(PLAYBACK_HW, 44100, 1, SND_PCM_FORMAT_S16_LE);
AlsaSource source;
source.open(RECORD_HW, 16000, 1, SND_PCM_FORMAT_S16_LE);
// Initialize keyword spotter.
PocketSphinxKWS spotter;
spotter.initialize(PocketSphinxKWS::parseConfig(argc, argv), KEYWORD_FILE);
// Initialize main logic.
DemoManMonitor monitor(8000, &printer, &source, &sink, &spotter, &alarm, light);
setQuietMode(monitor, quietSwitch);
cout << "Listening... (press Ctrl-C to stop)" << endl;
while (shouldRun) {
// Check quite mode switch and update state.
bool newQuietSwitch = (digitalRead(QUIET_PIN) == HIGH);
if (newQuietSwitch != quietSwitch) {
quietSwitch = newQuietSwitch;
setQuietMode(monitor, quietSwitch);
}
// Update main logic state.
monitor.update();
}
digitalWrite(LED_PIN, LOW);
}
catch (AlsaError ex) {
cerr << "ALSA ERROR " << ex.message << " (" << ex.code << ") while calling: " << ex.what() << endl;
return 1;
}
catch (exception ex) {
cerr << "ERROR: " << ex.what() << endl;
return 1;
}
return 0;
}