init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Sat Dec 15 20:09:19 2018 +0000
Parent:
10:c8798fd9773b
Child:
12:8345612bf867
Commit message:
3

Changed in this revision

SI7060.cpp Show annotated file Show diff for this revision Revisions of this file
SI7060.h Show annotated file Show diff for this revision Revisions of this file
filesystem.cpp Show annotated file Show diff for this revision Revisions of this file
filesystem.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI7060.cpp	Sat Dec 15 20:09:19 2018 +0000
@@ -0,0 +1,41 @@
+#include "SI7060.h"
+
+//DOES NOT WORK, RETURNS -47.400002
+
+uint8_t _address = I2C_ADDRESS_SI7060_01 << 1;
+
+SI7060::SI7060(PinName sda, PinName scl):  _i2c(sda,scl)
+{
+    requireSoftReset = true;  //TODO: this can be removed when i2c sleep issue resolved    
+}
+
+char SI7060::readValue(uint8_t registerAdd) {
+    char _ret[1];
+    char cmd[1];
+    cmd[0] = registerAdd;
+    _i2c.write(_address, cmd, 1);
+    _i2c.read(_address, _ret, 1);
+    return _ret[0];
+};
+void SI7060::writeValue(uint8_t registerAdd, uint8_t value) {
+    char cmd[2];
+    cmd[0] = registerAdd;
+    cmd[1] = value;
+    _i2c.write(_address, cmd, 2);
+};
+
+float SI7060::getTemperature(void) 
+{
+    float _temp;
+    uint8_t _Dspsigm;
+    uint8_t _Dspsigl;
+    uint8_t _ret;
+    _ret = readValue(CMD_SI7060_Dspsigm);
+    _Dspsigm = (_ret&0x7F);
+    _ret = readValue(CMD_SI7060_Dspsigl);
+    _Dspsigl = _ret;
+    _temp = 55+ ((float)(256*_Dspsigm)+(float)(_Dspsigl-16384))/160;
+    return _temp;
+}
+
+SI7060::~SI7060(){};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI7060.h	Sat Dec 15 20:09:19 2018 +0000
@@ -0,0 +1,37 @@
+#ifndef SI7060_H
+#define SI7060_H
+
+#include "main.h"
+
+namespace mbed {
+
+    #define I2C_ADDRESS_SI7060_00 0x30
+    #define I2C_ADDRESS_SI7060_01 0x31
+    #define I2C_ADDRESS_SI7060_02 0x32
+    #define I2C_ADDRESS_SI7060_03 0x33
+    #define CMD_SI7060_ID 0xC0
+    #define CMD_SI7060_Dspsigm 0xC1 
+    #define CMD_SI7060_Dspsigl 0xC2 
+    #define CMD_SI7060_meas 0xC4 
+    #define CMD_SI7060_sw_op 0xC6 
+    #define CMD_SI7060_sw_hyst 0xC7 
+    
+    class SI7060
+    {
+        public:  
+            SI7060(PinName sda, PinName scl);
+            ~SI7060();
+            
+            //funcs
+            char readValue(uint8_t registerAdd);
+            void writeValue(uint8_t registerAdd, uint8_t value);
+            float getTemperature(void);
+            
+        private:
+        
+        protected:
+            I2C _i2c;
+    };
+
+} //Namespace
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/filesystem.cpp	Sat Dec 15 20:09:19 2018 +0000
@@ -0,0 +1,72 @@
+#include "filesystem.h"
+
+#define FILENAME_TEMPERATURE "log_temperature.txt"
+#define FILENAME_LOCATION "log_location.txt"
+#define FILENAME_ACTIVITY "log_activity.txt"
+#define FILENAME_PENDING "pending_broadcasts.txt"
+
+Filesystem::Filesystem(PinName mosi, PinName miso, PinName clk, PinName cs):  bd(mosi,miso,clk,cs), fs("fs")
+{
+    /*int err;
+    DEBUG("Mounting the filesystem... ");
+    fflush(stdout);
+    err = fs.mount(&_bd);
+    DEBUG("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        // Reformat if we can't mount the filesystem
+        // this should only happen on the first boot
+        DEBUG("No filesystem found, formatting... ");
+        fflush(stdout);
+        err = fs.reformat(&bd);
+        DEBUG("%s\n", (err ? "Fail :(" : "OK"));
+    }*/
+}
+
+void Filesystem::addLogEntry_temperature(time_t timestamp, float temperature) 
+{
+    int err;
+    //MOUNT FS
+    DEBUG("Mounting the filesystem... ");
+    fflush(stdout);
+    err = fs.mount(&bd);
+    DEBUG("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        // Reformat if we can't mount the filesystem
+        DEBUG("No filesystem found, formatting... ");
+        fflush(stdout);
+        err = fs.reformat(&bd);
+        DEBUG("%s\n", (err ? "Fail :(" : "OK"));
+    }
+    //WRITE TO FILE
+    fflush(stdout);
+    FILE *f = fopen("/fs/log_temperature.txt", "a+");
+    DEBUG("%s\n", (!f ? "No File :(" : "OK"));
+    if (f) {
+        fflush(stdout);
+        err = fprintf(f, "%u,%.1f\r\n", timestamp, temperature); //needs \r\n for new line
+        DEBUG("Data written to fs\n");
+        if (err < 0) {
+            DEBUG("error: %s (%d)\n", strerror(errno), -errno);
+        }  
+    } else {
+        DEBUG("error: %s (%d)\n", strerror(errno), -errno);
+    }
+    //SAVE FILE
+    DEBUG("Closing file... ");
+    fflush(stdout);
+    err = fclose(f);
+    DEBUG("%s\n", (err < 0 ? "Fail :(" : "OK"));
+    if (err < 0) {
+        DEBUG("error: %s (%d)\n", strerror(errno), -errno);
+    }
+    //UNMOUNT
+    DEBUG("Unmounting... ");
+    fflush(stdout);
+    err = fs.unmount();
+    DEBUG("%s\n", (err < 0 ? "Fail :(" : "OK"));
+    if (err < 0) {
+        DEBUG("error: %s (%d)\n", strerror(-err), err);
+    }
+}
+
+Filesystem::~Filesystem(){};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/filesystem.h	Sat Dec 15 20:09:19 2018 +0000
@@ -0,0 +1,33 @@
+#ifndef FILESYSTEM_H
+#define FILESYSTEM_H
+
+#include "main.h"
+
+#include "SDBlockDevice.h"
+
+//#include "LittleFileSystem.h"
+#include "FATFileSystem.h"
+
+//#define SD_MOUNT_PATH   "sd"
+
+namespace mbed {
+    
+    class Filesystem
+    {
+        public:  
+            Filesystem(PinName mosi, PinName miso, PinName clk, PinName cs);
+            ~Filesystem();
+            
+            //funcs
+            void addLogEntry_temperature(time_t timestamp, float temperature);
+            
+        private:
+        
+        protected:
+            SDBlockDevice bd;
+            //LittleFileSystem fs;
+            FATFileSystem fs;
+    };
+
+} //Namespace
+#endif
--- a/main.cpp	Sat Dec 15 14:33:59 2018 +0000
+++ b/main.cpp	Sat Dec 15 20:09:19 2018 +0000
@@ -68,10 +68,11 @@
 //------------------------------------------------------------------------------ 
 //BLE myble;
 WatchdogTimer watchdog(65.0); //Do not set to less than 4500ms or can cause issues with softdevice
