Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed
Fork of LoadCell_STM32 by
functions.cpp
00001 // LoadCell_STM32_RAM v2 functions 00002 // (C) Tapio Valli 2018-02-17 00003 00004 #include "main.h" 00005 00006 int err; 00007 00008 void initArray(Array *a, size_t initialSize) { 00009 a->array = (uint16_t *)malloc(initialSize * sizeof(uint16_t)); 00010 a->used = 0; 00011 a->size = initialSize; 00012 } 00013 00014 void insertArray(Array *a, uint32_t newsize) { 00015 a->size = newsize; 00016 a->array = (uint16_t *)realloc(a->array, a->size * sizeof(uint16_t)); 00017 a->used = 0; 00018 } 00019 00020 void freeArray(Array *a) { 00021 free(a->array); 00022 a->array = NULL; 00023 a->used = a->size = 0; 00024 } 00025 00026 size_t string_parser(char *input, char ***word_array) 00027 { 00028 size_t n = 0; 00029 const char *p = input; 00030 00031 while ( *p ) 00032 { 00033 while ( isspace( ( unsigned char )*p ) ) ++p; 00034 n += *p != '\0'; 00035 while ( *p && !isspace( ( unsigned char )*p ) ) ++p; 00036 } 00037 00038 if ( n ) 00039 { 00040 size_t i = 0; 00041 00042 *word_array = (char**)malloc( n * sizeof( char * ) ); 00043 00044 p = input; 00045 00046 while ( *p ) 00047 { 00048 while ( isspace( ( unsigned char )*p ) ) ++p; 00049 if ( *p ) 00050 { 00051 const char *q = p; 00052 while ( *p && !isspace( ( unsigned char )*p ) ) ++p; 00053 00054 size_t length = p - q; 00055 00056 ( *word_array )[i] = ( char * )malloc( length + 1 ); 00057 00058 strncpy( ( *word_array )[i], q, length ); 00059 ( *word_array )[i][length] = '\0'; 00060 00061 ++i; 00062 } 00063 } 00064 } 00065 00066 return n; 00067 } 00068 00069 void PlotData(uint32_t XCoordinate,uint32_t YCoordinate) 00070 { 00071 // Plot at x,y 00072 lcd.DrawHLine(XCoordinate,YCoordinate,1); 00073 } 00074 00075 void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor) 00076 { 00077 00078 // #define LCD_COLOR_BLUE ((uint32_t)0xFF0000FF) 00079 // #define LCD_COLOR_GREEN ((uint32_t)0xFF00FF00) 00080 // #define LCD_COLOR_RED ((uint32_t)0xFFFF0000) 00081 // #define LCD_COLOR_CYAN ((uint32_t)0xFF00FFFF) 00082 // #define LCD_COLOR_MAGENTA ((uint32_t)0xFFFF00FF) 00083 // #define LCD_COLOR_YELLOW ((uint32_t)0xFFFFFF00) 00084 // #define LCD_COLOR_LIGHTBLUE ((uint32_t)0xFF8080FF) 00085 // #define LCD_COLOR_LIGHTGREEN ((uint32_t)0xFF80FF80) 00086 // #define LCD_COLOR_LIGHTRED ((uint32_t)0xFFFF8080) 00087 // #define LCD_COLOR_LIGHTCYAN ((uint32_t)0xFF80FFFF) 00088 // #define LCD_COLOR_LIGHTMAGENTA ((uint32_t)0xFFFF80FF) 00089 // #define LCD_COLOR_LIGHTYELLOW ((uint32_t)0xFFFFFF80) 00090 // #define LCD_COLOR_DARKBLUE ((uint32_t)0xFF000080) 00091 // #define LCD_COLOR_DARKGREEN ((uint32_t)0xFF008000) 00092 // #define LCD_COLOR_DARKRED ((uint32_t)0xFF800000) 00093 // #define LCD_COLOR_DARKCYAN ((uint32_t)0xFF008080) 00094 // #define LCD_COLOR_DARKMAGENTA ((uint32_t)0xFF800080) 00095 // #define LCD_COLOR_DARKYELLOW ((uint32_t)0xFF808000) 00096 // #define LCD_COLOR_WHITE ((uint32_t)0xFFFFFFFF) 00097 // #define LCD_COLOR_LIGHTGRAY ((uint32_t)0xFFD3D3D3) 00098 // #define LCD_COLOR_GRAY ((uint32_t)0xFF808080) 00099 // #define LCD_COLOR_DARKGRAY ((uint32_t)0xFF404040) 00100 // #define LCD_COLOR_BLACK ((uint32_t)0xFF000000) 00101 // #define LCD_COLOR_BROWN ((uint32_t)0xFFA52A2A) 00102 // #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500) 00103 // #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000) 00104 00105 lcd.Clear(BackGroundColor); 00106 lcd.SetBackColor(BackGroundColor); 00107 lcd.SetTextColor(ForeGroundColor); 00108 lcd.SetFont(&Font20); 00109 } 00110 00111 void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode) 00112 { 00113 char IntStr[50]; 00114 00115 // InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20); 00116 00117 lcd.ClearStringLine(Line); 00118 snprintf(IntStr,50,Str); 00119 lcd.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode); 00120 } 00121 00122 void CountDown(uint32_t millisecs) 00123 { 00124 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); 00125 00126 LCDWrite(5," ",CENTER_MODE); 00127 wait_ms(1); 00128 00129 LCDWrite(5,"Starting in 3... ",CENTER_MODE); 00130 wait_ms(millisecs); 00131 00132 LCDWrite(5,"Starting in 2... ",CENTER_MODE); 00133 wait_ms(millisecs); 00134 00135 LCDWrite(5,"Starting in 1... ",CENTER_MODE); 00136 wait_ms(millisecs); 00137 00138 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); 00139 LCDWrite(5,"GO!",CENTER_MODE); 00140 } 00141 00142 void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us) 00143 { 00144 AnalogIn ain(A0); 00145 uint32_t i; 00146 00147 // Measure NoOfPoints values (f.ex. 19200) 00148 for(i=0;i<NoOfPoints;i++) { 00149 Data->array[i]=(uint16_t)(ain.read_u16()); 00150 wait_us(Period_us); 00151 } 00152 00153 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE); 00154 LCDWrite(5,"DONE!",CENTER_MODE); 00155 00156 pc.printf("Sampling done.\r\n",i); 00157 } 00158 00159 void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) { 00160 00161 uint32_t i1,i2; 00162 00163 uint32_t BufferSum,BufferAvg; 00164 uint32_t XCoord,YCoord; 00165 char MyStr[50]; 00166 00167 pc.printf("Averaging... \r\n"); 00168 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED); 00169 00170 for(i1=0;i1<NoOfPoints;i1++) { 00171 BufferSum=0; 00172 00173 // Read AvgSize samples 00174 for(i2=i1;i2<i1+AvgSize;i2++) { 00175 BufferSum=BufferSum+(uint32_t)Data->array[i2]; 00176 } 00177 00178 BufferAvg=BufferSum/AvgSize; 00179 00180 // Calculate two coords and plot 00181 XCoord=((i1*480.0)/NoOfPoints); 00182 YCoord=(272.0*(BufferAvg/65536.0)); 00183 00184 PlotData(XCoord,YCoord); 00185 } 00186 00187 pc.printf("Averaging done, Points = %u Avg = %u \r\n", i1,AvgSize); 00188 00189 LCDWrite(0,"",CENTER_MODE); 00190 snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize); 00191 LCDWrite(0,MyStr,RIGHT_MODE); 00192 } 00193 00194 void BlockDeviceErase() { 00195 pc.printf("Initializing the block device... "); 00196 fflush(stdout); 00197 int err = bd.init(); 00198 pc.printf("%s\n", (err ? "Fail" : "OK")); 00199 if (err) { 00200 error("error: %s (%d)\n", strerror(-err), err); 00201 } 00202 00203 pc.printf("Erasing the block device... "); 00204 fflush(stdout); 00205 err = bd.erase(0, bd.size()); 00206 printf("%s\n", (err ? "Fail :(" : "OK")); 00207 if (err) { 00208 error("error: %s (%d)\n", strerror(-err), err); 00209 } 00210 pc.printf("Device size is %d\n",bd.size()); 00211 00212 printf("Deinitializing the block device... "); 00213 fflush(stdout); 00214 err = bd.deinit(); 00215 printf("%s\n", (err ? "Fail" : "OK")); 00216 if (err) { 00217 error("error: %s (%d)\n", strerror(-err), err); 00218 } 00219 } 00220 00221 void MountFS() { 00222 pc.printf("Mounting the filesystem... "); 00223 fflush(stdout); 00224 int err = fs.mount(&bd); 00225 pc.printf("%s\n", (err ? "Fail" : "OK")); 00226 if (err) { 00227 // Reformat if we can't mount the filesystem 00228 // this should only happen on the first boot 00229 pc.printf("No filesystem found, formatting... "); 00230 fflush(stdout); 00231 err = fs.reformat(&bd,bd.get_program_size()); 00232 pc.printf("%s\n", (err ? "Fail" : "OK")); 00233 if (err) { 00234 error("error: %s (%d)\n", strerror(-err), err); 00235 } 00236 } 00237 } 00238 00239 void WriteData2FS(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) { 00240 00241 char FName[30]; 00242 uint32_t idx; 00243 00244 // Build the filename 00245 snprintf(FName,30,"/fs/meas%d.txt",FileNumber); 00246 00247 // Open the data file 00248 pc.printf("Opening %s ... ",FName); 00249 fflush(stdout); 00250 FILE *f = fopen(FName, "r+"); 00251 00252 pc.printf("%s\n", (!f ? "Fail" : "OK")); 00253 if (!f) { 00254 // Create the data file if it doesn't exist 00255 pc.printf("No file found, creating a new file... "); 00256 fflush(stdout); 00257 f = fopen(FName, "w+"); 00258 pc.printf("%s\n", (!f ? "Fail" : "OK")); 00259 if (!f) { 00260 error("error: %s (%d)\n", strerror(errno), -errno); 00261 return; 00262 } 00263 } 00264 00265 // Write data points to file 00266 pc.printf("Writing data ... "); 00267 for (idx=0;idx<NoOfPoints;idx++) { 00268 err=fprintf(f,"%d\n",(uint16_t)Data->array[idx]); 00269 if (err < 0) { 00270 pc.printf("Fail\n"); 00271 error("error: %s (%d)\n", strerror(errno), -errno); 00272 return; 00273 00274 } 00275 } 00276 pc.printf("\rWriting data done."); 00277 pc.printf("\r File size is %d",GetFileSize(f)); 00278 00279 // Close the file which also flushes any cached writes 00280 pc.printf(" Closing %s ... ",FName); 00281 fflush(stdout); 00282 err = fclose(f); 00283 printf("%s\n", (err < 0 ? "Fail" : "OK")); 00284 if (err < 0) { 00285 error("error: %s (%d)\n", strerror(errno), -errno); 00286 return; 00287 } 00288 } 00289 00290 void ReadFile2Data(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) { 00291 00292 char FName[30]; 00293 uint32_t idx; 00294 00295 // Build the filename 00296 snprintf(FName,30,"/fs/meas%d.txt",FileNumber); 00297 00298 // Open the data file for reading 00299 pc.printf("Opening %s ... ",FName); 00300 fflush(stdout); 00301 FILE *f = fopen(FName, "r"); 00302 00303 pc.printf("%s\n", (!f ? "Fail" : "OK")); 00304 if (!f) { 00305 // Print error and return 00306 pc.printf("File not found. Check name."); 00307 return; 00308 } 00309 00310 pc.printf("Seeking file... "); 00311 fflush(stdout); 00312 err = fseek(f, 0, SEEK_SET); 00313 printf("%s\n", (err < 0 ? "Fail" : "OK")); 00314 if (err < 0) { 00315 error("error: %s (%d)\n", strerror(errno), -errno); 00316 return; 00317 } 00318 00319 // Go through and increment the numbers 00320 pc.printf("Reading data... "); 00321 for(idx=0;idx<NoOfPoints;idx++) { 00322 // Get current stream position 00323 long pos=ftell(f); 00324 00325 // Parse out the number and put it in data array 00326 uint32_t number; 00327 fscanf(f,"%d",&number); 00328 Data->array[idx]=number; 00329 } 00330 00331 pc.printf("\rReading data done. "); 00332 00333 // Close the file which also flushes any cached writes 00334 pc.printf("Closing %s ... ",FName); 00335 fflush(stdout); 00336 err = fclose(f); 00337 pc.printf("%s\n", (err < 0 ? "Fail" : "OK")); 00338 if (err < 0) { 00339 error("error: %s (%d)\n", strerror(errno), -errno); 00340 return; 00341 } 00342 } 00343 00344 void DirListing() { 00345 00346 // Display the root directory 00347 pc.printf("Opening the root directory... "); 00348 fflush(stdout); 00349 DIR *d = opendir("/fs/"); 00350 pc.printf("%s\n", (!d ? "Fail" : "OK")); 00351 if (!d) { 00352 error("error: %s (%d)\n", strerror(errno), -errno); 00353 return; 00354 } 00355 00356 pc.printf("Root directory:\n"); 00357 while (true) { 00358 struct dirent *e = readdir(d); 00359 if (!e) { 00360 break; 00361 } 00362 00363 pc.printf(" %s\n", e->d_name); 00364 } 00365 00366 pc.printf("Closing the root directory... "); 00367 fflush(stdout); 00368 err = closedir(d); 00369 pc.printf("%s\n", (err < 0 ? "Fail" : "OK")); 00370 if (err < 0) { 00371 error("error: %s (%d)\n", strerror(errno), -errno); 00372 return; 00373 } 00374 } 00375 00376 uint32_t GetFileSize(FILE *fp) { 00377 fseek(fp, 0, SEEK_END); 00378 int size = ftell(fp); 00379 fseek(fp, 0, SEEK_SET); 00380 00381 return size; 00382 }
Generated on Fri Jul 15 2022 21:14:05 by
1.7.2
