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

Dependents:   TORTUGA_BLE

Fork of DataLogging by aapje monkey

BikeData.cpp

Committer:
ptuytsch
Date:
2016-07-12
Revision:
3:610ace66d7af
Parent:
2:f3c2bf5521e5
Child:
4:f91f45d52f9b

File content as of revision 3:610ace66d7af:

#include "mbed.h"
#include "BikeData.h"
#include "Data.h"
#include "eeprom.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 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);
/*
EEPROM ADRESSES:

0->3: overall distance
4->7: overall time
8: name size
9->...: bike name
*/

//CONSTRUCTOR
BikeData::BikeData(PinName pin) : 
    button(pin),
    logging(false),
    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);
        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(){
    int8_t size = 0;
    mem.read(8,size);
    return size;
    }
    
void BikeData::getBikeName(char *name){
    uint8_t size = getBikeNameSize();
    //uint8_t* name;
    mem.read(9,name,size);
    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");
    
    }
    
void BikeData::startTrip(){
    //start trip function, creating new tripdata object
    tripData = new Data(0.0,0,PPR,periphery);
    dataSet[TRIP] = tripData; // setting the pointer position to the tripdata object
    logging = true; // start logging
    }
    
void BikeData::pauzeTrip(){
    logging = !logging;
    }

void BikeData::stopTrip(){
    logging = false;
    }
    
void BikeData::pressed(void){
        //count each pulse of the sensor
        count++;
    }
    
Data* BikeData::getDataSet(uint8_t type){
        return dataSet[type];
    }
    
float BikeData::getSpeed(void){
    return lastDistance/Period * 3.6;
    }
    
uint8_t BikeData::getLastCount(void){
    return lastCount;
   } 
   
bool BikeData::isLogging(void){
    return logging;
   } 
    
//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;
        
        //printf("\n==================\n\n"); // debugging
        if(logging){ // only interval when needed
             //printf("Trip:\n------------------\n");
             tripData->interval(count); // send interval to the tripdata object
         }
        //printf("Overall:\n------------------\n");
        overallData->interval(count);
        mem.write(0,overallData->getDistance()); // update the eeprom values
        mem.write(4,(int32_t)overallData->getTime());
        
        //printf("batChange:\n------------------\n");
        batChangeData->interval(count);
        
        //printf("speed: %f\ncount: %i\n",lastDistance/Period * 3.6, lastCount); // debugging
        count = 0;
    }