Skip to content

Commit 3936e2e

Browse files
committed
[wip]: Step counter
1 parent 0757f70 commit 3936e2e

File tree

4 files changed

+118
-32
lines changed

4 files changed

+118
-32
lines changed

platformio.ini

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ lib_deps =
119119
; Common build environment for ESP32 platform
120120
[common:esp32]
121121
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.33/platform-espressif32.zip
122-
lib_ignore = WiFiNINA, WiFi101
122+
lib_ignore = WiFiNINA, WiFi101, WiFiNINA_-_Adafruit_Fork
123123
monitor_filters = esp32_exception_decoder, time
124124
; upload_speed = 921600
125125

@@ -148,7 +148,7 @@ board_build.core = earlephilhower
148148
board_build.filesystem_size = 0.5m
149149
build_flags = -DUSE_TINYUSB
150150
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
151-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
151+
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library, WiFiNINA_-_Adafruit_Fork
152152
lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040
153153

154154

@@ -197,13 +197,32 @@ board_build.filesystem = littlefs
197197
[env:featheresp32s2]
198198
extends = common:esp32
199199
board = featheresp32-s2
200-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2 -DBOARD_HAS_PSRAM -DBUILD_OFFLINE_ONLY
200+
build_flags =
201+
-DARDUINO_ADAFRUIT_FEATHER_ESP32S2
202+
-DBOARD_HAS_PSRAM
203+
-DBUILD_OFFLINE_ONLY
204+
; -DSDFAT_FILE_TYPE=3
201205
board_build.partitions = tinyuf2-partitions-4MB-noota.csv
202206
;board_build.partitions = tinyuf2-partitions-4MB.csv
203207
;build_type = debug
204208
monitor_filters = esp32_exception_decoder
205209
extra_scripts = pre:rename_usb_config.py
206210

211+
[env:featheresp32s2_debug]
212+
extends = env:featheresp32s2
213+
build_type = debug
214+
build_flags =
215+
-DARDUINO_ADAFRUIT_FEATHER_ESP32S2
216+
-DBOARD_HAS_PSRAM
217+
-DBUILD_OFFLINE_ONLY
218+
; -DSDFAT_FILE_TYPE=3
219+
-DDEBUG=1
220+
-DCFG_TUSB_DEBUG=1
221+
-DESP_LOG_LEVEL=ESP_LOG_VERBOSE
222+
-DARDUINO_CORE_DEBUG_LEVEL=5
223+
-DCORE_DEBUG_LEVEL=5
224+
-DARDUHAL_LOG_LEVEL=5
225+
207226
; Adafruit Feather ESP32-S2 TFT
208227
[env:adafruit_feather_esp32s2_tft]
209228
extends = common:esp32
@@ -467,43 +486,53 @@ build_flags = -DUSE_TINYUSB=1
467486
upload_port = /dev/cu.usbmodem1201
468487

469488
[env:raspberrypi_pico]
489+
extends = common:rp2040
470490
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#develop
471491
board = rpipico
472492
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
473493
board_build.filesystem_size = 0.5m
474494
build_flags = -DUSE_TINYUSB
475495
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
476-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
477496

478497
[env:raspberrypi_pico_2]
498+
extends = common:rp2040
479499
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#develop
480500
board = rpipico2
481501
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
482502
board_build.filesystem_size = 0.5m
483503
build_flags = -DUSE_TINYUSB -DBUILD_OFFLINE_ONLY
484504
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
485-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
505+
506+
[env:raspberrypi_pico_2w]
507+
extends: common:rp2040
508+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#develop
509+
board = rpipico2w
510+
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
511+
board_build.filesystem_size = 0.5m
512+
build_flags = -DUSE_TINYUSB -DBUILD_OFFLINE_ONLY
513+
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
486514

487515
[env:adafruit_feather_adalogger]
516+
extends = common:rp2040
488517
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#develop
489518
board = adafruit_feather_adalogger
490519
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
491520
board_build.filesystem_size = 0.5m
492521
build_flags = -DUSE_TINYUSB
493522
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
494-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
495523

496524
[env:adafruit_metro_rp2350]
525+
extends = common:rp2040
497526
platform = https://github.com/brentru/platform-raspberrypi.git#develop
498527
board = adafruit_metro_rp2350
499528
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
500529
board_build.filesystem_size = 0.5m
501530
build_flags = -DUSE_TINYUSB -DBUILD_OFFLINE_ONLY
502531
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
503-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
504532

505533

506534
[env:adafruit_metro_rp2350_debug]
535+
extends = common:rp2040
507536
platform = https://github.com/brentru/platform-raspberrypi.git#develop
508537
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git
509538
board = adafruit_metro_rp2350
@@ -542,7 +571,6 @@ build_flags =
542571
; build_flags = -DPIO_FRAMEWORK_ARDUINO_NO_USB
543572
; -DPIO_FRAMEWORK_ARDUINO_ENABLE_IPV6
544573
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
545-
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
546574

