Ben Gordon
/
ELEC351_GroupA_alternate
temp
SDReader.cpp@0:2a4af0cb6e8d, 2018-12-06 (annotated)
- Committer:
- BenRJG
- Date:
- Thu Dec 06 15:38:09 2018 +0000
- Revision:
- 0:2a4af0cb6e8d
Imported Code from Kiel; Added button functionality; Added set DateTime Functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BenRJG | 0:2a4af0cb6e8d | 1 | #include "SDReader.hpp" |
BenRJG | 0:2a4af0cb6e8d | 2 | #include "Button.hpp" |
BenRJG | 0:2a4af0cb6e8d | 3 | |
BenRJG | 0:2a4af0cb6e8d | 4 | // Thread for saving DATA to the SD card |
BenRJG | 0:2a4af0cb6e8d | 5 | void SDThread(void) |
BenRJG | 0:2a4af0cb6e8d | 6 | { |
BenRJG | 0:2a4af0cb6e8d | 7 | // Create object SD of the SDReader class with set SPI pins |
BenRJG | 0:2a4af0cb6e8d | 8 | SDReader SD(PB_5, D12, D13, D10); // mosi, miso, sclk, cs |
BenRJG | 0:2a4af0cb6e8d | 9 | |
BenRJG | 0:2a4af0cb6e8d | 10 | // Create switch to dismount the SD card |
BenRJG | 0:2a4af0cb6e8d | 11 | BUTTON onBoardSwitch(USER_BUTTON); |
BenRJG | 0:2a4af0cb6e8d | 12 | |
BenRJG | 0:2a4af0cb6e8d | 13 | // Create variable to store state of SD card |
BenRJG | 0:2a4af0cb6e8d | 14 | enum State {MOUNTED, UNMOUNTED, MOUNTING, UNMOUNTING}; |
BenRJG | 0:2a4af0cb6e8d | 15 | State Mounted = MOUNTING; |
BenRJG | 0:2a4af0cb6e8d | 16 | |
BenRJG | 0:2a4af0cb6e8d | 17 | while(1) |
BenRJG | 0:2a4af0cb6e8d | 18 | { |
BenRJG | 0:2a4af0cb6e8d | 19 | // FSM for each state of SD card |
BenRJG | 0:2a4af0cb6e8d | 20 | switch(Mounted) |
BenRJG | 0:2a4af0cb6e8d | 21 | { |
BenRJG | 0:2a4af0cb6e8d | 22 | case UNMOUNTED : |
BenRJG | 0:2a4af0cb6e8d | 23 | wait_ms(100); |
BenRJG | 0:2a4af0cb6e8d | 24 | if(onBoardSwitch.poll() == 1) |
BenRJG | 0:2a4af0cb6e8d | 25 | { |
BenRJG | 0:2a4af0cb6e8d | 26 | Mounted = MOUNTING; |
BenRJG | 0:2a4af0cb6e8d | 27 | } |
BenRJG | 0:2a4af0cb6e8d | 28 | break; |
BenRJG | 0:2a4af0cb6e8d | 29 | |
BenRJG | 0:2a4af0cb6e8d | 30 | case MOUNTED : |
BenRJG | 0:2a4af0cb6e8d | 31 | //SD.UPLOAD(1.404f,0.303f,0.124f); |
BenRJG | 0:2a4af0cb6e8d | 32 | wait_ms(100); |
BenRJG | 0:2a4af0cb6e8d | 33 | if(onBoardSwitch.poll() == 1) |
BenRJG | 0:2a4af0cb6e8d | 34 | { |
BenRJG | 0:2a4af0cb6e8d | 35 | Mounted = UNMOUNTING; |
BenRJG | 0:2a4af0cb6e8d | 36 | } |
BenRJG | 0:2a4af0cb6e8d | 37 | break; |
BenRJG | 0:2a4af0cb6e8d | 38 | |
BenRJG | 0:2a4af0cb6e8d | 39 | case UNMOUNTING : |
BenRJG | 0:2a4af0cb6e8d | 40 | SD.unmount(); |
BenRJG | 0:2a4af0cb6e8d | 41 | Terminal_THREAD.signal_set(Signal_UnMount); |
BenRJG | 0:2a4af0cb6e8d | 42 | Mounted = UNMOUNTED; |
BenRJG | 0:2a4af0cb6e8d | 43 | break; |
BenRJG | 0:2a4af0cb6e8d | 44 | |
BenRJG | 0:2a4af0cb6e8d | 45 | case MOUNTING : |
BenRJG | 0:2a4af0cb6e8d | 46 | if(SD.INIT() == 0){ |
BenRJG | 0:2a4af0cb6e8d | 47 | Mounted = UNMOUNTED; |
BenRJG | 0:2a4af0cb6e8d | 48 | Terminal_THREAD.signal_set(Signal_UnMount); |
BenRJG | 0:2a4af0cb6e8d | 49 | } else { |
BenRJG | 0:2a4af0cb6e8d | 50 | Mounted = MOUNTED; |
BenRJG | 0:2a4af0cb6e8d | 51 | } |
BenRJG | 0:2a4af0cb6e8d | 52 | break; |
BenRJG | 0:2a4af0cb6e8d | 53 | } |
BenRJG | 0:2a4af0cb6e8d | 54 | } |
BenRJG | 0:2a4af0cb6e8d | 55 | } |
BenRJG | 0:2a4af0cb6e8d | 56 | |
BenRJG | 0:2a4af0cb6e8d | 57 | BYTE SDReader::INIT(void) |
BenRJG | 0:2a4af0cb6e8d | 58 | { |
BenRJG | 0:2a4af0cb6e8d | 59 | BYTE Mount; |
BenRJG | 0:2a4af0cb6e8d | 60 | |
BenRJG | 0:2a4af0cb6e8d | 61 | // call the SDBlockDevice instance initialisation method. |
BenRJG | 0:2a4af0cb6e8d | 62 | if(sd.init() != 0){ |
BenRJG | 0:2a4af0cb6e8d | 63 | Mount = 0; |
BenRJG | 0:2a4af0cb6e8d | 64 | } else { |
BenRJG | 0:2a4af0cb6e8d | 65 | Mount = 1; |
BenRJG | 0:2a4af0cb6e8d | 66 | } |
BenRJG | 0:2a4af0cb6e8d | 67 | |
BenRJG | 0:2a4af0cb6e8d | 68 | if(Mount == 0){ |
BenRJG | 0:2a4af0cb6e8d | 69 | return Mount; |
BenRJG | 0:2a4af0cb6e8d | 70 | } else { |
BenRJG | 0:2a4af0cb6e8d | 71 | //Remove existing file |
BenRJG | 0:2a4af0cb6e8d | 72 | remove( "/sd/ELEC351.csv" ); |
BenRJG | 0:2a4af0cb6e8d | 73 | |
BenRJG | 0:2a4af0cb6e8d | 74 | FILE* fp = fopen("/sd/ELEC351.csv","a+"); |
BenRJG | 0:2a4af0cb6e8d | 75 | //Check file handle (stream) |
BenRJG | 0:2a4af0cb6e8d | 76 | if (fp == NULL) { |
BenRJG | 0:2a4af0cb6e8d | 77 | return Mount; |
BenRJG | 0:2a4af0cb6e8d | 78 | } |
BenRJG | 0:2a4af0cb6e8d | 79 | |
BenRJG | 0:2a4af0cb6e8d | 80 | //Put some text in the file... |
BenRJG | 0:2a4af0cb6e8d | 81 | fprintf(fp, " TIME STAMP, light, temperature, pressure\n"); |
BenRJG | 0:2a4af0cb6e8d | 82 | |
BenRJG | 0:2a4af0cb6e8d | 83 | //Close the file |
BenRJG | 0:2a4af0cb6e8d | 84 | fclose(fp); |
BenRJG | 0:2a4af0cb6e8d | 85 | |
BenRJG | 0:2a4af0cb6e8d | 86 | return Mount; |
BenRJG | 0:2a4af0cb6e8d | 87 | } |
BenRJG | 0:2a4af0cb6e8d | 88 | } |
BenRJG | 0:2a4af0cb6e8d | 89 | |
BenRJG | 0:2a4af0cb6e8d | 90 | void SDReader::UPLOAD(FLOAT_32 LIGHT, FLOAT_32 TEMP, FLOAT_32 PRESS) |
BenRJG | 0:2a4af0cb6e8d | 91 | { |
BenRJG | 0:2a4af0cb6e8d | 92 | FILE* fp = fopen("/sd/ELEC351.csv","a+"); |
BenRJG | 0:2a4af0cb6e8d | 93 | //Check file handle (stream) |
BenRJG | 0:2a4af0cb6e8d | 94 | if (fp == NULL) { |
BenRJG | 0:2a4af0cb6e8d | 95 | return; |
BenRJG | 0:2a4af0cb6e8d | 96 | } |
BenRJG | 0:2a4af0cb6e8d | 97 | |
BenRJG | 0:2a4af0cb6e8d | 98 | //Put some text in the file... |
BenRJG | 0:2a4af0cb6e8d | 99 | fprintf(fp, " TIME STAMP, %1.4f, %1.4f, %1.4f\n", LIGHT, TEMP, PRESS); |
BenRJG | 0:2a4af0cb6e8d | 100 | |
BenRJG | 0:2a4af0cb6e8d | 101 | //Close the file |
BenRJG | 0:2a4af0cb6e8d | 102 | fclose(fp); |
BenRJG | 0:2a4af0cb6e8d | 103 | } |
BenRJG | 0:2a4af0cb6e8d | 104 | |
BenRJG | 0:2a4af0cb6e8d | 105 | BYTE* SDReader::DOWNLOAD(void) |
BenRJG | 0:2a4af0cb6e8d | 106 | { |
BenRJG | 0:2a4af0cb6e8d | 107 | FILE* fp = fopen("/sd/ELEC351.csv","r"); |
BenRJG | 0:2a4af0cb6e8d | 108 | if (fp == NULL) { |
BenRJG | 0:2a4af0cb6e8d | 109 | |
BenRJG | 0:2a4af0cb6e8d | 110 | } |
BenRJG | 0:2a4af0cb6e8d | 111 | |
BenRJG | 0:2a4af0cb6e8d | 112 | //Read back all strings |
BenRJG | 0:2a4af0cb6e8d | 113 | BYTE* s1 = new BYTE; |
BenRJG | 0:2a4af0cb6e8d | 114 | |
BenRJG | 0:2a4af0cb6e8d | 115 | //while (fscanf(fp, "%s", s1) == 1) |
BenRJG | 0:2a4af0cb6e8d | 116 | //{ |
BenRJG | 0:2a4af0cb6e8d | 117 | // |
BenRJG | 0:2a4af0cb6e8d | 118 | //} |
BenRJG | 0:2a4af0cb6e8d | 119 | |
BenRJG | 0:2a4af0cb6e8d | 120 | fgets(s1, sizeof(s1), fp); |
BenRJG | 0:2a4af0cb6e8d | 121 | |
BenRJG | 0:2a4af0cb6e8d | 122 | //Close File |
BenRJG | 0:2a4af0cb6e8d | 123 | fclose(fp); |
BenRJG | 0:2a4af0cb6e8d | 124 | |
BenRJG | 0:2a4af0cb6e8d | 125 | return s1; |
BenRJG | 0:2a4af0cb6e8d | 126 | } |
BenRJG | 0:2a4af0cb6e8d | 127 | |
BenRJG | 0:2a4af0cb6e8d | 128 | void SDReader::unmount(void) |
BenRJG | 0:2a4af0cb6e8d | 129 | { |
BenRJG | 0:2a4af0cb6e8d | 130 | sd.deinit(); |
BenRJG | 0:2a4af0cb6e8d | 131 | return; |
BenRJG | 0:2a4af0cb6e8d | 132 | } |
BenRJG | 0:2a4af0cb6e8d | 133 |