sanguino/cores/arduino/WMath.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4 Part of the Wiring project - http://wiring.org.co
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 Copyright (c) 2004-06 Hernando Barragan
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 You should have received a copy of the GNU Lesser General
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Public License along with this library; if not, write to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 Boston, MA 02111-1307 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 $Id$
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 extern "C" {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #include "stdlib.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 void randomSeed(unsigned int seed)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 if (seed != 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 srandom(seed);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 long random(long howbig)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 if (howbig == 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 return random() % howbig;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 long random(long howsmall, long howbig)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 if (howsmall >= howbig) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 return howsmall;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 long diff = howbig - howsmall;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 return random(diff) + howsmall;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 long map(long x, long in_min, long in_max, long out_min, long out_max)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 unsigned int makeWord(unsigned int w) { return w; }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }

mercurial