STM32L476
Dependencies: MbedJSONValue SDFileSystem WConstants mbed-dev DS1820 TinyGPSPlus epd1in54
Fork of A_SSL_Main by
Diff: main.cpp
- Revision:
- 6:d30c8e8678c9
- Parent:
- 5:4525141f69d2
- Child:
- 8:bc5a3b2ff424
diff -r 4525141f69d2 -r d30c8e8678c9 main.cpp --- a/main.cpp Tue Aug 28 17:01:08 2018 +0000 +++ b/main.cpp Thu Aug 30 18:54:42 2018 +0000 @@ -6,23 +6,20 @@ #include "epd1in54.h" #include "stdio.h" #include "math.h" -//#include "GPS.h" #include "TinyGPSPlus.h" +#include "SDFileSystem.h" +#include "errno.h" #define TX5 PG_7 #define RX5 PG_8 #define GPSBaud 9600 #define Serial0Baud 115200 - +#define SD_FILE_SYSTEM_H +SDFileSystem* fs; +FILE* fp; -#define NUM_OF_BLOCKS 5 -#define BLOCK_START_ADDR 0 -#define BLOCK_SIZE 512 -#define BLOCK_END_ADDR (BLOCK_SIZE * NUM_OF_BLOCKS) -#define BUFFER_WORDS_SIZE ((BLOCK_SIZE * NUM_OF_BLOCKS) >> 2) // Total data size in bytes -//GPS gps(PG_7, PG_8, 4800); // (Tx, Rx, Baud rate) Serial GPSSerial(TX5, RX5); TinyGPSPlus tgps; @@ -31,11 +28,7 @@ int i = 0; -//extern Serial serialGPS; -//extern TinyGPSPlus gps; -//Serial serialGPS(PG_7, PG_8,9600); -//serial_baud(serialGPS,9600); //DS18B20 temperature sensor Serial serial(USBTX, USBRX); @@ -45,6 +38,7 @@ float tCompare = 0; int ab=1; + SDFileSystem sd(PI_3, PC_2, PD_3, PB_12, "sd"); //E-ink Display // Control @@ -58,85 +52,6 @@ PinName cs; unsigned char frame_black[EPD_HEIGHT*EPD_WIDTH/8]; -uint32_t aTxBuffer[BUFFER_WORDS_SIZE]; -uint32_t aRxBuffer[BUFFER_WORDS_SIZE]; - - - - - -//SD card -//static void print_demo_title(void); -//static void print_PASS(void); -//static void print_FAIL(void); -//static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset); -//static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength); -static void print_demo_title(void) -{ - BSP_LCD_Init(); - BSP_LCD_Clear(LCD_COLOR_WHITE); - BSP_LCD_SetTextColor(LCD_COLOR_BLUE); - BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80); - BSP_LCD_SetTextColor(LCD_COLOR_WHITE); - BSP_LCD_SetBackColor(LCD_COLOR_BLUE); - BSP_LCD_SetFont(&Font24); - BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SDCARD", CENTER_MODE); - BSP_LCD_SetFont(&Font12); - BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE); - BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SDCard", CENTER_MODE); - BSP_LCD_SetFont(&Font20); -} - -//static void print_PASS(void) -//{ - // BSP_LCD_SetTextColor(LCD_COLOR_BLACK); - // BSP_LCD_SetBackColor(LCD_COLOR_GREEN); - // BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo OK", CENTER_MODE); - //while(1); -//} - -static void print_FAIL(void) -{ - BSP_LCD_SetBackColor(LCD_COLOR_RED); - BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo FAILED", CENTER_MODE); - // while(1); -} - -/** - * @brief Fills buffer with user predefined data. - * @param pBuffer: pointer on the buffer to fill - * @param uwBufferLenght: size of the buffer to fill - * @param uwOffset: first value to fill on the buffer - * @retval None - */ -static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) -{ - uint32_t tmpIndex = 0; - /* Put in global buffer different values */ - for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) { - pBuffer[tmpIndex] = tmpIndex + uwOffset; - } -} - -/** - * @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 - */ -static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength) -{ - while (BufferLength--) { - if (*pBuffer1 != *pBuffer2) { - return 1; - } - pBuffer1++; - pBuffer2++; - } - return 0; -} - //function to determine if within polygon geofence //first argument is an integer, the rest are floats @@ -212,9 +127,9 @@ } -float getTemp(float t) +float getTemp() { - + float t; // getTemp(tRead); if(ds1820.begin()) { ds1820.startConversion(); @@ -260,86 +175,118 @@ return t; } +int sdCard() +{ +//SD card + // Create and mount SDFileSystem + /*fs = new SDFileSystem(PI_3, PC_2, PD_3, PB_12, "sd"); // mosi, miso, sck, cs + serial.printf("Mounting file system...\r\n"); + int err = fs->mount(); + serial.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); + if (err) + return err; + + // Open the file. + serial.printf("Opening file '/sd/mytest/sdtest.txt'... "); + fp = fopen("/sd/mytest/sdtest.txt", "w+"); + serial.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n")); + + if (!fp) { + // Check whether directory '/sd/mytest' exists. + serial.printf("\r\nChecking directory '/sd/mytest'...\r\n"); + struct stat info; + err = stat("/sd/mytest", &info); + if (err) { + serial.printf("Directory '/sd/mytest' does not exist.\r\n"); + serial.printf("Trying to create it..."); + err = mkdir("/sd/mytest", 0777); + serial.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); + if (err) + return err; + } + + // Create a new 'sdtest.txt' file. + serial.printf("File not found, creating a new one...\r\n"); + fp = fopen("/sd/mytest/sdtest.txt", "w+"); + serial.printf("%s\r\n", (!fp ? "Failed :(" : "OK")); + if (!fp) { + error("error: %s (%d)\r\n", strerror(errno), -errno); + return errno; + } + } + for (int i = 0; i < 10; i++) { + serial.printf("Writing numbers (%d/%d)... ", i, 10); + err = fprintf(fp, " %d\r\n", i); + if (err < 0) { + serial.printf("Fail :(\r\n"); + error("error: %s (%d)\r\n", strerror(errno), -errno); + } else + serial.printf("OK\r\n"); + } + + serial.printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10); + err = fclose(fp); + serial.printf("Closing file '/sd/mytest/sdtest.txt'... "); + //serial.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); + //if (err) + // return err; + */ + //Mount the filesystem + sd.mount(); + + //Perform a write test + printf("\nWriting to SD card..."); + FILE *fp = fopen("/sd/sdtest.txt", "w"); + if (fp != NULL) { + fprintf(fp, "We're writing to an SD card!"); + fclose(fp); + printf("success!\n"); + } else { + printf("failed!\n"); + } + + //Perform a read test + printf("Reading from SD card..."); + fp = fopen("/sd/sdtest.txt", "r"); + if (fp != NULL) { + char c = fgetc(fp); + if (c == 'W') + printf("success!\n"); + else + printf("incorrect char (%c)!\n", c); + fclose(fp); + } else { + printf("failed!\n"); + } + + //Unmount the filesystem + sd.unmount(); + + +} + int main() { GPSSerial.baud(GPSBaud); wait(0.001); - uint8_t status; - print_demo_title(); - wait(0.2); - // Initialization - status = BSP_SD_Init(); - if (status == MSD_OK) { - BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT OK", LEFT_MODE); - } - else if (status == MSD_ERROR_SD_NOT_PRESENT) { - BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD CARD NOT FOUND", LEFT_MODE); - print_FAIL(); - } - else { - BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT FAIL", LEFT_MODE); - print_FAIL(); - } - // Erase - status = BSP_SD_Erase(BLOCK_START_ADDR, BLOCK_END_ADDR); - if (status == MSD_OK) { - BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE OK", LEFT_MODE); - } - else { - BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE FAIL", LEFT_MODE); - print_FAIL(); - } - - // Prepare the buffer to write - Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF); - serial.printf("tx = 0x%02X\n", aTxBuffer); - - // Write - status = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT); - //status = BSP_SD_WriteBlocks_DMA(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS); - if (status == MSD_OK) { - BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE OK", LEFT_MODE); - - } - else { - BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE FAIL", LEFT_MODE); - print_FAIL(); - } - - // Read - status = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT); - //status = BSP_SD_ReadBlocks_DMA(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS); - if (status == MSD_OK) { - BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ OK", LEFT_MODE); - serial.printf("rx = 0x%02X\n", aRxBuffer); - } - else { - BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ FAIL", LEFT_MODE); - print_FAIL(); - } - - // Compare data - if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) == 0) { - BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA OK", LEFT_MODE); - } - else { - BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA FAIL", LEFT_MODE); - print_FAIL(); - } - + //Variables int inpoly; float x[5] = { 1,4,4,1,1}; float y[5] = { 1,1,4,4,1}; - float xtest = 1.5; - float ytest = 2; - float cx = 0; - float cy = 0; - float r = 5; - float ans = 0; + float xtest; + float ytest; + float cx = 1; + float cy = 50; + float r = 15; + int ans = 0; + float liquidTemp = 0; + + wait(2); + while(1){ //inCircle(r, cx, cy, xtest, ytest); @@ -354,13 +301,22 @@ // serial.printf("%f\t%c\t%f\t%c\t%f\t%f\t%f\n\r",gps.longitude, gps.ns,gps.latitude,gps.ew, gps.alt, gps.geoid, gps.time); // serial.printf("%d:%d:%d",gps.hour,gps.minute,gps.seconed); // } - + sdCard(); while(1) { + //wait (1); + if (tgps.encode(GPSSerial.getc())) { displayInfo(); - wait(0.1); //little delay to prevent double writing + xtest = tgps.location.lng(); + ytest = tgps.location.lat(); + serial.printf("\r\n %3.6f, %3.6f, \r\n", ytest, xtest); + ans = inCircle(r, cx, cy, xtest, ytest); + wait(5); //little delay to prevent double writing + liquidTemp = getTemp(); + serial.printf("\r\n In geofence = %d: \r\n", ans); + } if (millis() > 5000 && tgps.charsProcessed() < 10) @@ -369,7 +325,7 @@ // while(true); break; } - + } serial.printf("%d\r\n",i);