| 
					
				 | 
			
			
				@@ -11,42 +11,6 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "language.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#if 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template <typename T> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-static T eeprom_read(T *address); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template<> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-char eeprom_read<char>(char *address) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return eeprom_read_byte(reinterpret_cast<uint8_t*>(address)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#endif 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template <typename T> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-static void eeprom_write(T *address, T value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template<> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void eeprom_write<char>(char *addres, char value) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    eeprom_write_byte(reinterpret_cast<uint8_t*>(addres), static_cast<uint8_t>(value)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template <typename T> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-static bool eeprom_is_uninitialized(T *address); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-template <> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-bool eeprom_is_uninitialized<char>(char *address) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return (0xff == eeprom_read_byte(reinterpret_cast<uint8_t*>(address))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-bool eeprom_is_sheet_initialized(uint8_t sheet_num) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  return (0xffff != eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base-> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  s[sheet_num].z_offset)))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void eeprom_init() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     eeprom_init_default_byte((uint8_t*)EEPROM_POWER_COUNT, 0); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -72,23 +36,13 @@ void eeprom_init() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[0].z_offset)), last_babystep); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); ++i) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        bool is_uninitialized = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (uint_least8_t j = 0; j < (sizeof(Sheet::name)/sizeof(Sheet::name[0])); ++j) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!eeprom_is_uninitialized(&(EEPROM_Sheets_base->s[i].name[j]))) is_uninitialized = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if(is_uninitialized) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            SheetName sheetName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            eeprom_default_sheet_name(i,sheetName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[a]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // initialize the sheet names in eeprom 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        SheetName sheetName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        eeprom_default_sheet_name(i, sheetName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        eeprom_init_default_block(EEPROM_Sheets_base->s[i].name, (sizeof(Sheet::name)/sizeof(Sheet::name[0])), sheetName.c); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         eeprom_switch_to_next_sheet(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -182,6 +136,20 @@ void eeprom_switch_to_next_sheet() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+bool __attribute__((noinline)) eeprom_is_sheet_initialized(uint8_t sheet_num) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return (eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->s[sheet_num].z_offset))) != EEPROM_EMPTY_VALUE16); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+bool __attribute__((noinline)) eeprom_is_initialized_block(const void *__p, size_t __n) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const uint8_t *top = (const uint8_t*)__p + __n; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (const uint8_t *p = (const uint8_t *)__p; p < top; p++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (eeprom_read_byte(p) != EEPROM_EMPTY_VALUE) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void __attribute__((noinline)) eeprom_increment_byte(uint8_t *__p) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     eeprom_write_byte(__p, eeprom_read_byte(__p) + 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -194,6 +162,7 @@ void __attribute__((noinline)) eeprom_increment_dword(uint32_t *__p) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     eeprom_write_dword(__p, eeprom_read_dword(__p) + 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void __attribute__((noinline)) eeprom_add_byte(uint8_t *__p, uint8_t add) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     eeprom_write_byte(__p, eeprom_read_byte(__p) + add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -206,17 +175,23 @@ void __attribute__((noinline)) eeprom_add_dword(uint32_t *__p, uint32_t add) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     eeprom_write_dword(__p, eeprom_read_dword(__p) + add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (eeprom_read_byte(__p) == 0xff) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (eeprom_read_byte(__p) == EEPROM_EMPTY_VALUE) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         eeprom_write_byte(__p, def); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (eeprom_read_word(__p) == 0xffff) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (eeprom_read_word(__p) == EEPROM_EMPTY_VALUE16) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         eeprom_write_word(__p, def); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (eeprom_read_dword(__p) == 0xffffffff) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (eeprom_read_dword(__p) == EEPROM_EMPTY_VALUE32) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         eeprom_write_dword(__p, def); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+void __attribute__((noinline)) eeprom_init_default_block(void *__p, size_t __n, const void *def) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (!eeprom_is_initialized_block(__p, __n)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        eeprom_update_block(def, __p, __n); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |