-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtil.cpp
70 lines (67 loc) · 2.88 KB
/
Util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Util.c
*
* Created: 02.02.2015 18:08:48
* Author: Mircea Diaconescu
*/
#include "Util.h"
/************************************************************************/
/* @operator */
/* Allow the usage of the "new" C++ operator */
/************************************************************************/
void * operator new( size_t size) {
return malloc( size);
};
/************************************************************************/
/* @operator */
/* Allow the usage of the "delete" C++ operator */
/************************************************************************/
void operator delete( void * ptr) {
free( ptr);
};
/************************************************************************/
/* @method */
/* Calculate the current MCU free memory value (in bytes) */
/* @return the number of free RAM bytes available */
/************************************************************************/
uint16_t getFreeMCUMemory() {
uint16_t free_memory;
if ( (uint16_t)__brkval == 0)
return ( ( (uint16_t)&free_memory) - ( (uint16_t)&__bss_end));
else
return ( ( (uint16_t)&free_memory) - ( (uint16_t)__brkval));
};
/************************************************************************/
/* @method */
/* Extract data from PROGMEM and return a RAM pointer to it. */
/* NOTE: use 'free' within the caller code to deallocate memory */
/* after the result of this method is not needed anymore! */
/* @param pmD */
/* the PROGMEM data */
/* @param length */
/* optional (auto-computed if not provided) parameter that */
/* specifies the number of bytes to load from PROGMEM */
/* @return the pointer to the RAM data */
/************************************************************************/
char* getPMData( const char pmData[], size_t length) {
char c = 0;
const char* data = pmData;
uint8_t len = length;
// compute the length (number of bytes to load) if not
// specified by the function 'length' parameter
if ( len == 0) {
// read the data from PROGMEM for length computation
while ( 0 != ( c = pgm_read_byte(data++))) {
len++;
}
data = pmData;
}
// load the data from PROGMEM and store it in RAM
char* ramData = (char*)malloc( len + 1);
while ( 0 != ( c = pgm_read_byte(data++))) {
*(ramData) = c;
ramData++;
}
*(ramData) = '\0';
return (ramData - len);
};