Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from osbock/master

Prevent buffer overflows and improve comparison
  • Loading branch information...
commit dd0d52e5326ad581cd8558879b76185b24fa1f67 2 parents c0a23a7 + 28ab475
Paint Your Dragon PaintYourDragon authored

Showing 1 changed file with 37 additions and 19 deletions. Show diff stats Hide diff stats

  1. +37 19 ircommander.pde
56 ircommander.pde
@@ -19,6 +19,7 @@
19 19
20 20 // the maximum pulse we'll listen for - 65 milliseconds is a long time
21 21 #define MAXPULSE 65000
  22 +#define NUMPULSES 50
22 23
23 24 // what our timing resolution should be, larger is better
24 25 // as its more 'precise' - but too large and you wont get
@@ -29,7 +30,7 @@
29 30 #define FUZZINESS 20
30 31
31 32 // we will store up to 100 pulse pairs (this is -a lot-)
32   -uint16_t pulses[100][2]; // pair is high and low pulse
  33 +uint16_t pulses[NUMPULSES][2]; // pair is high and low pulse
33 34 uint8_t currentpulse = 0; // index for pulses we're storing
34 35
35 36 #include "ircodes.h"
@@ -47,54 +48,69 @@ void loop(void) {
47 48 Serial.print("Heard ");
48 49 Serial.print(numberpulses);
49 50 Serial.println("-pulse long IR signal");
50   - if (IRcompare(numberpulses, ApplePlaySignal)) {
  51 + if (IRcompare(numberpulses, ApplePlaySignal,sizeof(ApplePlaySignal)/4)) {
51 52 Serial.println("PLAY");
52 53 }
53   - if (IRcompare(numberpulses, AppleRewindSignal)) {
  54 + if (IRcompare(numberpulses, AppleRewindSignal,sizeof(AppleRewindSignal)/4)) {
54 55 Serial.println("REWIND");
55 56 }
56   - if (IRcompare(numberpulses, AppleForwardSignal)) {
  57 + if (IRcompare(numberpulses, AppleForwardSignal,sizeof(AppleForwardSignal)/4)) {
57 58 Serial.println("FORWARD");
58 59 }
  60 + delay(500);
59 61 }
60 62
61   -boolean IRcompare(int numpulses, int Signal[]) {
62   -
63   - for (int i=0; i< numpulses-1; i++) {
  63 +//KGO: added size of compare sample. Only compare the minimum of the two
  64 +boolean IRcompare(int numpulses, int Signal[], int refsize) {
  65 + int count = min(numpulses,refsize);
  66 + Serial.print("count set to: ");
  67 + Serial.println(count);
  68 + for (int i=0; i< count-1; i++) {
64 69 int oncode = pulses[i][1] * RESOLUTION / 10;
65 70 int offcode = pulses[i+1][0] * RESOLUTION / 10;
66 71
67   - /*
  72 +#ifdef DEBUG
68 73 Serial.print(oncode); // the ON signal we heard
69 74 Serial.print(" - ");
70 75 Serial.print(Signal[i*2 + 0]); // the ON signal we want
71   - */
  76 +#endif
72 77
73 78 // check to make sure the error is less than FUZZINESS percent
74 79 if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
75   - //Serial.print(" (ok)");
  80 +#ifdef DEBUG
  81 + Serial.print(" (ok)");
  82 +#endif
76 83 } else {
77   - //Serial.print(" (x)");
  84 +#ifdef DEBUG
  85 + Serial.print(" (x)");
  86 +#endif
78 87 // we didn't match perfectly, return a false match
79 88 return false;
80 89 }
81 90
82   - /*
  91 +
  92 +#ifdef DEBUG
83 93 Serial.print(" \t"); // tab
84 94 Serial.print(offcode); // the OFF signal we heard
85 95 Serial.print(" - ");
86 96 Serial.print(Signal[i*2 + 1]); // the OFF signal we want
87   - */
  97 +#endif
88 98
89 99 if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
90   - //Serial.print(" (ok)");
  100 +#ifdef DEBUG
  101 + Serial.print(" (ok)");
  102 +#endif
91 103 } else {
92   - //Serial.print(" (x)");
  104 +#ifdef DEBUG
  105 + Serial.print(" (x)");
  106 +#endif
93 107 // we didn't match perfectly, return a false match
94 108 return false;
95 109 }
96 110
97   - //Serial.println();
  111 +#ifdef DEBUG
  112 + Serial.println();
  113 +#endif
98 114 }
99 115 // Everything matched!
100 116 return true;
@@ -118,7 +134,9 @@ int listenForIR(void) {
118 134 // If the pulse is too long, we 'timed out' - either nothing
119 135 // was received or the code is finished, so print what
120 136 // we've grabbed so far, and then reset
121   - if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
  137 +
  138 + // KGO: Added check for end of receive buffer
  139 + if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
122 140 return currentpulse;
123 141 }
124 142 }
@@ -130,7 +148,8 @@ int listenForIR(void) {
130 148 // pin is still LOW
131 149 lowpulse++;
132 150 delayMicroseconds(RESOLUTION);
133   - if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
  151 + // KGO: Added check for end of receive buffer
  152 + if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
134 153 return currentpulse;
135 154 }
136 155 }
@@ -140,7 +159,6 @@ int listenForIR(void) {
140 159 currentpulse++;
141 160 }
142 161 }
143   -
144 162 void printpulses(void) {
145 163 Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
146 164 for (uint8_t i = 0; i < currentpulse; i++) {

0 comments on commit dd0d52e

Please sign in to comment.
Something went wrong with that request. Please try again.