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 main.cpp Source File

main.cpp

00001 // LoadCell_STM32_RAM v2 main
00002 // (C) Tapio Valli 2018-02-17
00003 
00004 #include "mbed.h"
00005 #include "mbed_stats.h"
00006 
00007 #include "LCD_DISCO_F746NG.h"
00008 
00009 #include <stdint.h>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <assert.h>
00014 #include <ctype.h>
00015 
00016 #include "main.h"
00017 
00018 // define the Serial object
00019 Serial pc1(USBTX, USBRX);
00020 LCD_DISCO_F746NG lcd1;
00021 
00022 int main() {
00023 
00024     char CmdBuffer[30];
00025     char Arg[30]=" ";
00026     char Cmd[30]=" ";
00027     size_t n=0;
00028 
00029     uint32_t    NoOfPoints=19200;  
00030     uint32_t    AvgSize=10;
00031     uint32_t    Period_us=100;
00032     uint32_t    Count_ms=1000;
00033     
00034     mbed_stats_heap_t heap_stats;
00035         
00036     // Clear screen, set it up 
00037     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00038         
00039     //Initialize data storage
00040     Array Data;
00041     initArray(&Data,NoOfPoints);  // initially 19200 elements
00042     
00043     while (!((strcmp(Cmd,"quit")==0)&&(n==1))) {
00044         
00045         // Print Ready and current settings 
00046         pc1.printf("Ready. Settings are Points = %u, Avg = %u, Period_us = %u, Count_ms = %u \r\n",
00047                    NoOfPoints,AvgSize,Period_us,Count_ms);
00048                    
00049         mbed_stats_heap_get(&heap_stats);
00050         printf("Current heap: %lu\r\n", heap_stats.current_size);
00051         printf("Max heap size: %lu\r\n", heap_stats.max_size);
00052                                                                             
00053         pc1.gets(CmdBuffer,30);
00054         // pc1.printf("I got %s \r\n", CmdBuffer);
00055         strcpy(Cmd," ");
00056         strcpy(Arg," ");
00057                 
00058         // Parse command and possible numeric arg
00059         char s[] = "Initial string";
00060         char ** word_array=NULL;
00061         
00062         strcpy(s,CmdBuffer);
00063         n=string_parser(s,&word_array);
00064 
00065         for ( size_t i=0;i<n;i++) {
00066             if (i==0) {strcpy(Cmd,word_array[i]);}
00067             if (i==1) {strcpy(Arg,word_array[i]);}
00068             if (i>1) {pc1.printf("Wrong number of arguments \r\n");}
00069         }
00070         
00071         // pc1.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
00072         for ( size_t i=0;i<n;i++) free( word_array[i] );
00073         free(word_array);
00074     
00075         // Branch based on command        
00076         // meas: Sample and plot a data set
00077         if((strcmp(Cmd,"meas")==0)&&(n==1)) {
00078             
00079             // Countdown
00080             CountDown(Count_ms);
00081                                  
00082             // Sample & plot data one time
00083             SamplePoints(&Data,NoOfPoints,Period_us);
00084             AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
00085         }
00086         
00087         // setpoints: Adjust sampled points                          
00088         else if((strcmp(Cmd,"setpoints")==0)&&(n==2)) {
00089             // Allocate more or less data space
00090             NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
00091             insertArray(&Data,NoOfPoints);
00092         }
00093             
00094         // setavg: Adjust average amount in samples            
00095         else if((strcmp(Cmd,"setavg")==0)&&(n==2)) {
00096             AvgSize=(uint32_t) strtol(Arg,NULL,10);
00097         }
00098             
00099         // setperiod: Adjust sample period in us
00100         else if((strcmp(Cmd,"setperiod")==0)&&(n==2)) {
00101             Period_us=(uint32_t) strtol(Arg,NULL,10);
00102         }
00103         
00104         // setcount: Adjust countdown period in ms
00105         else if((strcmp(Cmd,"setcount")==0)&&(n==2)) {
00106             Count_ms=(uint32_t) strtol(Arg,NULL,10);
00107         }
00108         
00109         // quit: Exit on next while
00110         else if((strcmp(Cmd,"quit")==0)&&(n==1)) {
00111             // Do nothing yet
00112         }
00113         else {
00114             pc1.printf("Wrong command or argument \r\n");
00115         }
00116     }
00117     
00118     // Free memory after quit
00119     freeArray(&Data);
00120     pc1.printf("Exit. Data freed. \r\n");
00121     
00122     // Infinite loop
00123     while(1) 
00124     {
00125         // Do nothing
00126     }
00127 }