Tapio Valli / Mbed 2 deprecated LoadCell_STM32

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG SDRAM_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers functions.cpp Source File

functions.cpp

00001 // LoadCell_STM32_RAM v2 functions
00002 // (C) Tapio Valli 2018-02-17
00003 
00004 #include "mbed.h"
00005 #include "LCD_DISCO_F746NG.h"
00006 
00007 #include <stdint.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <assert.h>
00012 #include <ctype.h>
00013 
00014 #include "main.h"
00015 
00016 // define the Serial object
00017 Serial pc2(USBTX, USBRX);
00018 
00019 LCD_DISCO_F746NG lcd2;
00020 
00021 void initArray(Array *a, size_t initialSize) {
00022   a->array = (uint32_t *)malloc(initialSize * sizeof(uint32_t));
00023   a->used = 0;
00024   a->size = initialSize;
00025 }
00026 
00027 void insertArray(Array *a, uint32_t element) {
00028   // a->used is the number of used entries, because a->array[a->used++] 
00029   // updates a->used only *after* the array has been accessed.
00030   // Therefore a->used can go up to a->size
00031    
00032   if (a->used == a->size) {
00033     a->size *= 2;
00034     a->array = (uint32_t *)realloc(a->array, a->size * sizeof(uint32_t));
00035   }
00036   a->array[a->used++] = element;
00037 }
00038 
00039 void freeArray(Array *a) {
00040   free(a->array);
00041   a->array = NULL;
00042   a->used = a->size = 0;
00043 }
00044 
00045 size_t string_parser(char *input, char ***word_array) 
00046 {
00047     size_t n = 0;
00048     const char *p = input;
00049 
00050     while ( *p )
00051     {
00052         while ( isspace( ( unsigned char )*p ) ) ++p;
00053         n += *p != '\0';
00054         while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
00055     }
00056 
00057     if ( n )
00058     {
00059         size_t i = 0;
00060 
00061         *word_array = (char**)malloc( n * sizeof( char * ) ); 
00062 
00063         p = input;
00064 
00065         while ( *p )
00066         {
00067             while ( isspace( ( unsigned char )*p ) ) ++p;
00068             if ( *p )
00069             {
00070                 const char *q = p;
00071                 while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
00072 
00073                 size_t length = p - q;
00074 
00075                 ( *word_array )[i] = ( char * )malloc( length + 1 );
00076 
00077                 strncpy( ( *word_array )[i], q, length );
00078                 ( *word_array )[i][length] = '\0';
00079 
00080                 ++i;
00081             }
00082         }           
00083     }
00084 
00085     return n;
00086 }  
00087 
00088 void PlotData(uint32_t XCoordinate,uint32_t YCoordinate)
00089 {
00090     // Plot at x,y
00091     lcd2.DrawHLine(XCoordinate,YCoordinate,1);        
00092 }
00093 
00094 void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor)
00095 {
00096 
00097 // #define LCD_COLOR_BLUE          ((uint32_t)0xFF0000FF)
00098 // #define LCD_COLOR_GREEN         ((uint32_t)0xFF00FF00)
00099 // #define LCD_COLOR_RED           ((uint32_t)0xFFFF0000)
00100 // #define LCD_COLOR_CYAN          ((uint32_t)0xFF00FFFF)
00101 // #define LCD_COLOR_MAGENTA       ((uint32_t)0xFFFF00FF)
00102 // #define LCD_COLOR_YELLOW        ((uint32_t)0xFFFFFF00)
00103 // #define LCD_COLOR_LIGHTBLUE     ((uint32_t)0xFF8080FF)
00104 // #define LCD_COLOR_LIGHTGREEN    ((uint32_t)0xFF80FF80)
00105 // #define LCD_COLOR_LIGHTRED      ((uint32_t)0xFFFF8080)
00106 // #define LCD_COLOR_LIGHTCYAN     ((uint32_t)0xFF80FFFF)
00107 // #define LCD_COLOR_LIGHTMAGENTA  ((uint32_t)0xFFFF80FF)
00108 // #define LCD_COLOR_LIGHTYELLOW   ((uint32_t)0xFFFFFF80)
00109 // #define LCD_COLOR_DARKBLUE      ((uint32_t)0xFF000080)
00110 // #define LCD_COLOR_DARKGREEN     ((uint32_t)0xFF008000)
00111 // #define LCD_COLOR_DARKRED       ((uint32_t)0xFF800000)
00112 // #define LCD_COLOR_DARKCYAN      ((uint32_t)0xFF008080)
00113 // #define LCD_COLOR_DARKMAGENTA   ((uint32_t)0xFF800080)
00114 // #define LCD_COLOR_DARKYELLOW    ((uint32_t)0xFF808000)
00115 // #define LCD_COLOR_WHITE         ((uint32_t)0xFFFFFFFF)
00116 // #define LCD_COLOR_LIGHTGRAY     ((uint32_t)0xFFD3D3D3)
00117 // #define LCD_COLOR_GRAY          ((uint32_t)0xFF808080)
00118 // #define LCD_COLOR_DARKGRAY      ((uint32_t)0xFF404040)
00119 // #define LCD_COLOR_BLACK         ((uint32_t)0xFF000000)
00120 // #define LCD_COLOR_BROWN         ((uint32_t)0xFFA52A2A)
00121 // #define LCD_COLOR_ORANGE        ((uint32_t)0xFFFFA500)
00122 // #define LCD_COLOR_TRANSPARENT   ((uint32_t)0xFF000000)
00123 
00124     lcd2.Clear(BackGroundColor);
00125     lcd2.SetBackColor(BackGroundColor);
00126     lcd2.SetTextColor(ForeGroundColor);
00127     lcd2.SetFont(&Font20);
00128 }
00129 
00130 void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode)
00131 {
00132     char IntStr[50];
00133     
00134 //    InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20);
00135     
00136     lcd2.ClearStringLine(Line);   
00137     snprintf(IntStr,50,Str);
00138     lcd2.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
00139 }
00140 
00141 void CountDown(uint32_t millisecs)
00142 {
00143     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00144 
00145     LCDWrite(5," ",CENTER_MODE);
00146     wait_ms(1);
00147 
00148     LCDWrite(5,"Starting in 3... ",CENTER_MODE);
00149     wait_ms(millisecs);
00150     
00151     LCDWrite(5,"Starting in 2... ",CENTER_MODE);
00152     wait_ms(millisecs);
00153 
00154     LCDWrite(5,"Starting in 1... ",CENTER_MODE);
00155     wait_ms(millisecs);
00156     
00157     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00158     LCDWrite(5,"GO!",CENTER_MODE);
00159 }
00160 
00161 void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us)
00162 {
00163     AnalogIn    ain(A0);
00164     uint32_t i;
00165   
00166     // Measure NoOfPoints values (f.ex. 19200) 
00167     for(i=0;i<NoOfPoints;i++) {
00168         Data->array[i]=(uint32_t)((0x0000 << 16) | ain.read_u16());
00169         wait_us(Period_us);
00170     }
00171 
00172     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00173     LCDWrite(5,"DONE!",CENTER_MODE);
00174 
00175     pc2.printf("Sampling done.\r\n",i);
00176 }
00177 
00178 void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) {
00179     
00180     uint32_t i1,i2;
00181     
00182     uint32_t BufferSum,BufferAvg;
00183     uint32_t XCoord,YCoord;
00184     char MyStr[50];
00185     
00186     pc2.printf("Start reading... \r\n");
00187     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED);  
00188 
00189     for(i1=0;i1<NoOfPoints;i1++) {
00190         BufferSum=0;
00191         
00192         // Read AvgSize samples
00193         for(i2=i1;i2<i1+AvgSize;i2++) {
00194             BufferSum=BufferSum+Data->array[i2];
00195         }
00196         
00197         BufferAvg=BufferSum/AvgSize;
00198 
00199         // Calculate two coords and plot
00200         XCoord=((i1*480.0)/NoOfPoints);
00201         YCoord=(272.0*(BufferAvg/65536.0));
00202         
00203         PlotData(XCoord,YCoord);
00204     }  
00205     
00206     pc2.printf("Done all, Points = %u Avg = %u \r\n", i1,AvgSize);
00207 
00208     LCDWrite(0,"",CENTER_MODE);
00209     snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize);
00210     LCDWrite(0,MyStr,RIGHT_MODE);
00211 }