]> www.fi.muni.cz Git - bike-lights.git/commitdiff
logging.c: don't wear out eeprom on frequent reboots
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 19:31:32 +0000 (21:31 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 19:31:32 +0000 (21:31 +0200)
logging.c

index 04f9515ceddd067e7adb454641e1d8180d75c26a..5c5beeb30848f4fd4895a10474cd56c93f705d39 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -6,18 +6,34 @@
 #include "lights.h"
 
 #define LOG_BUFFER 128
-static unsigned char log_state EEMEM;
 static unsigned char log_buffer_ee[LOG_BUFFER] EEMEM;
 static unsigned char log_buffer_count;
 static unsigned char log_buffer[LOG_BUFFER];
+static unsigned char log_state EEMEM;
+static unsigned char reboot_count EEMEM = 0;
+static unsigned char can_write_eeprom = 0;
 
 void log_set_state(unsigned char val)
 {
-       eeprom_write_byte(&log_state, val);
+       if (can_write_eeprom)
+               eeprom_write_byte(&log_state, val);
 }
 
 void log_init()
 {
+       unsigned char r_count;
+
+       r_count = eeprom_read_byte(&reboot_count);
+
+       if (r_count < 5) {
+               r_count++;
+               eeprom_write_byte(&reboot_count, r_count);
+               can_write_eeprom = 1;
+       } else {
+               //eeprom_write_byte(&log_state, 0xFF);
+               can_write_eeprom = 0;
+       }
+
        log_set_state(1);
        log_buffer_count = 0;
 }
@@ -42,6 +58,10 @@ void log_flush() {
        unsigned char i;
 
        log_buffer_count = LOG_BUFFER;
+
+       if (!can_write_eeprom)
+               return;
+
        for (i=0; i < LOG_BUFFER; i++) {
                eeprom_write_byte(&log_buffer_ee[i],
                        log_buffer[i]);