tortuga DataLogging Library: battery, speed, time, average...

Dependents:   TORTUGA_BLE

Fork of DataLogging by aapje monkey

Revision:
4:f91f45d52f9b
Parent:
3:610ace66d7af
Child:
5:fad416fb6979
--- a/BikeData.cpp	Tue Jul 12 11:54:41 2016 +0000
+++ b/BikeData.cpp	Fri Jul 15 12:48:06 2016 +0000
@@ -1,17 +1,21 @@
 #include "mbed.h"
 #include "BikeData.h"
 #include "Data.h"
-#include "eeprom.h"
+#include "_24LCXXX.h"
 
-#define Period 1 //1000ms period of sampling
-#define PPR 3    // Pulses Per Rotation
-#define periphery 1.3 //periphery of the wheel in meters
+#define Period 1        //1000ms period of sampling
+#define PPR 51          // Pulses Per Rotation
+#define periphery 1.595928736  //periphery of the wheel in meters
 
 #define EEPROM_ADDR 0x0   // I2c EEPROM address is 0x00
 #define SDA PB_9            // I2C SDA pin
 #define SCL PB_8           // I2C SCL pin
 
-EEPROM mem(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32);
+
+I2C i2c(PB_9, PB_8);
+Serial pc(SERIAL_TX, SERIAL_RX);
+_24LCXXX mem(&i2c);
+
 /*
 EEPROM ADRESSES:
 
@@ -28,37 +32,65 @@
     count(0),
     lastDistance(0)
     {
+       
         //setting things up for logging the raw data.
         float readDistance;
-        mem.read(0,readDistance);
-        int32_t readTime;
-        mem.read(4,readTime);
+        printf("reading...\n");
+        if(mem.nbyte_read(0,&readDistance,sizeof(float))){
+            printf("eeprom reading error");
+            }
+        printf("data read: %f\n",readDistance);
+        uint32_t readTime;
+        if(mem.nbyte_read(4,&readTime,sizeof(uint32_t))){
+            printf("eeprom reading error");
+            }
+        printf("data read: %#x\n",readTime);
+        
         overallData = new Data(readDistance,readTime,PPR,periphery);
         dataSet[OVERALL] = overallData;
         batChangeData = new Data(0.0,0,PPR,periphery);
         dataSet[BATCHANGE] = batChangeData;
+        
         tick.attach(this,&BikeData::interval,Period);
         button.fall(this,&BikeData::pressed);
     }
     
 int8_t BikeData::getBikeNameSize(){
+    //printf("size retrieving\n");
     int8_t size = 0;
-    mem.read(8,size);
+    //mem.read(8,size);
+    if(mem.nbyte_read(8,&size,sizeof(int8_t))){
+            printf("eeprom reading error");
+            }
+    printf("size retrieved: %i\n", size);
     return size;
     }
     
 void BikeData::getBikeName(char *name){
+    printf("name retrieving\n");
     uint8_t size = getBikeNameSize();
     //uint8_t* name;
-    mem.read(9,name,size);
-    printf("name retrieved\n");
+    //mem.read(9,name, size);
+    if(mem.nbyte_read(9,name,size)){
+            printf("eeprom reading error");
+            }
+    //printf("name retrieved\n");
     //return name;
     }
     
 void BikeData::setBikeName(char *name, uint8_t length){
-    mem.write(8,length);
-    mem.write(9,name,length);
-    printf("name set\n");
+    printf("setting bike name size to eeprom\n");
+    //mem.write(8,(int8_t)length);
+    printf("length: %i\n", length);
+    if(mem.nbyte_write(8,&length,1)){
+            printf("eeprom writing error");
+            }
+    //printf("setting bike name to eeprom\n");
+    //mem.write(9,name, length);
+    if(mem.nbyte_write(9,name,length)){
+            printf("eeprom writing error");
+            }
+    //printf("name set\n");
     
     }
     
@@ -100,10 +132,6 @@
     
 //interval function is called each second to monitor the progress by the datasets
 void BikeData::interval(void){
-        //keep the pointers well placed
-        dataSet[TRIP] = tripData;
-        dataSet[OVERALL] = overallData;
-        dataSet[BATCHANGE] = batChangeData;
         //calculate the distances
         lastDistance = count * periphery / PPR;
         lastCount = count;
@@ -115,12 +143,19 @@
          }
         //printf("Overall:\n------------------\n");
         overallData->interval(count);
-        mem.write(0,overallData->getDistance()); // update the eeprom values
-        mem.write(4,(int32_t)overallData->getTime());
+        
+        // update the eeprom values
+        float overallDistance = overallData->getDistance();
+        if(mem.nbyte_write(0,&overallDistance,4)){
+            printf("eeprom writing error distance\n");
+            }
+        uint32_t overallTime = overallData->getTime();
+        if(mem.nbyte_write(4,&overallTime,4)){
+            printf("eeprom writing error time\n");
+            }
         
         //printf("batChange:\n------------------\n");
         batChangeData->interval(count);
-        
         //printf("speed: %f\ncount: %i\n",lastDistance/Period * 3.6, lastCount); // debugging
         count = 0;
     }