|
@@ -16,6 +16,8 @@
|
16
|
16
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
17
|
*/
|
18
|
18
|
|
|
19
|
+/* Portions subject to the following copyright notices */
|
|
20
|
+
|
19
|
21
|
/*
|
20
|
22
|
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
|
21
|
23
|
|
|
@@ -32,11 +34,43 @@
|
32
|
34
|
limitations under the License.
|
33
|
35
|
*/
|
34
|
36
|
|
|
37
|
+/*
|
|
38
|
+ PLAY Embedded demos - Copyright (C) 2014-2017 Rocco Marco Guglielmi
|
|
39
|
+
|
|
40
|
+ This file is part of PLAY Embedded demos.
|
|
41
|
+
|
|
42
|
+ Licensed under the Apache License, Version 2.0 (the "License");
|
|
43
|
+ you may not use this file except in compliance with the License.
|
|
44
|
+ You may obtain a copy of the License at
|
|
45
|
+
|
|
46
|
+ http://www.apache.org/licenses/LICENSE-2.0
|
|
47
|
+
|
|
48
|
+ Unless required by applicable law or agreed to in writing, software
|
|
49
|
+ distributed under the License is distributed on an "AS IS" BASIS,
|
|
50
|
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
51
|
+ See the License for the specific language governing permissions and
|
|
52
|
+ limitations under the License.
|
|
53
|
+*/
|
|
54
|
+
|
35
|
55
|
#include <stdbool.h>
|
36
|
56
|
|
37
|
57
|
#include "ch.h"
|
38
|
58
|
#include "hal.h"
|
39
|
59
|
|
|
60
|
+static event_listener_t el;
|
|
61
|
+static event_source_t IR_receiver;
|
|
62
|
+/*===========================================================================*/
|
|
63
|
+/* IR remote related code. */
|
|
64
|
+/*===========================================================================*/
|
|
65
|
+
|
|
66
|
+#define DELTA 5
|
|
67
|
+#define START_PULSE 45
|
|
68
|
+#define END_PULSE 405
|
|
69
|
+#define RPT_CMD_PULSE 959
|
|
70
|
+#define ZERO_PULSE 6
|
|
71
|
+#define ONE_PULSE 17
|
|
72
|
+#define COMMA_PULSE 23
|
|
73
|
+
|
40
|
74
|
/* PWM configuration for IR output */
|
41
|
75
|
static PWMConfig pwmcfg = {
|
42
|
76
|
380000, /* 380 kHz PWM clock frequency. */
|
|
@@ -59,6 +93,58 @@ static const GPTConfig gpt4cfg = {
|
59
|
93
|
0, 0
|
60
|
94
|
};
|
61
|
95
|
|
|
96
|
+/*===========================================================================*/
|
|
97
|
+/* ICU related code. */
|
|
98
|
+/*===========================================================================*/
|
|
99
|
+static int32_t index = -1;
|
|
100
|
+static bool START_OCCURED = FALSE, REPEAT_FLAG = FALSE;
|
|
101
|
+static uint32_t tmp, command;
|
|
102
|
+static void icuwidthcb(ICUDriver *icup) {
|
|
103
|
+
|
|
104
|
+ icucnt_t cnt = icuGetWidthX(icup);
|
|
105
|
+ if((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))){
|
|
106
|
+ index = 0;
|
|
107
|
+ START_OCCURED = TRUE;
|
|
108
|
+ }
|
|
109
|
+ else if((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))){
|
|
110
|
+ tmp |= 1 << (31 - index);
|
|
111
|
+ index++;
|
|
112
|
+ }
|
|
113
|
+ else if((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))){
|
|
114
|
+ tmp &= ~(1 << (31 - index));
|
|
115
|
+ index++;
|
|
116
|
+ }
|
|
117
|
+ else if((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))){
|
|
118
|
+ if((START_OCCURED) && (index == 32)) {
|
|
119
|
+ command = tmp;
|
|
120
|
+ } else {
|
|
121
|
+ command = 0;
|
|
122
|
+ }
|
|
123
|
+ REPEAT_FLAG = FALSE;
|
|
124
|
+ START_OCCURED = FALSE;
|
|
125
|
+ index = -1;
|
|
126
|
+ }
|
|
127
|
+ else if((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))){
|
|
128
|
+ REPEAT_FLAG = TRUE;
|
|
129
|
+ }
|
|
130
|
+ else if((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))){
|
|
131
|
+ chEvtBroadcastFlags(&IR_receiver, 0);
|
|
132
|
+ }
|
|
133
|
+ else{
|
|
134
|
+ /* Long dead pulse nothing to do */
|
|
135
|
+ }
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+static ICUConfig icucfg = {
|
|
139
|
+ ICU_INPUT_ACTIVE_HIGH,
|
|
140
|
+ 10000, /* 10kHz ICU clock frequency. */
|
|
141
|
+ icuwidthcb,
|
|
142
|
+ NULL,
|
|
143
|
+ NULL,
|
|
144
|
+ ICU_CHANNEL_1,
|
|
145
|
+ 0
|
|
146
|
+};
|
|
147
|
+
|
62
|
148
|
/*
|
63
|
149
|
* Green LED blinker thread, times are in milliseconds.
|
64
|
150
|
*/
|
|
@@ -127,10 +213,23 @@ int main(void) {
|
127
|
213
|
halInit();
|
128
|
214
|
chSysInit();
|
129
|
215
|
|
|
216
|
+ chEvtObjectInit(&IR_receiver);
|
|
217
|
+ chEvtRegister(&IR_receiver, &el, 0);
|
|
218
|
+/*
|
|
219
|
+ * Initializes the ICU driver 3.
|
|
220
|
+ * GPIOC6 is the ICU input.
|
|
221
|
+ * The two pins have to be externally connected together.
|
|
222
|
+ */
|
|
223
|
+ sdStart(&SD2, NULL);
|
|
224
|
+ icuStart(&ICUD3, &icucfg);
|
|
225
|
+ palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
|
|
226
|
+ icuStartCapture(&ICUD3);
|
|
227
|
+ icuEnableNotifications(&ICUD3);
|
|
228
|
+
|
130
|
229
|
/*
|
131
|
230
|
* Creates the blinker thread.
|
132
|
231
|
*/
|
133
|
|
- chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
|
232
|
+ //chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
134
|
233
|
|
135
|
234
|
/* Configure PWM */
|
136
|
235
|
pwmStart(&PWMD1, &pwmcfg);
|
|
@@ -141,8 +240,17 @@ int main(void) {
|
141
|
240
|
|
142
|
241
|
/* Send a continuous stream of power off signals to the TV */
|
143
|
242
|
while (true) {
|
144
|
|
- rc5_frame(false, 0x00, 0x0C);
|
145
|
|
-
|
146
|
|
- chThdSleepMilliseconds(90);
|
|
243
|
+ if (command == 0x807F18E7) {
|
|
244
|
+ if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
|
|
245
|
+ continue;
|
|
246
|
+ }
|
|
247
|
+ palClearPad(GPIOC, GPIOC_LED);
|
|
248
|
+ for (int i = 0; i < 3; i++) {
|
|
249
|
+ rc5_frame(false, 0x00, 0x0C);
|
|
250
|
+ chThdSleepMilliseconds(90);
|
|
251
|
+ }
|
|
252
|
+ palSetPad(GPIOC, GPIOC_LED);
|
|
253
|
+ }
|
|
254
|
+ chEvtWaitAny(ALL_EVENTS);
|
147
|
255
|
}
|
148
|
256
|
}
|