Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit f544fde

Browse files
committed
add more math feature.
1 parent f681138 commit f544fde

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

cores/arduino/Common.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
This library is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU Lesser General Public
5+
License as published by the Free Software Foundation; either
6+
version 2.1 of the License, or (at your option) any later version.
7+
This library is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10+
See the GNU Lesser General Public License for more details.
11+
You should have received a copy of the GNU Lesser General Public
12+
License along with this library; if not, write to the Free Software
13+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14+
*/
15+
16+
extern "C" {
17+
#include "stdlib.h"
18+
#include "stdint.h"
19+
}
20+
121
#include "Common.h"
222

3-
/* C++ prototypes */
23+
void randomSeed( uint32_t dwSeed )
24+
{
25+
if ( dwSeed != 0 )
26+
{
27+
srand( dwSeed ) ;
28+
}
29+
}
30+
31+
long random( long howbig )
32+
{
33+
if ( howbig == 0 )
34+
{
35+
return 0 ;
36+
}
37+
38+
return rand() % howbig;
39+
}
40+
41+
long random( long howsmall, long howbig )
42+
{
43+
if (howsmall >= howbig)
44+
{
45+
return howsmall;
46+
}
47+
48+
long diff = howbig - howsmall;
49+
50+
return random(diff) + howsmall;
51+
}
52+
453
long map(long x, long in_min, long in_max, long out_min, long out_max)
554
{
655
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

cores/arduino/WMath.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Common.h"
2+
extern "C" {
3+
#include "stdlib.h"
4+
#include "stdint.h"
5+
}
6+
7+
void randomSeed( uint32_t dwSeed )
8+
{
9+
if ( dwSeed != 0 )
10+
{
11+
srand( dwSeed ) ;
12+
}
13+
}
14+
15+
long random( long howbig )
16+
{
17+
if ( howbig == 0 )
18+
{
19+
return 0 ;
20+
}
21+
22+
return rand() % howbig;
23+
}
24+
25+
long random( long howsmall, long howbig )
26+
{
27+
if (howsmall >= howbig)
28+
{
29+
return howsmall;
30+
}
31+
32+
long diff = howbig - howsmall;
33+
34+
return random(diff) + howsmall;
35+
}
36+
37+
/* C++ prototypes */
38+
long map(long x, long in_min, long in_max, long out_min, long out_max)
39+
{
40+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
41+
}
42+
43+
uint16_t makeWord(uint16_t w) { return w; }
44+
uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; }

0 commit comments

Comments
 (0)