STM32L476

Dependencies:   MbedJSONValue SDFileSystem WConstants mbed-dev DS1820 TinyGPSPlus epd1in54

Fork of A_SSL_Main by SilentSensors

Revision:
4:06edbeb05013
Parent:
3:bc4495101c7b
Child:
5:4525141f69d2
--- a/main.cpp	Wed Aug 22 18:13:37 2018 +0000
+++ b/main.cpp	Tue Aug 28 07:45:20 2018 +0000
@@ -5,6 +5,16 @@
 #include "DS1820.h"
 #include "epd1in54.h"
 #include "stdio.h"
+#include "math.h"
+//#include "GPS.h"
+#include "TinyGPSPlus.h"
+
+
+#define TX5 PG_7
+#define RX5 PG_8
+#define GPSBaud 9600
+#define Serial0Baud 115200
+   
 
 #define NUM_OF_BLOCKS            5
 #define BLOCK_START_ADDR         0
@@ -12,6 +22,21 @@
 #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;
+
+void displayInfo(void);
+
+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);
  DS1820  ds1820(PG_15);    //pin name connected to the DS1820 data pin
@@ -37,150 +62,15 @@
 uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
 
 
-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);
-
-
-int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
-{
-  int i, j, c = 0;
-  for (i = 0, j = nvert-1; i < nvert; j = i++) {
-    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
-     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
-       c = !c;
-  }
-  return c;
-}
-
-
-float getTemp(float t)
-{
-
-    // getTemp(tRead);
-
-    if(ds1820.begin()) {
-        ds1820.startConversion();
-        wait(1.0);
-        t = ds1820.read();         // read temperature
-        tCompare;
-        serial.printf("temp = %3.1f\r\n", t);     // Display temp on terminal
-
-    } else
-        serial.printf("No DS1820 sensor found!\r\n");
 
 
 
-    
-    mosi = PB_5;
-    miso = PB_4;
-    sclk = PA_5;
-    cs = PA_15;
-    rst = PI_6;
-    dc = PG_6;
-    busy = PI_11;
-
-    char cVal[32];
-    //char* t_str;
-    sprintf(cVal,"%.2f", t);
-    
-    
-     
-    memset(frame_black, 0xFF, sizeof(unsigned char)*EPD_HEIGHT*EPD_WIDTH/8);
-    Epd epd = Epd(mosi, miso, sclk, cs, dc, rst, busy);
-    
-    
-    if (epd.Init(lut_full_update) != 0) {
-        return -1;
-    }
-    /*Write strings to the buffer */
-    epd.DrawStringAt(frame_black, 50, 30, cVal, &Font24, COLORED);
-    epd.DrawStringAt(frame_black, 28, 10, "Temperature", &Font16, COLORED);
-    /* Display the frame_buffer */
-    epd.SetFrameMemory(frame_black, 0, 0, epd.width, epd.height);
-    epd.DisplayFrame();
-    epd.Sleep();
-    return t;
-}
-
-int main()
-{
-    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);
- 
-    // Write
-    status = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT);
-    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);
-    if (status == MSD_OK) {
-      BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ OK", LEFT_MODE);
-    }
-    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();
-    }
-
-    int inpoly;
-    float x[5] = { 1,4,4,1,1};
-    float y[5] = { 1,1,4,4,1};
-    float xtest = 2;
-    float ytest = 2;
-  
-  while(1){ 
-    tempSerial = getTemp(t);
-    inpoly = pnpoly(5,x, y, xtest,ytest);
-    serial.printf("inpoly = %d\r\n", inpoly);
-    wait(5);
-  }
-}
-
+//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();
@@ -248,3 +138,248 @@
 }
 
 
