-
Notifications
You must be signed in to change notification settings - Fork 3
/
M62429.cpp
103 lines (84 loc) · 3.4 KB
/
M62429.cpp
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
///////////////////////////////////////////////////////////////////////////////
//
// NEC/Renesas M62429 Digital Volume Control Driver Library for Arduino
// Copyright (c) 2014, 2019 Roger A. Krupski <rakrupski@verizon.net>
//
// Last update: 02 December 2019
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include "M62429.h"
M62429::M62429 (uint8_t clk_pin, uint8_t dat_pin)
{
uint8_t x;
x = digitalPinToPort (clk_pin); // pin 5 on the M62429
_CLK_OUT = portOutputRegister (x);
_CLK_DDR = portModeRegister (x);
_CLK_BIT = digitalPinToBitMask (clk_pin);
x = digitalPinToPort (dat_pin); // pin 4 on the M62429
_DAT_OUT = portOutputRegister (x);
_DAT_DDR = portModeRegister (x);
_DAT_BIT = digitalPinToBitMask (dat_pin);
*_DAT_OUT &= ~_DAT_BIT; // digitalWrite low
*_CLK_OUT &= ~_CLK_BIT; // digitalWrite low
*_DAT_DDR |= _DAT_BIT; // pinmode output
*_CLK_DDR |= _CLK_BIT; // pinmode output
}
uint16_t M62429::setLeft (int8_t vol) // set CH1 (pins 1 [in] and 2 [out])
{
return _setVolume (vol, 0, 1);
}
uint16_t M62429::setRight (int8_t vol) // set CH2 (pins 8 [in] and 7 [out])
{
return _setVolume (vol, 1, 1);
}
uint16_t M62429::setBoth (int8_t vol) // both simultaneously
{
return _setVolume (vol, 0, 0);
}
uint16_t M62429::_setVolume (int8_t volume, uint8_t chan, uint8_t both)
{
uint8_t bits; // 11 bit control
uint16_t atten; // volume converted to attenuation
uint16_t data; // control word is built by OR-ing in the bits
// constrain volume to 0...100
volume = volume < 0 ? 0 : volume > 100 ? 100 : volume;
// convert volume 0...100 to attenuation range 0...84
atten = ((volume * 84) / 100);
// initialize (clear) data
data = 0;
data |= chan ? (1 << 0) : (0 << 0); // D0 (channel select: 0=ch1, 1=ch2)
data |= both ? (1 << 1) : (0 << 1); // D1 (individual/both select: 0=both, 1=individual)
data |= (atten & (0b11111 << 2)); // D2...D6 (0...84 in steps of 4)
data |= ((atten << 7) & (0b11 << 7)); // D7 & D8 (0...3)
data |= (1 << 9); // D9 and...
data |= (1 << 10); // ...D10 must both be 1
for (bits = 0; bits < 11; bits++) { // send out 10 control bits
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
*_DAT_OUT &= ~_DAT_BIT;
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
*_CLK_OUT &= ~_CLK_BIT;
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
data & _BV(bits) ? *_DAT_OUT |= _DAT_BIT : *_DAT_OUT &= ~_DAT_BIT;
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
*_CLK_OUT |= _CLK_BIT;
}
// send final (11th) bit
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
*_DAT_OUT |= _DAT_BIT; // final clock latches data in
__builtin_avr_delay_cycles ((F_CPU / 1e6) * 2); // 2 usec
*_CLK_OUT &= ~_CLK_BIT;
return data; // return bit pattern in case you want it :)
}