-
Notifications
You must be signed in to change notification settings - Fork 3
/
mcu.hpp
154 lines (127 loc) · 3.83 KB
/
mcu.hpp
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
/**
mcu.hpp
Main header file.
Part of Magnetic stripe Card Utility.
Copyright (c) 2010 Wincent Balin
*/
#ifndef MCU_HPP
#define MCU_HPP
#include <iostream>
#include <string>
#include <vector>
#include "RtAudio.h"
#include <inttypes.h>
// For assertions
#include <cassert>
#include <cstring>
// Version of the program
#define VERSION 1.1
// Initial silence threshold
#define SILENCE_THRES 5000
// Percent of highest value to set silence_thres to
#define AUTO_THRES 30
// Frequency threshold (in percent)
#define FREQ_THRES 60
// Seconds before termination of print_max_level()
#define MAX_TERM 60
// Silence interval after sample (in milliseconds)
#define END_LENGTH 200
// We use signed 16 bit value as a sample
typedef int16_t sample_t;
/**
Definition of the magnetic bitstring parser.
*/
class MagneticBitstringParser
{
public:
virtual ~MagneticBitstringParser(void) { }
virtual void parse(std::string& bitstring, std::string& result);
void set_name(const char* parser_name) { name = parser_name; }
std::string get_name(void) { return name; }
void set_char_length(unsigned int length) { char_length = length; parity_bit = length - 1; }
void set_start_sentinel(const char* sentinel) { assert(strlen(sentinel) == char_length); start_sentinel = sentinel; }
void set_end_sentinel(const char* sentinel) { assert(strlen(sentinel) == char_length); end_sentinel = sentinel; }
unsigned char decode_char(std::string& bits);
bool check_parity(std::string& bits);
protected:
std::string name; // of the encoding
unsigned int char_length; // in bits
unsigned int parity_bit;
std::string start_sentinel;
std::string end_sentinel;
};
/**
Definition of IATA parser.
*/
class IATAParser : public MagneticBitstringParser
{
public:
IATAParser(void)
{
set_name("IATA");
set_char_length(7);
set_start_sentinel("1010001");
set_end_sentinel("1111100");
}
virtual ~IATAParser(void) { }
};
/**
Definition of ABA parser.
*/
class ABAParser : public MagneticBitstringParser
{
public:
ABAParser(void)
{
set_name("ABA");
set_char_length(5);
set_start_sentinel("11010");
set_end_sentinel("11111");
}
virtual ~ABAParser(void) { }
};
/**
RtAudio input function.
*/
int input(void* out_buffer, void* in_buffer, unsigned int n_buffer_frames,
double stream_time, RtAudioStreamStatus status, void* data);
/**
Definition of the MCU.
*/
class MCU
{
public:
MCU(int argc, char** argv);
void run(RtAudioCallback input_function, std::vector<sample_t>* b);
private:
// Methods
void print_version(void);
void print_help(void);
void list_devices(std::vector<RtAudio::DeviceInfo>& dev, std::vector<int>& index);
void print_devices(std::vector<RtAudio::DeviceInfo>& dev);
unsigned int greatest_sample_rate(int device_index);
void print_max_level(unsigned int sample_rate);
void silence_pause(void);
void get_dsp(unsigned int sample_rate);
void decode_aiken_biphase(std::vector<sample_t>& input);
sample_t evaluate_max(void);
void cleanup(void);
// Properties
RtAudio adc; // Sound input
std::vector<RtAudio::DeviceInfo> devices; // List of devices
std::vector<int> device_indexes; // List of original device indexes
std::vector<sample_t>* buffer;
unsigned int buffer_index; // Current buffer index = 0
// Start and end index of sample
unsigned int sample_start;
unsigned int sample_end;
std::string bitstring; // String of bits
sample_t silence_thres; // Silence threshold = SILENCE_THRES
// Configuration properties
int auto_thres; // = AUTO_THRES
bool max_level; // = false
bool verbose; // = true
bool list_input_devices; // = false
int device_number; // = 0
};
#endif /* MCU_HPP */