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:
Fri Nov 16 14:52:41 2018 +0000
Revision:
4:bb12605080d5
Parent:
3:c15c22484205
Added decrement and clear functions

Who changed what in which revision?

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