From 9984962b98cae0464983e0b0eb896e0dfb0b8429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 21 Jan 2016 13:17:13 +0100 Subject: [PATCH] added Matcher and Meanbuf files --- matcher.c | 28 ++++++++++++++++++++++++++++ matcher.h | 21 +++++++++++++++++++++ meanbuf.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ meanbuf.h | 21 +++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 matcher.c create mode 100644 matcher.h create mode 100644 meanbuf.c create mode 100644 meanbuf.h diff --git a/matcher.c b/matcher.c new file mode 100644 index 0000000..312a644 --- /dev/null +++ b/matcher.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "matcher.h" + + +/** Handle incoming char. Returns true if this char completed the match. */ +bool matcher_test(matcher_t * m, uint8_t b) +{ + // If mismatch, rewind (and check at 0) + if (m->pattern[m->cursor] != b) { + m->cursor = 0; + } + + // Check for match + if (m->pattern[m->cursor] == b) { + // Good char + m->cursor++; + if (m->pattern[m->cursor] == 0) { // end of pattern + m->cursor = 0; // rewind + return true; // indicate success + } + } + + return false; +} diff --git a/matcher.h b/matcher.h new file mode 100644 index 0000000..6aedcb7 --- /dev/null +++ b/matcher.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include + +typedef struct { + const char *pattern; + size_t cursor; +} matcher_t; + + +/** + * Consume an incoming character. + * If this char was the last char of the pattern, returns true and rewinds. + * + * If the char is not in the pattern, resets match state. + * + * @returns true if the char concluded the expected pattern. + */ +bool matcher_test(matcher_t * mb, uint8_t b); diff --git a/meanbuf.c b/meanbuf.c new file mode 100644 index 0000000..65ccfa4 --- /dev/null +++ b/meanbuf.c @@ -0,0 +1,48 @@ +#include +#include + +#include "meanbuf.h" + + +/** Init a buffer */ +void meanbuf_init(meanbuf_t *mb, size_t size) +{ + if (size < 1) size = 1; + + mb->buf = calloc(size, sizeof(float)); // calloc, so it starts with zeros. + mb->cap = size; + mb->nw = 0; + mb->mean = 0; + + // clean buffer + for (uint16_t i = 0; i < size; i++) { + mb->buf[i] = 0; + } +} + + +void meanbuf_deinit(meanbuf_t *mb) +{ + if (mb->buf != NULL) { + free(mb->buf); + } +} + + +/** Add a value to the buffer. Returns current mean. */ +float meanbuf_add(meanbuf_t *mb, float f) +{ + // add sample + mb->buf[mb->nw++] = f; + if (mb->nw == mb->cap) mb->nw = 0; + + // calculate average + float acc = 0; + for (size_t i = 0; i < mb->cap; i++) { + acc += mb->buf[i]; + } + + acc /= mb->cap; + + return mb->mean = acc; +} diff --git a/meanbuf.h b/meanbuf.h new file mode 100644 index 0000000..4e655d9 --- /dev/null +++ b/meanbuf.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + + +typedef struct { + float * buf; // buffer (allocated at init) + size_t cap; // capacity + size_t nw; // next write index + float mean; // updated on write +} meanbuf_t; + + +/** Init a buffer */ +void meanbuf_init(meanbuf_t *mb, size_t size); + +/** Deinit a buffer (free buffer array) */ +void meanbuf_deinit(meanbuf_t *mb); + +/** Add a value to the buffer. Returns current mean. */ +float meanbuf_add(meanbuf_t *mb, float f);