Browse Source

Remove noise from the NEC reading function

The comparisons were long-winded and repetitive, so I made a macro to
make them more concise.  Also, all the NEC timing macros now have names
starting with NEC_.
Clara Hobbs 7 years ago
parent
commit
972a97e1df
1 changed files with 16 additions and 15 deletions
  1. 16
    15
      src/main.c

+ 16
- 15
src/main.c View File

61
 /*
61
 /*
62
  * NEC timing macros
62
  * NEC timing macros
63
  */
63
  */
64
-#define DELTA          5
65
-#define START_PULSE    45
66
-#define END_PULSE      405
67
-#define RPT_CMD_PULSE  959
68
-#define ZERO_PULSE     6
69
-#define ONE_PULSE      17
70
-#define COMMA_PULSE    23
64
+#define NEC_START_PULSE 45
65
+#define NEC_END_PULSE 405
66
+#define NEC_RPT_CMD_PULSE 959
67
+#define NEC_ZERO_PULSE 6
68
+#define NEC_ONE_PULSE 17
69
+#define NEC_COMMA_PULSE 23
70
+
71
+/* Tolerance */
72
+#define NEC_DELTA 5
73
+#define CLOSE_TO(var, target, error) (((var) > ((target) - (error))) && ((var) < ((target) + (error))))
71
 
74
 
72
 
75
 
73
 static event_listener_t el;
76
 static event_listener_t el;
112
     static uint32_t tmp;
115
     static uint32_t tmp;
113
 
116
 
114
     icucnt_t cnt = icuGetWidthX(icup);
117
     icucnt_t cnt = icuGetWidthX(icup);
115
-    if ((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))) {
118
+    if (CLOSE_TO(cnt, NEC_START_PULSE, NEC_DELTA)) {
116
         index = 0;
119
         index = 0;
117
         START_OCCURED = true;
120
         START_OCCURED = true;
118
-    } else if ((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))) {
121
+    } else if (CLOSE_TO(cnt, NEC_ONE_PULSE, NEC_DELTA)) {
119
         tmp |= 1 << (31 - index);
122
         tmp |= 1 << (31 - index);
120
         index++;
123
         index++;
121
-    } else if ((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))) {
124
+    } else if (CLOSE_TO(cnt, NEC_ZERO_PULSE, NEC_DELTA)) {
122
         tmp &= ~(1 << (31 - index));
125
         tmp &= ~(1 << (31 - index));
123
         index++;
126
         index++;
124
-    } else if ((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))) {
127
+    } else if (CLOSE_TO(cnt, NEC_END_PULSE, 4*NEC_DELTA)) {
125
         if ((START_OCCURED) && (index == 32)) {
128
         if ((START_OCCURED) && (index == 32)) {
126
             nec_addr = (tmp >> 24) & 0xFF;
129
             nec_addr = (tmp >> 24) & 0xFF;
127
             nec_data = (tmp >> 8) & 0xFF;
130
             nec_data = (tmp >> 8) & 0xFF;
132
         nec_repeat = false;
135
         nec_repeat = false;
133
         START_OCCURED = false;
136
         START_OCCURED = false;
134
         index = -1;
137
         index = -1;
135
-    } else if ((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))) {
138
+    } else if (CLOSE_TO(cnt, NEC_RPT_CMD_PULSE, NEC_DELTA)) {
136
         nec_repeat = true;
139
         nec_repeat = true;
137
-    } else if ((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))) {
140
+    } else if (CLOSE_TO(cnt, NEC_COMMA_PULSE, NEC_DELTA)) {
138
         chEvtBroadcastFlags(&IR_receiver, 0);
141
         chEvtBroadcastFlags(&IR_receiver, 0);
139
-    } else {
140
-        /* Long dead pulse nothing to do */
141
     }
142
     }
142
 }
143
 }
143
 
144
 

Loading…
Cancel
Save