Data sample and LCD plot

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG SDRAM_DISCO_F746NG mbed

Revision:
1:c3c61d08f31b
Parent:
0:b3e6088c873f
Child:
2:386f65563144
--- a/main.cpp	Sat Feb 10 20:19:36 2018 +0000
+++ b/main.cpp	Fri Feb 23 16:56:03 2018 +0000
@@ -1,159 +1,139 @@
-#include "mbed.h"
-#include <string.h>
-#include "LCD_DISCO_F746NG.h"
-#include "SDRAM_DISCO_F746NG.h"
-
-LCD_DISCO_F746NG lcd;
-SDRAM_DISCO_F746NG sdram;
+// LoadCell_STM32_RAM v2 main
+// (C) Tapio Valli 2018-02-17
 
-#define BUFFER_SIZE         ((uint32_t)0x0001)
-#define WRITE_READ_ADDR     ((uint32_t)0x0800)
- 
-void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
-uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
- 
-Ticker      ms_tick;
-uint32_t    idx;
-uint32_t    WRITE_READ_ADDR;     (()0x0800)
+#include "mbed.h"
+#include <mbed_stats.h>
+#include "mbed_mem_trace.h"
+
+#include "LCD_DISCO_F746NG.h"
 
-void onTimingEventTicker(void)
-{
-    // this code will run every timing event
-    uint32_t    result;
-    uint32_t    y_coord;        
-    
-    AnalogIn   ain(A0);
-        
-    result = (0x0000 << 16) | ain.read_u16();
-    
-    // Fill the write buffer
-    FillBuffer(WriteBuffer, BUFFER_SIZE, result);
-    
-    // Write buffer
-    sdram.WriteData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR + idx, WriteBuffer, BUFFER_SIZE);
-    
-    idx++;
-}
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
 
