Data sample and LCD plot

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG SDRAM_DISCO_F746NG mbed

Committer:
tapiov
Date:
Fri Feb 23 20:28:44 2018 +0000
Revision:
2:386f65563144
Parent:
1:c3c61d08f31b
New commit for forum review

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