-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTDA7313.cpp
More file actions
301 lines (240 loc) · 5.85 KB
/
Copy pathTDA7313.cpp
File metadata and controls
301 lines (240 loc) · 5.85 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* TDA7313.cpp - TDA7313 audoprocessor library.
* Author - Ildus Kurbangaliev, 2017-01-17.
* Released into the public domain.
*/
using namespace std;
#include "TDA7313.h"
#include <cassert>
#include <cstring>
TDA7313::TDA7313() {
#ifdef ARDUINO
Wire.begin();
#endif
//initizalize volume, set to -78.75dB (lowest level)
vol_ctrl_data = 0b00111111;
//initialize audio switch, gain 11.25dB, loudness on, input 1
switch_data = 0b01000000;
//initialize bass, 0dB
bass_data = 0b01100111;
//initialize treble, 0dB
treble_data = 0b01110111;
//initialize attenuators
lf_att_data = 0b10000000;
rf_att_data = 0b10100000;
lr_att_data = 0b11000000;
rr_att_data = 0b11100000;
}
/* I2C communication */
void TDA7313::write(int options) {
#ifdef ARDUINO
Wire.beginTransmission(0x44); // 01000100
std::vector<unsigned char> *v = get_i2c_sequence(options);
for (int i = 0; i < v->size(); i++) {
Wire.write((*v)[i]);
}
delete v;
Wire.endTransmission();
#endif
}
void TDA7313::set_volume(unsigned char vol) {
vol_ctrl_data = vol & 0b00111111;
}
unsigned char TDA7313::get_volume(void) {
return vol_ctrl_data; // high two bits are always 0, no need to clean
}
bool TDA7313::is_volume_at_min(void) {
return vol_ctrl_data == 0b00111111;
}
bool TDA7313::is_volume_at_max(void) {
return vol_ctrl_data == 0;
}
/*
* Increase volume for 1.25dB
*/
void TDA7313::increase_volume(void) {
if (is_volume_at_max())
return;
vol_ctrl_data -= 1;
}
/*
* Decrease volume for 1.25dB
*/
void TDA7313::decrease_volume(void) {
if (is_volume_at_min())
return;
vol_ctrl_data += 1;
}
std::vector<unsigned char>* TDA7313::get_i2c_sequence(int options) {
std::vector<unsigned char> *v = new std::vector<unsigned char>();
if (options & OPT_VOLUME)
v->push_back(vol_ctrl_data);
if (options & OPT_SWITCH)
v->push_back(switch_data);
if (options & OPT_BASS)
v->push_back(bass_data);
if (options & OPT_TREBLE)
v->push_back(treble_data);
//attenuators
if (options & OPT_ATTENUATORS) {
for (int i; i < 4; i++) {
unsigned char *att_data = get_attenuator(i);
v->push_back(*att_data);
}
}
return v;
}
/* Set audio source, values 0..2 (3 inputs), bits - 000000XX */
void TDA7313::set_input(int num) {
if (!(num >= 0 && num <= 2))
return;
switch_data = (switch_data & 0b11111100) + num;
}
int TDA7313::get_input(void) {
return switch_data & 0b11;
}
/* Loudness, bits - 00000X00 */
void TDA7313::set_loudness(bool on) {
switch_data ^= (-((int)(!on)) ^ switch_data) & (1 << 2);
}
bool TDA7313::get_loudness(void) {
return !((bool)((switch_data >> 2) & 1));
}
/*
* Set gain. Bits 000XX000
* 0 11.25dB
* 1 7.5dB
* 2 3.75dB
* 3 0dB
*/
void TDA7313::set_gain(int num) {
assert(num >= 0 && num <= 3);
switch_data &= 0b11100111;
switch_data |= num << 3;
}
int TDA7313::get_gain(void) {
return switch_data >> 3 & 0b11;
}
void TDA7313::mute(void) {
for (int i = 0; i < 4; i++) {
temp_memory[i] = attenuator_get_value(i);
attenuator_set_value(i, 0xFF);
}
}
void TDA7313::unmute(void) {
for (int i = 0; i < 4; i++) {
attenuator_set_value(i, temp_memory[i]);
temp_memory[i] = 0;
}
}
unsigned char *TDA7313::get_attenuator(int input) {
switch (input) {
case 0: return &lf_att_data;
case 1: return &rf_att_data;
case 2: return &lr_att_data;
};
return &rr_att_data;
}
void TDA7313::attenuator_set_value(int input, unsigned char value) {
unsigned char *att_data = get_attenuator(input);
value &= 0b00011111;
*att_data = (*att_data & 0b11100000) + value;
}
unsigned char TDA7313::attenuator_get_value(int input) {
unsigned char *att_data = get_attenuator(input);
return *att_data & 0b00011111;
}
/* minimum: -38.75dB, means it's muted */
void TDA7313::attenuator_decrease(int input) {
unsigned char val = attenuator_get_value(input);
if (val == 0b11111)
return;
val += 1;
attenuator_set_value(input, val);
}
/* maximum: 0 */
void TDA7313::attenuator_increase(int input) {
unsigned char val = attenuator_get_value(input);
if (val == 0)
return;
attenuator_set_value(input, --val);
}
/* these functions are for bass and treble, and operate with low 4 bits */
static inline unsigned char get_value(unsigned char data) {
return data & 0b1111;
}
static void set_value(unsigned char *data, unsigned char val) {
val &= 0b00001111; // clear high 4 bits
*data = (*data & 0b11110000) + val;
}
//14dB
static inline bool is_max(unsigned char data) {
return (data & 0b1111) == 0b1000;
}
static inline bool is_min(unsigned char data) {
return (data & 0b1111) == 0;
}
static void increase_value(unsigned char *data) {
unsigned char val = get_value(*data);
if (is_max(val))
return;
if (val == 0b0111) {
val = 0b1111;
} else if ((1 << 3) & val) { //checking sign
val -= 1;
} else {
val += 1;
}
set_value(data, val);
}
static void decrease_value(unsigned char *data) {
unsigned char val = get_value(*data);
if (is_min(val))
return;
if (val == 0b1111) {
val = 0b0111;
} else if ((1 << 3) & val) { //checking sign
val += 1;
} else {
val -= 1;
}
set_value(data, val);
}
/* bass */
unsigned char TDA7313::get_bass_value(void) {
return get_value(bass_data);
}
void TDA7313::set_bass_value(unsigned char val) {
set_value(&bass_data, val);
}
bool TDA7313::is_bass_at_max(void) {
return is_max(bass_data);
}
bool TDA7313::is_bass_at_min(void) {
return is_min(bass_data);
}
void TDA7313::increase_bass(void) {
increase_value(&bass_data);
}
void TDA7313::decrease_bass(void) {
decrease_value(&bass_data);
}
/* treble */
unsigned char TDA7313::get_treble_value(void) {
return get_value(treble_data);
}
void TDA7313::set_treble_value(unsigned char val) {
set_value(&treble_data, val);
}
bool TDA7313::is_treble_at_max(void) {
return is_max(treble_data);
}
bool TDA7313::is_treble_at_min(void) {
return is_min(treble_data);
}
void TDA7313::increase_treble(void) {
increase_value(&treble_data);
}
void TDA7313::decrease_treble(void) {
decrease_value(&treble_data);
}