547575

548576
[env:raspberypi_picow]

src/components/i2c/drivers/drvIsm330dhcx.cpp

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,58 +46,95 @@ bool drvIsm330dhcx::begin() {
4646
_imu->setAccelDataRate(LSM6DS_RATE_104_HZ);
4747
_imu->setGyroRange(LSM6DS_GYRO_RANGE_500_DPS);
4848
_imu->setGyroDataRate(LSM6DS_RATE_104_HZ);
49+
_imu->highPassFilter(true, LSM6DS_HPF_ODR_DIV_100);
4950
_imu->configInt1(false, false, false, false, true);
5051
_imu->configInt2(false, false, false);
52+
_imu->enablePedometer(true);
5153
_imu->enableWakeup(true);
5254

5355
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Sensor initialised successfully");
5456
return true;
5557
}
5658

5759
bool drvIsm330dhcx::readAllEvents(sensors_event_t *accel, sensors_event_t *gyro,
58-
sensors_event_t *temp) {
60+
sensors_event_t *temp, boolean *shake, uint16_t *steps) {
5961
if (!_imu) {
6062
return false;
6163
}
64+
if (_lastPoll > 0 && _internalPollPeriod > 0 && millis() - _lastPoll < _internalPollPeriod) {
65+
// Limit polling to every 200ms, return cache, but not first time
66+
return true;
67+
}
68+
_lastPoll = millis(); // TODO: set in fastTicks, if used, instead
69+
if (_imu->shake()) {
70+
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Shake detected!");
71+
*shake = true;
72+
}
73+
uint16_t step_change = _imu->readPedometer();
74+
if (step_change > 0) {
75+
WS_DEBUG_PRINT("[drvIsm330dhcx] Steps detected: ")
76+
WS_DEBUG_PRINTLN(step_change);
77+
steps = steps + step_change;
78+
_imu->resetPedometer();
79+
}
6280
return _imu->getEvent(accel, gyro, temp);
6381
}
6482

