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