Просмотр исходного кода

Using define instead of magic numbers. Started to implement speed vs sleep time logic

tags/v1.2.0
Bertrand Lemasle 7 лет назад
Родитель
Сommit
0d24ef07e3
8 измененных файлов: 81 добавлений и 34 удалений
  1. +26
    -0
      GpsTracker/Config.h
  2. +22
    -0
      GpsTracker/Core.cpp
  3. +2
    -0
      GpsTracker/Core.h
  4. +7
    -3
      GpsTracker/Gps.cpp
  5. +2
    -1
      GpsTracker/Gps.h
  6. +1
    -0
      GpsTracker/GpsTracker.h
  7. +6
    -3
      GpsTracker/GpsTracker.ino
  8. +15
    -27
      GpsTracker/Time2.h

+ 26
- 0
GpsTracker/Config.h Просмотреть файл

@@ -0,0 +1,26 @@
#pragma once

struct sleepTimings_t {
uint8_t speed;
uint16_t seconds;
};

#define SLEEP_DEFAULT_TIME_SECONDS 1800

#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000
#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 30000

namespace config {

static const sleepTimings_t defaultSleepTimings[] PROGMEM = {
{ 5, SLEEP_DEFAULT_TIME_SECONDS },
{ 10, 1200 },
{ 20, 600 },
{ 30, 540 },
{ 50, 480 },
{ 80, 240 },
{ 100, 210 },
{ 180, 180 },
};

}

+ 22
- 0
GpsTracker/Core.cpp Просмотреть файл

@@ -1,7 +1,29 @@
#include "Core.h"
#include "Config.h"

#define LOGGER_NAME "Core"

namespace core {
uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;;

namespace details {
template<typename T, size_t N> size_t getFlashArraySize(T(&)[N]) { return N; }
template<typename T> void readFromFlash(const T *source, T &dest) {
memcpy_P(&dest, source, sizeof(T));
}
}

void setSleepTime(uint8_t velocity) {
for (uint8_t i = 0; i < details::getFlashArraySize(config::defaultSleepTimings); i++) {
sleepTimings_t timing;
details::readFromFlash(&config::defaultSleepTimings[i], timing);

if (velocity > timing.speed) continue;

sleepTime = timing.seconds;
return;
}

sleepTime = SLEEP_DEFAULT_TIME_SECONDS;
}
}

+ 2
- 0
GpsTracker/Core.h Просмотреть файл

@@ -5,5 +5,7 @@
#include "Debug.h"

namespace core {
extern uint16_t sleepTime;

void setSleepTime(uint8_t velocity);
}

+ 7
- 3
GpsTracker/Gps.cpp Просмотреть файл

@@ -1,10 +1,10 @@
#include "Gps.h"
#include "Config.h"
#include "Debug.h"
#include "Hardware.h"
#include "MainUnit.h"

#define LOGGER_NAME "Gps"
#define WAIT_FOR_FIX_DELAY 10000

#define TIME_YEAR_OFFSET 0
#define TIME_MONTH_OFFSET 4
@@ -27,8 +27,8 @@ namespace gps {
currentStatus = hardware::sim808::device.getGpsStatus();
if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break;

mainunit::deepSleep(WAIT_FOR_FIX_DELAY);
timeout -= WAIT_FOR_FIX_DELAY;
mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS);
timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
} while (timeout > 1);

if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -39,6 +39,10 @@ namespace gps {
return currentStatus;
}

void getVelocity(uint8_t &velocity) {
hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity);
}

void getTime(tmElements_t &time) {
char *timeStr;
char buffer[4];


+ 2
- 1
GpsTracker/Gps.h Просмотреть файл

@@ -16,6 +16,7 @@ namespace gps {
inline void powerOff() { hardware::sim808::gpsPowerOff(); }

SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout);
void getTime(tmElements_t &time);

void getVelocity(uint8_t &velocity);
void getTime(tmElements_t &time);
}

+ 1
- 0
GpsTracker/GpsTracker.h Просмотреть файл

@@ -5,6 +5,7 @@
#include <SIM808.h>

#include "Debug.h"
#include "Config.h"
#include "Core.h"

#include "Gps.h"


+ 6
- 3
GpsTracker/GpsTracker.ino Просмотреть файл

@@ -13,9 +13,8 @@ void setup() {
}

void loop() {

gps::powerOn();
SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(30000);
SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
gps::powerOff();

if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -26,11 +25,15 @@ void loop() {
rtc::powerOff();

positions::appendLast();

uint8_t velocity;
gps::getVelocity(velocity);
core::setSleepTime(velocity);
}

if (positions::needsToSend()) {
positions::send();
}

mainunit::deepSleep(10); //TODO : duration
mainunit::deepSleep(core::sleepTime);
}

+ 15
- 27
GpsTracker/Time2.h Просмотреть файл

@@ -10,30 +10,18 @@

/*============================================================================*/

typedef unsigned long timestamp_t;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;

/* low level functions to convert to and from system time */
void breakTime(const timestamp_t time, tmElements_t &tm); // break timestamp_t into elements
timestamp_t makeTime(const tmElements_t &tm); // convert time elements into timestamp_t

tmElements_t operator += (const timestamp_t b) {

}

tmElements_t operator + (const tmElements_t a, const timestamp_t b) {
tmElements_t result;
timestamp_t aTimestamp = makeTime(a);
breakTime(aTimestamp + b, result);

return result;
}
typedef unsigned long timestamp_t;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;

/* low level functions to convert to and from system time */
void breakTime(const timestamp_t time, tmElements_t &tm); // break timestamp_t into elements
timestamp_t makeTime(const tmElements_t &tm); // convert time elements into timestamp_t

Загрузка…
Отмена
Сохранить