29
29
30
30
#include < inttypes.h>
31
31
#include " Arduino.h"
32
- #include < array>
33
32
34
33
class CurieEEPROM
35
34
{
@@ -66,12 +65,13 @@ class CurieEEPROM
66
65
{
67
66
return t;
68
67
}
69
- auto bytes = to_bytes (t);
68
+ byte * bytes = to_bytes (t);
70
69
for (int i = 0 ; i < byteCount; i++)
71
70
{
72
71
bytes[i] = read8 (addr+i);
73
72
}
74
73
from_bytes (bytes, t);
74
+ delete bytes;
75
75
return t;
76
76
}
77
77
template < typename T > T put (uint32_t addr, T t)
@@ -88,11 +88,13 @@ class CurieEEPROM
88
88
{
89
89
return t;
90
90
}
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);
92
94
93
95
// check if address is empty and available for writing new data
94
96
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++)
96
98
{
97
99
uint32_t data32 = read (addr+i*sizeof (uint32_t ));
98
100
if (data32 != 0xFFFFFFFF )
@@ -102,7 +104,7 @@ class CurieEEPROM
102
104
}
103
105
if (blockAvailable)
104
106
{
105
- for (int i = 0 ; i<sizeof (dwords)/ 4 ; i++)
107
+ for (int i = 0 ; i<size ; i++)
106
108
{
107
109
write (addr+i*sizeof (uint32_t ), dwords[i]);
108
110
}
@@ -117,7 +119,7 @@ class CurieEEPROM
117
119
}
118
120
119
121
// update blockdata buffer
120
- for (int i = 0 ; i<sizeof (dwords)/ 4 ; i++)
122
+ for (int i = 0 ; i<size ; i++)
121
123
{
122
124
blockdata[addr/4 + i] = dwords[i];
123
125
}
@@ -137,37 +139,32 @@ class CurieEEPROM
137
139
delay (3 ); // give it enough time to finish writing
138
140
}
139
141
}
142
+ delete dwords;
140
143
return t;
141
144
}
142
145
143
146
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)
145
148
{
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);
151
152
152
- return bytes ;
153
+ return buffer ;
153
154
}
154
155
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)
156
157
{
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;
164
163
}
165
164
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 )
167
166
{
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));
171
168
return object;
172
169
}
173
170
};
0 commit comments