Skip to content

Commit 257a151

Browse files
committed
Fix calculation of pause length in LEGO PF protocol
Fix #384 Integer overflow in LEGO Power Functions affects pause between messages Is rebased version of PR Arduino-IRremote#385
1 parent 048efb2 commit 257a151

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

examples/LegoPowerFunctionsTests/LegoPowerFunctionsTests.ino

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* LegoPowerFunctionsTest: LEGO Power Functions Tests
3-
* Copyright (c) 2016 Philipp Henkel
3+
* Copyright (c) 2016, 2017 Philipp Henkel
44
*/
55

66
#include <ir_Lego_PF_BitStreamEncoder.h>
@@ -78,14 +78,14 @@ void testMessageBitCount(LegoPfBitStreamEncoder& bitStreamEncoder) {
7878
logTestResult(bitCount == 18);
7979
}
8080

81-
boolean check(LegoPfBitStreamEncoder& bitStreamEncoder, int markDuration, int pauseDuration) {
81+
boolean check(LegoPfBitStreamEncoder& bitStreamEncoder, unsigned long markDuration, unsigned long pauseDuration) {
8282
bool result = true;
8383
result = result && bitStreamEncoder.getMarkDuration() == markDuration;
8484
result = result && bitStreamEncoder.getPauseDuration() == pauseDuration;
8585
return result;
8686
}
8787

88-
boolean checkNext(LegoPfBitStreamEncoder& bitStreamEncoder, int markDuration, int pauseDuration) {
88+
boolean checkNext(LegoPfBitStreamEncoder& bitStreamEncoder, unsigned long markDuration, unsigned long pauseDuration) {
8989
bool result = bitStreamEncoder.next();
9090
result = result && check(bitStreamEncoder, markDuration, pauseDuration);
9191
return result;
@@ -129,16 +129,16 @@ void testMessage407Repeated(LegoPfBitStreamEncoder& bitStreamEncoder) {
129129
bool result = true;
130130
result = result && check(bitStreamEncoder, 158, 1026);
131131
result = result && checkDataBitsOfMessage407(bitStreamEncoder);
132-
result = result && checkNext(bitStreamEncoder, 158, 1026 + 5 * 16000 - 10844);
132+
result = result && checkNext(bitStreamEncoder, 158, 1026L + 5L * 16000L - 10844L);
133133
result = result && checkNext(bitStreamEncoder, 158, 1026);
134134
result = result && checkDataBitsOfMessage407(bitStreamEncoder);
135-
result = result && checkNext(bitStreamEncoder, 158, 1026 + 5 * 16000 - 10844);
135+
result = result && checkNext(bitStreamEncoder, 158, 1026L + 5L * 16000L - 10844L);
136136
result = result && checkNext(bitStreamEncoder, 158, 1026);
137137
result = result && checkDataBitsOfMessage407(bitStreamEncoder);
138-
result = result && checkNext(bitStreamEncoder, 158, 1026 + 8 * 16000 - 10844);
138+
result = result && checkNext(bitStreamEncoder, 158, 1026L + 8L * 16000L - 10844L);
139139
result = result && checkNext(bitStreamEncoder, 158, 1026);
140140
result = result && checkDataBitsOfMessage407(bitStreamEncoder);
141-
result = result && checkNext(bitStreamEncoder, 158, 1026 + 8 * 16000 - 10844);
141+
result = result && checkNext(bitStreamEncoder, 158, 1026L + 8L * 16000L - 10844L);
142142
result = result && checkNext(bitStreamEncoder, 158, 1026);
143143
result = result && checkDataBitsOfMessage407(bitStreamEncoder);
144144
result = result && checkNext(bitStreamEncoder, 158, 1026);
@@ -191,7 +191,3 @@ void testGetMessageLengthAllLow(LegoPfBitStreamEncoder& bitStreamEncoder) {
191191
bitStreamEncoder.reset(0x0, false);
192192
logTestResult(bitStreamEncoder.getMessageLength() == 9104);
193193
}
194-
195-
196-
197-

ir_Lego_PF_BitStreamEncoder.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// L E E O O
55
// L EEEE E EEE O O
66
// L E E E O O LEGO Power Functions
7-
// LLLLLL EEEEEE EEEE OOOO Copyright (c) 2016 Philipp Henkel
7+
// LLLLLL EEEEEE EEEE OOOO Copyright (c) 2016, 2017 Philipp Henkel
88
//==============================================================================
99

1010
//+=============================================================================
@@ -14,25 +14,25 @@ class LegoPfBitStreamEncoder {
1414
private:
1515
uint16_t data;
1616
bool repeatMessage;
17-
int messageBitIdx;
18-
int repeatCount;
19-
int messageLength;
17+
uint8_t messageBitIdx;
18+
uint8_t repeatCount;
19+
uint16_t messageLength;
2020

21+
public:
2122
// HIGH data bit = IR mark + high pause
2223
// LOW data bit = IR mark + low pause
23-
static const int LOW_BIT_DURATION = 421;
24-
static const int HIGH_BIT_DURATION = 711;
25-
static const int START_BIT_DURATION = 1184;
26-
static const int STOP_BIT_DURATION = 1184;
27-
static const int IR_MARK_DURATION = 158;
28-
static const int HIGH_PAUSE_DURATION = HIGH_BIT_DURATION - IR_MARK_DURATION;
29-
static const int LOW_PAUSE_DURATION = LOW_BIT_DURATION - IR_MARK_DURATION;
30-
static const int START_PAUSE_DURATION = START_BIT_DURATION - IR_MARK_DURATION;
31-
static const int STOP_PAUSE_DURATION = STOP_BIT_DURATION - IR_MARK_DURATION;
32-
static const int MESSAGE_BITS = 18;
33-
static const int MAX_MESSAGE_LENGTH = 16000;
24+
static const uint16_t LOW_BIT_DURATION = 421;
25+
static const uint16_t HIGH_BIT_DURATION = 711;
26+
static const uint16_t START_BIT_DURATION = 1184;
27+
static const uint16_t STOP_BIT_DURATION = 1184;
28+
static const uint8_t IR_MARK_DURATION = 158;
29+
static const uint16_t HIGH_PAUSE_DURATION = HIGH_BIT_DURATION - IR_MARK_DURATION;
30+
static const uint16_t LOW_PAUSE_DURATION = LOW_BIT_DURATION - IR_MARK_DURATION;
31+
static const uint16_t START_PAUSE_DURATION = START_BIT_DURATION - IR_MARK_DURATION;
32+
static const uint16_t STOP_PAUSE_DURATION = STOP_BIT_DURATION - IR_MARK_DURATION;
33+
static const uint8_t MESSAGE_BITS = 18;
34+
static const uint16_t MAX_MESSAGE_LENGTH = 16000;
3435

35-
public:
3636
void reset(uint16_t data, bool repeatMessage) {
3737
this->data = data;
3838
this->repeatMessage = repeatMessage;
@@ -43,9 +43,9 @@ class LegoPfBitStreamEncoder {
4343

4444
int getChannelId() const { return 1 + ((data >> 12) & 0x3); }
4545

46-
int getMessageLength() const {
46+
uint16_t getMessageLength() const {
4747
// Sum up all marks
48-
int length = MESSAGE_BITS * IR_MARK_DURATION;
48+
uint16_t length = MESSAGE_BITS * IR_MARK_DURATION;
4949

5050
// Sum up all pauses
5151
length += START_PAUSE_DURATION;
@@ -75,9 +75,9 @@ class LegoPfBitStreamEncoder {
7575
}
7676
}
7777

78-
int getMarkDuration() const { return IR_MARK_DURATION; }
78+
uint8_t getMarkDuration() const { return IR_MARK_DURATION; }
7979

80-
int getPauseDuration() const {
80+
uint32_t getPauseDuration() const {
8181
if (messageBitIdx == 0)
8282
return START_PAUSE_DURATION;
8383
else if (messageBitIdx < MESSAGE_BITS - 1) {
@@ -88,26 +88,26 @@ class LegoPfBitStreamEncoder {
8888
}
8989

9090
private:
91-
int getDataBitPause() const {
91+
uint16_t getDataBitPause() const {
9292
const int pos = MESSAGE_BITS - 2 - messageBitIdx;
9393
const bool isHigh = data & (1 << pos);
9494
return isHigh ? HIGH_PAUSE_DURATION : LOW_PAUSE_DURATION;
9595
}
9696

97-
int getStopPause() const {
97+
uint32_t getStopPause() const {
9898
if (repeatMessage) {
9999
return getRepeatStopPause();
100100
} else {
101101
return STOP_PAUSE_DURATION;
102102
}
103103
}
104104

105-
int getRepeatStopPause() const {
105+
uint32_t getRepeatStopPause() const {
106106
if (repeatCount == 0 || repeatCount == 1) {
107-
return STOP_PAUSE_DURATION + 5 * MAX_MESSAGE_LENGTH - messageLength;
107+
return STOP_PAUSE_DURATION + (uint32_t)5 * MAX_MESSAGE_LENGTH - messageLength;
108108
} else if (repeatCount == 2 || repeatCount == 3) {
109109
return STOP_PAUSE_DURATION
110-
+ (6 + 2 * getChannelId()) * MAX_MESSAGE_LENGTH - messageLength;
110+
+ (uint32_t)(6 + 2 * getChannelId()) * MAX_MESSAGE_LENGTH - messageLength;
111111
} else {
112112
return STOP_PAUSE_DURATION;
113113
}

0 commit comments

Comments
 (0)