Skip to content

Commit 1f92bd8

Browse files
committed
feat(pir): added motion sensor implementation
1 parent 589a9b4 commit 1f92bd8

7 files changed

Lines changed: 143 additions & 5 deletions

File tree

app/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ config ENABLE_ANALOG
8787
help
8888
Enable support for Analog sensor.
8989

90+
config ENABLE_MOTION_SENSOR
91+
bool "Enable Motion Sensor"
92+
default n
93+
help
94+
Enable support for Motion sensor.
95+
9096
source "Kconfig.zephyr"
9197
endmenu
9298

app/boards/nucleo_wl55jc.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
zephyr,user {
99
io-channels = <&adc1 6>;
1010
io-channels-names = "a0";
11+
motion-gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
1112
};
1213
};
1314

app/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CONFIG_I2C=y
1414
CONFIG_GPIO=y
1515
CONFIG_SPI=y
1616

17+
CONFIG_BQ274XX=y
1718
CONFIG_BQ274XX_PM=y
1819

1920
CONFIG_PM_DEVICE=y
@@ -46,7 +47,7 @@ CONFIG_SETTINGS_RUNTIME=y
4647
CONFIG_FLASH=y
4748
CONFIG_FLASH_MAP=y
4849

49-
CONFIG_BOOTLOADER_MCUBOOT=y
50+
CONFIG_BOOTLOADER_MCUBOOT=n
5051

5152
# ADC config
5253
CONFIG_ADC=y

app/src/Application.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55
#include "sensor.hpp"
66

77
// Number of supported sensor types used in the sensors array
8-
#define BASE_SENSOR_COUNT 1
8+
#define BASE_SENSOR_COUNT 0
99

1010
#ifdef CONFIG_ENABLE_ANALOG
1111
#define ANALOG_SENSOR_COUNT 1 // Analog sensor is enabled
1212
#else
1313
#define ANALOG_SENSOR_COUNT 0 // Analog sensor is disabled
1414
#endif
1515

16+
#ifdef CONFIG_ENABLE_MOTION_SENSOR
17+
#define MOTION_SENSOR_COUNT 1 // Motion sensor is enabled
18+
#else
19+
#define MOTION_SENSOR_COUNT 0 // Motion sensor is disabled
20+
#endif
21+
1622
// The final fixed size is calculated by the preprocessor
17-
#define NUMBER_OF_SENSORS (BASE_SENSOR_COUNT + ANALOG_SENSOR_COUNT)
23+
#define NUMBER_OF_SENSORS (BASE_SENSOR_COUNT + ANALOG_SENSOR_COUNT + MOTION_SENSOR_COUNT)
1824

1925
class LoRaWANHandler;
2026
class SleepManager;

app/src/main.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "sensors/bme280/bme280.hpp"
1111
#include "sensors/analog/analog.hpp"
1212
#include "sensors/bq27441/bq27441.hpp"
13+
#include "sensors/motion_sensor/pir.hpp"
1314
#include "utils/banner.hpp"
1415
#include "utils/sleep-manager.hpp"
1516

@@ -21,22 +22,37 @@ LOG_MODULE_REGISTER(main_entry, LOG_LEVEL_DBG);
2122
#define APP_SLEEP_DURATION_MS 10000
2223
#endif
2324

25+
#ifdef CONFIG_ENABLE_ANALOG
2426
static const struct adc_dt_spec soil_sensor_adc_spec =
2527
ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);
28+
#endif
29+
30+
#ifdef CONFIG_ENABLE_MOTION_SENSOR
31+
static const struct gpio_dt_spec motion_sensor_gpio_spec =
32+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), motion_gpios);
33+
#endif
2634

2735

