Lil' Blackbox

Coding => v1.0 - v1.1 => Topic started by: Rx7man on February 18, 2016, 12:15:18 AM

Title: Handy functions and classes
Post by: Rx7man on February 18, 2016, 12:15:18 AM
Well.. I'm getting a collection of functions, etc that may be handy for other people.. I'm not quite ready to really publish them, but here's a little sample of something I'm working on. 

DISCLAIMER: by no means do I say or imply there aren't other (better) versions of anything I write out there... I've just decided to write all my own code for everything.

We all like using arrays for stuff, but the problem with them is if you return them from a function, you don't know how long they are.. you also can't dynamically change their size easily, etc.. so I've made a class for them.


#include "stdlib.h"

template <class T>
class MyArray {
public:
T * values;
int Count;
MyArray(){
Count = 0;
}


MyArray(int count){
Serial.print("initializing myarray ");
Serial.println(count);

values = (T *) malloc(count * sizeof(T));
if (values==NULL){
Count = 0;
}
else{
Count = count;
}
}

T& operator[](int j) {
if (j< this->Count){
return this->values[j];
}
}

int changesize(int newsize){
int oldcount = Count;
Serial.print("Changing size from ");
Serial.print(Count);
Serial.print(" to ");
Serial.println (newsize);

values = (T *) realloc(values, sizeof(T)*newsize);


if (values==NULL){
Count = 0;
}
else{
Count = newsize; //set the new count
if (Count>oldcount){ //initialize added members to default value
T defaultvalue;
for(int i = oldcount; i<Count; i++)
values[i] = defaultvalue;
}
}
}

/*
void DebugMe(){ //function to display
Serial.print("Count= ");
Serial.println(Count);

for(int i = 0; i<Count; i++){
Serial.print("index = ");
Serial.print(i);
Serial.print("  value = ");
Serial.println(values[i]);
}
}*/
};

MyArray<int> getarray(){
MyArray<int> newarray(10);
for (int i = 0; i< newarray.Count; i++){
newarray.values[i] = i;
}
return newarray;
}


void setup()
{
Serial.begin(115200);
MyArray<int> returnedarray(0);
returnedarray = getarray();

for(int i = 0; i< returnedarray.Count; i++){

Serial.println(returnedarray[i]);
}
       //Need to add code here to dispose/destroy the array since it's no longer needed and free the memory.. working on that

}
void loop(){}



I'll post my floating point version of the map() function as well.. and probably my 2dimensional, interpolated map I use for position control and boost... I'm working on rewriting them to make them easier to save/load from EEPROM as well so that values aren't hard-coded into the program...
Title: Re: Handy functions and classes
Post by: Bdubb'z on February 18, 2016, 12:02:32 PM
Nice work, thanks for sharing!!
Title: Re: Handy functions and classes
Post by: Rx7man on February 18, 2016, 12:25:01 PM
Thanks

Here's my map function that takes floating point numbers.. I use it all the time... I added a "constrain" option to it since it's sometimes needed





float mapf(float value, float oldlow, float oldhigh, float newlow, float newhigh, boolean Constrain)
{
//I think this is identical to the arduino map, but using floats rather than ints, and preventing a divide by 0 error
float rval = 0;
if ((oldhigh - oldlow) != 0) {
rval = float(value - oldlow) / float(oldhigh - oldlow) * float(newhigh - newlow) + newlow;
}//if
else {
rval = 0;
}//else
if (Constrain){
rval = constrain(rval, newlow, newhigh);
}
return rval;
} //mapf