-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomekit-operation.c
268 lines (238 loc) · 8.56 KB
/
homekit-operation.c
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
#include <stdio.h>
#include <Arduino.h>
#include <port.h>
#include <homekit/types.h>
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
#include "control-protocol.h"
#define ACCESSORY_NAME ("Marantz HiFi")
#define ACCESSORY_SN (__DATE__ " " __TIME__)
#define ACCESSORY_MANUFACTURER ("DH")
#define ACCESSORY_MODEL ("Marantz PM4000 DIY control by ESP8266 with Arduino Homekit")
#define ACCESSORY_FW ("0.0.17")
// TODO: move control part to separate C++ file and leave here only bridge to HomeKit
// ----------------------- power control -----------------------------------------------
// TODO: hold internally Power parameter value - replace with a real check to GPIO value
enum PowerState
{
PowerOff = 0,
PowerOn = 1
} powerState = PowerOff;
homekit_value_t GetPowerState()
{
return HOMEKIT_BOOL(powerState);
}
void SetPowerState(homekit_value_t value)
{
powerState = value.bool_value;
ExecuteUpdatePower(powerState);
}
void ExecuteUpdatePower(enum PowerState actualState)
{
if (actualState == PowerOn)
{
SendMarantzPowerOn();
}
else
{
SendMarantzPowerOff();
}
}
// ----------------------- input control -----------------------------------------------
enum AmplifierInput
{
InputCD,
InputPhono,
InputTuner,
InputAUX,
InputTape,
InputMD
} amplifierInput = InputCD;
void SetInputCD(homekit_value_t);
void SetInputPhono(homekit_value_t);
void SetInputTuner(homekit_value_t);
void SetInputTape(homekit_value_t);
void SetInputAux(homekit_value_t);
void SetInputCdrMd(homekit_value_t);
homekit_characteristic_t inputCD_characteristic = HOMEKIT_CHARACTERISTIC_(ON, true, .setter = SetInputCD);
homekit_characteristic_t inputPhono_characteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetInputPhono);
homekit_characteristic_t inputTuner_characteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetInputTuner);
homekit_characteristic_t inputTape_characteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetInputTape);
homekit_characteristic_t inputAux_characteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetInputAux);
homekit_characteristic_t inputMD_characteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetInputCdrMd);
homekit_characteristic_t* GetInputCharacteristic(enum AmplifierInput value)
{
switch (value)
{
case InputCD: return &inputCD_characteristic;
case InputPhono: return &inputPhono_characteristic;
case InputTuner: return &inputTuner_characteristic;
case InputAUX: return &inputAux_characteristic;
case InputTape: return &inputTape_characteristic;
case InputMD: return &inputMD_characteristic;
default: return &inputCD_characteristic;
}
}
void SetInputStateInHomeKit(enum AmplifierInput newValue)
{
if (newValue == amplifierInput)
{
homekit_characteristic_notify(GetInputCharacteristic(amplifierInput), HOMEKIT_BOOL(true));
}
else
{
homekit_characteristic_notify(GetInputCharacteristic(amplifierInput), HOMEKIT_BOOL(false));
amplifierInput = newValue;
}
}
void SetInputCD(homekit_value_t value)
{
SendMarantzInputCD();
SetInputStateInHomeKit(InputCD);
}
void SetInputPhono(homekit_value_t value)
{
SendMarantzInputPhono();
SetInputStateInHomeKit(InputPhono);
}
void SetInputTuner(homekit_value_t value)
{
SendMarantzInputTuner();
SetInputStateInHomeKit(InputTuner);
}
void SetInputTape(homekit_value_t value)
{
SendMarantzInputTape();
SetInputStateInHomeKit(InputTape);
}
void SetInputAux(homekit_value_t value)
{
SendMarantzInputAux();
SetInputStateInHomeKit(InputAUX);
}
void SetInputCdrMd(homekit_value_t value)
{
SendMarantzInputCdrMd();
SetInputStateInHomeKit(InputMD);
}
// ----------------------- volume control ----------------------------------------------
void SetVolumeUp(homekit_value_t);
void SetVolumeDown(homekit_value_t);
homekit_characteristic_t volumeUpCharacteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetVolumeUp);
homekit_characteristic_t volumeDownCharacteristic = HOMEKIT_CHARACTERISTIC_(ON, false, .setter = SetVolumeDown);
void SetVolumeUp(homekit_value_t value)
{
SendMarantzVolumeUp();
homekit_characteristic_notify(&volumeUpCharacteristic, HOMEKIT_BOOL(false));
}
void SetVolumeDown(homekit_value_t value)
{
SendMarantzVolumeDown();
homekit_characteristic_notify(&volumeDownCharacteristic, HOMEKIT_BOOL(false));
}
void SetMute(homekit_value_t value)
{
SendMarantzMute();
}
// ----------------------- HomeKit configuration ---------------------------------------
void IdentifyAccessory(homekit_value_t _value)
{
for (int j = 0; j < 3; j++)
{
SetPowerState(HOMEKIT_BOOL(true));
delay(500);
SetPowerState(HOMEKIT_BOOL(false));
delay(500);
}
}
homekit_accessory_t *accessories[] =
{
HOMEKIT_ACCESSORY(
.id = 1,
.category = homekit_accessory_category_speaker,
.services = (homekit_service_t *[]){
HOMEKIT_SERVICE(
ACCESSORY_INFORMATION,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(IDENTIFY, IdentifyAccessory), // Can be triggered from ipad on pairing
HOMEKIT_CHARACTERISTIC(MANUFACTURER, ACCESSORY_MANUFACTURER),
HOMEKIT_CHARACTERISTIC(MODEL, ACCESSORY_MODEL),
HOMEKIT_CHARACTERISTIC(NAME, ACCESSORY_NAME),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, ACCESSORY_SN),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, ACCESSORY_FW),
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.primary = true,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(ON, false, .getter = GetPowerState, .setter = SetPowerState),
HOMEKIT_CHARACTERISTIC(NAME, "/Power"), // Homekit bug? Only names with special characters are set.
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input CD"),
&inputCD_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input Phono"),
&inputPhono_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input Tuner"),
&inputTuner_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input Tape"),
&inputTape_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input AUX"),
&inputAux_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Input CD-R MD"),
&inputMD_characteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Volume UP"),
&volumeUpCharacteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(NAME, "/Volume Down"),
&volumeDownCharacteristic,
NULL}),
HOMEKIT_SERVICE(
SWITCH,
.characteristics = (homekit_characteristic_t *[]){
HOMEKIT_CHARACTERISTIC(ON, false, .setter = SetMute),
HOMEKIT_CHARACTERISTIC(NAME, "/Mute"),
NULL}),
NULL}),
NULL
};
homekit_server_config_t config =
{
.accessories = accessories,
.password = "111-11-111",
//.on_event = on_homekit_event,
.setupId = "ABCD"
};
void accessory_init()
{
pinMode(ControlLinePin, OUTPUT);
}