From 946b34f5c2667aa6d6efb8d75d55cc2a50978cc8 Mon Sep 17 00:00:00 2001 From: DAsamhan <23522019@std.stei.itb.ac.id> Date: Sat, 29 Jun 2024 10:18:56 +0700 Subject: [PATCH] add callback --- .../drivers/ble-scanner/src/ble-scanner.cpp | 4 ++++ debug-app/drivers/ir-driver/src/ir-driver.cpp | 2 +- .../module/driver-hook/src/driver-hook.cpp | 21 +++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/debug-app/drivers/ble-scanner/src/ble-scanner.cpp b/debug-app/drivers/ble-scanner/src/ble-scanner.cpp index 7c15760..532870a 100644 --- a/debug-app/drivers/ble-scanner/src/ble-scanner.cpp +++ b/debug-app/drivers/ble-scanner/src/ble-scanner.cpp @@ -45,6 +45,10 @@ void BleScanner::SetScan(bool enable){ } } +void BleScanner::RegisterCallback(void (*callback)(std::string, std::vector<uint8_t>, int8_t)){ + registeredCallback = callback; +} + void BleScanner::BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad){ char addr_str[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); diff --git a/debug-app/drivers/ir-driver/src/ir-driver.cpp b/debug-app/drivers/ir-driver/src/ir-driver.cpp index 97104ae..6a4dd00 100644 --- a/debug-app/drivers/ir-driver/src/ir-driver.cpp +++ b/debug-app/drivers/ir-driver/src/ir-driver.cpp @@ -11,7 +11,7 @@ static IrDriver *context; IrDriver::IrDriver(){ context = this; int err = gpio_pin_configure_dt(&irSensorNode, GPIO_INPUT); - gpio_pin_interrupt_configure_dt(&irSensorNode, GPIO_INT_EDGE_BOTH); + gpio_pin_interrupt_configure_dt(&irSensorNode, GPIO_INT_EDGE_RISING); gpio_init_callback(&irSensorCb, InterruptCallback, BIT(irSensorNode.pin)); gpio_add_callback(irSensorNode.port, &irSensorCb); diff --git a/debug-app/module/driver-hook/src/driver-hook.cpp b/debug-app/module/driver-hook/src/driver-hook.cpp index 8704d5f..18dd26a 100644 --- a/debug-app/module/driver-hook/src/driver-hook.cpp +++ b/debug-app/module/driver-hook/src/driver-hook.cpp @@ -24,6 +24,8 @@ DriverHook::DriverHook(){ ledDriver = new LedDriver(); servoDriver = new ServoDriver(); irDriver = new IrDriver(); + irDriver->RegisterInterrupt(IrSensorCallback); + bleScanner->RegisterCallback(BleDataCallback); zbus_chan_add_obs(&driverRequestChannel, &driverHookListener, K_NO_WAIT); } @@ -32,6 +34,23 @@ DriverHook::~DriverHook(){ } +void DriverHook::IrSensorCallback(uint8_t data){ + LOG_INF("Iterrupt triggeredd. pin %d", data); + std::vector<uint8_t> sensorData; + sensorData.push_back(data); + + DriverMessage_t sentMessage = { + .number = IR_DRIVER, + .type = REQUEST_SET, + .value = sensorData + }; + + zbus_chan_pub(&driverResponseChannel, &sentMessage, K_MSEC(200)); +} + +void DriverHook::BleDataCallback(std::string addr, std::vector<uint8_t> data, int8_t rssi){ + LOG_HEXDUMP_INF(data.data(), data.size(), "BLE data:"); +} void DriverHook::Init(){ @@ -93,8 +112,6 @@ void DriverHook::DriverRequestListener(const struct zbus_channel *chan){ break; } - LOG_INF("Sensor data %d", msg->value[0]); - int status = zbus_chan_pub(&driverResponseChannel, &sentMessage, K_MSEC(200)); LOG_INF("Status pub %d", status); } \ No newline at end of file -- GitLab