.clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL
};
+static int step_count = 0;
+static bool is_paused = false;
+static uint32_t pause_start_ms = 0;
+
+ const int16_t SWING_THRESHOLD = 15000;
+ const uint32_t PAUSE_DURATION_MS = 500;
+
/* ICM 20948 configuration */
icm0948_config_i2c_t icm_config = {
.i2c_port = I2C_NUM_0,
.i2c_addr = ICM_20948_I2C_ADDR_AD1
};
+void step_counter(icm20948_agmt_t agmt) {
+
+ int16_t x = agmt.acc.axes.x; // x-axis
+ if (x < 0) x = -x; // absolute value
+
+ uint32_t now_ms = xTaskGetTickCount() * portTICK_PERIOD_MS;
+
+ // If currently paused, check if time is up
+ if (is_paused) {
+ if (now_ms - pause_start_ms >= PAUSE_DURATION_MS) {
+ is_paused = false; // pause finished
+ }
+ return; // idling
+ }
+
+ // check for the arm movement
+ if (x >= SWING_THRESHOLD) {
+ step_count += 2; // add 2 steps
+ ESP_LOGI(TAG, "Swing detected. Total steps: %d", step_count);
+
+ is_paused = true; // add a buffer to prevent over counting
+ pause_start_ms = now_ms;
+ }
+
+}
+
+float calculate_calories_from_steps(int steps, float height_cm, float weight_kg) {
+
+ float stride_m = (height_cm / 100.0f) * 0.415f;
+
+ float distance_m = steps * stride_m;
+
+ float calories = (distance_m / 1000.0f) * weight_kg * 0.55f;
+
+ return calories;
+}
void print_agmt(icm20948_agmt_t agmt)
{
icm20948_agmt_t agmt; // = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0}};
if (icm20948_get_agmt(&icm, &agmt) == ICM_20948_STAT_OK) {
print_agmt(agmt);
+ step_counter(agmt);
+ ESP_LOGI(TAG, "Calories burned: %.2f kcal\n Step Count: %d", calculate_calories_from_steps(step_count, 170.0f, 65.5f), step_count);
} else {
ESP_LOGE(TAG, "Uh oh");
}