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 main
tapiov 1:c3c61d08f31b 2 // (C) Tapio Valli 2018-02-17
tapiov 0:b3e6088c873f 3
tapiov 1:c3c61d08f31b 4 #include "mbed.h"
tapiov 2:386f65563144 5 #include "mbed_stats.h"
tapiov 1:c3c61d08f31b 6
tapiov 1:c3c61d08f31b 7 #include "LCD_DISCO_F746NG.h"
tapiov 0:b3e6088c873f 8
tapiov 1:c3c61d08f31b 9 #include <stdint.h>
tapiov 1:c3c61d08f31b 10 #include <stdio.h>
tapiov 1:c3c61d08f31b 11 #include <stdlib.h>
tapiov 1:c3c61d08f31b 12 #include <string.h>
tapiov 1:c3c61d08f31b 13 #include <assert.h>
tapiov 1:c3c61d08f31b 14 #include <ctype.h>
tapiov 0:b3e6088c873f 15
tapiov 1:c3c61d08f31b 16 #include "main.h"
tapiov 0:b3e6088c873f 17
tapiov 1:c3c61d08f31b 18 // define the Serial object
tapiov 1:c3c61d08f31b 19 Serial pc1(USBTX, USBRX);
tapiov 1:c3c61d08f31b 20 LCD_DISCO_F746NG lcd1;
tapiov 0:b3e6088c873f 21
tapiov 0:b3e6088c873f 22 int main() {
tapiov 1:c3c61d08f31b 23
tapiov 1:c3c61d08f31b 24 char CmdBuffer[30];
tapiov 1:c3c61d08f31b 25 char Arg[30]=" ";
tapiov 1:c3c61d08f31b 26 char Cmd[30]=" ";
tapiov 2:386f65563144 27 size_t n=0;
tapiov 0:b3e6088c873f 28
tapiov 1:c3c61d08f31b 29 uint32_t NoOfPoints=19200;
tapiov 1:c3c61d08f31b 30 uint32_t AvgSize=10;
tapiov 1:c3c61d08f31b 31 uint32_t Period_us=100;
tapiov 1:c3c61d08f31b 32 uint32_t Count_ms=1000;
tapiov 2:386f65563144 33
tapiov 2:386f65563144 34 mbed_stats_heap_t heap_stats;
tapiov 1:c3c61d08f31b 35
tapiov 1:c3c61d08f31b 36 // Clear screen, set it up
tapiov 1:c3c61d08f31b 37 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 2:386f65563144 38
tapiov 1:c3c61d08f31b 39 //Initialize data storage
tapiov 1:c3c61d08f31b 40 Array Data;
tapiov 1:c3c61d08f31b 41 initArray(&Data,NoOfPoints); // initially 19200 elements
tapiov 0:b3e6088c873f 42
tapiov 2:386f65563144 43 while (!((strcmp(Cmd,"quit")==0)&&(n==1))) {
tapiov 1:c3c61d08f31b 44
tapiov 1:c3c61d08f31b 45 // Print Ready and current settings
tapiov 1:c3c61d08f31b 46 pc1.printf("Ready. Settings are Points = %u, Avg = %u, Period_us = %u, Count_ms = %u \r\n",
tapiov 1:c3c61d08f31b 47 NoOfPoints,AvgSize,Period_us,Count_ms);
tapiov 2:386f65563144 48
tapiov 2:386f65563144 49 mbed_stats_heap_get(&heap_stats);
tapiov 2:386f65563144 50 printf("Current heap: %lu\r\n", heap_stats.current_size);
tapiov 2:386f65563144 51 printf("Max heap size: %lu\r\n", heap_stats.max_size);
tapiov 1:c3c61d08f31b 52
tapiov 1:c3c61d08f31b 53 pc1.gets(CmdBuffer,30);
tapiov 1:c3c61d08f31b 54 // pc1.printf("I got %s \r\n", CmdBuffer);
tapiov 1:c3c61d08f31b 55 strcpy(Cmd," ");
tapiov 1:c3c61d08f31b 56 strcpy(Arg," ");
tapiov 1:c3c61d08f31b 57
tapiov 1:c3c61d08f31b 58 // Parse command and possible numeric arg
tapiov 1:c3c61d08f31b 59 char s[] = "Initial string";
tapiov 2:386f65563144 60 char ** word_array=NULL;
tapiov 1:c3c61d08f31b 61
tapiov 1:c3c61d08f31b 62 strcpy(s,CmdBuffer);
tapiov 2:386f65563144 63 n=string_parser(s,&word_array);
tapiov 1:c3c61d08f31b 64
tapiov 2:386f65563144 65 for ( size_t i=0;i<n;i++) {
tapiov 1:c3c61d08f31b 66 if (i==0) {strcpy(Cmd,word_array[i]);}
tapiov 1:c3c61d08f31b 67 if (i==1) {strcpy(Arg,word_array[i]);}
tapiov 1:c3c61d08f31b 68 if (i>1) {pc1.printf("Wrong number of arguments \r\n");}
tapiov 0:b3e6088c873f 69 }
tapiov 0:b3e6088c873f 70
tapiov 1:c3c61d08f31b 71 // pc1.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
tapiov 2:386f65563144 72 for ( size_t i=0;i<n;i++) free( word_array[i] );
tapiov 2:386f65563144 73 free(word_array);
tapiov 1:c3c61d08f31b 74
tapiov 1:c3c61d08f31b 75 // Branch based on command
tapiov 1:c3c61d08f31b 76 // meas: Sample and plot a data set
tapiov 2:386f65563144 77 if((strcmp(Cmd,"meas")==0)&&(n==1)) {
tapiov 1:c3c61d08f31b 78
tapiov 1:c3c61d08f31b 79 // Countdown
tapiov 1:c3c61d08f31b 80 CountDown(Count_ms);
tapiov 1:c3c61d08f31b 81
tapiov 1:c3c61d08f31b 82 // Sample & plot data one time
tapiov 1:c3c61d08f31b 83 SamplePoints(&Data,NoOfPoints,Period_us);
tapiov 1:c3c61d08f31b 84 AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
tapiov 1:c3c61d08f31b 85 }
tapiov 0:b3e6088c873f 86
tapiov 1:c3c61d08f31b 87 // setpoints: Adjust sampled points
tapiov 2:386f65563144 88 else if((strcmp(Cmd,"setpoints")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 89 // Allocate more or less data space
tapiov 1:c3c61d08f31b 90 NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
tapiov 1:c3c61d08f31b 91 insertArray(&Data,NoOfPoints);
tapiov 1:c3c61d08f31b 92 }
tapiov 1:c3c61d08f31b 93
tapiov 1:c3c61d08f31b 94 // setavg: Adjust average amount in samples
tapiov 2:386f65563144 95 else if((strcmp(Cmd,"setavg")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 96 AvgSize=(uint32_t) strtol(Arg,NULL,10);
tapiov 1:c3c61d08f31b 97 }
tapiov 1:c3c61d08f31b 98
tapiov 1:c3c61d08f31b 99 // setperiod: Adjust sample period in us
tapiov 2:386f65563144 100 else if((strcmp(Cmd,"setperiod")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 101 Period_us=(uint32_t) strtol(Arg,NULL,10);
tapiov 1:c3c61d08f31b 102 }
tapiov 1:c3c61d08f31b 103
tapiov 1:c3c61d08f31b 104 // setcount: Adjust countdown period in ms
tapiov 2:386f65563144 105 else if((strcmp(Cmd,"setcount")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 106 Count_ms=(uint32_t) strtol(Arg,NULL,10);
tapiov 2:386f65563144 107 }
tapiov 2:386f65563144 108
tapiov 2:386f65563144 109 // quit: Exit on next while
tapiov 2:386f65563144 110 else if((strcmp(Cmd,"quit")==0)&&(n==1)) {
tapiov 2:386f65563144 111 // Do nothing yet
tapiov 2:386f65563144 112 }
tapiov 2:386f65563144 113 else {
tapiov 2:386f65563144 114 pc1.printf("Wrong command or argument \r\n");
tapiov 2:386f65563144 115 }
tapiov 1:c3c61d08f31b 116 }
tapiov 1:c3c61d08f31b 117
tapiov 1:c3c61d08f31b 118 // Free memory after quit
tapiov 1:c3c61d08f31b 119 freeArray(&Data);
tapiov 2:386f65563144 120 pc1.printf("Exit. Data freed. \r\n");
tapiov 1:c3c61d08f31b 121
tapiov 1:c3c61d08f31b 122 // Infinite loop
tapiov 1:c3c61d08f31b 123 while(1)
tapiov 1:c3c61d08f31b 124 {
tapiov 1:c3c61d08f31b 125 // Do nothing
tapiov 0:b3e6088c873f 126 }
tapiov 0:b3e6088c873f 127 }