LoadCell_STM32_SDRAM_v1
Dependencies: BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed
Fork of LoadCell_STM32 by
functions.cpp@6:641b171407c9, 2018-03-08 (annotated)
- Committer:
- tapiov
- Date:
- Thu Mar 08 20:14:53 2018 +0000
- Revision:
- 6:641b171407c9
- Parent:
- 5:f8d3bcb187de
First commit to publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tapiov | 1:c3c61d08f31b | 1 | // LoadCell_STM32_RAM v2 functions |
tapiov | 1:c3c61d08f31b | 2 | // (C) Tapio Valli 2018-02-17 |
tapiov | 1:c3c61d08f31b | 3 | |
tapiov | 1:c3c61d08f31b | 4 | #include "main.h" |
tapiov | 1:c3c61d08f31b | 5 | |
tapiov | 6:641b171407c9 | 6 | int err; |
tapiov | 1:c3c61d08f31b | 7 | |
tapiov | 1:c3c61d08f31b | 8 | void initArray(Array *a, size_t initialSize) { |
tapiov | 5:f8d3bcb187de | 9 | a->array = (uint16_t *)malloc(initialSize * sizeof(uint16_t)); |
tapiov | 1:c3c61d08f31b | 10 | a->used = 0; |
tapiov | 1:c3c61d08f31b | 11 | a->size = initialSize; |
tapiov | 1:c3c61d08f31b | 12 | } |
tapiov | 1:c3c61d08f31b | 13 | |
tapiov | 5:f8d3bcb187de | 14 | void insertArray(Array *a, uint32_t newsize) { |
tapiov | 3:22b1719c2b92 | 15 | a->size = newsize; |
tapiov | 5:f8d3bcb187de | 16 | a->array = (uint16_t *)realloc(a->array, a->size * sizeof(uint16_t)); |
tapiov | 3:22b1719c2b92 | 17 | a->used = 0; |
tapiov | 3:22b1719c2b92 | 18 | } |
tapiov | 3:22b1719c2b92 | 19 | |
tapiov | 1:c3c61d08f31b | 20 | void freeArray(Array *a) { |
tapiov | 1:c3c61d08f31b | 21 | free(a->array); |
tapiov | 1:c3c61d08f31b | 22 | a->array = NULL; |
tapiov | 1:c3c61d08f31b | 23 | a->used = a->size = 0; |
tapiov | 1:c3c61d08f31b | 24 | } |
tapiov | 1:c3c61d08f31b | 25 | |
tapiov | 1:c3c61d08f31b | 26 | size_t string_parser(char *input, char ***word_array) |
tapiov | 1:c3c61d08f31b | 27 | { |
tapiov | 1:c3c61d08f31b | 28 | size_t n = 0; |
tapiov | 1:c3c61d08f31b | 29 | const char *p = input; |
tapiov | 1:c3c61d08f31b | 30 | |
tapiov | 1:c3c61d08f31b | 31 | while ( *p ) |
tapiov | 1:c3c61d08f31b | 32 | { |
tapiov | 1:c3c61d08f31b | 33 | while ( isspace( ( unsigned char )*p ) ) ++p; |
tapiov | 1:c3c61d08f31b | 34 | n += *p != '\0'; |
tapiov | 1:c3c61d08f31b | 35 | while ( *p && !isspace( ( unsigned char )*p ) ) ++p; |
tapiov | 1:c3c61d08f31b | 36 | } |
tapiov | 1:c3c61d08f31b | 37 | |
tapiov | 1:c3c61d08f31b | 38 | if ( n ) |
tapiov | 1:c3c61d08f31b | 39 | { |
tapiov | 1:c3c61d08f31b | 40 | size_t i = 0; |
tapiov | 1:c3c61d08f31b | 41 | |
tapiov | 1:c3c61d08f31b | 42 | *word_array = (char**)malloc( n * sizeof( char * ) ); |
tapiov | 1:c3c61d08f31b | 43 | |
tapiov | 1:c3c61d08f31b | 44 | p = input; |
tapiov | 1:c3c61d08f31b | 45 | |
tapiov | 1:c3c61d08f31b | 46 | while ( *p ) |
tapiov | 1:c3c61d08f31b | 47 | { |
tapiov | 1:c3c61d08f31b | 48 | while ( isspace( ( unsigned char )*p ) ) ++p; |
tapiov | 1:c3c61d08f31b | 49 | if ( *p ) |
tapiov | 1:c3c61d08f31b | 50 | { |
tapiov | 1:c3c61d08f31b | 51 | const char *q = p; |
tapiov | 1:c3c61d08f31b | 52 | while ( *p && !isspace( ( unsigned char )*p ) ) ++p; |
tapiov | 1:c3c61d08f31b | 53 | |
tapiov | 1:c3c61d08f31b | 54 | size_t length = p - q; |
tapiov | 1:c3c61d08f31b | 55 | |
tapiov | 1:c3c61d08f31b | 56 | ( *word_array )[i] = ( char * )malloc( length + 1 ); |
tapiov | 1:c3c61d08f31b | 57 | |
tapiov | 1:c3c61d08f31b | 58 | strncpy( ( *word_array )[i], q, length ); |
tapiov | 1:c3c61d08f31b | 59 | ( *word_array )[i][length] = '\0'; |
tapiov | 1:c3c61d08f31b | 60 | |
tapiov | 1:c3c61d08f31b | 61 | ++i; |
tapiov | 1:c3c61d08f31b | 62 | } |
tapiov | 1:c3c61d08f31b | 63 | } |
tapiov | 1:c3c61d08f31b | 64 | } |
tapiov | 1:c3c61d08f31b | 65 | |
tapiov | 1:c3c61d08f31b | 66 | return n; |
tapiov | 1:c3c61d08f31b | 67 | } |
tapiov | 1:c3c61d08f31b | 68 | |
tapiov | 1:c3c61d08f31b | 69 | void PlotData(uint32_t XCoordinate,uint32_t YCoordinate) |
tapiov | 1:c3c61d08f31b | 70 | { |
tapiov | 1:c3c61d08f31b | 71 | // Plot at x,y |
tapiov | 6:641b171407c9 | 72 | lcd.DrawHLine(XCoordinate,YCoordinate,1); |
tapiov | 1:c3c61d08f31b | 73 | } |
tapiov | 1:c3c61d08f31b | 74 | |
tapiov | 1:c3c61d08f31b | 75 | void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor) |
tapiov | 1:c3c61d08f31b | 76 | { |
tapiov | 1:c3c61d08f31b | 77 | |
tapiov | 1:c3c61d08f31b | 78 | // #define LCD_COLOR_BLUE ((uint32_t)0xFF0000FF) |
tapiov | 1:c3c61d08f31b | 79 | // #define LCD_COLOR_GREEN ((uint32_t)0xFF00FF00) |
tapiov | 1:c3c61d08f31b | 80 | // #define LCD_COLOR_RED ((uint32_t)0xFFFF0000) |
tapiov | 1:c3c61d08f31b | 81 | // #define LCD_COLOR_CYAN ((uint32_t)0xFF00FFFF) |
tapiov | 1:c3c61d08f31b | 82 | // #define LCD_COLOR_MAGENTA ((uint32_t)0xFFFF00FF) |
tapiov | 1:c3c61d08f31b | 83 | // #define LCD_COLOR_YELLOW ((uint32_t)0xFFFFFF00) |
tapiov | 1:c3c61d08f31b | 84 | // #define LCD_COLOR_LIGHTBLUE ((uint32_t)0xFF8080FF) |
tapiov | 1:c3c61d08f31b | 85 | // #define LCD_COLOR_LIGHTGREEN ((uint32_t)0xFF80FF80) |
tapiov | 1:c3c61d08f31b | 86 | // #define LCD_COLOR_LIGHTRED ((uint32_t)0xFFFF8080) |
tapiov | 1:c3c61d08f31b | 87 | // #define LCD_COLOR_LIGHTCYAN ((uint32_t)0xFF80FFFF) |
tapiov | 1:c3c61d08f31b | 88 | // #define LCD_COLOR_LIGHTMAGENTA ((uint32_t)0xFFFF80FF) |
tapiov | 1:c3c61d08f31b | 89 | // #define LCD_COLOR_LIGHTYELLOW ((uint32_t)0xFFFFFF80) |
tapiov | 1:c3c61d08f31b | 90 | // #define LCD_COLOR_DARKBLUE ((uint32_t)0xFF000080) |
tapiov | 1:c3c61d08f31b | 91 | // #define LCD_COLOR_DARKGREEN ((uint32_t)0xFF008000) |
tapiov | 1:c3c61d08f31b | 92 | // #define LCD_COLOR_DARKRED ((uint32_t)0xFF800000) |
tapiov | 1:c3c61d08f31b | 93 | // #define LCD_COLOR_DARKCYAN ((uint32_t)0xFF008080) |
tapiov | 1:c3c61d08f31b | 94 | // #define LCD_COLOR_DARKMAGENTA ((uint32_t)0xFF800080) |
tapiov | 1:c3c61d08f31b | 95 | // #define LCD_COLOR_DARKYELLOW ((uint32_t)0xFF808000) |
tapiov | 1:c3c61d08f31b | 96 | // #define LCD_COLOR_WHITE ((uint32_t)0xFFFFFFFF) |
tapiov | 1:c3c61d08f31b | 97 | // #define LCD_COLOR_LIGHTGRAY ((uint32_t)0xFFD3D3D3) |
tapiov | 1:c3c61d08f31b | 98 | // #define LCD_COLOR_GRAY ((uint32_t)0xFF808080) |
tapiov | 1:c3c61d08f31b | 99 | // #define LCD_COLOR_DARKGRAY ((uint32_t)0xFF404040) |
tapiov | 1:c3c61d08f31b | 100 | // #define LCD_COLOR_BLACK ((uint32_t)0xFF000000) |
tapiov | 1:c3c61d08f31b | 101 | // #define LCD_COLOR_BROWN ((uint32_t)0xFFA52A2A) |
tapiov | 1:c3c61d08f31b | 102 | // #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500) |
tapiov | 1:c3c61d08f31b | 103 | // #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000) |
tapiov | 1:c3c61d08f31b | 104 | |
tapiov | 6:641b171407c9 | 105 | lcd.Clear(BackGroundColor); |
tapiov | 6:641b171407c9 | 106 | lcd.SetBackColor(BackGroundColor); |
tapiov | 6:641b171407c9 | 107 | lcd.SetTextColor(ForeGroundColor); |
tapiov | 6:641b171407c9 | 108 | lcd.SetFont(&Font20); |
tapiov | 1:c3c61d08f31b | 109 | } |
tapiov | 1:c3c61d08f31b | 110 | |
tapiov | 1:c3c61d08f31b | 111 | void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode) |
tapiov | 1:c3c61d08f31b | 112 | { |
tapiov | 1:c3c61d08f31b | 113 | char IntStr[50]; |
tapiov | 1:c3c61d08f31b | 114 | |
tapiov | 1:c3c61d08f31b | 115 | // InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20); |
tapiov | 1:c3c61d08f31b | 116 | |
tapiov | 6:641b171407c9 | 117 | lcd.ClearStringLine(Line); |
tapiov | 1:c3c61d08f31b | 118 | snprintf(IntStr,50,Str); |
tapiov | 6:641b171407c9 | 119 | lcd.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode); |
tapiov | 1:c3c61d08f31b | 120 | } |
tapiov | 1:c3c61d08f31b | 121 | |
tapiov | 1:c3c61d08f31b | 122 | void CountDown(uint32_t millisecs) |
tapiov | 1:c3c61d08f31b | 123 | { |
tapiov | 1:c3c61d08f31b | 124 | InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); |
tapiov | 1:c3c61d08f31b | 125 | |
tapiov | 1:c3c61d08f31b | 126 | LCDWrite(5," ",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 127 | wait_ms(1); |
tapiov | 1:c3c61d08f31b | 128 | |
tapiov | 1:c3c61d08f31b | 129 | LCDWrite(5,"Starting in 3... ",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 130 | wait_ms(millisecs); |
tapiov | 1:c3c61d08f31b | 131 | |
tapiov | 1:c3c61d08f31b | 132 | LCDWrite(5,"Starting in 2... ",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 133 | wait_ms(millisecs); |
tapiov | 1:c3c61d08f31b | 134 | |
tapiov | 1:c3c61d08f31b | 135 | LCDWrite(5,"Starting in 1... ",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 136 | wait_ms(millisecs); |
tapiov | 1:c3c61d08f31b | 137 | |
tapiov | 1:c3c61d08f31b | 138 | InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); |
tapiov | 1:c3c61d08f31b | 139 | LCDWrite(5,"GO!",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 140 | } |
tapiov | 1:c3c61d08f31b | 141 | |
tapiov | 1:c3c61d08f31b | 142 | void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us) |
tapiov | 1:c3c61d08f31b | 143 | { |
tapiov | 1:c3c61d08f31b | 144 | AnalogIn ain(A0); |
tapiov | 1:c3c61d08f31b | 145 | uint32_t i; |
tapiov | 1:c3c61d08f31b | 146 | |
tapiov | 1:c3c61d08f31b | 147 | // Measure NoOfPoints values (f.ex. 19200) |
tapiov | 1:c3c61d08f31b | 148 | for(i=0;i<NoOfPoints;i++) { |
tapiov | 5:f8d3bcb187de | 149 | Data->array[i]=(uint16_t)(ain.read_u16()); |
tapiov | 1:c3c61d08f31b | 150 | wait_us(Period_us); |
tapiov | 1:c3c61d08f31b | 151 | } |
tapiov | 1:c3c61d08f31b | 152 | |
tapiov | 1:c3c61d08f31b | 153 | InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); |
tapiov | 1:c3c61d08f31b | 154 | LCDWrite(5,"DONE!",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 155 | |
tapiov | 6:641b171407c9 | 156 | pc.printf("Sampling done.\r\n",i); |
tapiov | 1:c3c61d08f31b | 157 | } |
tapiov | 1:c3c61d08f31b | 158 | |
tapiov | 1:c3c61d08f31b | 159 | void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) { |
tapiov | 1:c3c61d08f31b | 160 | |
tapiov | 1:c3c61d08f31b | 161 | uint32_t i1,i2; |
tapiov | 1:c3c61d08f31b | 162 | |
tapiov | 1:c3c61d08f31b | 163 | uint32_t BufferSum,BufferAvg; |
tapiov | 1:c3c61d08f31b | 164 | uint32_t XCoord,YCoord; |
tapiov | 1:c3c61d08f31b | 165 | char MyStr[50]; |
tapiov | 1:c3c61d08f31b | 166 | |
tapiov | 6:641b171407c9 | 167 | pc.printf("Averaging... \r\n"); |
tapiov | 1:c3c61d08f31b | 168 | InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED); |
tapiov | 1:c3c61d08f31b | 169 | |
tapiov | 1:c3c61d08f31b | 170 | for(i1=0;i1<NoOfPoints;i1++) { |
tapiov | 1:c3c61d08f31b | 171 | BufferSum=0; |
tapiov | 1:c3c61d08f31b | 172 | |
tapiov | 1:c3c61d08f31b | 173 | // Read AvgSize samples |
tapiov | 1:c3c61d08f31b | 174 | for(i2=i1;i2<i1+AvgSize;i2++) { |
tapiov | 5:f8d3bcb187de | 175 | BufferSum=BufferSum+(uint32_t)Data->array[i2]; |
tapiov | 1:c3c61d08f31b | 176 | } |
tapiov | 1:c3c61d08f31b | 177 | |
tapiov | 1:c3c61d08f31b | 178 | BufferAvg=BufferSum/AvgSize; |
tapiov | 1:c3c61d08f31b | 179 | |
tapiov | 1:c3c61d08f31b | 180 | // Calculate two coords and plot |
tapiov | 1:c3c61d08f31b | 181 | XCoord=((i1*480.0)/NoOfPoints); |
tapiov | 1:c3c61d08f31b | 182 | YCoord=(272.0*(BufferAvg/65536.0)); |
tapiov | 1:c3c61d08f31b | 183 | |
tapiov | 1:c3c61d08f31b | 184 | PlotData(XCoord,YCoord); |
tapiov | 1:c3c61d08f31b | 185 | } |
tapiov | 1:c3c61d08f31b | 186 | |
tapiov | 6:641b171407c9 | 187 | pc.printf("Averaging done, Points = %u Avg = %u \r\n", i1,AvgSize); |
tapiov | 1:c3c61d08f31b | 188 | |
tapiov | 1:c3c61d08f31b | 189 | LCDWrite(0,"",CENTER_MODE); |
tapiov | 1:c3c61d08f31b | 190 | snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize); |
tapiov | 1:c3c61d08f31b | 191 | LCDWrite(0,MyStr,RIGHT_MODE); |
tapiov | 1:c3c61d08f31b | 192 | } |
tapiov | 6:641b171407c9 | 193 | |
tapiov | 6:641b171407c9 | 194 | void BlockDeviceErase() { |
tapiov | 6:641b171407c9 | 195 | pc.printf("Initializing the block device... "); |
tapiov | 6:641b171407c9 | 196 | fflush(stdout); |
tapiov | 6:641b171407c9 | 197 | int err = bd.init(); |
tapiov | 6:641b171407c9 | 198 | pc.printf("%s\n", (err ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 199 | if (err) { |
tapiov | 6:641b171407c9 | 200 | error("error: %s (%d)\n", strerror(-err), err); |
tapiov | 6:641b171407c9 | 201 | } |
tapiov | 6:641b171407c9 | 202 | |
tapiov | 6:641b171407c9 | 203 | pc.printf("Erasing the block device... "); |
tapiov | 6:641b171407c9 | 204 | fflush(stdout); |
tapiov | 6:641b171407c9 | 205 | err = bd.erase(0, bd.size()); |
tapiov | 6:641b171407c9 | 206 | printf("%s\n", (err ? "Fail :(" : "OK")); |
tapiov | 6:641b171407c9 | 207 | if (err) { |
tapiov | 6:641b171407c9 | 208 | error("error: %s (%d)\n", strerror(-err), err); |
tapiov | 6:641b171407c9 | 209 | } |
tapiov | 6:641b171407c9 | 210 | pc.printf("Device size is %d\n",bd.size()); |
tapiov | 6:641b171407c9 | 211 | |
tapiov | 6:641b171407c9 | 212 | printf("Deinitializing the block device... "); |
tapiov | 6:641b171407c9 | 213 | fflush(stdout); |
tapiov | 6:641b171407c9 | 214 | err = bd.deinit(); |
tapiov | 6:641b171407c9 | 215 | printf("%s\n", (err ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 216 | if (err) { |
tapiov | 6:641b171407c9 | 217 | error("error: %s (%d)\n", strerror(-err), err); |
tapiov | 6:641b171407c9 | 218 | } |
tapiov | 6:641b171407c9 | 219 | } |
tapiov | 6:641b171407c9 | 220 | |
tapiov | 6:641b171407c9 | 221 | void MountFS() { |
tapiov | 6:641b171407c9 | 222 | pc.printf("Mounting the filesystem... "); |
tapiov | 6:641b171407c9 | 223 | fflush(stdout); |
tapiov | 6:641b171407c9 | 224 | int err = fs.mount(&bd); |
tapiov | 6:641b171407c9 | 225 | pc.printf("%s\n", (err ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 226 | if (err) { |
tapiov | 6:641b171407c9 | 227 | // Reformat if we can't mount the filesystem |
tapiov | 6:641b171407c9 | 228 | // this should only happen on the first boot |
tapiov | 6:641b171407c9 | 229 | pc.printf("No filesystem found, formatting... "); |
tapiov | 6:641b171407c9 | 230 | fflush(stdout); |
tapiov | 6:641b171407c9 | 231 | err = fs.reformat(&bd,bd.get_program_size()); |
tapiov | 6:641b171407c9 | 232 | pc.printf("%s\n", (err ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 233 | if (err) { |
tapiov | 6:641b171407c9 | 234 | error("error: %s (%d)\n", strerror(-err), err); |
tapiov | 6:641b171407c9 | 235 | } |
tapiov | 6:641b171407c9 | 236 | } |
tapiov | 6:641b171407c9 | 237 | } |
tapiov | 6:641b171407c9 | 238 | |
tapiov | 6:641b171407c9 | 239 | void WriteData2FS(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) { |
tapiov | 6:641b171407c9 | 240 | |
tapiov | 6:641b171407c9 | 241 | char FName[30]; |
tapiov | 6:641b171407c9 | 242 | uint32_t idx; |
tapiov | 6:641b171407c9 | 243 | |
tapiov | 6:641b171407c9 | 244 | // Build the filename |
tapiov | 6:641b171407c9 | 245 | snprintf(FName,30,"/fs/meas%d.txt",FileNumber); |
tapiov | 6:641b171407c9 | 246 | |
tapiov | 6:641b171407c9 | 247 | // Open the data file |
tapiov | 6:641b171407c9 | 248 | pc.printf("Opening %s ... ",FName); |
tapiov | 6:641b171407c9 | 249 | fflush(stdout); |
tapiov | 6:641b171407c9 | 250 | FILE *f = fopen(FName, "r+"); |
tapiov | 6:641b171407c9 | 251 | |
tapiov | 6:641b171407c9 | 252 | pc.printf("%s\n", (!f ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 253 | if (!f) { |
tapiov | 6:641b171407c9 | 254 | // Create the data file if it doesn't exist |
tapiov | 6:641b171407c9 | 255 | pc.printf("No file found, creating a new file... "); |
tapiov | 6:641b171407c9 | 256 | fflush(stdout); |
tapiov | 6:641b171407c9 | 257 | f = fopen(FName, "w+"); |
tapiov | 6:641b171407c9 | 258 | pc.printf("%s\n", (!f ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 259 | if (!f) { |
tapiov | 6:641b171407c9 | 260 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 261 | return; |
tapiov | 6:641b171407c9 | 262 | } |
tapiov | 6:641b171407c9 | 263 | } |
tapiov | 6:641b171407c9 | 264 | |
tapiov | 6:641b171407c9 | 265 | // Write data points to file |
tapiov | 6:641b171407c9 | 266 | pc.printf("Writing data ... "); |
tapiov | 6:641b171407c9 | 267 | for (idx=0;idx<NoOfPoints;idx++) { |
tapiov | 6:641b171407c9 | 268 | err=fprintf(f,"%d\n",(uint16_t)Data->array[idx]); |
tapiov | 6:641b171407c9 | 269 | if (err < 0) { |
tapiov | 6:641b171407c9 | 270 | pc.printf("Fail\n"); |
tapiov | 6:641b171407c9 | 271 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 272 | return; |
tapiov | 6:641b171407c9 | 273 | |
tapiov | 6:641b171407c9 | 274 | } |
tapiov | 6:641b171407c9 | 275 | } |
tapiov | 6:641b171407c9 | 276 | pc.printf("\rWriting data done."); |
tapiov | 6:641b171407c9 | 277 | pc.printf("\r File size is %d",GetFileSize(f)); |
tapiov | 6:641b171407c9 | 278 | |
tapiov | 6:641b171407c9 | 279 | // Close the file which also flushes any cached writes |
tapiov | 6:641b171407c9 | 280 | pc.printf(" Closing %s ... ",FName); |
tapiov | 6:641b171407c9 | 281 | fflush(stdout); |
tapiov | 6:641b171407c9 | 282 | err = fclose(f); |
tapiov | 6:641b171407c9 | 283 | printf("%s\n", (err < 0 ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 284 | if (err < 0) { |
tapiov | 6:641b171407c9 | 285 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 286 | return; |
tapiov | 6:641b171407c9 | 287 | } |
tapiov | 6:641b171407c9 | 288 | } |
tapiov | 6:641b171407c9 | 289 | |
tapiov | 6:641b171407c9 | 290 | void ReadFile2Data(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) { |
tapiov | 6:641b171407c9 | 291 | |
tapiov | 6:641b171407c9 | 292 | char FName[30]; |
tapiov | 6:641b171407c9 | 293 | uint32_t idx; |
tapiov | 6:641b171407c9 | 294 | |
tapiov | 6:641b171407c9 | 295 | // Build the filename |
tapiov | 6:641b171407c9 | 296 | snprintf(FName,30,"/fs/meas%d.txt",FileNumber); |
tapiov | 6:641b171407c9 | 297 | |
tapiov | 6:641b171407c9 | 298 | // Open the data file for reading |
tapiov | 6:641b171407c9 | 299 | pc.printf("Opening %s ... ",FName); |
tapiov | 6:641b171407c9 | 300 | fflush(stdout); |
tapiov | 6:641b171407c9 | 301 | FILE *f = fopen(FName, "r"); |
tapiov | 6:641b171407c9 | 302 | |
tapiov | 6:641b171407c9 | 303 | pc.printf("%s\n", (!f ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 304 | if (!f) { |
tapiov | 6:641b171407c9 | 305 | // Print error and return |
tapiov | 6:641b171407c9 | 306 | pc.printf("File not found. Check name."); |
tapiov | 6:641b171407c9 | 307 | return; |
tapiov | 6:641b171407c9 | 308 | } |
tapiov | 6:641b171407c9 | 309 | |
tapiov | 6:641b171407c9 | 310 | pc.printf("Seeking file... "); |
tapiov | 6:641b171407c9 | 311 | fflush(stdout); |
tapiov | 6:641b171407c9 | 312 | err = fseek(f, 0, SEEK_SET); |
tapiov | 6:641b171407c9 | 313 | printf("%s\n", (err < 0 ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 314 | if (err < 0) { |
tapiov | 6:641b171407c9 | 315 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 316 | return; |
tapiov | 6:641b171407c9 | 317 | } |
tapiov | 6:641b171407c9 | 318 | |
tapiov | 6:641b171407c9 | 319 | // Go through and increment the numbers |
tapiov | 6:641b171407c9 | 320 | pc.printf("Reading data... "); |
tapiov | 6:641b171407c9 | 321 | for(idx=0;idx<NoOfPoints;idx++) { |
tapiov | 6:641b171407c9 | 322 | // Get current stream position |
tapiov | 6:641b171407c9 | 323 | long pos=ftell(f); |
tapiov | 6:641b171407c9 | 324 | |
tapiov | 6:641b171407c9 | 325 | // Parse out the number and put it in data array |
tapiov | 6:641b171407c9 | 326 | uint32_t number; |
tapiov | 6:641b171407c9 | 327 | fscanf(f,"%d",&number); |
tapiov | 6:641b171407c9 | 328 | Data->array[idx]=number; |
tapiov | 6:641b171407c9 | 329 | } |
tapiov | 6:641b171407c9 | 330 | |
tapiov | 6:641b171407c9 | 331 | pc.printf("\rReading data done. "); |
tapiov | 6:641b171407c9 | 332 | |
tapiov | 6:641b171407c9 | 333 | // Close the file which also flushes any cached writes |
tapiov | 6:641b171407c9 | 334 | pc.printf("Closing %s ... ",FName); |
tapiov | 6:641b171407c9 | 335 | fflush(stdout); |
tapiov | 6:641b171407c9 | 336 | err = fclose(f); |
tapiov | 6:641b171407c9 | 337 | pc.printf("%s\n", (err < 0 ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 338 | if (err < 0) { |
tapiov | 6:641b171407c9 | 339 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 340 | return; |
tapiov | 6:641b171407c9 | 341 | } |
tapiov | 6:641b171407c9 | 342 | } |
tapiov | 6:641b171407c9 | 343 | |
tapiov | 6:641b171407c9 | 344 | void DirListing() { |
tapiov | 6:641b171407c9 | 345 | |
tapiov | 6:641b171407c9 | 346 | // Display the root directory |
tapiov | 6:641b171407c9 | 347 | pc.printf("Opening the root directory... "); |
tapiov | 6:641b171407c9 | 348 | fflush(stdout); |
tapiov | 6:641b171407c9 | 349 | DIR *d = opendir("/fs/"); |
tapiov | 6:641b171407c9 | 350 | pc.printf("%s\n", (!d ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 351 | if (!d) { |
tapiov | 6:641b171407c9 | 352 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 353 | return; |
tapiov | 6:641b171407c9 | 354 | } |
tapiov | 6:641b171407c9 | 355 | |
tapiov | 6:641b171407c9 | 356 | pc.printf("Root directory:\n"); |
tapiov | 6:641b171407c9 | 357 | while (true) { |
tapiov | 6:641b171407c9 | 358 | struct dirent *e = readdir(d); |
tapiov | 6:641b171407c9 | 359 | if (!e) { |
tapiov | 6:641b171407c9 | 360 | break; |
tapiov | 6:641b171407c9 | 361 | } |
tapiov | 6:641b171407c9 | 362 | |
tapiov | 6:641b171407c9 | 363 | pc.printf(" %s\n", e->d_name); |
tapiov | 6:641b171407c9 | 364 | } |
tapiov | 6:641b171407c9 | 365 | |
tapiov | 6:641b171407c9 | 366 | pc.printf("Closing the root directory... "); |
tapiov | 6:641b171407c9 | 367 | fflush(stdout); |
tapiov | 6:641b171407c9 | 368 | err = closedir(d); |
tapiov | 6:641b171407c9 | 369 | pc.printf("%s\n", (err < 0 ? "Fail" : "OK")); |
tapiov | 6:641b171407c9 | 370 | if (err < 0) { |
tapiov | 6:641b171407c9 | 371 | error("error: %s (%d)\n", strerror(errno), -errno); |
tapiov | 6:641b171407c9 | 372 | return; |
tapiov | 6:641b171407c9 | 373 | } |
tapiov | 6:641b171407c9 | 374 | } |
tapiov | 6:641b171407c9 | 375 | |
tapiov | 6:641b171407c9 | 376 | uint32_t GetFileSize(FILE *fp) { |
tapiov | 6:641b171407c9 | 377 | fseek(fp, 0, SEEK_END); |
tapiov | 6:641b171407c9 | 378 | int size = ftell(fp); |
tapiov | 6:641b171407c9 | 379 | fseek(fp, 0, SEEK_SET); |
tapiov | 6:641b171407c9 | 380 | |
tapiov | 6:641b171407c9 | 381 | return size; |
tapiov | 6:641b171407c9 | 382 | } |