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 6 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,18 +95,26 @@ static const GPTConfig gpt4cfg = {
95 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 114
     icucnt_t cnt = icuGetWidthX(icup);
107 115
     if ((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))) {
108 116
         index = 0;
109
-        START_OCCURED = TRUE;
117
+        START_OCCURED = true;
110 118
     } else if ((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))) {
111 119
         tmp |= 1 << (31 - index);
112 120
         index++;
@@ -115,15 +123,17 @@ static void icuwidthcb(ICUDriver *icup) {
115 123
         index++;
116 124
     } else if ((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))) {
117 125
         if ((START_OCCURED) && (index == 32)) {
118
-            command = tmp;
126
+            nec_addr = (tmp >> 24) & 0xFF;
127
+            nec_data = (tmp >> 8) & 0xFF;
119 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 134
         index = -1;
125 135
     } else if ((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))) {
126
-        REPEAT_FLAG = TRUE;
136
+        nec_repeat = true;
127 137
     } else if ((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))) {
128 138
         chEvtBroadcastFlags(&IR_receiver, 0);
129 139
     } else {
@@ -131,6 +141,7 @@ static void icuwidthcb(ICUDriver *icup) {
131 141
     }
132 142
 }
133 143
 
144
+/* Configuration for the ICU */
134 145
 static ICUConfig icucfg = {
135 146
     ICU_INPUT_ACTIVE_HIGH,
136 147
     10000,            /* 10kHz ICU clock frequency. */
@@ -141,6 +152,7 @@ static ICUConfig icucfg = {
141 152
     0
142 153
 };
143 154
 
155
+
144 156
 /*
145 157
  * Synchronously send one bit for RC5
146 158
  */
@@ -179,11 +191,12 @@ void rc5_frame(bool toggle, int addr, int data)
179 191
     }
180 192
 }
181 193
 
194
+
182 195
 /*
183 196
  * Application entry point.
184 197
  */
185
-int main(void) {
186
-
198
+int main(void)
199
+{
187 200
     /*
188 201
      * System initializations.
189 202
      * - HAL initialization, this also initializes the configured device drivers
@@ -216,7 +229,7 @@ int main(void) {
216 229
 
217 230
     /* Send a continuous stream of power off signals to the TV */
218 231
     while (true) {
219
-        if (command == 0x807F18E7) {
232
+        if (nec_addr == 0x80 && nec_data == 0x18) {
220 233
             if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
221 234
                 continue;
222 235
             }

Loading…
Cancel
Save