83+
bool drvIsm330dhcx::readAllEvents() {
84+
return readAllEvents(&_lastAccelEvent, &_lastGyroEvent, &_lastTempEvent,
85+
&_last_shake, &_last_steps);
86+
}
87+
6588
bool drvIsm330dhcx::computeAccelMagnitude(float &magnitude) {
66-
sensors_event_t accel, gyro, temp;
67-
if (!readAllEvents(&accel, &gyro, &temp)) {
89+
if (!readAllEvents()) {
6890
return false;
6991
}
70-
magnitude = sqrtf(accel.acceleration.x * accel.acceleration.x +
71-
accel.acceleration.y * accel.acceleration.y +
72-
accel.acceleration.z * accel.acceleration.z);
92+
magnitude = sqrtf(_lastAccelEvent.acceleration.x * _lastAccelEvent.acceleration.x +
93+
_lastAccelEvent.acceleration.y * _lastAccelEvent.acceleration.y +
94+
_lastAccelEvent.acceleration.z * _lastAccelEvent.acceleration.z);
7395
return true;
7496
}
7597

7698
bool drvIsm330dhcx::getEventRaw(sensors_event_t *rawEvent) {
7799
if (!_imu) {
78100
return false;
79101
}
80-
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Getting raw magnitude event...");
81-
float magnitude = 0.0f;
82-
if (!computeAccelMagnitude(magnitude)) {
102+
103+
if (!readAllEvents()) {
83104
return false;
84105
}
85-
rawEvent->data[0] = magnitude;
86-
WS_DEBUG_PRINT("[drvIsm330dhcx] Raw magnitude: ");
87-
WS_DEBUG_PRINTLN(magnitude);
106+
// Return step count as raw event, counter cleared at read
107+
rawEvent->data[0] = (float)_last_steps;
108+
_last_steps = 0;
88109
return true;
110+
111+
112+
////MAGNITUDE ONLY - DISABLED FOR NOW
113+
// WS_DEBUG_PRINTLN("[drvIsm330dhcx] Getting raw magnitude event...");
114+
// float magnitude = 0.0f;
115+
// if (!computeAccelMagnitude(magnitude)) {
116+
// return false;
117+
// }
118+
// rawEvent->data[0] = magnitude;
119+
// WS_DEBUG_PRINT("[drvIsm330dhcx] Raw magnitude: ");
120+
// WS_DEBUG_PRINTLN(magnitude);
121+
// return true;
89122
}
90123

91124
bool drvIsm330dhcx::getEventBoolean(sensors_event_t *booleanEvent) {
92125
if (!_imu) {
93126
return false;
94127
}
95-
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Checking for tap/threshold event...");
96-
bool tap = _imu->shake();
97-
booleanEvent->data[0] = tap ? 1.0f : 0.0f;
98-
if (tap) {
99-
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Threshold event detected");
128+
if (!readAllEvents()) {
129+
return false;
130+
}
131+
WS_DEBUG_PRINT("[drvIsm330dhcx] Checking for shake/tap/threshold event...");
132+
WS_DEBUG_PRINTLN(_last_shake ? " DETECTED!" : " none.");
133+
booleanEvent->data[0] = _last_shake ? 1.0f : 0.0f;
134+
if (_last_shake) {
135+
WS_DEBUG_PRINTLN("[drvIsm330dhcx] *** Threshold event detected ***");
100136
}
137+
_last_shake = false;
101138
return true;
102139
}
103140

@@ -106,17 +143,23 @@ bool drvIsm330dhcx::getEventAccelerometer(sensors_event_t *accelEvent) {
106143
return false;
107144
}
108145
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Getting accelerometer event...");
109-
sensors_event_t gyro, temp;
110-
return readAllEvents(accelEvent, &gyro, &temp);
146+
bool result = readAllEvents()
147+
? memcpy(accelEvent, &_lastAccelEvent, sizeof(sensors_event_t)),
148+
true
149+
: false;
150+
return result;
111151
}
112152

113153
bool drvIsm330dhcx::getEventGyroscope(sensors_event_t *gyroEvent) {
114154
if (!_imu) {
115155
return false;
116156
}
117157
WS_DEBUG_PRINTLN("[drvIsm330dhcx] Getting gyroscope event...");
118-
sensors_event_t accel, temp;
119-
return readAllEvents(&accel, gyroEvent, &temp);
158+
bool result = readAllEvents()
159+
? memcpy(gyroEvent, &_lastGyroEvent, sizeof(sensors_event_t)),
160+
true
161+
: false;
162+
return result;
120163
}
121164

122165
void drvIsm330dhcx::ConfigureDefaultSensorTypes() {

src/components/i2c/drivers/drvIsm330dhcx.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,27 @@ class drvIsm330dhcx : public drvBase {
2525
bool getEventGyroscope(sensors_event_t *gyroEvent) override;
2626

2727
void ConfigureDefaultSensorTypes() override;
28+
void setInternalPollingInterval(uint32_t interval_ms) { // override {
29+
// Polling interval is managed internally.
30+
_internalPollPeriod = interval_ms < 0 ? 0 : interval_ms;
31+
}
2832

2933
private:
3034
bool readAllEvents(sensors_event_t *accel, sensors_event_t *gyro,
31-
sensors_event_t *temp);
35+
sensors_event_t *temp, boolean *shake,
36+
uint16_t *steps);
37+
bool readAllEvents();
3238
bool computeAccelMagnitude(float &magnitude);
3339

40+
bool _has_last_events = false; ///< Flag to track if last events are stored
41+
bool _last_shake = false; ///< Last state of shake / tap detection
42+
sensors_event_t _lastAccelEvent; ///< Last accelerometer event
43+
sensors_event_t _lastGyroEvent; ///< Last gyroscope event
44+
sensors_event_t _lastTempEvent; ///< Last temperature event (raw)
45+
uint16_t _last_steps = 0; ///< Last step count
46+
uint32_t _lastPoll = 0; ///< Last poll time
47+
uint32_t _internalPollPeriod = 200; ///< Internal Polling interval in ms
48+
3449
Adafruit_ISM330DHCX *_imu = nullptr; ///< Pointer to the ISM330DHCX sensor
3550
};
3651

src/components/i2c/drivers/drvLsm6dso32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool drvLsm6dso32::begin() {
3333
WS_DEBUG_PRINTHEX(addr);
3434
WS_DEBUG_PRINTLN("...");
3535

36-
if (!_imu->begin_I2C(addr, _i2c)) {
36+
if (!_imu->begin_I2C(addr, _i2c)) { // consider 3rd argument of sensor id, ensure we can run two of these. Could pass mux(+255+muxADDR) add 1000 for bus 1 vs 0 and add sensor addr
3737
WS_DEBUG_PRINTLN("[drvLsm6dso32] Failed to initialise sensor");
3838
delete _imu;
3939
_imu = nullptr;
@@ -44,7 +44,7 @@ bool drvLsm6dso32::begin() {
4444
_imu->setAccelDataRate(LSM6DS_RATE_104_HZ);
4545
_imu->setGyroRange(LSM6DS_GYRO_RANGE_500_DPS);
4646
_imu->setGyroDataRate(LSM6DS_RATE_104_HZ);
47-
_imu->configInt1(false, false, false, false, true);
47+
_imu->configInt1(false, false, false, true, true);
4848
_imu->configInt2(false, false, false);
4949
_imu->enableWakeup(true);
5050

0 commit comments

Comments
 (0)