Skip to content

Commit 513e104

Browse files
authored
Merge pull request Arduino-IRremote#427 from philipphenkel/lego_pf_integer_overflow_2
Fix calculation of pause length in LEGO PF protocol
2 parents 048efb2 + ccc9d01 commit 513e104

File tree

7 files changed

+40
-41
lines changed

7 files changed

+40
-41
lines changed

Contributors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ These are the active contributors of this project that you may contact if there
1616
- [Sebazzz](https://github.com/sebazz): Contributor
1717
- [lumbric](https://github.com/lumbric): Contributor
1818
- [ElectricRCAircraftGuy](https://github.com/electricrcaircraftguy): Active Contributor
19-
- [henkel](https://github.com/henkel): Contributor
19+
- [philipphenkel](https://github.com/philipphenkel): Active Contributor
2020
- [MCUdude](https://github.com/MCUdude): Contributor
2121

2222
Note: This list is being updated constantly so please let [z3t0](https://github.com/z3t0) know if you have been missed.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This library enables you to send and receive using infra-red signals on an Ardui
88

99
Tutorials and more information will be made available on [the official homepage](http://z3t0.github.io/Arduino-IRremote/).
1010

11-
## Version - 2.2.1
11+
## Version - 2.2.3
1212

1313
## Installation
1414
1. Navigate to the [Releases](https://github.com/z3t0/Arduino-IRremote/releases) page.
@@ -65,7 +65,7 @@ Check [here](Contributing.md) for some guidelines.
6565

6666
## Contact
6767
68-
Please only email me if it is more appropriate than creating an Issue / PR. I **will** not respond to requests for adding support for particular boards, unless of course you are the creator of the board and would like to cooperate on the project. I will also **ignore** any emails asking me to tell you how to implement your ideas. However, if you have a private inquiry that you would only apply to you and you would prefer it to be via email, by all means.
68+
Please only email me if it is more appropriate than creating an Issue / PR. I **will** not respond to requests for adding support for particular boards, unless of course you are the creator of the board and would like to cooperate on the project. I will also **ignore** any emails asking me to tell you how to implement your ideas. However, if you have a private inquiry that you would only apply to you and you would prefer it to be via email, by all means.
6969

7070
## Contributors
7171
Check [here](Contributors.md)

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.2.3 - 2017/03/27
2+
- Fix calculation of pause length in LEGO PF protocol [PR #427](https://github.com/z3t0/Arduino-IRremote/pull/427)
3+
14
## 2.2.2 - 2017/01/20
25
- Fixed naming bug [PR #398](https://github.com/z3t0/Arduino-IRremote/pull/398)
36

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
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "git",
88
"url": "https://github.com/z3t0/Arduino-IRremote.git"
99
},
10-
"version": "2.2.1",
10+
"version": "2.2.3",
1111
"frameworks": "arduino",
1212
"platforms": "atmelavr",
1313
"authors" :

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=IRremote
2-
version=2.2.1
2+
version=2.2.3
33
author=shirriff
44
maintainer=shirriff
55
sentence=Send and receive infrared signals with multiple protocols

0 commit comments

Comments
 (0)