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 6 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,13 +61,16 @@
61 61
 /*
62 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 76
 static event_listener_t el;
@@ -112,16 +115,16 @@ static void icuwidthcb(ICUDriver *icup)
112 115
     static uint32_t tmp;
113 116
 
114 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 119
         index = 0;
117 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 122
         tmp |= 1 << (31 - index);
120 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 125
         tmp &= ~(1 << (31 - index));
123 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 128
         if ((START_OCCURED) && (index == 32)) {
126 129
             nec_addr = (tmp >> 24) & 0xFF;
127 130
             nec_data = (tmp >> 8) & 0xFF;
@@ -132,12 +135,10 @@ static void icuwidthcb(ICUDriver *icup)
132 135
         nec_repeat = false;
133 136
         START_OCCURED = false;
134 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 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 141
         chEvtBroadcastFlags(&IR_receiver, 0);
139
-    } else {
140
-        /* Long dead pulse nothing to do */
141 142
     }
142 143
 }
143 144
 

Loading…
Cancel
Save