-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathReceiveAndSendDynamicPins.ino
278 lines (242 loc) · 9.33 KB
/
ReceiveAndSendDynamicPins.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
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
269
270
271
272
273
274
275
276
277
278
/*
* ReceiveAndSendDynamicPins.cpp
*
* Example how to use pins specified at runtime.
*
* Serves as a IR remote macro expander
* Receives Samsung32 protocol and on receiving a specified input frame, it sends multiple Samsung32 frames.
* This serves as a Netflix-key emulation for my old Samsung H5273 TV.
*
* Copyright (C) 2019-2020 Armin Joachimsmeyer
*
* This file is part of IRMP https://github.com/IRMP-org/IRMP.
*
* IRMP 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/gpl.html>.
*
*/
// ATMEL ATTINY85
// Piezo speaker must have a 270 ohm resistor in series for USB programming and running at the Samsung TV.
// IR LED has a 270 ohm resistor in series.
// +-\/-+
// !RESET (5) PB5 1| |8 Vcc
// USB+ 3.6V Z-Diode, 1.5kOhm to VCC Piezo (3) PB3 2| |7 PB2 (2) TX Debug output
// USB- 3.6V Z-Diode IR Output (4) PB4 3| |6 PB1 (1) Feedback LED
// GND 4| |5 PB0 (0) IR Input
// +----+
/*
* Pin mapping table for different platforms
*
* Platform IR input IR output Tone
* -----------------------------------------
* DEFAULT/AVR 2 3 4
* ATtinyX5 0 4 3
* ATtin167 9 8 5 // Digispark pro number schema
* ATtin167 3 2 7
* SAMD21 3 4 5
* ESP8266 14 // D5 12 // D6 %
* ESP32 15 4 %
* BluePill PA6 PA7 PA3
* APOLLO3 11 12 5
*/
/*
* Allow dynamic specification of input and output pins.
* Requires additional 200 bytes program memory.
* This must be first, it is used in PinDefinitionsAndMore.h
*/
#define IRMP_IRSND_ALLOW_DYNAMIC_PINS
#include <Arduino.h>
/*
* Set library modifiers first to set input and output pin etc.
*/
#include "PinDefinitionsAndMore.h"
//#define IR_OUTPUT_IS_ACTIVE_LOW
#define IRSND_IR_FREQUENCY 38000
#define IRMP_PROTOCOL_NAMES 1 // Enable protocol number mapping to protocol strings - requires some FLASH.
#define IRSND_PROTOCOL_NAMES 1 // Enable protocol number mapping to protocol strings - requires some FLASH.
#define IRMP_SUPPORT_SAMSUNG_PROTOCOL 1
#define IRSND_SUPPORT_SAMSUNG_PROTOCOL 1
/*
* After setting the definitions we can include the code and compile it.
*/
#define USE_ONE_TIMER_FOR_IRMP_AND_IRSND // otherwise we get an error on AVR platform: redefinition of 'void __vector_8()
#include <irmp.hpp>
#include <irsnd.hpp>
IRMP_DATA irmp_data;
uint8_t sIRMPInputPin;
IRMP_DATA irsnd_data;
void sendSamsungSmartHubMacro(bool aDoSelect);
void IRSendWithDelay(uint16_t aCommand, uint16_t aDelayMillis);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/ \
|| defined(SERIALUSB_PID) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_attiny3217)
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif
#if defined(ESP8266)
Serial.println(); // to separate it from the internal boot output
#endif
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRMP));
tone(TONE_PIN, 2200);
digitalWrite(LED_BUILTIN, HIGH);
delay(400);
digitalWrite(LED_BUILTIN, LOW);
noTone(TONE_PIN);
/*
* Request pin number from user by serial monitor
*/
Serial.println(F("Please enter pin number to use for receiving IR signals."));
while (Serial.available() == 0)
{
; // Wait for user response
}
sIRMPInputPin = Serial.parseInt();
while (Serial.available())
{
Serial.read(); // read rest of line and CR/LF from buffer to enable for next input
}
Serial.print(F("Ready to receive IR signals of protocols: "));
irmp_print_active_protocols(&Serial);
Serial.println(F("at pin "));
Serial.println(sIRMPInputPin);
irmp_init(sIRMPInputPin, LED_BUILTIN); // Enable receive signal feedback at LED_BUILTIN for receive (and send)
Serial.println(F("Please enter pin number to use for sending IR signals."));
while (Serial.available() == 0)
{
; // Wait for user response
}
uint8_t tIRSNDOutputPin = Serial.parseInt();
Serial.print(F("Ready to send IR signals at pin "));
Serial.println(tIRSNDOutputPin);
irsnd_init(tIRSNDOutputPin); // feedback LED is specified at irmp_init()
irsnd_data.protocol = IRMP_SAMSUNG32_PROTOCOL;
irsnd_data.address = 0x0707;
irsnd_data.flags = 1; // repeat frame 1 time
}
void loop()
{
/*
* Check if new data available and get them
*/
if (irmp_get_data(&irmp_data))
{
irmp_result_print(&irmp_data);
/*
* Here data is available -> evaluate IR command
*/
switch (irmp_data.command)
{
case 0xB847: // The play key on the bottom of my Samsung remote
Serial.println(F("Play key detected, open Netflix"));
sendSamsungSmartHubMacro(true);
break;
case 0xB54A: // The pause key on the bottom of my Samsung remote
Serial.println(F("Pause key detected, open SmartHub"));
sendSamsungSmartHubMacro(false);
break;
default:
break;
}
// Flush repeats received
irmp_get_data(&irmp_data);
}
}
void IRSendWithDelay(uint16_t aCommand, uint16_t aDelayMillis)
{
irsnd_data.command = aCommand;
// true = wait for frame and trailing space/gap to end. This stores timer state and restores it after sending.
if (!irsnd_send_data(&irsnd_data, true))
{
Serial.println(F("Protocol not found")); // name of protocol is printed by irsnd_data_print()
}
irsnd_data_print(&Serial,&irsnd_data);
delay(aDelayMillis);
}
bool sMacroWasCalledBefore = false;
#define INITIAL_WAIT_TIME_APPS_READY_MILLIS 70000 // Time to let the TV load all software before Netflix can be started without an error
#define INITIAL_WAIT_TIME_SMARTHUB_READY_MILLIS 20000 // Time to let the TV load all software before SmartHub manu can be displayed
/*
* This macro calls the last SmartHub application you selected manually
*
* @param aDoSelect - if true select the current app (needs longer initial wait time) else show smarthub menu
*
*/
void sendSamsungSmartHubMacro(bool aDoSelect)
{
uint32_t tWaitTimeAfterBoot;
if (aDoSelect)
{
tWaitTimeAfterBoot = INITIAL_WAIT_TIME_APPS_READY_MILLIS;
}
else
{
tWaitTimeAfterBoot = INITIAL_WAIT_TIME_SMARTHUB_READY_MILLIS;
}
if (millis() < tWaitTimeAfterBoot)
{
// division by 1000 and printing requires much (8%) program memory
Serial.print(F("It is "));
Serial.print(millis() / 1000);
Serial.print(F(" seconds after boot, Samsung H5273 TV requires "));
Serial.print(tWaitTimeAfterBoot / 1000);
Serial.println(F(" seconds after boot to be ready for the command"));
tone(TONE_PIN, 2200);
delay(100);
noTone(TONE_PIN);
delay(100);
tone(TONE_PIN, 2200);
delay(100);
noTone(TONE_PIN);
while (millis() < tWaitTimeAfterBoot)
{
delay(10); // blocking wait
}
}
// Do beep feedback for special key to be received
tone(TONE_PIN, 2200);
delay(200);
noTone(TONE_PIN);
irmp_init(sIRMPInputPin); // restore timer for IR receive after using of tone
Serial.println(F("Wait for \"not supported\" to disappear"));
delay(2000);
Serial.println(F("Start sending of Samsung IR macro"));
IRSendWithDelay(0xE51A, 2000); // Menu and wait for the Menu to pop up
Serial.println(F("Wait for the menu to pop up"));
if (!sMacroWasCalledBefore)
{
delay(2000); // wait additional time for the Menu load
}
for (uint_fast8_t i = 0; i < 4; ++i)
{
IRSendWithDelay(0x9E61, 250); // Down arrow
}
IRSendWithDelay(0x9D62, 400); // Right arrow
for (uint_fast8_t i = 0; i < 2; ++i)
{
IRSendWithDelay(0x9E61, 250); // Down arrow
}
delay(250);
IRSendWithDelay(0x9768, 1); // Enter for SmartHub
if (aDoSelect)
{
Serial.println(F("Wait for SmartHub to show up, before entering current application"));
delay(10000); // Wait not longer than 12 seconds, because smarthub menu then disappears
IRSendWithDelay(0x9768, 1); // Enter for last application (e.g. Netflix or Amazon)
}
sMacroWasCalledBefore = true;
Serial.println(F("Done"));
}