aboutsummaryrefslogtreecommitdiffstats
path: root/wirish
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2015-06-13 22:26:26 -0700
committerbnewbold <bnewbold@robocracy.org>2015-06-13 22:26:26 -0700
commit730fbfc090054e1c0dbd8d2c063a1dbfeb49e7c4 (patch)
tree4d4139ac8551a35689cc2cec8aa1fc2f2f7d6fbd /wirish
parent94c8e3c9e98412feaef8e5aa8b29b1ec0047e9cf (diff)
downloadlibrambutan-master.tar.gz
librambutan-master.zip
new MIT-licensed wirish_math implementationHEADmaster
Diffstat (limited to 'wirish')
-rw-r--r--wirish/wirish_math.cpp68
1 files changed, 39 insertions, 29 deletions
diff --git a/wirish/wirish_math.cpp b/wirish/wirish_math.cpp
index 1443b3c..7853eea 100644
--- a/wirish/wirish_math.cpp
+++ b/wirish/wirish_math.cpp
@@ -1,24 +1,38 @@
-/*
- * Modified by LeafLabs, LLC.
+/******************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2015 Bryan Newbold
*
- * Part of the Wiring project - http://wiring.org.co Copyright (c)
- * 2004-06 Hernando Barragan Modified 13 August 2006, David A. Mellis
- * for Arduino - http://www.arduino.cc/
+ * 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:
*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
+ * 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.
+ *****************************************************************************/
+
+/*
+ * Arduino-compatible digital math implementation.
*
- * This library 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
- * Lesser General Public License for more details.
+ * This replaces the old copyleft wirish_math implementation of randomSeed(),
+ * and random().
*
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * This file is a "clean" implementation using only the API documentation
+ * without directly copying from the Arduino source. It simply wraps routines
+ * provided in stdlib.
*/
#include <stdlib.h>
@@ -30,20 +44,16 @@ void randomSeed(unsigned int seed) {
}
}
-long random(long howbig) {
- if (howbig == 0) {
- return 0;
+long random(long max) {
+ if (max <= 0) {
+ max = 1;
}
-
- return rand() % howbig;
+ return map(rand(), 0, RAND_MAX, 0, max-1);
}
-long random(long howsmall, long howbig) {
- if (howsmall >= howbig) {
- return howsmall;
+long random(long min, long max) {
+ if (max <= min) {
+ max = min + 1;
}
-
- long diff = howbig - howsmall;
- return random(diff) + howsmall;
+ return map(rand(), 0, RAND_MAX, min, max-1);
}
-