forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ refactor + testsketch
1 parent
8d3bf7b
commit 6ef02ae
Showing
3 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// FILE: DistanceTable.cpp | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.01 | ||
// PURPOSE: library for a memory efficient DistanceTable for Arduino | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
// 0.1.00 - initial version | ||
// 0.1.01 - refactor | ||
|
||
#include "DistanceTable.h" | ||
|
||
DistanceTable::DistanceTable(uint8_t size) | ||
{ | ||
_size = size; | ||
uint16_t s = size * (size-1)/2; | ||
_distanceTable = (double*) malloc(s * sizeof(double)); | ||
if (_distanceTable == NULL) _size = 0; | ||
} | ||
|
||
DistanceTable::~DistanceTable() | ||
{ | ||
if (_distanceTable != NULL) free(_distanceTable); | ||
} | ||
|
||
void DistanceTable::clear() | ||
{ | ||
uint16_t s = _size * (_size-1)/2; | ||
for (uint16_t index = 0; index < s; index++) | ||
{ | ||
_distanceTable[index] = 0; | ||
} | ||
}; | ||
|
||
void DistanceTable::set(uint8_t x, uint8_t y, double value ) | ||
{ | ||
if ( x >= _size || y >= _size) return; | ||
if ( x == y ) return; | ||
// need swap? | ||
if ( x < y ) | ||
{ | ||
uint8_t t = x; x = y; y = t; | ||
} | ||
uint16_t index = x; | ||
index = index * (index-1)/2 + y; | ||
_distanceTable[index] = value; | ||
}; | ||
|
||
double DistanceTable::get(uint8_t x, uint8_t y) | ||
{ | ||
if ( x >= _size || y >= _size) return -1; | ||
if ( x == y ) return 0; | ||
// need swap? | ||
if ( x < y ) | ||
{ | ||
uint8_t t = x; x = y; y = t; | ||
} | ||
uint16_t index = x; | ||
index = index * (index-1)/2 + y; | ||
return _distanceTable[index]; | ||
}; | ||
|
||
|
||
void DistanceTable::dump() | ||
{ | ||
uint16_t index = 0; | ||
for (uint16_t i = 0; i < _size-1; i++) | ||
{ | ||
for (uint16_t j = 0; j <= i; j++) | ||
{ | ||
Serial.print(_distanceTable[index++]); | ||
Serial.print("\t"); | ||
} | ||
Serial.println(); | ||
} | ||
Serial.println(); | ||
}; | ||
|
||
|
||
// --- END OF FILE --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// FILE: DistanceTable.h | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.01 | ||
// PURPOSE: memory efficient DistanceTable for Arduino | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#ifndef DistanceTable_h | ||
#define DistanceTable_h | ||
|
||
#include "Arduino.h" | ||
|
||
#define DISTANCETABLE_LIB_VERSION "0.1.01" | ||
|
||
class DistanceTable | ||
{ | ||
public: | ||
DistanceTable(uint8_t); | ||
~DistanceTable(); | ||
|
||
void clear(); | ||
void set(uint8_t x, uint8_t y, double value ); | ||
double get(uint8_t x, uint8_t y); | ||
void dump(); | ||
|
||
protected: | ||
int _error; | ||
uint8_t _size; | ||
double * _distanceTable; | ||
}; | ||
|
||
#endif | ||
// --- END OF FILE --- |
95 changes: 95 additions & 0 deletions
95
libraries/DistanceTable/example/distanceTable/distanceTable.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// FILE: distanceTable.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.00 | ||
// PURPOSE: demo of memory efficient distance table class | ||
// DATE: 2015-06-18 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include "DistanceTable.h" | ||
|
||
uint32_t freeRam() | ||
{ | ||
extern int __heap_start, *__brkval; | ||
int v; | ||
return (uint32_t) &v - (__brkval == 0 ? (uint32_t) &__heap_start : (uint32_t) __brkval); | ||
}; | ||
|
||
DistanceTable dt(20); | ||
|
||
uint32_t start; | ||
uint32_t stop; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
Serial.print("DistanceTable: "); | ||
Serial.println(DISTANCETABLE_LIB_VERSION); | ||
Serial.println("DistanceTable test 20x20: "); | ||
|
||
Serial.print("clear:\t"); | ||
start = micros(); | ||
dt.clear(); | ||
stop = micros(); | ||
Serial.println(stop - start); | ||
|
||
|
||
Serial.print("set:\t"); | ||
start = micros(); | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
for (int j = 0; j < 20; j++) | ||
{ | ||
dt.set(i, j, i * j); | ||
} | ||
} | ||
stop = micros(); | ||
Serial.println(stop - start); | ||
|
||
|
||
Serial.print("get:\t"); | ||
int count = 0; | ||
start = micros(); | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
for (int j = 0; j < 20; j++) | ||
{ | ||
if ( dt.get(i, j) < 0.5 ) count++; | ||
} | ||
} | ||
stop = micros(); | ||
Serial.println(stop - start); | ||
Serial.print("count:\t"); | ||
Serial.println(count); | ||
|
||
Serial.print("ram:\t"); | ||
Serial.println(freeRam()); | ||
|
||
Serial.println(); | ||
Serial.println("dump:\t"); | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
for (int j = 0; j < 20; j++) | ||
{ | ||
Serial.print( dt.get(i, j), 1); | ||
Serial.print("\t"); | ||
} | ||
Serial.println(); | ||
} | ||
|
||
Serial.println(); | ||
Serial.println("dump:\t"); | ||
dt.dump(); | ||
|
||
Serial.println(); | ||
Serial.println("done..."); | ||
} | ||
|
||
void loop() | ||
{ | ||
} | ||
|
||
|