Simple counter class, non-volatile between resets and power on/off.
Simple Counter class, used to store count array in non-volatile flash memory between resets and power on/off.
Diff: Counter.cpp
- Revision:
- 2:f02ac448ecd3
- Parent:
- 1:e8eeddee2959
- Child:
- 3:c15c22484205
diff -r e8eeddee2959 -r f02ac448ecd3 Counter.cpp --- a/Counter.cpp Mon Nov 12 19:01:53 2018 +0000 +++ b/Counter.cpp Mon Nov 12 19:08:35 2018 +0000 @@ -1,11 +1,13 @@ #include "Counter.h" +//#define COUNTER_DEBUG + #ifdef COUNTER_DEBUG Serial debug_out(USBTX, USBRX); #endif Counter::Counter() { - new_count = flash_size() - SECTOR_SIZE; + is_new_count = flash_size() - SECTOR_SIZE; address = (int)((int*)(flash_size() - SECTOR_SIZE) + 1); #ifdef COUNTER_DEBUG debug_out.printf("Constructing Object...\r\n"); @@ -31,10 +33,10 @@ #ifdef COUNTER_DEBUG debug_out.printf("Initializing...\r\n"); #endif - if(*((int*)new_count) == -1) { // then we know it hasn't been initialized yet - erase_sector(new_count); + if(*((int*)is_new_count) == -1) { // then we know it hasn't been initialized yet + erase_sector(is_new_count); int zero = 0; - program_flash(new_count, (char*)&zero, sizeof(int)); + program_flash(is_new_count, (char*)&zero, sizeof(int)); memwrite(); } else { memread(); @@ -51,7 +53,7 @@ #endif erase_sector(address); int zero = 0; - program_flash(new_count, (char*)&zero, sizeof(int)); + program_flash(is_new_count, (char*)&zero, sizeof(int)); program_flash(address, (char*)&count, sizeof(int) * MAX_LEN); #ifdef COUNTER_DEBUG print_memory();