Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed
Fork of LoadCell_STM32 by
Diff: functions.cpp
- Revision:
- 6:641b171407c9
- Parent:
- 5:f8d3bcb187de
--- a/functions.cpp Sat Feb 24 16:18:46 2018 +0000
+++ b/functions.cpp Thu Mar 08 20:14:53 2018 +0000
@@ -1,24 +1,9 @@
// LoadCell_STM32_RAM v2 functions
// (C) Tapio Valli 2018-02-17
-#include "mbed.h"
-#include "LCD_DISCO_F746NG.h"
-
-#include <stdio.h>
-#include <errno.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <ctype.h>
-
#include "main.h"
-// define the Serial object
-Serial pc2(USBTX, USBRX);
-
-LCD_DISCO_F746NG lcd2;
+int err;
void initArray(Array *a, size_t initialSize) {
a->array = (uint16_t *)malloc(initialSize * sizeof(uint16_t));
@@ -84,7 +69,7 @@
void PlotData(uint32_t XCoordinate,uint32_t YCoordinate)
{
// Plot at x,y
- lcd2.DrawHLine(XCoordinate,YCoordinate,1);
+ lcd.DrawHLine(XCoordinate,YCoordinate,1);
}
void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor)
@@ -117,10 +102,10 @@
// #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500)
// #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000)
- lcd2.Clear(BackGroundColor);
- lcd2.SetBackColor(BackGroundColor);
- lcd2.SetTextColor(ForeGroundColor);
- lcd2.SetFont(&Font20);
+ lcd.Clear(BackGroundColor);
+ lcd.SetBackColor(BackGroundColor);
+ lcd.SetTextColor(ForeGroundColor);
+ lcd.SetFont(&Font20);
}
void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode)
@@ -129,9 +114,9 @@
// InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20);
- lcd2.ClearStringLine(Line);
+ lcd.ClearStringLine(Line);
snprintf(IntStr,50,Str);
- lcd2.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
+ lcd.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
}
void CountDown(uint32_t millisecs)
@@ -168,7 +153,7 @@
InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
LCDWrite(5,"DONE!",CENTER_MODE);
- pc2.printf("Sampling done.\r\n",i);
+ pc.printf("Sampling done.\r\n",i);
}
void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) {
@@ -179,7 +164,7 @@
uint32_t XCoord,YCoord;
char MyStr[50];
- pc2.printf("Start reading... \r\n");
+ pc.printf("Averaging... \r\n");
InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED);
for(i1=0;i1<NoOfPoints;i1++) {
@@ -199,9 +184,199 @@
PlotData(XCoord,YCoord);
}
- pc2.printf("Done all, Points = %u Avg = %u \r\n", i1,AvgSize);
+ pc.printf("Averaging done, Points = %u Avg = %u \r\n", i1,AvgSize);
LCDWrite(0,"",CENTER_MODE);
snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize);
LCDWrite(0,MyStr,RIGHT_MODE);
}
+
+void BlockDeviceErase() {
+ pc.printf("Initializing the block device... ");
+ fflush(stdout);
+ int err = bd.init();
+ pc.printf("%s\n", (err ? "Fail" : "OK"));
+ if (err) {
+ error("error: %s (%d)\n", strerror(-err), err);
+ }
+
+ pc.printf("Erasing the block device... ");
+ fflush(stdout);
+ err = bd.erase(0, bd.size());
+ printf("%s\n", (err ? "Fail :(" : "OK"));
+ if (err) {
+ error("error: %s (%d)\n", strerror(-err), err);
+ }
+ pc.printf("Device size is %d\n",bd.size());
+
+ printf("Deinitializing the block device... ");
+ fflush(stdout);
+ err = bd.deinit();
+ printf("%s\n", (err ? "Fail" : "OK"));
+ if (err) {
+ error("error: %s (%d)\n", strerror(-err), err);
+ }
+}
+
+void MountFS() {
+ pc.printf("Mounting the filesystem... ");
+ fflush(stdout);
+ int err = fs.mount(&bd);
+ pc.printf("%s\n", (err ? "Fail" : "OK"));
+ if (err) {
+ // Reformat if we can't mount the filesystem
+ // this should only happen on the first boot
+ pc.printf("No filesystem found, formatting... ");
+ fflush(stdout);
+ err = fs.reformat(&bd,bd.get_program_size());
+ pc.printf("%s\n", (err ? "Fail" : "OK"));
+ if (err) {
+ error("error: %s (%d)\n", strerror(-err), err);
+ }
+ }
+}
+
+void WriteData2FS(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) {
+
+ char FName[30];
+ uint32_t idx;
+
+ // Build the filename
+ snprintf(FName,30,"/fs/meas%d.txt",FileNumber);
+
+ // Open the data file
+ pc.printf("Opening %s ... ",FName);
+ fflush(stdout);
+ FILE *f = fopen(FName, "r+");
+
+ pc.printf("%s\n", (!f ? "Fail" : "OK"));
+ if (!f) {
+ // Create the data file if it doesn't exist
+ pc.printf("No file found, creating a new file... ");
+ fflush(stdout);
+ f = fopen(FName, "w+");
+ pc.printf("%s\n", (!f ? "Fail" : "OK"));
+ if (!f) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+ }
+
+ // Write data points to file
+ pc.printf("Writing data ... ");
+ for (idx=0;idx<NoOfPoints;idx++) {
+ err=fprintf(f,"%d\n",(uint16_t)Data->array[idx]);
+ if (err < 0) {
+ pc.printf("Fail\n");
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+
+ }
+ }
+ pc.printf("\rWriting data done.");
+ pc.printf("\r File size is %d",GetFileSize(f));
+
+ // Close the file which also flushes any cached writes
+ pc.printf(" Closing %s ... ",FName);
+ fflush(stdout);
+ err = fclose(f);
+ printf("%s\n", (err < 0 ? "Fail" : "OK"));
+ if (err < 0) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+}
+
+void ReadFile2Data(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber) {
+
+ char FName[30];
+ uint32_t idx;
+
+ // Build the filename
+ snprintf(FName,30,"/fs/meas%d.txt",FileNumber);
+
+ // Open the data file for reading
+ pc.printf("Opening %s ... ",FName);
+ fflush(stdout);
+ FILE *f = fopen(FName, "r");
+
+ pc.printf("%s\n", (!f ? "Fail" : "OK"));
+ if (!f) {
+ // Print error and return
+ pc.printf("File not found. Check name.");
+ return;
+ }
+
+ pc.printf("Seeking file... ");
+ fflush(stdout);
+ err = fseek(f, 0, SEEK_SET);
+ printf("%s\n", (err < 0 ? "Fail" : "OK"));
+ if (err < 0) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+
+ // Go through and increment the numbers
+ pc.printf("Reading data... ");
+ for(idx=0;idx<NoOfPoints;idx++) {
+ // Get current stream position
+ long pos=ftell(f);
+
+ // Parse out the number and put it in data array
+ uint32_t number;
+ fscanf(f,"%d",&number);
+ Data->array[idx]=number;
+ }
+
+ pc.printf("\rReading data done. ");
+
+ // Close the file which also flushes any cached writes
+ pc.printf("Closing %s ... ",FName);
+ fflush(stdout);
+ err = fclose(f);
+ pc.printf("%s\n", (err < 0 ? "Fail" : "OK"));
+ if (err < 0) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+}
+
+void DirListing() {
+
+ // Display the root directory
+ pc.printf("Opening the root directory... ");
+ fflush(stdout);
+ DIR *d = opendir("/fs/");
+ pc.printf("%s\n", (!d ? "Fail" : "OK"));
+ if (!d) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+
+ pc.printf("Root directory:\n");
+ while (true) {
+ struct dirent *e = readdir(d);
+ if (!e) {
+ break;
+ }
+
+ pc.printf(" %s\n", e->d_name);
+ }
+
+ pc.printf("Closing the root directory... ");
+ fflush(stdout);
+ err = closedir(d);
+ pc.printf("%s\n", (err < 0 ? "Fail" : "OK"));
+ if (err < 0) {
+ error("error: %s (%d)\n", strerror(errno), -errno);
+ return;
+ }
+}
+
+uint32_t GetFileSize(FILE *fp) {
+ fseek(fp, 0, SEEK_END);
+ int size = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ return size;
+}
