Browse Source

More formatting, improve NEC organization

Now the NEC reading code doesn't leak excessive information to the rest
of the firmware, keeping its internal state in static variables at
function scope.
Clara Hobbs 7 years ago
parent
commit
f37c086531
1 changed files with 30 additions and 17 deletions
  1. 30
    17
      src/main.c

+ 30
- 17
src/main.c View File

95
     0, 0
95
     0, 0
96
 };
96
 };
97
 
97
 
98
-/*===========================================================================*/
99
-/* ICU related code.                                                         */
100
-/*===========================================================================*/
101
-static int32_t index = -1;
102
-static bool START_OCCURED = FALSE, REPEAT_FLAG = FALSE;
103
-static uint32_t tmp, command;
104
-
105
-static void icuwidthcb(ICUDriver *icup) {
98
+
99
+/*
100
+ * Variables for holding the most recently read information from icuwidthcb
101
+ */
102
+static uint8_t nec_addr, nec_data;
103
+static bool nec_repeat = false;
104
+
105
+/*
106
+ * ICU pulse width callback for reading NEC remote control frames
107
+ */
108
+static void icuwidthcb(ICUDriver *icup)
109
+{
110
+    static int32_t index = -1;
111
+    static bool START_OCCURED = false;
112
+    static uint32_t tmp;
113
+
106
     icucnt_t cnt = icuGetWidthX(icup);
114
     icucnt_t cnt = icuGetWidthX(icup);
107
     if ((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))) {
115
     if ((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))) {
108
         index = 0;
116
         index = 0;
109
-        START_OCCURED = TRUE;
117
+        START_OCCURED = true;
110
     } else if ((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))) {
118
     } else if ((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))) {
111
         tmp |= 1 << (31 - index);
119
         tmp |= 1 << (31 - index);
112
         index++;
120
         index++;
115
         index++;
123
         index++;
116
     } else if ((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))) {
124
     } else if ((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))) {
117
         if ((START_OCCURED) && (index == 32)) {
125
         if ((START_OCCURED) && (index == 32)) {
118
-            command = tmp;
126
+            nec_addr = (tmp >> 24) & 0xFF;
127
+            nec_data = (tmp >> 8) & 0xFF;
119
         } else {
128
         } else {
120
-            command = 0;
129
+            nec_addr = 0;
130
+            nec_data = 0;
121
         }
131
         }
122
-        REPEAT_FLAG = FALSE;
123
-        START_OCCURED = FALSE;
132
+        nec_repeat = false;
133
+        START_OCCURED = false;
124
         index = -1;
134
         index = -1;
125
     } else if ((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))) {
135
     } else if ((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))) {
126
-        REPEAT_FLAG = TRUE;
136
+        nec_repeat = true;
127
     } else if ((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))) {
137
     } else if ((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))) {
128
         chEvtBroadcastFlags(&IR_receiver, 0);
138
         chEvtBroadcastFlags(&IR_receiver, 0);
129
     } else {
139
     } else {
131
     }
141
     }
132
 }
142
 }
133
 
143
 
144
+/* Configuration for the ICU */
134
 static ICUConfig icucfg = {
145
 static ICUConfig icucfg = {
135
     ICU_INPUT_ACTIVE_HIGH,
146
     ICU_INPUT_ACTIVE_HIGH,
136
     10000,            /* 10kHz ICU clock frequency. */
147
     10000,            /* 10kHz ICU clock frequency. */
141
     0
152
     0
142
 };
153
 };
143
 
154
 
155
+
144
 /*
156
 /*
145
  * Synchronously send one bit for RC5
157
  * Synchronously send one bit for RC5
146
  */
158
  */
179
     }
191
     }
180
 }
192
 }
181
 
193
 
194
+
182
 /*
195
 /*
183
  * Application entry point.
196
  * Application entry point.
184
  */
197
  */
185
-int main(void) {
186
-
198
+int main(void)
199
+{
187
     /*
200
     /*
188
      * System initializations.
201
      * System initializations.
189
      * - HAL initialization, this also initializes the configured device drivers
202
      * - HAL initialization, this also initializes the configured device drivers
216
 
229
 
217
     /* Send a continuous stream of power off signals to the TV */
230
     /* Send a continuous stream of power off signals to the TV */
218
     while (true) {
231
     while (true) {
219
-        if (command == 0x807F18E7) {
232
+        if (nec_addr == 0x80 && nec_data == 0x18) {
220
             if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
233
             if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
221
                 continue;
234
                 continue;
222
             }
235
             }

Loading…
Cancel
Save