Skip to content

Commit

Permalink
Allow IMU_SKIP to be changed by button.
Browse files Browse the repository at this point in the history
Progress further to allow timezones to be set by button by allowing IMU_SKIP and timezone settings to be set in and recalled from NVS.
  • Loading branch information
johnheenan committed May 5, 2020
1 parent dedbbdc commit 119ddb8
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 99 deletions.
15 changes: 0 additions & 15 deletions include/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
#include "hal.hpp"
#include "translations.hpp"

extern const char *tz_dst_name;
extern month_t tz_dst_month;
extern int8_t tz_dst_hour;
extern int tz_dst_offset;

extern week_t tz_week;
extern dow_t tz_wday;
extern bool tz_uses_dst;
extern int tz_offset;

extern const char *tz_std_name;
extern month_t tz_std_month;
extern int8_t tz_std_hour;
extern int tz_std_offset;

extern time_t utcDST;

void initClock();
Expand Down
39 changes: 35 additions & 4 deletions include/eeprom.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
// Edits and additions by John Heenan
#pragma once
#include <Arduino.h>
#include <EEPROM.h>

#define MAG_CALIBRATION_ADDRESS 0x00
#include <Preferences.h> // now recommended to use NVS with Preferences library rather than EEPROM library
#include <NTP.h> // for enums

#define MAGBIAS_FLOATS_MAX 3
void storeMagBiasEEPROM(float *magbias);
void getMagBiasEEPROM(float *magbias);
void getMagBiasEEPROM(float *magbias);

// Following added by John Heenan, 2020

#define SETTINGS_STRLEN_MAX 10

typedef struct
{
float magbias[3];
bool imu_skip;
bool dst_none;

bool tz_uses_dst;
int tz_offset;
week_t tz_week;
dow_t tz_wday;
char tz_dst_name[SETTINGS_STRLEN_MAX + 1];
month_t tz_dst_month;
int8_t tz_dst_hour;
int tz_dst_offset;
char tz_std_name[SETTINGS_STRLEN_MAX + 1];
month_t tz_std_month;
int8_t tz_std_hour;
int tz_std_offset;

} settings_t;

extern settings_t settings;
size_t storeSettings();
size_t loadSettings();
1 change: 0 additions & 1 deletion include/mpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "hal.hpp"
#include "MPU9250.hpp"
#include "wristband-tft.hpp"
#include "eeprom.hpp"

#define GYRO_CALIBRATION_BASE_ADDRESS 0x10
#define MAG_CALIBRATION_BASE_ADDRESS 0x20
Expand Down
2 changes: 2 additions & 0 deletions include/pages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "pages/page-battery.hpp"
#include "pages/page-bearing.hpp"
#include "pages/page-temperature.hpp"
#include "pages/page-imu.hpp"
#include "eeprom.h"

void handleUi();
void increasePage();
Expand Down
6 changes: 6 additions & 0 deletions include/pages/page-imu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Arduino.h>
#include "wristband-tft.hpp"
#include "mpu.hpp"
#include "eeprom.h"

void actionIMU();
1 change: 1 addition & 0 deletions include/wristband-tft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <SPI.h>
#include <Wire.h>
#include <hal.hpp>
#include "eeprom.hpp"

#define SEG7_BACKGROUND 0x0821

Expand Down
25 changes: 24 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ build_flags =
-DIMU_SKIP # Remove comment to keep IMU (accelerometer, gyrometer and magnetometer) shut down
; -DDST_NONE # See timezone.hpp. Do not use daylight savings time, make sure TZ_offset is correct in timezone.hpp
; -DDST_CEST_CET # The default if no DST_XXX is defined. Northern Hemisphere example
; -DDST_AEDT_AEST # Southern Hemisphere example
;-DDST_AEDT_AEST # Southern Hemisphere example

;upload_protocol = espota
;upload_port = 192.168.1.177
Expand All @@ -95,3 +95,26 @@ upload_port = COM36
upload_speed = 2000000
monitor_port = COM36

