Go Down

Topic: Some new "ideas for" core functions (Read 16167 times) previous topic - next topic

HazardsMind

Sep 05, 2014, 04:24 am Last Edit: Sep 20, 2014, 04:26 pm by HazardsMind Reason: 1
Hello, as the title says, I have some new core functions I would like to add to future IDE updates.

1) avg( ) "Average", this function will average the contents of a numeric array and return the value.
Code: [Select]
template<class T, size_t size> T avg(T(&x)[size])
{
 T tmp;
 for(int i = 0, j = size; i < j; i++)
 {
   tmp += x[i];
 } return (tmp/size);
}


Example sketch:
Code: [Select]
int myIntArray[5] = {25,324,420,1000,10};
float myFloatArray[5]={25,324,420,1000,10};

void setup()
{
 Serial.begin(115200);
 Serial.println(avg(myIntArray));
 Serial.println(avg(myFloatArray));
}

void loop() {}



Output:
Quote
355
355.80





2) shed(), Nothing fancy, just a simple little function to get the values after the decimal point, of a float or double
Code: [Select]
float shed(float val)
{
  return val - long(val); // stupidly simple function
}


Example Sketch:
Code: [Select]
float val = 123.4567;

void setup(){
 Serial.begin(115200);
 Serial.println(shed(val),4);
}

void loop() {}



Output:
Quote
0.4567





3) _map "templated map" this allows the map function to do ints, longs and floats with their expected outputs
Code: [Select]
template<class T>
T _map(T x, T in_min, T in_max, T out_min, T out_max)
{
 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


Example Sketch:
Code: [Select]
void setup()
{
 Serial.begin(115200);
 for(byte i = 0; i < 101; i++)
 {
   Serial.print(map(i, 0, 100, 0, 10));
   Serial.print("\t");
   Serial.println(_map<float>(i, 0, 100, 0, 10));
   delay(10);
 }
}

void loop() {}


Output: See attachment


I'm still thinking of more I can make, but for now this is all I got.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Robin2

I would prefer to see this sort of thing in a tutorial so that readers can add to their knowledge rather than have the learning experience abstracted away from them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

HazardsMind

#2
Sep 05, 2014, 06:05 pm Last Edit: Sep 06, 2014, 03:57 am by HazardsMind Reason: 1
3 more, mirrorByte, mirrorInt and mirrorLong.

(I am at work and I don't have my Nano with me, so I cant give any real outputs)

mirrorByte:
Code: [Select]

unsigned char mirrorByte(unsigned char B)
{
 unsigned char h, l;
 static const unsigned char table[16] = {
   0x0, 0x8, 0x4, 0xC,
   0x2, 0xA, 0x6, 0xE,
   0x1, 0x9, 0x5, 0xD,
   0x3, 0xB, 0x7, 0xF
 };
 h = table[ (B >> 4) ];
 l = table[ B & 0x0f ] << 4;

 return (l | h );
}

mirrorByte(0x67) should return 0xE6

mirrorInt:
Code: [Select]

unsigned short mirrorInt(short B)

  unsigned char h = mirrorByte(B >> 8 );
  unsigned short l = mirrorByte(B);
 
  return ((l << 8 ) | h);
}

mirrorInt(0x675D) -> 0xBAE6

mirrorLong:
Code: [Select]

unsigned long mirrorLong(long B)
{
 unsigned long h = mirrorInt(B >> 16);
 unsigned long l = mirrorInt(B);
 return ((l << 16) | h);
}

mirrorLong(0x675DAF39) -> 0x9CF5BAE6
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

CrossRoads

I can't keep track of all the stuff that already exists - I find it easier to just add this kind of stuff right in the code when needed, figuring out how to do it as needed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Coding Badly


RE: shed
Any particular reason you not using...
http://www.cplusplus.com/reference/cmath/modf/

Coding Badly


RE: _map<float>
In the not too distant past, that would never have made it into the core.  I suspect now it will be met with significant resistance.

Remember, the Arduino core is intended for people who have never programmed.  More syntax to learn (the <template parameter>) is a bad thing.

Coding Badly


RE: shed
The typical name for such functions is frac or fractfraction is probably the best choice for the intended audience.

Coding Badly


RE: mirrorInt
Is that going to work on a 32 bit processor?

HazardsMind



RE: mirrorInt
Is that going to work on a 32 bit processor?



That issue did come to mind but thinking just for the arduino it should be fine. Maybe I can use sizeof as a multiplier instead of just having 8 as a shifting value.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind



RE: shed
Any particular reason you not using...
http://www.cplusplus.com/reference/cmath/modf/



I don't go through each and every function of the C library, but from what I see, mine is easier to understand. Plus the user doesn't need to know referencing to use it.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

Update:

mirrorInt and mirrorLong functions fixed. (should be able to support 32 bit systems)
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Coding Badly


RE: mirrorInt
Is that going to work on a 32 bit processor?

That issue did come to mind but thinking just for the arduino it should be fine. Maybe I can use sizeof as a multiplier instead of just having 8 as a shifting value.


Or you could use unsigned short.

HazardsMind

Ok, I change it to unsigned short.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Coding Badly


RE: shed
Any particular reason you not using...
http://www.cplusplus.com/reference/cmath/modf/

I don't go through each and every function of the C library, but from what I see, mine is easier to understand.


And fails when the whole number exceeds the maximum value of a long.

Quote
Plus the user doesn't need to know referencing to use it.


You could modify your version to use modf and get the best of both.

HazardsMind

#14
Sep 06, 2014, 04:38 am Last Edit: Sep 06, 2014, 04:42 am by HazardsMind Reason: 1
I tried the modf function and compared it to a modified version of mine.
Code: [Select]
double intpart;
double param = 12.123456789;
double fractpart = modf(param , &intpart);
#define fract(x) float(x) - (unsigned long)x

void setup(){
  Serial.begin(115200);
  Serial.println(fractpart,6); // 4190 bytes
  Serial.println(fract(param),6); // 4092 bytes
}

void loop() {

}


Results:
Quote
0.123457
0.123457


No difference in results. You can try it yourself if you want to.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up