+//function to determine if within polygon geofence 
+//first argument is an integer, the rest are floats
+//The last vertix should be the same as the first vertix
+int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
+{
+  int i, j, c = 0;
+  for (i = 0, j = nvert-1; i < nvert; j = i++) {
+    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
+     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
+       c = !c;
+  }
+  return c;
+}
+
+
+int inCircle(float radius, float xcentre, float ycentre, float testx, float testy)
+{
+    float distance;
+    int test;
+    distance = (sqrt((testx-xcentre)*(testx-xcentre)+(testy-ycentre)*(testy-ycentre)));
+       
+     if(distance < radius){
+            test = 1;
+            }
+        else{
+            test = 0;
+            }
+  return (test);
+}
+
+void displayInfo(void)
+{
+  serial.printf("Location: "); 
+  if (tgps.location.isValid())
+  {
+    serial.printf("%3.6f, %3.6f", tgps.location.lat(), tgps.location.lng());
+  }
+  else
+  {
+    serial.printf("INVALID");
+  }
+
+  serial.printf(" - Date/Time: ");
+  if (tgps.date.isValid())
+  {
+    serial.printf("%d/", tgps.date.year());
+    if (tgps.date.month() < 10) serial.printf("0");
+    serial.printf("%d/", tgps.date.month());
+    if (tgps.date.day() < 10) serial.printf("0");
+    serial.printf("%d", tgps.date.day());
+  }
+  else
+  {
+    serial.printf("INVALID");
+  }
+
+  serial.printf(" ");
+  if (tgps.time.isValid())
+  {
+    if (tgps.time.hour() < 10) serial.printf("0");
+    serial.printf("%d:", tgps.time.hour());
+    if (tgps.time.minute() < 10) serial.printf("0");
+    serial.printf("%d:", tgps.time.minute());
+    if (tgps.time.second() < 10) serial.printf("0");
+    serial.printf("%d\r\n", tgps.time.second());
+  }
+  else
+  {
+    serial.printf("INVALID\r\n");
+  }
+}
+
+
+float getTemp(float t)
+{
+
+    // getTemp(tRead);
+    if(ds1820.begin()) {
+        ds1820.startConversion();
+        wait(1.0);
+        t = ds1820.read();         // read temperature
+        tCompare;
+        serial.printf("temp = %3.1f\r\n", t);     // Display temp on terminal
+
+    } else
+        serial.printf("No DS1820 sensor found!\r\n");
+
+
+
+    
+    mosi = PB_5;
+    miso = PB_4;
+    sclk = PA_5;
+    cs = PA_15;
+    rst = PI_6;
+    dc = PG_6;
+    busy = PI_11;
+
+    char cVal[32];
+    //char* t_str;
+    sprintf(cVal,"%.2f", t);
+    
+    
+     
+    memset(frame_black, 0xFF, sizeof(unsigned char)*EPD_HEIGHT*EPD_WIDTH/8);
+    Epd epd = Epd(mosi, miso, sclk, cs, dc, rst, busy);
+    
+    
+    if (epd.Init(lut_full_update) != 0) {
+        return -1;
+    }
+    /*Write strings to the buffer */
+    epd.DrawStringAt(frame_black, 50, 30, cVal, &Font24, COLORED);
+    epd.DrawStringAt(frame_black, 28, 10, "Temperature", &Font16, COLORED);
+    /* Display the frame_buffer */
+    epd.SetFrameMemory(frame_black, 0, 0, epd.width, epd.height);
+    epd.DisplayFrame();
+    epd.Sleep();
+    return t;
+}
+
+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();
+    }
+
+    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;
+ 
+  while(1){ 
+    //inCircle(r, cx, cy, xtest, ytest);
+    //serial.printf("rx =  0x%02X\n", aRxBuffer);
+    //tempSerial = getTemp(t);
+    //inpoly = pnpoly(5,x, y, xtest,ytest);
+    //serial.printf("inpoly = %d\r\n", inpoly);
+    //ans = inCircle(r, cx, cy, xtest, ytest);
+    //serial.printf("distance = %3.1f\r\n", ans);
+   // GPS
+   //if(gps.sample()){
+     //         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);
+     //       }
+   
+   while(1) {
+
+        if (tgps.encode(GPSSerial.getc()))
+        {
+            displayInfo();
+            wait(0.1); //little delay to prevent double writing
+        }
+
+        if (millis() > 5000 && tgps.charsProcessed() < 10)
+        {
+            serial.printf("No GPS detected: check wiring.\r\n");
+//            while(true);
+            break;
+        }
+
+
+      }
+      serial.printf("%d\r\n",i);
+      i++;
+      wait(1.0);
+    }
+
+    
+    wait(5);
+  }
+
+
+
+