[env:po]
platform = espressif32
board = esp32dev
framework = ${common_env_data.framework}
monitor_speed = ${common_env_data.monitor_speed}
lib_ldf_mode = ${common_env_data.lib_ldf_mode}
lib_deps = ${common_env_data.lib_deps}
build_flags =
${common_env_data.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
-DDEBUG_PORT=Serial # see hal.hpp as there is no configuration.h file
; -DEEPROM_REINIT
-DIMU_SKIP # now used for an initial setting that can be changed
; -DDST_NONE # See timezone.hpp. Do not use daylight savings time, make sure TZ_offset is correct in timezone.hpp
; -DDST_CEST_CET # The default if no DST_XXX is defined. Northern Hemisphere example
; -DDST_AEDT_AEST # Southern Hemisphere example

;upload_protocol = espota
;upload_port = 192.168.1.177
;upload_flags = --auth=wristbandpass
upload_port = COM36
upload_speed = 2000000
monitor_port = COM36
43 changes: 13 additions & 30 deletions src/hardware/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,10 @@

#include "clock.hpp"
#include "timezone.hpp"
#include "eeprom.hpp"

PCF8563_Class rtc;

uint8_t current_dayweek = 8;
uint8_t current_minute = 0;

bool tz_uses_dst = TZ_USES_DST;
int tz_offset = TZ_OFFSET;
week_t tz_week = TZ_WEEK;
dow_t tz_wday = TZ_WDAY;

const char *tz_dst_name = TZ_DST_NAME;
month_t tz_dst_month = TZ_DST_MONTH;
int8_t tz_dst_hour = TZ_DST_HOUR;
int tz_dst_offset = TZ_DST_OFFSET;

const char *tz_std_name = TZ_STD_NAME;
month_t tz_std_month = TZ_STD_MONTH;
int8_t tz_std_hour = TZ_STD_HOUR;
int tz_std_offset = TZ_STD_OFFSET;

void initClock()
{
rtc.begin(Wire);
Expand Down Expand Up @@ -63,8 +46,8 @@ RTC_Date getUTCTime()

if (!isUsingDST())
{
timeStructure.tm_hour = now.hour - tz_offset / 60;
timeStructure.tm_min = now.minute - tz_offset % 60;
timeStructure.tm_hour = now.hour - settings.tz_offset / 60;
timeStructure.tm_min = now.minute - settings.tz_offset % 60;
utcNow = mktime(&timeStructure);
utcStructure = localtime(&utcNow);
}
Expand All @@ -73,13 +56,13 @@ RTC_Date getUTCTime()
if (!utcDST) //todo jh also call beginDST if year changes
beginDST(now.year);

timeStructure.tm_hour = now.hour - tz_dst_offset / 60;
timeStructure.tm_min = now.minute - tz_dst_offset % 60;
timeStructure.tm_hour = now.hour - settings.tz_dst_offset / 60;
timeStructure.tm_min = now.minute - settings.tz_dst_offset % 60;
utcNowIfDST = mktime(&timeStructure);
summerIfDST = isDST(utcNowIfDST);

timeStructure.tm_hour = now.hour - tz_std_offset / 60;
timeStructure.tm_min = now.minute - tz_std_offset % 60;
timeStructure.tm_hour = now.hour - settings.tz_std_offset / 60;
timeStructure.tm_min = now.minute - settings.tz_std_offset % 60;
utcNowIfSTD = mktime(&timeStructure);
summerIfSTD = isDST(utcNowIfSTD);

Expand All @@ -105,12 +88,12 @@ void setTime(RTC_Date datetime)

bool isUsingDST()
{
return tz_uses_dst;
return settings.tz_uses_dst;
}

bool isNothernHemispere()
{
return tz_dst_month < tz_std_month;
return settings.tz_dst_month < settings.tz_std_month;
}

// Next three functions contain code adapted by John Heenan from Arduino NTP code
Expand Down Expand Up @@ -166,10 +149,10 @@ time_t calcDateDST(int year, int8_t week, int8_t wday, int8_t month, int8_t hour

void beginDST(int year)
{
time_t dstTime = calcDateDST(year, tz_week, tz_wday, tz_dst_month, tz_dst_hour);
utcDST = dstTime - (tz_dst_offset * SECS_PER_MINUTES);
time_t stdTime = calcDateDST(year, tz_week, tz_wday, tz_std_month, tz_std_hour);
utcSTD = stdTime - (tz_std_offset * SECS_PER_MINUTES);
time_t dstTime = calcDateDST(year, settings.tz_week, settings.tz_wday, settings.tz_dst_month, settings.tz_dst_hour);
utcDST = dstTime - (settings.tz_dst_offset * SECS_PER_MINUTES);
time_t stdTime = calcDateDST(year, settings.tz_week, settings.tz_wday, settings.tz_std_month, settings.tz_std_hour);
utcSTD = stdTime - (settings.tz_std_offset * SECS_PER_MINUTES);
}

bool isDST(time_t utcNow)
Expand Down
81 changes: 69 additions & 12 deletions src/hardware/eeprom.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,79 @@

// Edits and additions by John Heenan 2020. Converted to use Preferences
#include "eeprom.hpp"
#include "timezone.hpp"

Preferences prefs;
settings_t settings;

void storeMagBiasEEPROM(float *magbias)
{
EEPROM.begin(3 * sizeof(float));
for (uint8_t i = 0; i < 3; i++)
{
EEPROM.put(MAG_CALIBRATION_ADDRESS + (i * sizeof(float)), magbias[i]);
}
EEPROM.commit();
EEPROM.end();
settings.magbias[0] = magbias[0];
settings.magbias[1] = magbias[1];
settings.magbias[2] = magbias[2];
storeSettings();
}

void getMagBiasEEPROM(float *magbias)
{
EEPROM.begin(3 * sizeof(float));
for (uint8_t i = 0; i < 3; i++)
magbias[0] = settings.magbias[0];
magbias[1] = settings.magbias[1];
magbias[2] = settings.magbias[2];
}


size_t storeSettings()
{
return prefs.putBytes("settings", &settings, sizeof(settings));
}

char *strcpy_settings(char *to, const char *from)
{
size_t len = strlen(from);
if (len > SETTINGS_STRLEN_MAX)
len = SETTINGS_STRLEN_MAX;
len++;
memcpy(&settings.tz_dst_name, to, len);
settings.tz_dst_name[len + 1] = 0;
return to + len;
}

size_t loadSettings() //must be called first before any other functions on this page
{
prefs.begin("eeprom");
size_t settingsLen = 0;
#ifndef EEPROM_REINIT
settingsLen = prefs.getBytesLength("settings");
#endif
if (settingsLen){
size_t len= prefs.getBytes("settings", &settings, settingsLen);
return len;
}
else
{
magbias[i] = EEPROM.read(MAG_CALIBRATION_ADDRESS + (i * sizeof(float)));
settings.magbias[0] = 0.0;
settings.magbias[1] = 0.0;
settings.magbias[2] = 0.0;
#ifdef IMU_SKIP
settings.imu_skip = true;
#else
settings.imu_skip = false;
#endif
settings.tz_uses_dst = TZ_USES_DST;
settings.tz_offset = TZ_OFFSET;
settings.tz_week = TZ_WEEK;
settings.tz_wday = TZ_WDAY;

strcpy_settings(settings.tz_dst_name, TZ_DST_NAME);
settings.tz_dst_month = TZ_DST_MONTH;
settings.tz_dst_hour = TZ_DST_HOUR;
settings.tz_dst_offset = TZ_DST_OFFSET;

strcpy_settings(settings.tz_std_name, TZ_STD_NAME);
settings.tz_std_month = TZ_STD_MONTH;
settings.tz_std_hour = TZ_STD_HOUR;
settings.tz_std_offset = TZ_STD_OFFSET;

return storeSettings();
}
EEPROM.end();
}
}
15 changes: 6 additions & 9 deletions src/hardware/sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
void handleSleep(bool showMsg)
{
//tftSleep(showMsg);
mpuSleep();
#ifndef IMU_SKIP
mpuSleep();
#endif
if (!settings.imu_skip)
mpuSleep();
tftSleep(false);
deactivateWifi();
rtcSleep();
pinMode(39, GPIO_MODE_INPUT);
#ifndef IMU_SKIP
esp_sleep_enable_ext1_wakeup(GPIO_SEL_33 | GPIO_SEL_39, ESP_EXT1_WAKEUP_ANY_HIGH);
#else
esp_sleep_enable_ext1_wakeup(GPIO_SEL_33, ESP_EXT1_WAKEUP_ANY_HIGH);
#endif
if (!settings.imu_skip)
esp_sleep_enable_ext1_wakeup(GPIO_SEL_33 | GPIO_SEL_39, ESP_EXT1_WAKEUP_ANY_HIGH);
else
esp_sleep_enable_ext1_wakeup(GPIO_SEL_33, ESP_EXT1_WAKEUP_ANY_HIGH);
esp_deep_sleep_disable_rom_logging();
esp_deep_sleep_start();
}
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

void setup()
{
loadSettings();
Serial.begin(115200);
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
Wire.setClock(400000);
Expand All @@ -17,11 +18,10 @@ void setup()
deactivateWifi();
btStop();
setupADC();
#ifndef IMU_SKIP
initMPU();
#else
mpuDeepSleep();
#endif
if (!settings.imu_skip)
initMPU();
else
mpuDeepSleep();
initButton();
setupBattery();
}
Expand Down
18 changes: 18 additions & 0 deletions src/pages/page-imu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "pages/page-imu.hpp"

void actionIMU()
{
settings.imu_skip = settings.imu_skip ? false : true;
storeSettings();
if (settings.imu_skip)
{
mpuDeepSleep();
msgInfo("IMU (Acceler) is OFF", "Watch using less power");
}
else
{
initMPU();
msgInfo("IMU (Acceler) is ON", "Watch using more power");
}
sleep(3);
}
Loading

1 comment on commit 119ddb8

@johnheenan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is information about this branch here TioRuben#8 (comment)

Please sign in to comment.