tortuga DataLogging Library: battery, speed, time, average...
Fork of DataLogging by
BikeData.cpp@2:f3c2bf5521e5, 2016-04-30 (annotated)
- Committer:
- ptuytsch
- Date:
- Sat Apr 30 13:29:23 2016 +0000
- Revision:
- 2:f3c2bf5521e5
- Child:
- 3:610ace66d7af
adding BikeData;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 2:f3c2bf5521e5 | 1 | #include "mbed.h" |
ptuytsch | 2:f3c2bf5521e5 | 2 | #include "BikeData.h" |
ptuytsch | 2:f3c2bf5521e5 | 3 | #include "Data.h" |
ptuytsch | 2:f3c2bf5521e5 | 4 | #include "eeprom.h" |
ptuytsch | 2:f3c2bf5521e5 | 5 | |
ptuytsch | 2:f3c2bf5521e5 | 6 | #define Period 1 //1000ms |
ptuytsch | 2:f3c2bf5521e5 | 7 | |
ptuytsch | 2:f3c2bf5521e5 | 8 | #define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00 |
ptuytsch | 2:f3c2bf5521e5 | 9 | #define SDA PB_9 // I2C SDA pin |
ptuytsch | 2:f3c2bf5521e5 | 10 | #define SCL PB_8 // I2C SCL pin |
ptuytsch | 2:f3c2bf5521e5 | 11 | |
ptuytsch | 2:f3c2bf5521e5 | 12 | EEPROM mem(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32); |
ptuytsch | 2:f3c2bf5521e5 | 13 | /* |
ptuytsch | 2:f3c2bf5521e5 | 14 | EEPROM ADRESSES: |
ptuytsch | 2:f3c2bf5521e5 | 15 | |
ptuytsch | 2:f3c2bf5521e5 | 16 | 0->3: overall distance |
ptuytsch | 2:f3c2bf5521e5 | 17 | 4->7: overall time |
ptuytsch | 2:f3c2bf5521e5 | 18 | */ |
ptuytsch | 2:f3c2bf5521e5 | 19 | |
ptuytsch | 2:f3c2bf5521e5 | 20 | //CONSTRUCTOR |
ptuytsch | 2:f3c2bf5521e5 | 21 | BikeData::BikeData(PinName pin) : |
ptuytsch | 2:f3c2bf5521e5 | 22 | button(pin), |
ptuytsch | 2:f3c2bf5521e5 | 23 | trackTrip(false), |
ptuytsch | 2:f3c2bf5521e5 | 24 | count(0) |
ptuytsch | 2:f3c2bf5521e5 | 25 | { |
ptuytsch | 2:f3c2bf5521e5 | 26 | float readDistance; |
ptuytsch | 2:f3c2bf5521e5 | 27 | mem.read(0,readDistance); |
ptuytsch | 2:f3c2bf5521e5 | 28 | int32_t readTime; |
ptuytsch | 2:f3c2bf5521e5 | 29 | mem.read(4,readTime); |
ptuytsch | 2:f3c2bf5521e5 | 30 | overallData = new Data(readDistance,readTime); |
ptuytsch | 2:f3c2bf5521e5 | 31 | dataSet[OVERALL] = tripData; |
ptuytsch | 2:f3c2bf5521e5 | 32 | batChangeData = new Data(0,0); |
ptuytsch | 2:f3c2bf5521e5 | 33 | dataSet[BATCHANGE] = tripData; |
ptuytsch | 2:f3c2bf5521e5 | 34 | tick.attach(this,&BikeData::interval,Period); |
ptuytsch | 2:f3c2bf5521e5 | 35 | button.fall(this,&BikeData::pressed); |
ptuytsch | 2:f3c2bf5521e5 | 36 | } |
ptuytsch | 2:f3c2bf5521e5 | 37 | |
ptuytsch | 2:f3c2bf5521e5 | 38 | void BikeData::startTrip(){ |
ptuytsch | 2:f3c2bf5521e5 | 39 | tripData = new Data(0,0); |
ptuytsch | 2:f3c2bf5521e5 | 40 | dataSet[TRIP] = tripData; |
ptuytsch | 2:f3c2bf5521e5 | 41 | trackTrip = true; |
ptuytsch | 2:f3c2bf5521e5 | 42 | } |
ptuytsch | 2:f3c2bf5521e5 | 43 | |
ptuytsch | 2:f3c2bf5521e5 | 44 | void BikeData::pauzeTrip(){ |
ptuytsch | 2:f3c2bf5521e5 | 45 | trackTrip = false; |
ptuytsch | 2:f3c2bf5521e5 | 46 | } |
ptuytsch | 2:f3c2bf5521e5 | 47 | |
ptuytsch | 2:f3c2bf5521e5 | 48 | void BikeData::stopTrip(){ |
ptuytsch | 2:f3c2bf5521e5 | 49 | trackTrip = false; |
ptuytsch | 2:f3c2bf5521e5 | 50 | } |
ptuytsch | 2:f3c2bf5521e5 | 51 | |
ptuytsch | 2:f3c2bf5521e5 | 52 | void BikeData::pressed(void){ |
ptuytsch | 2:f3c2bf5521e5 | 53 | count++; |
ptuytsch | 2:f3c2bf5521e5 | 54 | } |
ptuytsch | 2:f3c2bf5521e5 | 55 | |
ptuytsch | 2:f3c2bf5521e5 | 56 | Data* BikeData::getDataSet(uint8_t type){ |
ptuytsch | 2:f3c2bf5521e5 | 57 | return dataSet[type]; |
ptuytsch | 2:f3c2bf5521e5 | 58 | } |
ptuytsch | 2:f3c2bf5521e5 | 59 | |
ptuytsch | 2:f3c2bf5521e5 | 60 | void BikeData::interval(void){ |
ptuytsch | 2:f3c2bf5521e5 | 61 | if(trackTrip){ |
ptuytsch | 2:f3c2bf5521e5 | 62 | printf("Trip:\n"); |
ptuytsch | 2:f3c2bf5521e5 | 63 | tripData->interval(count); |
ptuytsch | 2:f3c2bf5521e5 | 64 | } |
ptuytsch | 2:f3c2bf5521e5 | 65 | printf("Overall:\n"); |
ptuytsch | 2:f3c2bf5521e5 | 66 | overallData->interval(count); |
ptuytsch | 2:f3c2bf5521e5 | 67 | mem.write(0,overallData->getDistance()); |
ptuytsch | 2:f3c2bf5521e5 | 68 | mem.write(4,(int32_t)overallData->getTime()); |
ptuytsch | 2:f3c2bf5521e5 | 69 | printf("batChange:\n"); |
ptuytsch | 2:f3c2bf5521e5 | 70 | batChangeData->interval(count); |
ptuytsch | 2:f3c2bf5521e5 | 71 | count = 0; |
ptuytsch | 2:f3c2bf5521e5 | 72 | } |