-

Committer:
rogeranderson
Date:
Mon Nov 19 19:00:30 2018 +0000
Revision:
6:9f8adf722fc3
Parent:
5:bb12605080d5
enable debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kpan 0:693f2f97c8c1 1 #include "Counter.h"
rogeranderson 6:9f8adf722fc3 2 #include "mbed.h"
kpan 0:693f2f97c8c1 3
kpan 2:f02ac448ecd3 4 //#define COUNTER_DEBUG
kpan 2:f02ac448ecd3 5
rogeranderson 6:9f8adf722fc3 6
kpan 0:693f2f97c8c1 7 Serial debug_out(USBTX, USBRX);
rogeranderson 6:9f8adf722fc3 8
kpan 0:693f2f97c8c1 9 Counter::Counter()
kpan 0:693f2f97c8c1 10 {
kpan 2:f02ac448ecd3 11 is_new_count = flash_size() - SECTOR_SIZE;
kpan 0:693f2f97c8c1 12 address = (int)((int*)(flash_size() - SECTOR_SIZE) + 1);
kpan 0:693f2f97c8c1 13 #ifdef COUNTER_DEBUG
kpan 0:693f2f97c8c1 14 debug_out.printf("Constructing Object...\r\n");
kpan 0:693f2f97c8c1 15 #endif
kpan 5:bb12605080d5 16 memset(&count, 0, MAX_LEN);
kpan 0:693f2f97c8c1 17 this->init();
kpan 0:693f2f97c8c1 18 }
kpan 0:693f2f97c8c1 19
rogeranderson 6:9f8adf722fc3 20
kpan 0:693f2f97c8c1 21 void Counter::print_memory()
kpan 0:693f2f97c8c1 22 {
kpan 0:693f2f97c8c1 23 int *ptr = (int*) address;
rogeranderson 6:9f8adf722fc3 24 printf("PartyID\tVoteCt\tMemCt\tAddress\r\n");
kpan 1:e8eeddee2959 25 for(int i=0; i<MAX_LEN; i++) {
rogeranderson 6:9f8adf722fc3 26 printf("%dID\t%d\t%d\t%p\r\n", i, count[i], ptr[i], (ptr+i));
kpan 0:693f2f97c8c1 27 }
kpan 0:693f2f97c8c1 28 }
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 4:c15c22484205 79 }
kpan 4:c15c22484205 80
kpan 4:c15c22484205 81 void Counter::decrement(int party_id)
kpan 4:c15c22484205 82 {
kpan 4:c15c22484205 83 #ifdef COUNTER_DEBUG
kpan 4:c15c22484205 84 debug_out.printf("Decrementing %d\r\n", party_id);
kpan 4:c15c22484205 85 #endif
kpan 4:c15c22484205 86 count[party_id]--;
kpan 4:c15c22484205 87 memwrite();
kpan 4:c15c22484205 88 }
kpan 4:c15c22484205 89
kpan 4:c15c22484205 90 void Counter::clear(){
kpan 4:c15c22484205 91 #ifdef COUNTER_DEBUG
kpan 4:c15c22484205 92 debug_out.printf("Clearing stored values\r\n");
kpan 4:c15c22484205 93 #endif
kpan 5:bb12605080d5 94 memset(&count, 0, MAX_LEN);
kpan 0:693f2f97c8c1 95 }