forked from sunrutcon/PT2258
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPT2258.cpp
More file actions
89 lines (75 loc) · 2.98 KB
/
PT2258.cpp
File metadata and controls
89 lines (75 loc) · 2.98 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
/* Arduino PT2258 Library
*
* This file is part of the Arduino PT2258 Library
*
*/
#include <arduino.h>
#include <wire.h>
#include <PT2258.h>
unsigned char channell_address01[6] =
{CHANNEL1_VOLUME_STEP_01, CHANNEL2_VOLUME_STEP_01, CHANNEL3_VOLUME_STEP_01,
CHANNEL4_VOLUME_STEP_01, CHANNEL5_VOLUME_STEP_01, CHANNEL6_VOLUME_STEP_01};
unsigned char channell_address10[6] =
{CHANNEL1_VOLUME_STEP_10, CHANNEL2_VOLUME_STEP_10, CHANNEL3_VOLUME_STEP_10,
CHANNEL4_VOLUME_STEP_10, CHANNEL5_VOLUME_STEP_10, CHANNEL6_VOLUME_STEP_10};
// helper method
unsigned char PT2258::HEX2BCD (unsigned char x)
{
unsigned char y;
y = (x / 10) << 4;
y = y | (x % 10);
return (y);
}
// helper method
int PT2258::writeI2CChar(unsigned char c)
{
// shift address to right - Wire library always uses 7 bit addressing
Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2258
Wire.write(c);
int rtnCode = Wire.endTransmission(); // stop transmitting
return rtnCode;
}
// initialize PT2258
int PT2258::init(void)
{
delay(300); // in case this is first time - I2C bus not ready for this long on power on with 10uF cref
unsigned char masterVolumeValue = 0x00; //master volume 0dB
unsigned char channelVolume = 0x00; // channel volume 0dB
// initialize device
writeI2CChar(SYSTEM_RESET);
// set channell volumes to zero
for(int chno=0; chno<6; chno++){
Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2258
Wire.write(channell_address01[chno] | (HEX2BCD(channelVolume) & 0x0f));
Wire.write(channell_address10[chno] | ((HEX2BCD(channelVolume) & 0xf0)>>4));
Wire.endTransmission(); // stop transmitting
}
// set the master voume
Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2258
Wire.write(MASTER_VOLUME_1STEP | (HEX2BCD(masterVolumeValue) & 0x0f));
Wire.write(MASTER_VOLUME_10STEP | ((HEX2BCD(masterVolumeValue) & 0xf0)>>4));
Wire.endTransmission(); // stop transmitting
// set mute off
return writeI2CChar(MUTE | 0x00);
}
// Set mute: 1 -> mute on, 0 -> mute off
void PT2258::setMute(char in_mute)
{
writeI2CChar(MUTE | in_mute);
}
// Set channel volume, attenuation range : 0 to 79dB
void PT2258::setChannelVolume(unsigned char chvol, char chno)
{
Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2258
Wire.write(channell_address01[chno] | (HEX2BCD(chvol) & 0x0f));
Wire.write(channell_address10[chno] | ((HEX2BCD(chvol) & 0xf0)>>4));
Wire.endTransmission(); // stop transmitting
}
// Set master volume, attenuation range : 0 to 79dB
void PT2258::setMasterVolume(unsigned char mvol)
{
Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2258
Wire.write(MASTER_VOLUME_1STEP | (HEX2BCD(mvol) & 0x0f));
Wire.write(MASTER_VOLUME_10STEP | ((HEX2BCD(mvol) & 0xf0)>>4));
Wire.endTransmission(); // stop transmitting
}