-#if NEED_CONSOLE_OUTPUT
-Serial uart(PN_UART_TX, PN_UART_RX, 115200);
+//SERIAL DEBUG?
+#if CONSOLE_DEBUG
+    Serial uart(PN_UART_TX, PN_UART_RX, 115200);
 #endif
-
+        
 //------------------------------------------------------------------------------
 //TIMERS
 //------------------------------------------------------------------------------ 
@@ -332,6 +333,14 @@
         if (RET_coldBoot != 1) gotoSleep(30000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
         watchdog.kick();
         
+        Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
+        
+        //GET TEMPERATURE
+        SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
+        float temperature_c = temperature.getTemperature();
+        DEBUG("temp: %f \n", temperature_c);
+        filesystem.addLogEntry_temperature(RET_RTCunixtime, 10.5);
+        
         //MAIN LOGIC
         DEBUG("mode: %i  time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
         mainStateEngine();
--- a/main.h	Sat Dec 15 14:33:59 2018 +0000
+++ b/main.h	Sat Dec 15 20:09:19 2018 +0000
@@ -22,6 +22,7 @@
 #include "board.h"
 #include "WatchdogTimer.h"
 #include "ble/BLE.h"
+
 //#include "nvstore.h"
 
 
@@ -29,7 +30,7 @@
 //peripheral Libraries
 //------------------------------------------------------------------------------
 #include "LIS3DH.h"
-
+#include "SI7060.h"
 
 //------------------------------------------------------------------------------
 //C Standard Libraries
@@ -57,6 +58,7 @@
 #include "common.h"
 #include "sensors.h"
 #include "modem.h"
+#include "filesystem.h"
 
 //------------------------------------------------------------------------------
 //Global macros
@@ -64,12 +66,12 @@
 #define lowByte(w) ((uint8_t) ((w) & 0xff))
 #define highByte(w) ((uint8_t) ((w) >> 8))
 
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */
-#if NEED_CONSOLE_OUTPUT
+#define CONSOLE_DEBUG 1 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */
+#if CONSOLE_DEBUG
 #define DEBUG(...) { uart.printf(__VA_ARGS__); }
 #else
 #define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+#endif /* #if CONSOLE_DEBUG */
 
 //------------------------------------------------------------------------------
 //Global data structures
@@ -77,5 +79,5 @@
 
 //VARS
 extern bool requireSoftReset;
-
+extern Serial uart;
 #endif 
--- a/mbed_app.json	Sat Dec 15 14:33:59 2018 +0000
+++ b/mbed_app.json	Sat Dec 15 20:09:19 2018 +0000
@@ -11,6 +11,7 @@
             "target.uart_hwfc": 0,
             "target.device_has_add": ["LOWPOWERTIMER"],
             "target.macros_add": ["MBED_TICKLESS"],
+            "target.components_add": ["SD"],
             "nvstore.max_keys": 20,
             "nvstore.area_1_address": "0x0007e000",
             "nvstore.area_1_size"   : "0x1000",