forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathir_bridge.ino
More file actions
182 lines (138 loc) · 5.15 KB
/
ir_bridge.ino
File metadata and controls
182 lines (138 loc) · 5.15 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
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
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Import libraries (BLEPeripheralObserver depends on SPI)
#include <SPI.h>
#include <BLEPeripheralObserver.h>
// https://github.com/shirriff/Arduino-IRremote
#include <IRremote.h>
#define IR_SEND_PIN 3
#define IR_RECV_PIN 4
#define LED_PIN 5
struct IRValue {
char type;
char bits;
unsigned int address;
unsigned long value;
};
// create IR send and recv instance, see pinouts above
IRsend irSend = IRsend(/*IR_SEND_PIN*/);
IRrecv irRecv = IRrecv(IR_RECV_PIN);
IRValue irValue;
//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST)
BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver();
// create service and characteristics
BLEService irService = BLEService("00004952-0000-bbbb-0123-456789abcdef");
BLEFixedLengthCharacteristic irOutputCharacteristic = BLEFixedLengthCharacteristic("00004953-0000-bbbb-0123-456789abcdef", BLEWrite, sizeof(irValue));
BLEFixedLengthCharacteristic irInputCharacteristic = BLEFixedLengthCharacteristic("00004954-0000-bbbb-0123-456789abcdef", BLENotify, sizeof(irValue));
void setup() {
Serial.begin(115200);
// set advertised local name and service UUID
blePeriphObserv.setLocalName("IR");
blePeriphObserv.setAdvertisedServiceUuid(irService.uuid());
// add service and characteristics
blePeriphObserv.addAttribute(irService);
blePeriphObserv.addAttribute(irOutputCharacteristic);
blePeriphObserv.addAttribute(irInputCharacteristic);
// assign event handlers for connected, disconnected to peripheral
blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// begin initialization
blePeriphObserv.begin();
Serial.println(F("BLE IR Peripheral"));
// enable the IR receiver
irRecv.enableIRIn();
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// poll peripheral
blePeriphObserv.poll();
// poll the ouput characteristic
pollIrOutput();
// poll the IR receiver
pollIrInput();
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
digitalWrite(LED_PIN, HIGH);
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
digitalWrite(LED_PIN, LOW);
}
void pollIrOutput() {
// check if central has written to the output value
if (irOutputCharacteristic.written()) {
// copy the value written
memcpy(&irValue, irOutputCharacteristic.value(), sizeof(irValue));
// extract the data
char irOutputType = irValue.type;
char irOutputBits = irValue.bits;
unsigned int irOutputAddress = irValue.address;
unsigned long irOutputValue = irValue.value;
int sendCount;
// calculate how many times to send the value, depends on the type
if (irOutputType == SONY || irOutputType == RC5 || irOutputType == RC6) {
sendCount = 3;
} else {
sendCount = 1;
}
for (int i = 0; i < sendCount; i++) {
switch (irOutputType) {
case NEC:
irSend.sendNEC(irOutputValue, irOutputBits);
break;
case SONY:
irSend.sendSony(irOutputValue, irOutputBits);
break;
case RC5:
irSend.sendRC5(irOutputValue, irOutputBits);
break;
case RC6:
irSend.sendRC6(irOutputValue, irOutputBits);
break;
case DISH:
irSend.sendDISH(irOutputValue, irOutputBits);
break;
case SHARP:
irSend.sendSharpRaw(irOutputValue, irOutputBits);
break;
case PANASONIC:
irSend.sendPanasonic(irOutputAddress, irOutputValue);
break;
case JVC:
irSend.sendJVC(irOutputValue, irOutputBits, 0);
break;
case SAMSUNG:
irSend.sendSAMSUNG(irOutputValue, irOutputBits);
break;
case SANYO:
case MITSUBISHI:
case LG:
default:
// not implemented
break;
}
delay(40);
}
// re-enable the IR receiver
irRecv.enableIRIn();
}
}
void pollIrInput() {
decode_results irDecodeResults;
// check if IR recv has a result that can be decoded
if (irRecv.decode(&irDecodeResults)) {
// must have non-zero number of bits and known decode type
if (irDecodeResults.bits && irDecodeResults.decode_type != UNKNOWN) {
// extract the decoded value
irValue.type = irDecodeResults.decode_type;
irValue.address = (irValue.type == PANASONIC) ? irDecodeResults.panasonicAddress : 0;
irValue.bits = irDecodeResults.bits;
irValue.value = irDecodeResults.value;
// update the IR characteristic with the result
irInputCharacteristic.setValue((unsigned char *)&irValue, sizeof(irValue));
}
// resume IR receiving
irRecv.resume();
}
}