Move algorithms from mware/ to algo/
authorbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Wed, 13 Aug 2008 09:45:31 +0000 (09:45 +0000)
committerbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Wed, 13 Aug 2008 09:45:31 +0000 (09:45 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1627 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/algo/mean.h [new file with mode: 0644]
bertos/algo/rle.c [new file with mode: 0644]
bertos/algo/rle.h [new file with mode: 0644]
bertos/mware/hex.c
bertos/mware/rle.c [deleted file]
bertos/mware/rle.h

diff --git a/bertos/algo/mean.h b/bertos/algo/mean.h
new file mode 100644 (file)
index 0000000..893241e
--- /dev/null
@@ -0,0 +1,41 @@
+#warning revise me!
+
+
+/**
+ *  DECLARE_SMEAN(temperature, uint8_t, uint16_t);
+ *  for (i = 0; i < TEMP_MEANS; ++i)
+ *    SMEAN_ADD(temperature, adc_get(), TEMP_MEANS);
+ *  printf("mean temperature = %d\n", SMEAN_GET(temperature));
+ */
+
+/**
+ * Instantiate a mean instance
+ */
+#define DECLARE_SMEAN(name, Type, SumType) \
+       struct { \
+               SumType sum; \
+               Type result; \
+               int count; \
+       } name = { 0, 0, 0 }
+
+/**
+ * Insert a new sample into the mean.
+ *
+ * \note \a mean and \a max_samples are evaluated multiple times
+ */
+#define SMEAN_ADD(mean, sample, max_samples) \
+       do { \
+               (mean).sum += (sample); \
+               if ((mean).count++ >= (max_samples)) \
+               { \
+                       (mean).result = (mean).sum / (max_samples); \
+                       (mean).sum = 0; \
+                       (mean).count = 0; \
+               } \
+       } while (0)
+
+/**
+ * Return current mean value.
+ */
+#define SMEAN_GET(mean)  ((mean).result)
+
diff --git a/bertos/algo/rle.c b/bertos/algo/rle.c
new file mode 100644 (file)
index 0000000..66701e9
--- /dev/null
@@ -0,0 +1,151 @@
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
+ *
+ * -->
+ *
+ * \brief General-purpose run-length {en,de}coding algorithm (implementation)
+ *
+ * Original source code from http://www.compuphase.com/compress.htm
+ *
+ * \version $Id$
+ * \author Bernie Innocenti <bernie@codewiz.org>
+ */
+
+#include "rle.h"
+
+
+/**
+ * Run-length encode \a len bytes from the \a input buffer
+ * to the \a output buffer.
+ */
+int rle(unsigned char *output, const unsigned char *input, int len)
+{
+       int count, index, i;
+       unsigned char first;
+       unsigned char *out;
+
+
+       out = output;
+       count = 0;
+       while (count < len)
+       {
+               index = count;
+               first = input[index++];
+
+               /* Scan for bytes identical to the first one */
+               while ((index < len) && (index - count < 127) && (input[index] == first))
+                       index++;
+
+               if (index - count == 1)
+               {
+                       /* Failed to "replicate" the current byte. See how many to copy.
+                        */
+                       while ((index < len) && (index - count < 127))
+                       {
+                               /* Avoid a replicate run of only 2-bytes after a literal run.
+                                * There is no gain in this, and there is a risc of loss if the
+                                * run after the two identical bytes is another literal run.
+                                * So search for 3 identical bytes.
+                                */
+                               if ((input[index] == input[index - 1]) &&
+                                       ((index > 1) && (input[index] == input[index - 2])))
+                               {
+                                       /* Reset the index so we can back up these three identical
+                                        * bytes in the next run.
+                                        */
+                                       index -= 2;
+                                       break;
+                               }
+
+                               index++;
+                       }
+
+                       /* Output a run of uncompressed bytes: write length and values */
+                       *out++ = (unsigned char)(count - index);
+                       for (i = count; i < index; i++)
+                               *out++ = input[i];
+           }
+               else
+               {
+                       /* Output a compressed run: write length and value */
+                       *out++ = (unsigned char)(index - count);
+                       *out++ = first;
+           }
+
+               count = index;
+       }
+
+       /* Output EOF marker */
+       *out++ = 0;
+
+       return (out - output);
+}
+
+
+/**
+ * Run-length decode from the \a input buffer to the \a output
+ * buffer.
+ *
+ * \note The output buffer must be large enough to accomodate
+ *       all decoded output.
+ */
+int unrle(unsigned char *output, const unsigned char *input)
+{
+       signed char count;
+       unsigned char *out;
+       unsigned char value;
+
+
+       out = output;
+
+       for (;;)
+       {
+               count = (signed char)*input++;
+               if (count > 0)
+               {
+                       /* replicate run */
+                       value = *input++;
+                       while (count--)
+                               *out++ = value;
+               }
+               else if (count < 0)
+               {
+                       /* literal run */
+                       while (count++)
+                               *out++ = *input++;
+               }
+               else
+                       /* EOF */
+                       break;
+       }
+
+       return (out - output);
+}
diff --git a/bertos/algo/rle.h b/bertos/algo/rle.h
new file mode 100644 (file)
index 0000000..6e056d2
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
+ *
+ * -->
+ *
+ * \version $Id$
+ * \author Bernie Innocenti <bernie@codewiz.org>
+ *
+ * \brief General-purpose run-length {en,de}coding algorithm (interface)
+ */
+#ifndef RLE_H
+#define RLE_H
+
+int rle(unsigned char *output, const unsigned char *input, int length);
+int unrle(unsigned char *output, const unsigned char *input);
+
+#endif /* RLE_H */
index dd326c439805af1c00e214e6d78b19efd4c17376..4b22f1ef20e2fcc3e80f8efb272f0d40fec4320d 100644 (file)
  * the GNU General Public License.
  *
  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
- *
  * -->
  *
  * \brief Poor man's hex arrays (implementation).
  *
  * \version $Id$
- *
  * \author Bernie Innocenti <bernie@codewiz.org>
  */
 
diff --git a/bertos/mware/rle.c b/bertos/mware/rle.c
deleted file mode 100644 (file)
index 66701e9..0000000
+++ /dev/null
@@ -1,151 +0,0 @@
-/**
- * \file
- * <!--
- * This file is part of BeRTOS.
- *
- * Bertos is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * As a special exception, you may use this file as part of a free software
- * library without restriction.  Specifically, if other files instantiate
- * templates or use macros or inline functions from this file, or you compile
- * this file and link it with other files to produce an executable, this
- * file does not by itself cause the resulting executable to be covered by
- * the GNU General Public License.  This exception does not however
- * invalidate any other reasons why the executable file might be covered by
- * the GNU General Public License.
- *
- * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
- * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
- *
- * -->
- *
- * \brief General-purpose run-length {en,de}coding algorithm (implementation)
- *
- * Original source code from http://www.compuphase.com/compress.htm
- *
- * \version $Id$
- * \author Bernie Innocenti <bernie@codewiz.org>
- */
-
-#include "rle.h"
-
-
-/**
- * Run-length encode \a len bytes from the \a input buffer
- * to the \a output buffer.
- */
-int rle(unsigned char *output, const unsigned char *input, int len)
-{
-       int count, index, i;
-       unsigned char first;
-       unsigned char *out;
-
-
-       out = output;
-       count = 0;
-       while (count < len)
-       {
-               index = count;
-               first = input[index++];
-
-               /* Scan for bytes identical to the first one */
-               while ((index < len) && (index - count < 127) && (input[index] == first))
-                       index++;
-
-               if (index - count == 1)
-               {
-                       /* Failed to "replicate" the current byte. See how many to copy.
-                        */
-                       while ((index < len) && (index - count < 127))
-                       {
-                               /* Avoid a replicate run of only 2-bytes after a literal run.
-                                * There is no gain in this, and there is a risc of loss if the
-                                * run after the two identical bytes is another literal run.
-                                * So search for 3 identical bytes.
-                                */
-                               if ((input[index] == input[index - 1]) &&
-                                       ((index > 1) && (input[index] == input[index - 2])))
-                               {
-                                       /* Reset the index so we can back up these three identical
-                                        * bytes in the next run.
-                                        */
-                                       index -= 2;
-                                       break;
-                               }
-
-                               index++;
-                       }
-
-                       /* Output a run of uncompressed bytes: write length and values */
-                       *out++ = (unsigned char)(count - index);
-                       for (i = count; i < index; i++)
-                               *out++ = input[i];
-           }
-               else
-               {
-                       /* Output a compressed run: write length and value */
-                       *out++ = (unsigned char)(index - count);
-                       *out++ = first;
-           }
-
-               count = index;
-       }
-
-       /* Output EOF marker */
-       *out++ = 0;
-
-       return (out - output);
-}
-
-
-/**
- * Run-length decode from the \a input buffer to the \a output
- * buffer.
- *
- * \note The output buffer must be large enough to accomodate
- *       all decoded output.
- */
-int unrle(unsigned char *output, const unsigned char *input)
-{
-       signed char count;
-       unsigned char *out;
-       unsigned char value;
-
-
-       out = output;
-
-       for (;;)
-       {
-               count = (signed char)*input++;
-               if (count > 0)
-               {
-                       /* replicate run */
-                       value = *input++;
-                       while (count--)
-                               *out++ = value;
-               }
-               else if (count < 0)
-               {
-                       /* literal run */
-                       while (count++)
-                               *out++ = *input++;
-               }
-               else
-                       /* EOF */
-                       break;
-       }
-
-       return (out - output);
-}
index 6e056d22b76b2340cc1bfdcc94a25b447f94a604..0ca9a28d6ba1b7dca0dbcc698212f4a705f9db48 100644 (file)
@@ -1,45 +1,2 @@
-/**
- * \file
- * <!--
- * This file is part of BeRTOS.
- *
- * Bertos is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * As a special exception, you may use this file as part of a free software
- * library without restriction.  Specifically, if other files instantiate
- * templates or use macros or inline functions from this file, or you compile
- * this file and link it with other files to produce an executable, this
- * file does not by itself cause the resulting executable to be covered by
- * the GNU General Public License.  This exception does not however
- * invalidate any other reasons why the executable file might be covered by
- * the GNU General Public License.
- *
- * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
- * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
- *
- * -->
- *
- * \version $Id$
- * \author Bernie Innocenti <bernie@codewiz.org>
- *
- * \brief General-purpose run-length {en,de}coding algorithm (interface)
- */
-#ifndef RLE_H
-#define RLE_H
-
-int rle(unsigned char *output, const unsigned char *input, int length);
-int unrle(unsigned char *output, const unsigned char *input);
-
-#endif /* RLE_H */
+#warning This header is OBSOLETE
+#include <algo/rle.h>