Skip to content

Commit 6885cde

Browse files
bigdinotechcalvinatintel
authored andcommitted
CurieEEPROM reimplement put and get to not use STL
-reimplement put() and get() to not use STL due to incompatibility with max() and min() macros
1 parent 0a38234 commit 6885cde

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

libraries/CurieEEPROM/src/CurieEEPROM.h

+22-25
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
#include <inttypes.h>
3131
#include "Arduino.h"
32-
#include <array>
3332

3433
class CurieEEPROM
3534
{
@@ -66,12 +65,13 @@ class CurieEEPROM
6665
{
6766
return t;
6867
}
69-
auto bytes = to_bytes(t);
68+
byte *bytes = to_bytes(t);
7069
for(int i = 0; i < byteCount; i++)
7170
{
7271
bytes[i] = read8(addr+i);
7372
}
7473
from_bytes(bytes, t);
74+
delete bytes;
7575
return t;
7676
}
7777
template< typename T > T put(uint32_t addr, T t)
@@ -88,11 +88,13 @@ class CurieEEPROM
8888
{
8989
return t;
9090
}
91-
const auto dwords = to_dwords(t);
91+
92+
size_t size = (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0));
93+
uint32_t *dwords = to_dwords(t);
9294

9395
//check if address is empty and available for writing new data
9496
bool blockAvailable = true;
95-
for(int i =0; i < (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)); i++)
97+
for(int i =0; i < size; i++)
9698
{
9799
uint32_t data32 = read(addr+i*sizeof(uint32_t));
98100
if(data32 != 0xFFFFFFFF)
@@ -102,7 +104,7 @@ class CurieEEPROM
102104
}
103105
if(blockAvailable)
104106
{
105-
for(int i = 0; i<sizeof(dwords)/4; i++)
107+
for(int i = 0; i<size; i++)
106108
{
107109
write(addr+i*sizeof(uint32_t), dwords[i]);
108110
}
@@ -117,7 +119,7 @@ class CurieEEPROM
117119
}
118120

119121
//update blockdata buffer
120-
for(int i = 0; i<sizeof(dwords)/4; i++)
122+
for(int i = 0; i<size; i++)
121123
{
122124
blockdata[addr/4 + i] = dwords[i];
123125
}
@@ -137,37 +139,32 @@ class CurieEEPROM
137139
delay(3); //give it enough time to finish writing
138140
}
139141
}
142+
delete dwords;
140143
return t;
141144
}
142145

143146
private:
144-
template< typename T > std::array< byte, sizeof(T) > to_bytes(const T& object)
147+
template< typename T > byte* to_bytes(const T& object)
145148
{
146-
std::array< byte, sizeof(T) > bytes ;
147-
148-
const byte* begin = reinterpret_cast< const byte* >( std::addressof(object)) ;
149-
const byte* end = begin + sizeof(T) ;
150-
std::copy( begin, end, std::begin(bytes)) ;
149+
size_t buffer_size = sizeof(object);
150+
byte *buffer = new byte[buffer_size];
151+
memcpy(buffer, &object, buffer_size);
151152

152-
return bytes;
153+
return buffer;
153154
}
154155

155-
template< typename T > std::array< uint32_t, (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)) > to_dwords( const T& object )
156+
template< typename T > uint32_t* to_dwords(const T& object)
156157
{
157-
std::array< uint32_t, (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)) > dwords;
158-
159-
const uint32_t* begin = reinterpret_cast< const uint32_t* >( std::addressof(object)) ;
160-
const uint32_t* end = begin + (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0));
161-
std::copy( begin, end, std::begin(dwords));
162-
163-
return dwords;
158+
size_t buffer_size = sizeof(object);
159+
uint32_t *buffer = new uint32_t[buffer_size];
160+
memcpy(buffer, &object, buffer_size);
161+
162+
return buffer;
164163
}
165164

166-
template< typename T > T& from_bytes(std::array< byte, sizeof(T) >& bytes, T& object )
165+
template< typename T > T& from_bytes(byte* bytes, T& object )
167166
{
168-
byte* begin_object = reinterpret_cast< byte* >( std::addressof(object) ) ;
169-
std::copy( std::begin(bytes), std::end(bytes), begin_object ) ;
170-
167+
memcpy(&object, bytes, sizeof(object));
171168
return object;
172169
}
173170
};

0 commit comments

Comments
 (0)