LoadCell_STM32_SDRAM_v1

Dependencies:   BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed

Fork of LoadCell_STM32 by Tapio Valli

Committer:
tapiov
Date:
Sat Feb 24 11:26:50 2018 +0000
Revision:
3:22b1719c2b92
Parent:
2:386f65563144
Child:
5:f8d3bcb187de
Data array resizing working.

Who changed what in which revision?

UserRevisionLine numberNew 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 "mbed.h"
tapiov 1:c3c61d08f31b 5 #include "LCD_DISCO_F746NG.h"
tapiov 1:c3c61d08f31b 6
tapiov 1:c3c61d08f31b 7 #include <stdint.h>
tapiov 1:c3c61d08f31b 8 #include <stdio.h>
tapiov 1:c3c61d08f31b 9 #include <stdlib.h>
tapiov 1:c3c61d08f31b 10 #include <string.h>
tapiov 1:c3c61d08f31b 11 #include <assert.h>
tapiov 1:c3c61d08f31b 12 #include <ctype.h>
tapiov 1:c3c61d08f31b 13
tapiov 1:c3c61d08f31b 14 #include "main.h"
tapiov 1:c3c61d08f31b 15
tapiov 1:c3c61d08f31b 16 // define the Serial object
tapiov 1:c3c61d08f31b 17 Serial pc2(USBTX, USBRX);
tapiov 1:c3c61d08f31b 18
tapiov 1:c3c61d08f31b 19 LCD_DISCO_F746NG lcd2;
tapiov 1:c3c61d08f31b 20
tapiov 1:c3c61d08f31b 21 void initArray(Array *a, size_t initialSize) {
tapiov 1:c3c61d08f31b 22 a->array = (uint32_t *)malloc(initialSize * sizeof(uint32_t));
tapiov 1:c3c61d08f31b 23 a->used = 0;
tapiov 1:c3c61d08f31b 24 a->size = initialSize;
tapiov 1:c3c61d08f31b 25 }
tapiov 1:c3c61d08f31b 26
tapiov 1:c3c61d08f31b 27 void insertArray(Array *a, uint32_t element) {
tapiov 1:c3c61d08f31b 28 // a->used is the number of used entries, because a->array[a->used++]
tapiov 1:c3c61d08f31b 29 // updates a->used only *after* the array has been accessed.
tapiov 1:c3c61d08f31b 30 // Therefore a->used can go up to a->size
tapiov 1:c3c61d08f31b 31
tapiov 1:c3c61d08f31b 32 if (a->used == a->size) {
tapiov 1:c3c61d08f31b 33 a->size *= 2;
tapiov 1:c3c61d08f31b 34 a->array = (uint32_t *)realloc(a->array, a->size * sizeof(uint32_t));
tapiov 1:c3c61d08f31b 35 }
tapiov 1:c3c61d08f31b 36 a->array[a->used++] = element;
tapiov 1:c3c61d08f31b 37 }
tapiov 1:c3c61d08f31b 38
tapiov 3:22b1719c2b92 39 void insertArray2(Array *a, uint32_t newsize) {
tapiov 3:22b1719c2b92 40 a->size = newsize;
tapiov 3:22b1719c2b92 41 a->array = (uint32_t *)realloc(a->array, a->size * sizeof(uint32_t));
tapiov 3:22b1719c2b92 42 a->used = 0;
tapiov 3:22b1719c2b92 43 }
tapiov 3:22b1719c2b92 44
tapiov 1:c3c61d08f31b 45 void freeArray(Array *a) {
tapiov 1:c3c61d08f31b 46 free(a->array);
tapiov 1:c3c61d08f31b 47 a->array = NULL;
tapiov 1:c3c61d08f31b 48 a->used = a->size = 0;
tapiov 1:c3c61d08f31b 49 }
tapiov 1:c3c61d08f31b 50
tapiov 1:c3c61d08f31b 51 size_t string_parser(char *input, char ***word_array)
tapiov 1:c3c61d08f31b 52 {
tapiov 1:c3c61d08f31b 53 size_t n = 0;
tapiov 1:c3c61d08f31b 54 const char *p = input;
tapiov 1:c3c61d08f31b 55
tapiov 1:c3c61d08f31b 56 while ( *p )
tapiov 1:c3c61d08f31b 57 {
tapiov 1:c3c61d08f31b 58 while ( isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 59 n += *p != '\0';
tapiov 1:c3c61d08f31b 60 while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 61 }
tapiov 1:c3c61d08f31b 62
tapiov 1:c3c61d08f31b 63 if ( n )
tapiov 1:c3c61d08f31b 64 {
tapiov 1:c3c61d08f31b 65 size_t i = 0;
tapiov 1:c3c61d08f31b 66
tapiov 1:c3c61d08f31b 67 *word_array = (char**)malloc( n * sizeof( char * ) );
tapiov 1:c3c61d08f31b 68
tapiov 1:c3c61d08f31b 69 p = input;
tapiov 1:c3c61d08f31b 70
tapiov 1:c3c61d08f31b 71 while ( *p )
tapiov 1:c3c61d08f31b 72 {
tapiov 1:c3c61d08f31b 73 while ( isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 74 if ( *p )
tapiov 1:c3c61d08f31b 75 {
tapiov 1:c3c61d08f31b 76 const char *q = p;
tapiov 1:c3c61d08f31b 77 while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 78
tapiov 1:c3c61d08f31b 79 size_t length = p - q;
tapiov 1:c3c61d08f31b 80
tapiov 1:c3c61d08f31b 81 ( *word_array )[i] = ( char * )malloc( length + 1 );
tapiov 1:c3c61d08f31b 82
tapiov 1:c3c61d08f31b 83 strncpy( ( *word_array )[i], q, length );
tapiov 1:c3c61d08f31b 84 ( *word_array )[i][length] = '\0';
tapiov 1:c3c61d08f31b 85
tapiov 1:c3c61d08f31b 86 ++i;
tapiov 1:c3c61d08f31b 87 }
tapiov 1:c3c61d08f31b 88 }
tapiov 1:c3c61d08f31b 89 }
tapiov 1:c3c61d08f31b 90
tapiov 1:c3c61d08f31b 91 return n;
tapiov 1:c3c61d08f31b 92 }
tapiov 1:c3c61d08f31b 93
tapiov 1:c3c61d08f31b 94 void PlotData(uint32_t XCoordinate,uint32_t YCoordinate)
tapiov 1:c3c61d08f31b 95 {
tapiov 1:c3c61d08f31b 96 // Plot at x,y
tapiov 1:c3c61d08f31b 97 lcd2.DrawHLine(XCoordinate,YCoordinate,1);
tapiov 1:c3c61d08f31b 98 }
tapiov 1:c3c61d08f31b 99
tapiov 1:c3c61d08f31b 100 void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor)
tapiov 1:c3c61d08f31b 101 {
tapiov 1:c3c61d08f31b 102
tapiov 1:c3c61d08f31b 103 // #define LCD_COLOR_BLUE ((uint32_t)0xFF0000FF)
tapiov 1:c3c61d08f31b 104 // #define LCD_COLOR_GREEN ((uint32_t)0xFF00FF00)
tapiov 1:c3c61d08f31b 105 // #define LCD_COLOR_RED ((uint32_t)0xFFFF0000)
tapiov 1:c3c61d08f31b 106 // #define LCD_COLOR_CYAN ((uint32_t)0xFF00FFFF)
tapiov 1:c3c61d08f31b 107 // #define LCD_COLOR_MAGENTA ((uint32_t)0xFFFF00FF)
tapiov 1:c3c61d08f31b 108 // #define LCD_COLOR_YELLOW ((uint32_t)0xFFFFFF00)
tapiov 1:c3c61d08f31b 109 // #define LCD_COLOR_LIGHTBLUE ((uint32_t)0xFF8080FF)
tapiov 1:c3c61d08f31b 110 // #define LCD_COLOR_LIGHTGREEN ((uint32_t)0xFF80FF80)
tapiov 1:c3c61d08f31b 111 // #define LCD_COLOR_LIGHTRED ((uint32_t)0xFFFF8080)
tapiov 1:c3c61d08f31b 112 // #define LCD_COLOR_LIGHTCYAN ((uint32_t)0xFF80FFFF)
tapiov 1:c3c61d08f31b 113 // #define LCD_COLOR_LIGHTMAGENTA ((uint32_t)0xFFFF80FF)
tapiov 1:c3c61d08f31b 114 // #define LCD_COLOR_LIGHTYELLOW ((uint32_t)0xFFFFFF80)
tapiov 1:c3c61d08f31b 115 // #define LCD_COLOR_DARKBLUE ((uint32_t)0xFF000080)
tapiov 1:c3c61d08f31b 116 // #define LCD_COLOR_DARKGREEN ((uint32_t)0xFF008000)
tapiov 1:c3c61d08f31b 117 // #define LCD_COLOR_DARKRED ((uint32_t)0xFF800000)
tapiov 1:c3c61d08f31b 118 // #define LCD_COLOR_DARKCYAN ((uint32_t)0xFF008080)
tapiov 1:c3c61d08f31b 119 // #define LCD_COLOR_DARKMAGENTA ((uint32_t)0xFF800080)
tapiov 1:c3c61d08f31b 120 // #define LCD_COLOR_DARKYELLOW ((uint32_t)0xFF808000)
tapiov 1:c3c61d08f31b 121 // #define LCD_COLOR_WHITE ((uint32_t)0xFFFFFFFF)
tapiov 1:c3c61d08f31b 122 // #define LCD_COLOR_LIGHTGRAY ((uint32_t)0xFFD3D3D3)
tapiov 1:c3c61d08f31b 123 // #define LCD_COLOR_GRAY ((uint32_t)0xFF808080)
tapiov 1:c3c61d08f31b 124 // #define LCD_COLOR_DARKGRAY ((uint32_t)0xFF404040)
tapiov 1:c3c61d08f31b 125 // #define LCD_COLOR_BLACK ((uint32_t)0xFF000000)
tapiov 1:c3c61d08f31b 126 // #define LCD_COLOR_BROWN ((uint32_t)0xFFA52A2A)
tapiov 1:c3c61d08f31b 127 // #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500)
tapiov 1:c3c61d08f31b 128 // #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000)
tapiov 1:c3c61d08f31b 129
tapiov 1:c3c61d08f31b 130 lcd2.Clear(BackGroundColor);
tapiov 1:c3c61d08f31b 131 lcd2.SetBackColor(BackGroundColor);
tapiov 1:c3c61d08f31b 132 lcd2.SetTextColor(ForeGroundColor);
tapiov 1:c3c61d08f31b 133 lcd2.SetFont(&Font20);
tapiov 1:c3c61d08f31b 134 }
tapiov 1:c3c61d08f31b 135
tapiov 1:c3c61d08f31b 136 void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode)
tapiov 1:c3c61d08f31b 137 {
tapiov 1:c3c61d08f31b 138 char IntStr[50];
tapiov 1:c3c61d08f31b 139
tapiov 1:c3c61d08f31b 140 // InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20);
tapiov 1:c3c61d08f31b 141
tapiov 1:c3c61d08f31b 142 lcd2.ClearStringLine(Line);
tapiov 1:c3c61d08f31b 143 snprintf(IntStr,50,Str);
tapiov 1:c3c61d08f31b 144 lcd2.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
tapiov 1:c3c61d08f31b 145 }
tapiov 1:c3c61d08f31b 146
tapiov 1:c3c61d08f31b 147 void CountDown(uint32_t millisecs)
tapiov 1:c3c61d08f31b 148 {
tapiov 1:c3c61d08f31b 149 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 150
tapiov 1:c3c61d08f31b 151 LCDWrite(5," ",CENTER_MODE);
tapiov 1:c3c61d08f31b 152 wait_ms(1);
tapiov 1:c3c61d08f31b 153
tapiov 1:c3c61d08f31b 154 LCDWrite(5,"Starting in 3... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 155 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 156
tapiov 1:c3c61d08f31b 157 LCDWrite(5,"Starting in 2... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 158 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 159
tapiov 1:c3c61d08f31b 160 LCDWrite(5,"Starting in 1... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 161 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 162
tapiov 1:c3c61d08f31b 163 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 164 LCDWrite(5,"GO!",CENTER_MODE);
tapiov 1:c3c61d08f31b 165 }
tapiov 1:c3c61d08f31b 166
tapiov 1:c3c61d08f31b 167 void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us)
tapiov 1:c3c61d08f31b 168 {
tapiov 1:c3c61d08f31b 169 AnalogIn ain(A0);
tapiov 1:c3c61d08f31b 170 uint32_t i;
tapiov 1:c3c61d08f31b 171
tapiov 1:c3c61d08f31b 172 // Measure NoOfPoints values (f.ex. 19200)
tapiov 1:c3c61d08f31b 173 for(i=0;i<NoOfPoints;i++) {
tapiov 1:c3c61d08f31b 174 Data->array[i]=(uint32_t)((0x0000 << 16) | ain.read_u16());
tapiov 1:c3c61d08f31b 175 wait_us(Period_us);
tapiov 1:c3c61d08f31b 176 }
tapiov 1:c3c61d08f31b 177
tapiov 1:c3c61d08f31b 178 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 179 LCDWrite(5,"DONE!",CENTER_MODE);
tapiov 1:c3c61d08f31b 180
tapiov 2:386f65563144 181 pc2.printf("Sampling done.\r\n",i);
tapiov 1:c3c61d08f31b 182 }
tapiov 1:c3c61d08f31b 183
tapiov 1:c3c61d08f31b 184 void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) {
tapiov 1:c3c61d08f31b 185
tapiov 1:c3c61d08f31b 186 uint32_t i1,i2;
tapiov 1:c3c61d08f31b 187
tapiov 1:c3c61d08f31b 188 uint32_t BufferSum,BufferAvg;
tapiov 1:c3c61d08f31b 189 uint32_t XCoord,YCoord;
tapiov 1:c3c61d08f31b 190 char MyStr[50];
tapiov 1:c3c61d08f31b 191
tapiov 1:c3c61d08f31b 192 pc2.printf("Start reading... \r\n");
tapiov 1:c3c61d08f31b 193 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED);
tapiov 1:c3c61d08f31b 194
tapiov 1:c3c61d08f31b 195 for(i1=0;i1<NoOfPoints;i1++) {
tapiov 1:c3c61d08f31b 196 BufferSum=0;
tapiov 1:c3c61d08f31b 197
tapiov 1:c3c61d08f31b 198 // Read AvgSize samples
tapiov 1:c3c61d08f31b 199 for(i2=i1;i2<i1+AvgSize;i2++) {
tapiov 1:c3c61d08f31b 200 BufferSum=BufferSum+Data->array[i2];
tapiov 1:c3c61d08f31b 201 }
tapiov 1:c3c61d08f31b 202
tapiov 1:c3c61d08f31b 203 BufferAvg=BufferSum/AvgSize;
tapiov 1:c3c61d08f31b 204
tapiov 1:c3c61d08f31b 205 // Calculate two coords and plot
tapiov 1:c3c61d08f31b 206 XCoord=((i1*480.0)/NoOfPoints);
tapiov 1:c3c61d08f31b 207 YCoord=(272.0*(BufferAvg/65536.0));
tapiov 1:c3c61d08f31b 208
tapiov 1:c3c61d08f31b 209 PlotData(XCoord,YCoord);
tapiov 1:c3c61d08f31b 210 }
tapiov 1:c3c61d08f31b 211
tapiov 1:c3c61d08f31b 212 pc2.printf("Done all, Points = %u Avg = %u \r\n", i1,AvgSize);
tapiov 1:c3c61d08f31b 213
tapiov 1:c3c61d08f31b 214 LCDWrite(0,"",CENTER_MODE);
tapiov 1:c3c61d08f31b 215 snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize);
tapiov 1:c3c61d08f31b 216 LCDWrite(0,MyStr,RIGHT_MODE);
tapiov 1:c3c61d08f31b 217 }