2836
int main(void) {
2937
printk("%s\n", APP_ASCII_BANNER);
3038
LOG_INF("===== Buzzverse Node System Booting (Zephyr Log) =====");
3139

32-
BME280 bme280(DEVICE_DT_GET_ANY(bosch_bme280));
40+
//BME280 bme280(DEVICE_DT_GET_ANY(bosch_bme280));
41+
#ifdef CONFIG_ENABLE_ANALOG
3342
Analog analog(&soil_sensor_adc_spec);
43+
#endif
44+
#ifdef CONFIG_ENABLE_MOTION_SENSOR
45+
MotionSensor motion_sensor(&motion_sensor_gpio_spec);
46+
#endif
3447

3548
// Array of available sensors
3649
etl::array<etl::unique_ptr<Sensor>, NUMBER_OF_SENSORS> sensors {
37-
etl::unique_ptr<BME280>(etl::move(&bme280)),
50+
//etl::unique_ptr<BME280>(etl::move(&bme280)),
3851
#ifdef CONFIG_ENABLE_ANALOG
3952
etl::unique_ptr<Analog>(etl::move(&analog)),
53+
#endif
54+
#ifdef CONFIG_ENABLE_MOTION_SENSOR
55+
etl::unique_ptr<MotionSensor>(etl::move(&motion_sensor)),
4056
#endif
4157
};
4258

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "pir.hpp"
2+
3+
#include <zephyr/drivers/gpio.h>
4+
#include <zephyr/logging/log.h>
5+
6+
#include "buzzverse/motion_sensor.pb.h"
7+
8+
LOG_MODULE_REGISTER(MotionSensor, LOG_LEVEL_DBG);
9+
10+
MotionSensor::MotionSensor(const struct gpio_dt_spec* gpio_spec)
11+
: m_gpio_spec(gpio_spec) {}
12+
13+
using Status = Sensor::Status;
14+
15+
Peripheral::Status MotionSensor::init() {
16+
if (!m_gpio_spec || !device_is_ready(m_gpio_spec->port)) {
17+
status = buzzverse_v1_Status_ComponentState_INITIALIZATION_FAILED;
18+
LOG_WRN("MotionSensor: GPIO device not ready or spec invalid");
19+
return Peripheral::Status::NOT_READY;
20+
}
21+
22+
int ret = gpio_pin_configure_dt(m_gpio_spec, GPIO_INPUT);
23+
if (ret != 0) {
24+
LOG_ERR("MotionSensor: Failed to configure GPIO pin %d (err %d)", m_gpio_spec->pin, ret);
25+
status = buzzverse_v1_Status_ComponentState_INITIALIZATION_FAILED;
26+
return Peripheral::Status::INIT_ERR;
27+
}
28+
29+
LOG_INF("MotionSensor: GPIO ready on pin %d", m_gpio_spec->pin);
30+
status = buzzverse_v1_Status_ComponentState_NORMAL;
31+
ready = true;
32+
return Peripheral::Status::OK;
33+
}
34+
35+
Status MotionSensor::read_data(buzzverse_v1_MotionSensorData& data) const {
36+
int val = gpio_pin_get_dt(m_gpio_spec);
37+
if (val < 0) {
38+
LOG_ERR("MotionSensor: GPIO read failed: %d", val);
39+
return Status::READ_ERR;
40+
}
41+
42+
data.motion_detected = (val > 0);
43+
44+
LOG_DBG("MotionSensor: GPIO raw: %d, motion detected: %d", val, data.motion_detected);
45+
return Status::OK;
46+
}
47+
48+
Status MotionSensor::get_packet(buzzverse_v1_Packet& packet) const {
49+
buzzverse_v1_MotionSensorData motion_data = buzzverse_v1_MotionSensorData_init_zero;
50+
51+
if (read_data(motion_data) != Sensor::Status::OK) {
52+
return Status::READ_ERR;
53+
}
54+
55+
packet = buzzverse_v1_Packet_init_default;
56+
packet.which_data = buzzverse_v1_Packet_motion_sensor_tag;
57+
packet.data.motion_sensor = motion_data;
58+
LOG_DBG("Packet constructed with motion sensor data.");
59+
return Status::OK;
60+
}
61+
62+
void MotionSensor::get_status(buzzverse_v1_Status& status_message) const {
63+
status_message.motion_status = status;
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef PIR_HPP
2+
#define PIR_HPP
3+
4+
#include <etl/string.h>
5+
#include <zephyr/drivers/gpio.h>
6+
7+
#include "buzzverse/motion_sensor.pb.h"
8+
#include "sensors/sensor.hpp"
9+
10+
class MotionSensor : public Sensor {
11+
public:
12+
explicit MotionSensor(const struct gpio_dt_spec* gpio_spec);
13+
14+
Peripheral::Status init() override;
15+
16+
bool is_ready() const override {
17+
return ready;
18+
}
19+
20+
etl::string<PERIPHERAL_NAME_SIZE> get_name() const override {
21+
return "MotionSensor";
22+
}
23+
24+
Status get_packet(buzzverse_v1_Packet& packet) const override;
25+
26+
void get_status(buzzverse_v1_Status& status_message) const override;
27+
28+
private:
29+
const struct gpio_dt_spec* m_gpio_spec;
30+
bool ready{false};
31+
buzzverse_v1_Status_ComponentState status{buzzverse_v1_Status_ComponentState_STATE_UNSPECIFIED};
32+
33+
/**
34+
* @brief Read data from the sensor
35+
*
36+
* @param data MotionSensor data structure to populate
37+
* @return Status
38+
* @retval OK if the data is successfully read
39+
* @retval READ_ERR if the sensor read fails
40+
*/
41+
Status read_data(buzzverse_v1_MotionSensorData& data) const;
42+
};
43+
44+
#endif // PIR_HPP

0 commit comments

Comments
 (0)