-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsa-pioneer-remote.ino
171 lines (130 loc) · 3.76 KB
/
psa-pioneer-remote.ino
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
#include <SPI.h>
#include <mcp_can.h>
#define KOHMS(i) 256-((i/100.0)*256.00)
// Pioneer WR resistance values
#define WR_SOURCE KOHMS(1.2)
#define WR_ATT KOHMS(3.5)
#define WR_DISPLAY KOHMS(5.75)
// PSA Remote CAN message values
#define REMOTE_ID 0x21F
#define FORWARD B10000000
#define BACKWARD B01000000
#define VOL_UP B00001000
#define VOL_DOWN B00000100
#define SOURCE B00000010
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
unsigned char remoteState = 0;
char msgString[128]; // Array to store serial string
#define CAN0_INT 3
#define CAN0_CS 10
#define DIGIPOT_CS 9
MCP_CAN CAN0(CAN0_CS);
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode (DIGIPOT_CS, OUTPUT);
SPI.begin();
shutdown();
int canOk = 0;
// Initialize MCP2515 running at 16MHz with a baudrate of 125kb/s and the masks and filters disabled.
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
if(canOk = CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_LISTENONLY); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT);
Serial.println("Setup OK");
}
void loop() {
if(!digitalRead(CAN0_INT)){
CAN0.readMsgBuf(&rxId, &len, rxBuf);
if((rxId & 0xFFF) != REMOTE_ID) return;
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
//Serial.print(msgString);
//Serial.println();
//Serial.println(rxId);
//print_binary(rxBuf[0], 8);
//Serial.println();
if(remoteState == rxBuf[0]){
return;
} else {
remoteState = rxBuf[0];
}
if(remoteState == 0){
shutdown();
}
if(remoteState & SOURCE){
digitalPotWrite(WR_SOURCE);
Serial.println("Source");
}
if((remoteState & VOL_UP) && (remoteState & VOL_DOWN)){
digitalPotWrite(WR_ATT);
Serial.println("Mute");
return;
}
if(remoteState & VOL_UP){
Serial.println("Volume up");
}
if(remoteState & VOL_DOWN){
Serial.println("Volume down");
}
if(remoteState & FORWARD){
Serial.println("Forward");
}
if(remoteState & BACKWARD){
Serial.println("Backward");
}
}
}
void digitalPotWrite(byte value)
{
digitalWrite(CAN0_CS, HIGH);
// take the SS pin low to select the chip:
digitalWrite(DIGIPOT_CS,LOW);
SPI.transfer(B00010001); // The command byte
SPI.transfer(value); // The data byte
// take the SS pin high to de-select the chip
digitalWrite(DIGIPOT_CS,HIGH);
}
void shutdown(){
digitalWrite(CAN0_CS, HIGH);
// take the SS pin low to select the chip:
digitalWrite(DIGIPOT_CS,LOW);
SPI.transfer(B00100001); // The command byte
SPI.transfer(0); // The data byte
// take the SS pin high to de-select the chip
digitalWrite(DIGIPOT_CS,HIGH);
}
void print_binary(int v, int num_places)
{
int mask=0, n;
for (n=1; n<=num_places; n++)
{
mask = (mask << 1) | 0x0001;
}
v = v & mask; // truncate v to specified number of places
while(num_places)
{
if (v & (0x0001 << num_places-1))
{
Serial.print("1");
}
else
{
Serial.print("0");
}
--num_places;
if(((num_places%4) == 0) && (num_places != 0))
{
Serial.print("_");
}
}
}