-uint32_t PlotData(void)
-{
-    // this code will run every timing event
-    uint32_t result;
-    uint32_t y_coord;        
-    
-    AnalogIn   ain(A0);
+#include "main.h"
 
-    if (x_coord == 0) { lcd.Clear(LCD_COLOR_BLUE); }
-        
-    result = (0x0000 << 16) | ain.read_u16();
-    
-    y_coord=(272.0*result)/16384.0;
+// define the Serial object
+Serial pc1(USBTX, USBRX);
+LCD_DISCO_F746NG lcd1;
 
 
-    if (x_coord < 480) { x_coord++; } else { x_coord=0; }
-    
-    //snprintf(my_str, 50,"Result = %f\0", result/4096.0);
-    //snprintf(my_str, 50,"Loops = %i\0", loops);
-      
-    //lcd.DisplayStringAt(0, LINE(5), (uint8_t *)my_str, CENTER_MODE);
-    
-    // Plot loops,y_coord
-    lcd.DrawHLine(loops,y_coord,1);
-        
-}
+// Global heap stats
+mbed_stats_heap_t heap_stats;
+//mbed_stats_stack_t stack_stats;
+//mbed_mem_trace_cb_t mem_trace_result;
+
+// Mem trace callback
+// mbed_mem_trace_cb_t mem_trace(void) 
+// {
+//     pc1.printf("Heap = %lu, Max = %lu Percentage = %lu \r\n",
+//                heap_stats.current_size,heap_stats.max_size,
+//                (100*(heap_stats.current_size/heap_stats.max_size)));
+                   
+//    pc1.printf("Stack = %lu, Max = %lu Percentage = %lu \r\n",
+//                stack_stats.reserved_size,stack_stats.max_size,
+//                (100*(stack_stats.reserved_size/stack_stats.max_size)));    
+//}
 
 int main() {
-    
-    uint32_t WriteBuffer[BUFFER_SIZE];
-    uint32_t ReadBuffer[BUFFER_SIZE];
-    FMC_SDRAM_CommandTypeDef SDRAMCommandStructure;
+
+    char CmdBuffer[30];
+    char Arg[30]=" ";
+    char Cmd[30]=" ";
 
-    // # of samples
-    idx=0;
+    uint32_t    NoOfPoints=19200;  
+    uint32_t    AvgSize=10;
+    uint32_t    Period_us=100;
+    uint32_t    Count_ms=1000;
+        
+//    mbed_mem_trace_set_callback(mem_trace);
+        
+    // Clear screen, set it up 
+    InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
     
-    // turn on 100 us interrupt
-    ms_tick.attach_us(onTimingEventTicker,100);
+    //Initialize data storage
+    Array Data;
+    initArray(&Data,NoOfPoints);  // initially 19200 elements
     
-    lcd.Clear(LCD_COLOR_BLUE);
-    lcd.SetBackColor(LCD_COLOR_BLUE);
-    lcd.SetTextColor(LCD_COLOR_WHITE);
-    
-    // Issue self-refresh command to SDRAM device
-    SDRAMCommandStructure.CommandMode=FMC_SDRAM_CMD_SELFREFRESH_MODE;
-    SDRAMCommandStructure.CommandTarget=FMC_SDRAM_CMD_TARGET_BANK2;
-    SDRAMCommandStructure.AutoRefreshNumber=1;
-    SDRAMCommandStructure.ModeRegisterDefinition = 0;
-    
-    while(1) {
+    while (Cmd!="quit") {
+        
+        // Print Ready and current settings 
+        pc1.printf("Ready. Settings are Points = %u, Avg = %u, Period_us = %u, Count_ms = %u \r\n",
+                   NoOfPoints,AvgSize,Period_us,Count_ms);
 
-        // Measure 19200 values (1.920 s time)
-        while(idx<19200) {
-            // Wait while the data is collected
+        mbed_stats_heap_get(&heap_stats);                
+        pc1.printf("Heap = %lu, Max = %lu Percentage = %lu \r\n",
+                   heap_stats.current_size,heap_stats.max_size,
+                   (100*(heap_stats.current_size/heap_stats.max_size)));
+                                                                            
+        pc1.gets(CmdBuffer,30);
+        // pc1.printf("I got %s \r\n", CmdBuffer);
+        strcpy(Cmd," ");
+        strcpy(Arg," ");
+                
+        // Parse command and possible numeric arg
+        char s[] = "Initial string";
+        char ** word_array = NULL;
+        
+        strcpy(s,CmdBuffer);
+        size_t n = string_parser(s, &word_array );
+
+        for ( size_t i = 0; i < n; i++ ) {
+            if (i==0) {strcpy(Cmd,word_array[i]);}
+            if (i==1) {strcpy(Arg,word_array[i]);}
+            if (i>1) {pc1.printf("Wrong number of arguments \r\n");}
         }
         
-        // Read back data from the SDRAM memory
-        sdram.ReadData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR + idx, ReadBuffer, BUFFER_SIZE); 
-        pc.printf("\nRead data DONE\n");                                 
+        // pc1.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
+        for ( size_t i = 0; i < n; i++ ) free( word_array[i] );
+        free( word_array );
+    
+        // Branch based on command        
+        // meas: Sample and plot a data set
+        if(strcmp(Cmd,"meas") == 0) {
+            
+            // Countdown
+            CountDown(Count_ms);
+                                 
+            // Sample & plot data one time
+            SamplePoints(&Data,NoOfPoints,Period_us);
+            
+            AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
+        }
         
-
-        // Read back data from the SDRAM memory
-//        sdram.ReadData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR, ReadBuffer, BUFFER_SIZE); 
-//        pc.printf("\nRead data DONE\n");
- 
-        // Checking data integrity 
-//        if (CompareBuffer(WriteBuffer, ReadBuffer, BUFFER_SIZE) != 0)
-//        {
-//            led1 = !led1;
-//            pc.printf("Write/Read buffers are different\n");
-//        }
-//        else
-//       {
-//            led1 = !led1;
-//            pc.printf("Write/Read buffers are identical\n");
-//       }
-  
-//        wait(1);
-         
+        // setpoints: Adjust sampled points                          
+        else if(strcmp(Cmd,"setpoints") == 0) {
+            // Allocate more or less data space
+            NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
+            insertArray(&Data,NoOfPoints);
+        }
+            
+        // setavg: Adjust average amount in samples            
+        else if(strcmp(Cmd,"setavg") == 0) {
+            AvgSize=(uint32_t) strtol(Arg,NULL,10);
+        }
+            
+        // setperiod: Adjust sample period in us
+        else if(strcmp(Cmd,"setperiod") == 0) {
+            Period_us=(uint32_t) strtol(Arg,NULL,10);
+        }
+        
+        // setcount: Adjust countdown period in ms
+        else if(strcmp(Cmd,"setcount") == 0) {
+            Count_ms=(uint32_t) strtol(Arg,NULL,10);
+        }        
+    }
+    
+    // Free memory after quit
+    freeArray(&Data);
+    pc1.printf("Exit. Data freed. \r\n", CmdBuffer);
+    
+    // Infinite loop
+    while(1) 
+    {
+        // Do nothing
     }
 }
-
-/**
-  * @brief  Fills buffer with user predefined data.
-  * @param  pBuffer: pointer on the buffer to fill
-  * @param  BufferLength: size of the buffer to fill
-  * @param  Value: first value to fill on the buffer
-  * @retval None
-  */
-void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Value)
-{
-  uint32_t tmpIndex = 0;
- 
-  /* Put in global buffer different values */
-  for (tmpIndex = 0; tmpIndex < BufferLength; tmpIndex++ )
-  {
-    pBuffer[tmpIndex] = tmpIndex + Value;
-  }
-} 
- 
-/**
-  * @brief  Compares two buffers.
-  * @param  pBuffer1, pBuffer2: buffers to be compared.
-  * @param  BufferLength: buffer's length
-  * @retval 0: pBuffer2 identical to pBuffer1
-  *         1: pBuffer2 differs from pBuffer1
-  */
-uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
-{
-  while (BufferLength--)
-  {
-    if (*pBuffer1 != *pBuffer2)
-    {
-      return 1;
-    }
- 
-    pBuffer1++;
-    pBuffer2++;
-  }
- 
-  return 0;
-}