From 8b136761bd3bad9b663d71f4719af8f6a6f9bd4e Mon Sep 17 00:00:00 2001 From: Simon Hyde Date: Mon, 27 Nov 2023 19:20:54 +0000 Subject: [PATCH] Remove code duplication These 2 plugins both duplicated the crc8 routine that is already implemented in 7_Utils --- RFLink/Plugins/Plugin_029.c | 33 ++++++++------------------------- RFLink/Plugins/Plugin_031.c | 35 ++++++++++------------------------- 2 files changed, 18 insertions(+), 50 deletions(-) diff --git a/RFLink/Plugins/Plugin_029.c b/RFLink/Plugins/Plugin_029.c index 80e52bdc..c2644cbf 100644 --- a/RFLink/Plugins/Plugin_029.c +++ b/RFLink/Plugins/Plugin_029.c @@ -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) { @@ -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 diff --git a/RFLink/Plugins/Plugin_031.c b/RFLink/Plugins/Plugin_031.c index 60ce9246..421cc78c 100644 --- a/RFLink/Plugins/Plugin_031.c +++ b/RFLink/Plugins/Plugin_031.c @@ -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) @@ -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