Skip to content

Commit

Permalink
Merge pull request #100 from simonhyde/remove_duplication
Browse files Browse the repository at this point in the history
Remove code duplication
  • Loading branch information
cpainchaud authored Nov 28, 2023
2 parents b4c982d + 8b13676 commit e90e5a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
33 changes: 8 additions & 25 deletions RFLink/Plugins/Plugin_029.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@

#ifdef PLUGIN_029
#include "../4_Display.h"
#include "../7_Utils.h"

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

boolean Plugin_029(byte function, const char *string)
{
Expand Down Expand Up @@ -180,28 +187,4 @@ boolean Plugin_029(byte function, const char *string)
return true;
}

/*********************************************************************************************\
* Calculates CRC-8 checksum
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
\*********************************************************************************************/
uint8_t Plugin_029_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
// Indicated changes are from reference CRC-8 function in OneWire library
while (len--)
{
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--)
{
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
crc <<= 1; // changed from right shift
if (mix)
crc ^= 0x31; // changed from 0x8C;
inbyte <<= 1; // changed from right shift
}
}
return crc;
}
#endif // PLUGIN_029
35 changes: 10 additions & 25 deletions RFLink/Plugins/Plugin_031.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@

#ifdef PLUGIN_031
#include "../4_Display.h"
#include "../7_Utils.h"

/*********************************************************************************************\
* Calculates CRC-8 checksum
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
\*********************************************************************************************/
#define Plugin_031_ProtocolAlectoCRC8(addr, len) crc8((addr), (len), 0x31, 0)

uint8_t Plugin_031_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len);
// unsigned int Plugin_031_ProtocolAlectoRainBase = 0;

boolean Plugin_031(byte function, const char *string)
Expand Down Expand Up @@ -188,28 +196,5 @@ boolean Plugin_031(byte function, const char *string)
return true;
}

/*********************************************************************************************\
* Calculates CRC-8 checksum
* reference http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
* http://lucsmall.com/2012/04/30/weather-station-hacking-part-3/
* https://github.com/lucsmall/WH2-Weather-Sensor-Library-for-Arduino/blob/master/WeatherSensorWH2.cpp
\*********************************************************************************************/
uint8_t Plugin_031_ProtocolAlectoCRC8(uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
// Indicated changes are from reference CRC-8 function in OneWire library
while (len--)
{
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--)
{
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
crc <<= 1; // changed from right shift
if (mix)
crc ^= 0x31; // changed from 0x8C;
inbyte <<= 1; // changed from right shift
}
}
return crc;
}

#endif // PLUGIN_031

0 comments on commit e90e5a8

Please sign in to comment.