From 8af75e5827ccc6d702211ae2cf922dcede5bf27e Mon Sep 17 00:00:00 2001 From: Dino Tinitigan Date: Wed, 27 Jan 2016 14:39:40 -0800 Subject: [PATCH] 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 --- libraries/CurieEEPROM/src/CurieEEPROM.h | 47 ++++++++++++------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/libraries/CurieEEPROM/src/CurieEEPROM.h b/libraries/CurieEEPROM/src/CurieEEPROM.h index fe0e9d06..20624cc9 100644 --- a/libraries/CurieEEPROM/src/CurieEEPROM.h +++ b/libraries/CurieEEPROM/src/CurieEEPROM.h @@ -29,7 +29,6 @@ #include #include "Arduino.h" -#include class CurieEEPROM { @@ -66,12 +65,13 @@ class CurieEEPROM { return t; } - auto bytes = to_bytes(t); + byte *bytes = to_bytes(t); for(int i = 0; i < byteCount; i++) { bytes[i] = read8(addr+i); } from_bytes(bytes, t); + delete bytes; return t; } template< typename T > T put(uint32_t addr, T t) @@ -88,11 +88,13 @@ class CurieEEPROM { return t; } - const auto dwords = to_dwords(t); + + size_t size = (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)); + uint32_t *dwords = to_dwords(t); //check if address is empty and available for writing new data bool blockAvailable = true; - for(int i =0; i < (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)); i++) + for(int i =0; i < size; i++) { uint32_t data32 = read(addr+i*sizeof(uint32_t)); if(data32 != 0xFFFFFFFF) @@ -102,7 +104,7 @@ class CurieEEPROM } if(blockAvailable) { - for(int i = 0; i std::array< byte, sizeof(T) > to_bytes(const T& object) + template< typename T > byte* to_bytes(const T& object) { - std::array< byte, sizeof(T) > bytes ; - - const byte* begin = reinterpret_cast< const byte* >( std::addressof(object)) ; - const byte* end = begin + sizeof(T) ; - std::copy( begin, end, std::begin(bytes)) ; + size_t buffer_size = sizeof(object); + byte *buffer = new byte[buffer_size]; + memcpy(buffer, &object, buffer_size); - return bytes; + return buffer; } - template< typename T > std::array< uint32_t, (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)) > to_dwords( const T& object ) + template< typename T > uint32_t* to_dwords(const T& object) { - std::array< uint32_t, (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)) > dwords; - - const uint32_t* begin = reinterpret_cast< const uint32_t* >( std::addressof(object)) ; - const uint32_t* end = begin + (sizeof(T)/4 + (((sizeof(T)%4)>1) ? 1 : 0)); - std::copy( begin, end, std::begin(dwords)); - - return dwords; + size_t buffer_size = sizeof(object); + uint32_t *buffer = new uint32_t[buffer_size]; + memcpy(buffer, &object, buffer_size); + + return buffer; } - template< typename T > T& from_bytes(std::array< byte, sizeof(T) >& bytes, T& object ) + template< typename T > T& from_bytes(byte* bytes, T& object ) { - byte* begin_object = reinterpret_cast< byte* >( std::addressof(object) ) ; - std::copy( std::begin(bytes), std::end(bytes), begin_object ) ; - + memcpy(&object, bytes, sizeof(object)); return object; } };