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.

Committer:
kpan
Date:
Mon Nov 12 18:43:22 2018 +0000
Revision:
0:693f2f97c8c1
Child:
1:e8eeddee2959
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kpan 0:693f2f97c8c1 1 #include "Counter.h"
kpan 0:693f2f97c8c1 2
kpan 0:693f2f97c8c1 3 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 4 Serial debug_out(USBTX, USBRX);
kpan 0:693f2f97c8c1 5 #endif
kpan 0:693f2f97c8c1 6 Counter::Counter()
kpan 0:693f2f97c8c1 7 {
kpan 0:693f2f97c8c1 8 is_written = flash_size() - SECTOR_SIZE;
kpan 0:693f2f97c8c1 9 address = (int)((int*)(flash_size() - SECTOR_SIZE) + 1);
kpan 0:693f2f97c8c1 10 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 11 debug_out.printf("Constructing Object...\r\n");
kpan 0:693f2f97c8c1 12 #endif
kpan 0:693f2f97c8c1 13 for(int i=0; i<PARTY_LIMIT; i++)
kpan 0:693f2f97c8c1 14 vc[i] = 0;
kpan 0:693f2f97c8c1 15 this->init();
kpan 0:693f2f97c8c1 16 }
kpan 0:693f2f97c8c1 17
kpan 0:693f2f97c8c1 18 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 19 void Counter::print_memory()
kpan 0:693f2f97c8c1 20 {
kpan 0:693f2f97c8c1 21 int *ptr = (int*) address;
kpan 0:693f2f97c8c1 22 debug_out.printf("PartyID\tVoteCt\tMemCt\tAddress\r\n");
kpan 0:693f2f97c8c1 23 for(int i=0; i<PARTY_LIMIT; i++) {
kpan 0:693f2f97c8c1 24 debug_out.printf("%dID\t%d\t%d\t%p\r\n", i, vc[i], ptr[i], (ptr+i));
kpan 0:693f2f97c8c1 25 }
kpan 0:693f2f97c8c1 26 }
kpan 0:693f2f97c8c1 27 #endif
kpan 0:693f2f97c8c1 28
kpan 0:693f2f97c8c1 29 void Counter::init()
kpan 0:693f2f97c8c1 30 {
kpan 0:693f2f97c8c1 31 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 32 debug_out.printf("Initializing...\r\n");
kpan 0:693f2f97c8c1 33 #endif
kpan 0:693f2f97c8c1 34 if(*((int*)is_written) == -1) { // then we know it hasn't been initialized yet
kpan 0:693f2f97c8c1 35 erase_sector(is_written);
kpan 0:693f2f97c8c1 36 int zero = 0;
kpan 0:693f2f97c8c1 37 program_flash(is_written, (char*)&zero, sizeof(int));
kpan 0:693f2f97c8c1 38 write_vc();
kpan 0:693f2f97c8c1 39 } else {
kpan 0:693f2f97c8c1 40 read_vc();
kpan 0:693f2f97c8c1 41 }
kpan 0:693f2f97c8c1 42 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 43 print_memory();
kpan 0:693f2f97c8c1 44 #endif
kpan 0:693f2f97c8c1 45 }
kpan 0:693f2f97c8c1 46
kpan 0:693f2f97c8c1 47 void Counter::write_vc()
kpan 0:693f2f97c8c1 48 {
kpan 0:693f2f97c8c1 49 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 50 debug_out.printf("Writing to Flash\r\n");
kpan 0:693f2f97c8c1 51 #endif
kpan 0:693f2f97c8c1 52 erase_sector(address);
kpan 0:693f2f97c8c1 53 int zero = 0;
kpan 0:693f2f97c8c1 54 program_flash(is_written, (char*)&zero, sizeof(int));
kpan 0:693f2f97c8c1 55 program_flash(address, (char*)&vc, sizeof(int) * PARTY_LIMIT);
kpan 0:693f2f97c8c1 56 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 57 print_memory();
kpan 0:693f2f97c8c1 58 #endif
kpan 0:693f2f97c8c1 59 }
kpan 0:693f2f97c8c1 60
kpan 0:693f2f97c8c1 61 void Counter::read_vc()
kpan 0:693f2f97c8c1 62 {
kpan 0:693f2f97c8c1 63 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 64 debug_out.printf("Reading from Flash\r\n");
kpan 0:693f2f97c8c1 65 #endif
kpan 0:693f2f97c8c1 66 for(int i=0; i<PARTY_LIMIT; i++) {
kpan 0:693f2f97c8c1 67 vc[i] = *((int*)address+i);
kpan 0:693f2f97c8c1 68 }
kpan 0:693f2f97c8c1 69 }
kpan 0:693f2f97c8c1 70
kpan 0:693f2f97c8c1 71 void Counter::increment(int party_id)
kpan 0:693f2f97c8c1 72 {
kpan 0:693f2f97c8c1 73 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 74 debug_out.printf("Incrementing %d\r\n", party_id);
kpan 0:693f2f97c8c1 75 #endif
kpan 0:693f2f97c8c1 76 vc[party_id]++;
kpan 0:693f2f97c8c1 77 write_vc();
kpan 0:693f2f97c8c1 78 }