Skip to content

Commit e90e5a8

Browse files
authored
Merge pull request #100 from simonhyde/remove_duplication
Remove code duplication
2 parents b4c982d + 8b13676 commit e90e5a8

File tree

2 files changed

+18
-50
lines changed

2 files changed

+18
-50
lines changed

RFLink/Plugins/Plugin_029.c

+8-25
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@
6161

6262
#ifdef PLUGIN_029
6363
#include "../4_Display.h"
64+
#include "../7_Utils.h"
6465

65-
uint8_t Plugin_029_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len);
66+
/*********************************************************************************************\
67+
* Calculates CRC-8 checksum
68+
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
69+
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
70+
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
71+
\*********************************************************************************************/
72+
#define Plugin_029_ProtocolAlectoCRC8(addr, len) crc8((addr), (len), 0x31, 0)
6673

6774
boolean Plugin_029(byte function, const char *string)
6875
{
@@ -180,28 +187,4 @@ boolean Plugin_029(byte function, const char *string)
180187
return true;
181188
}
182189

183-
/*********************************************************************************************\
184-
* Calculates CRC-8 checksum
185-
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
186-
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
187-
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
188-
\*********************************************************************************************/
189-
uint8_t Plugin_029_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len)
190-
{
191-
uint8_t crc = 0;
192-
// Indicated changes are from reference CRC-8 function in OneWire library
193-
while (len--)
194-
{
195-
uint8_t inbyte = *addr++;
196-
for (uint8_t i = 8; i; i--)
197-
{
198-
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
199-
crc <<= 1; // changed from right shift
200-
if (mix)
201-
crc ^= 0x31; // changed from 0x8C;
202-
inbyte <<= 1; // changed from right shift
203-
}
204-
}
205-
return crc;
206-
}
207190
#endif // PLUGIN_029

RFLink/Plugins/Plugin_031.c

+10-25
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@
7272

7373
#ifdef PLUGIN_031
7474
#include "../4_Display.h"
75+
#include "../7_Utils.h"
76+
77+
/*********************************************************************************************\
78+
* Calculates CRC-8 checksum
79+
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
80+
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
81+
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
82+
\*********************************************************************************************/
83+
#define Plugin_031_ProtocolAlectoCRC8(addr, len) crc8((addr), (len), 0x31, 0)
7584

76-
uint8_t Plugin_031_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len);
7785
// unsigned int Plugin_031_ProtocolAlectoRainBase = 0;
7886

7987
boolean Plugin_031(byte function, const char *string)
@@ -188,28 +196,5 @@ boolean Plugin_031(byte function, const char *string)
188196
return true;
189197
}
190198

191-
/*********************************************************************************************\
192-
* Calculates CRC-8 checksum
193-
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
194-
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
195-
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
196-
\*********************************************************************************************/
197-
uint8_t Plugin_031_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len)
198-
{
199-
uint8_t crc = 0;
200-
// Indicated changes are from reference CRC-8 function in OneWire library
201-
while (len--)
202-
{
203-
uint8_t inbyte = *addr++;
204-
for (uint8_t i = 8; i; i--)
205-
{
206-
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
207-
crc <<= 1; // changed from right shift
208-
if (mix)
209-
crc ^= 0x31; // changed from 0x8C;
210-
inbyte <<= 1; // changed from right shift
211-
}
212-
}
213-
return crc;
214-
}
199+
215200
#endif // PLUGIN_031

0 commit comments

Comments
 (0)