/
ZWave2IR.ino
170 lines (142 loc) · 4.73 KB
/
ZWave2IR.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
/*
* Receives ON/OFF command via Z-Wave and
* sends specified IR command using built-in IR-controller of Z-Uno
*
* (c) Z-Wave.Me 2016
*/
#include <IRController.h>
// You have to connect your IR-LED to PIN#6 of Z-Uno
// In other hand you can use PIN#4/PIN#5 for that purpose too - just modify the next string
IRTransmitterParams ir_transmitter(byte(IR_TRANSMITTER_OUTPUT_PIN6),
IR_FLAGS_OUTPUT_HIGHDRIVE,
IR_MS_PRESCALLER_4MHZ,
IR_CARRIER_PRESCALLER_8MHZ);
// LED pin number
#define LED_PIN 13
// For some cases use UART (Serial0/Serial1)
// It's a most comfortable way for debugging
// By default we use built-in USB CDC (Serial)
#define MY_SERIAL Serial
#define SWITCH_ON 0xff
#define SWITCH_OFF 0
// Use IRScanner.ino as IR-sniffer
// Sniffed raw16 commands
// This codes were shiffed from SONY TV IRController
// RAW: ON/OFF Decoded: MUTE/MUTE
word raw_command_on[] = {0x19, 0x990, 0x242, 0x26D, 0x243, 0x26C, 0x244, 0x26A, 0x246, 0x4C5, 0x243, 0x4BD, 0x24B, 0x4C0, 0x248, 0x267, 0x249, 0x4C2, 0x246, 0x269, 0x247, 0x267, 0x249, 0x265, 0x24B, 0x264};
word raw_command_off[] = {0x19, 0x987, 0x24B, 0x4C0, 0x248, 0x267, 0x249, 0x4C2, 0x247, 0x268, 0x248, 0x4C3, 0x245, 0x269, 0x246, 0x268, 0x248, 0x4C3, 0x245, 0x26A, 0x246, 0x269, 0x247, 0x267, 0x249, 0x266};
// Detected vendor
#define MY_IR_VENDOR IR_VENDOR_SONY // Use SONY protocol. See IRController.h for more details.
// Decoded commands
#define MY_IR_VENDOR_CMD_ON 0x290
#define MY_IR_VENDOR_CMD_OFF 0x290
#define DECODED_COMMAND_NBITS 12
// Last saved LED value
byte currentCh1 = 0;
byte currentCh2 = 0;
byte current_cmd = 0;
IRCommand_t vendor_cmd;
ZUNO_SETUP_DEBUG_MODE(DEBUG_ON);
// next macro sets up the Z-Uno channels
// in this example we set up 2 switch binary channels
// you can read more on http://z-uno.z-wave.me/Reference/ZUNO_SWITCH_BINARY/
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter1, setter1),
ZUNO_SWITCH_BINARY(getter2, setter2));
void setup() {
pinMode(LED_PIN, OUTPUT); // setup pin as output
// We have to setup IRLED pin as output
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
MY_SERIAL.begin(115200);
MY_SERIAL.println("\n **** Sketch is starting... ****\n");
// setting up IR-controller as IR-transmitter
ir_transmitter.setupVendor(MY_IR_VENDOR);
IR.begin(&ir_transmitter);
vendor_cmd.vendor = MY_IR_VENDOR;
vendor_cmd.n_bits = DECODED_COMMAND_NBITS;
}
void sendRAWCommand(WORD * raw16)
{
// we have to repeat packet
// like another IR-controllers do
byte count = 7;
while(count--)
{
IR.send_raw16(raw16);
delay(30);
}
}
void sendVendorCommand(unsigned long cmd)
{
// we have to repeat packet
// like another IR-controllers do
byte count = 7;
while(count--)
{
vendor_cmd.data[0] = cmd;
IR.sendCommand(&vendor_cmd);
delay(30);
}
}
void loop() {
switch(current_cmd)
{
case 1:
MY_SERIAL.println("\n RAW ON\n");
sendRAWCommand(raw_command_on);
break;
case 2:
MY_SERIAL.println("\n RAW OFF\n");
sendRAWCommand(raw_command_off);
break;
case 3:
MY_SERIAL.println("\n VENDOR ON\n");
sendVendorCommand(MY_IR_VENDOR_CMD_ON);
break;
case 4:
MY_SERIAL.println("\n VENDOR OFF\n");
sendVendorCommand(MY_IR_VENDOR_CMD_OFF);
break;
default:
// nothing to do
break;
}
current_cmd = 0;
delay(500);
}
// function, which sets new channel state
// this function runs only once the controller sends new value
void setter1 (byte value) {
// value is a variable, holding a "new value"
// which came from the controller or other Z-Wave device
if (value > 0) { // if greater then zero
current_cmd = 1; // will be dispatched in the next loop
} else { // if equals zero
current_cmd = 2;
}
// let's save our value for the situation, when the controller will ask us about it
currentCh1 = value;
}
// function, which returns the previously saved relay value
// this function runs only once the controller asks
byte getter1 (){
return currentCh1;
}
// function, which sets new relay state
// this function runs only once the controller sends new value
void setter2 (byte value) {
// value is a variable, holding a "new value"
// which came from the controller or other Z-Wave device
if (value > 0) { // if greater then zero
current_cmd = 3; // will be dispatched in the next loop
} else { // if equals zero
current_cmd = 4;
}
// let's save our value for the situation, when the controller will ask us about it
currentCh2 = value;
}
// function, which returns the previously saved relay value
// this function runs only once the controller asks
byte getter2 (){
return currentCh2;
}