0.1.0 Fletcher

This commit is contained in:
rob tillaart 2022-01-26 10:24:06 +01:00
parent c026ff58e7
commit 935ebba46e
13 changed files with 489 additions and 0 deletions

View File

@ -0,0 +1,14 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
libraries:
- "printHelpers"

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,17 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,76 @@
#pragma once
//
// FILE: Fletcher.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2022-01-25
// PURPOSE: Arduino Library for calculating Fletcher's checksum
//
// HISTORY
// 0.1.0 2022-01-25 initial version, straightforward implementation
//
#include "Arduino.h"
#define FLETCHER_LIB_VERSION (F("0.1.0"))
#define FLETCHER_16 255
#define FLETCHER_32 65535UL
#define FLETCHER_64 4294967295ULL
//
// straightforward implementation.
// max length buffer 65534.
// Wikipedia shows optimizations.
//
uint16_t fletcher16(uint8_t *data, uint16_t length)
{
uint16_t s1 = 0;
uint16_t s2 = 0;
for (uint16_t i = 0; i < length; i++)
{
s1 += data[i];
s1 %= FLETCHER_16;
s2 += s1;
s2 %= FLETCHER_16;
}
return (s2 << 8) | s1;
}
uint32_t fletcher32(uint16_t *data, uint16_t length)
{
uint32_t s1 = 0;
uint32_t s2 = 0;
for (uint16_t i = 0; i < length; i++)
{
s1 += data[i];
s1 %= FLETCHER_32;
s2 += s1;
s2 %= FLETCHER_32;
}
return (s2 << 16) | s1;
}
uint64_t fletcher64(uint32_t *data, uint16_t length)
{
uint64_t s1 = 0;
uint64_t s2 = 0;
for (uint16_t i = 0; i < length; i++)
{
s1 += data[i];
s1 %= FLETCHER_64;
s2 += s1;
s2 %= FLETCHER_64;
}
return (s2 << 32) | s1;
}
// -- END OF FILE --

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,58 @@
[![Arduino CI](https://github.com/RobTillaart/FLETCHER/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/FLETCHER/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/FLETCHER/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/FLETCHER/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/FLETCHER/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/FLETCHER/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/FLETCHER.svg?maxAge=3600)](https://github.com/RobTillaart/FLETCHER/releases)
# FLETCHER
Arduino Library for Fletcher's checksum
## Description
This library provides a Fletcher checksum of a data array.
Fletcher's checksum is in general faster than CRC but possibly not as
good in detecting errors.
See https://en.wikipedia.org/wiki/Fletcher%27s_checksum for details.
Relates to https://github.com/RobTillaart/CRC
Use https://github.com/RobTillaart/printHelpers to print the Fletcher64().
Tested on Arduino UNO only.
## Interface
The functions are straightforward, however one should define **length**
in terms of the data type and optional pad it with zeros.
A string "abcdef" has length 2 for **fletcher64()**.
- **uint16_t fletcher16(uint8_t \*data, uint16_t length)** idem.
- **uint32_t fletcher32(uint16_t \*data, uint16_t length)** idem.
- **uint64_t fletcher64(uint32_t \*data, uint16_t length)** idem.
TODO - class version.
## Operation
See examples.
## Future ideas
- improve documentation
- test other platforms
- Class versions
- incremental calculate e.g. for a stream.
- similar to CRC classes
- look for optimizations
- Fletcher24 ? and others?
- generic FletcherN(). for N = 1..32

View File

@ -0,0 +1,79 @@
//
// FILE: Fletcher_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
#include "Arduino.h"
#include "Fletcher.h"
char str[] = "Lorem ipsum dolor sit amet, \
consectetuer adipiscing elit. Aenean commodo ligula eget dolor. \
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient \
montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, \
pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. \
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. \
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. \
Nullam dictum felis eu pede mollis pretium. Integer tincidunt. \
Cras dapibus. Vivamus elementum semper nisi. \
Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, \
consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, \
viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus \
varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies \
nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.";
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.print("FLETCHER_LIB_VERSION: ");
Serial.println(FLETCHER_LIB_VERSION);
for (int i = 0; i < 60; i++)
{
Serial.print(str[i]);
}
Serial.println("...");
uint16_t len = strlen(str);
Serial.print("LENGTH STR: ");
Serial.println(len);
delay(100);
start = micros();
volatile uint16_t x = fletcher16((uint8_t *) str, len);
stop = micros();
Serial.print("FLETCHER16: ");
Serial.println(stop - start);
delay(100);
start = micros();
volatile uint32_t y = fletcher32((uint16_t *) str, (len + 1) / 2);
stop = micros();
Serial.print("FLETCHER32: ");
Serial.println(stop - start);
delay(100);
start = micros();
volatile uint64_t z = fletcher64((uint32_t *) str, (len + 3) / 4);
stop = micros();
Serial.print("FLETCHER64: ");
Serial.println(stop - start);
delay(100);
// keep compiler happy.
Serial.println((uint32_t) z * y * x, HEX);
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,57 @@
//
// FILE: Fletcher_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
#include "Arduino.h"
#include "Fletcher.h"
#include "printHelpers.h" // https://github.com/RobTillaart/printHelpers
// expected output
//
// FLETCHER_LIB_VERSION: 0.1.0
// 51440
// 8279
// 1575
// 4031760169
// 1448095018
// 3957429649
// 14467467625952928454
// 14467579776138987718
// 3543817411021686982
char str1[24] = "abcde";
char str2[24] = "abcdef";
char str3[24] = "abcdefgh";
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.print("FLETCHER_LIB_VERSION: ");
Serial.println(FLETCHER_LIB_VERSION);
Serial.println(fletcher16((uint8_t *) str1, 5));
Serial.println(fletcher16((uint8_t *) str2, 6));
Serial.println(fletcher16((uint8_t *) str3, 8));
Serial.println(fletcher32((uint16_t *) str1, 3));
Serial.println(fletcher32((uint16_t *) str2, 3));
Serial.println(fletcher32((uint16_t *) str3, 4));
Serial.println(print64(fletcher64((uint32_t *) str1, 2)));
Serial.println(print64(fletcher64((uint32_t *) str2, 2)));
Serial.println(print64(fletcher64((uint32_t *) str3, 2)));
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,14 @@
# Syntax Colouring Map For FLETCHER
# Data types (KEYWORD1)
Fletcher KEYWORD1
# Methods and Functions (KEYWORD2)
fletcher16 KEYWORD2
fletcher32 KEYWORD2
fletcher64 KEYWORD2
# Constants (LITERAL1)
FLETCHER_LIB_VERSION LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "Fletcher",
"keywords": "fletcher, checksum",
"description": "Arduino Library for calculating Fletcher's checksum.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/Fletcher.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": "Fletcher.h"
}

View File

@ -0,0 +1,11 @@
name=Fletcher
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence="Arduino Library for calculating Fletcher's checksum.
paragraph=
category=Signal Input/Output
url=https://github.com/RobTillaart/Fletcher
architectures=*
includes=Fletcher.h
depends=

View File

@ -0,0 +1,88 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2022-01-25
// PURPOSE: unit tests for the GAMMA library
// https://github.com/RobTillaart/GAMMA
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
// ----------------------------
// assertEqual(expected, actual)
// assertNotEqual(expected, actual)
// assertLess(expected, actual)
// assertMore(expected, actual)
// assertLessOrEqual(expected, actual)
// assertMoreOrEqual(expected, actual)
// assertTrue(actual)
// assertFalse(actual)
// assertNull(actual)
// assertNotNull(actual)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "Fletcher.h"
unittest_setup()
{
fprintf(stderr, "FLETCHER_LIB_VERSION: %s\n", (char *) FLETCHER_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constants)
{
assertEqual(FLETCHER_16, 255);
assertEqual(FLETCHER_32, 65535UL);
assertEqual(FLETCHER_64, 4294967295ULL);
}
unittest(test_fletcher16)
{
char str1[24] = "abcde";
char str2[24] = "abcdef";
char str3[24] = "abcdefgh";
assertEqual(51440UL, fletcher16((uint8_t *) str1, 5));
assertEqual( 8279UL, fletcher16((uint8_t *) str2, 6));
assertEqual( 1575UL, fletcher16((uint8_t *) str3, 8));
}
unittest(test_fletcher32)
{
char str1[24] = "abcde";
char str2[24] = "abcdef";
char str3[24] = "abcdefgh";
assertEqual(4031760169UL, fletcher32((uint16_t *) str1, 3));
assertEqual(1448095018UL, fletcher32((uint16_t *) str2, 3));
assertEqual(3957429649UL, fletcher32((uint16_t *) str3, 4));
}
unittest(test_fletcher64)
{
char str1[24] = "abcde";
char str2[24] = "abcdef";
char str3[24] = "abcdefgh";
assertEqual(14467467625952928454ULL, fletcher64((uint32_t *) str1, 2));
assertEqual(14467579776138987718ULL, fletcher64((uint32_t *) str2, 2));
assertEqual( 3543817411021686982ULL, fletcher64((uint32_t *) str3, 2));
}
unittest_main()
// --------