Skip to content
Snippets Groups Projects
Commit 5a928244 authored by Dzulfikar Ahmad  Samhan's avatar Dzulfikar Ahmad Samhan
Browse files

Implement main application

parent c4f251a2
Branches
1 merge request!7Implement main application
Showing
with 341 additions and 4 deletions
zephyr_include_directories(include)
add_subdirectory(module)
# add_subdirectory(drivers)
\ No newline at end of file
add_subdirectory_ifndef(CONFIG_SOC_POSIX drivers)
\ No newline at end of file
......@@ -14,6 +14,7 @@ CONFIG_THREAD_NAME=y
CONFIG_ASSERT=y
CONFIG_DEBUG=n
CONFIG_BOOT_BANNER=n
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
# Peripheral
# CONFIG_ADC=y
......@@ -32,6 +33,9 @@ CONFIG_ZBUS_LOG_LEVEL_INF=y
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
CONFIG_ZBUS_CHANNEL_NAME=y
#peripheral
CONFIG_PWM=y
#ble
CONFIG_BT=y
CONFIG_BT_OBSERVER=y
......@@ -65,3 +69,6 @@ CONFIG_NET_L2_ETHERNET=y
# Log
CONFIG_LOG=y
CONFIG_LOG_PROCESS_THREAD=y
CONFIG_LOG_BUFFER_SIZE=2048
CONFIG_LOG_MODE_DEFERRED=y
\ No newline at end of file
......@@ -56,7 +56,7 @@ int main()
{
#if !defined(CONFIG_SOC_POSIX)
WifiController wifiController;
BleScanner bleScanner;
// BleScanner bleScanner;
#endif
DebugHandler debugHanddler;
MainHandler mainHandler;
......
......@@ -4,6 +4,62 @@
* SPDX-License-Identifier: Apache-2.0
*/
&wifi {
/{
aliases {
pwm-0 = &ledc0;
pwm-led0 = &pwm_led_blue;
};
gpio_in{
compatible = "gpio-keys";
ir_Sensor: ir_sensor{
gpios = <&gpio0 25 GPIO_ACTIVE_HIGH>;
label = "ir sensor interrupt";
status = "okay";
};
};
gpio_out{
compatible = "gpio-leds";
led_pin: led_pin{
gpios = <&gpio0 26 GPIO_ACTIVE_HIGH>;
label = "led pin";
status = "okay";
};
};
pwmServo {
compatible = "pwm-leds";
pwm_led_blue: pwm_led_gpio0_2 {
label = "PWM SERVO";
pwms = <&ledc0 0 1000 PWM_POLARITY_NORMAL>;
};
};
};
&pinctrl {
ledc0_default: ledc0_default {
group1 {
pinmux = <LEDC_CH0_GPIO2>;
output-enable;
};
};
};
&ledc0 {
pinctrl-0 = <&ledc0_default>;
pinctrl-names = "default";
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
channel0@0 {
reg = <0x0>;
timer = <0>;
};
};
&wifi {
status = "okay";
};
\ No newline at end of file
add_subdirectory(ble-scanner)
add_subdirectory(wifi-controller)
add_subdirectory(ir-driver)
add_subdirectory(servo-driver)
add_subdirectory(led-driver)
\ No newline at end of file
......@@ -8,8 +8,6 @@
#include "ble-scanner.h"
LOG_MODULE_REGISTER(BleScanner);
ZBUS_CHAN_DECLARE(driverRequestChannel, bleScanChannel);
static BleScanner *context;
static struct bt_le_scan_param scan_param = {
......@@ -19,8 +17,6 @@ static struct bt_le_scan_param scan_param = {
.window = BT_GAP_SCAN_FAST_WINDOW,
};
// ZBUS_LISTENER_DEFINE(bleScanListener, context->DriverRequestListener);
BleScanner::BleScanner(){
context = this;
int err = bt_enable(NULL);
......@@ -30,35 +26,46 @@ BleScanner::BleScanner(){
return;
}
// zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT);
k_work_init(&bleWork, BleInterruptHandler);
}
//TODO: remove when bus communication has fully established
bt_le_scan_start(&scan_param, BleDevicefound);
void BleScanner::BleInterruptHandler(struct k_work *item){
if(context->registeredCallback != NULL){
context->registeredCallback(context->currAddr, context->currData, context->currRssi);
}
}
void BleScanner::SetScan(bool enable){
if(enable){
bt_le_scan_start(&scan_param, BleDevicefound);
}
else{
bt_le_scan_stop();
}
}
void BleScanner::RegisterCallback(void (*callback)(std::string, std::vector<uint8_t>, int8_t)){
registeredCallback = callback;
}
RawBleData_t BleScanner::GetLatestData(){
RawBleData_t rawData = {
.addrStr = currAddr,
.bleData = currData,
.rssi = currRssi
};
return rawData;
}
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));
BleData_t sentMessage = {
.address = std::string(addr_str, sizeof(addr_str)),
.data = std::vector<uint8_t>(ad->data, ad->data + ad->len)
};
zbus_chan_pub(&bleScanChannel, &sentMessage, K_MSEC(200));
}
std::string addrStr(addr_str, sizeof(addr_str));
std::vector<uint8_t> dataVec(ad->data, ad->data + ad->len);
context->currAddr = addrStr;
context->currData = dataVec;
context->currRssi = rssi;
// void DriverRequestListener(const struct zbus_channel *chan){
// const DriverMessage_t *msg;
// if (&driverRequestChannel == chan) {
// msg = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access
// if(msg->number == BLUETOOTH){
// if(msg->value[0]){
// bt_le_scan_start(&scan_param, bleDevicefound);
// }
// }
// }
// else{
// return;
// }
// }
int err = k_work_submit(&context->bleWork);
}
\ No newline at end of file
zephyr_library_sources(src/ir-driver.cpp)
\ No newline at end of file
#include "ir-driver.h"
#include <zephyr/drivers/gpio.h>
#define IRSENSOR_PIN DT_NODELABEL(ir_sensor)
struct gpio_dt_spec irSensorNode = GPIO_DT_SPEC_GET(IRSENSOR_PIN, gpios);
struct gpio_callback irSensorCb;
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_init_callback(&irSensorCb, InterruptCallback, BIT(irSensorNode.pin));
gpio_add_callback(irSensorNode.port, &irSensorCb);
k_work_init(&work, InterruptHandler);
}
IrDriver::~IrDriver(){
}
bool IrDriver::GetState(){
return gpio_pin_get_dt(&irSensorNode);
}
void IrDriver::RegisterInterrupt(void (*callback)(uint8_t)){
registeredCallback = callback;
}
void IrDriver::InterruptCallback(const struct device *dev, struct gpio_callback *cb, uint32_t pins){
k_work_submit(&context->work);
}
void IrDriver::InterruptHandler(struct k_work *item){
if(context->registeredCallback != NULL){
context->registeredCallback(gpio_pin_get_dt(&irSensorNode));
}
}
\ No newline at end of file
zephyr_library_sources(src/led-driver.cpp)
\ No newline at end of file
#include "led-driver.h"
#include <zephyr/drivers/gpio.h>
#define LED_PIN DT_NODELABEL(led_pin)
struct gpio_dt_spec ledPinNode = GPIO_DT_SPEC_GET(LED_PIN, gpios);
LedDriver::LedDriver(){
int err = gpio_pin_configure_dt(&ledPinNode, GPIO_OUTPUT_INACTIVE);
}
LedDriver::~LedDriver(){
}
void LedDriver::SetState(bool enable){
gpio_pin_set_dt(&ledPinNode, enable);
currentState = enable;
}
bool LedDriver::GetState(){
return currentState;
}
\ No newline at end of file
zephyr_library_sources(src/servo-driver.cpp)
\ No newline at end of file
#include "servo-driver.h"
#include <zephyr/drivers/pwm.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(Servo);
#define MIN_PERIOD PWM_SEC(1U) / 128U
#define MAX_PERIOD PWM_SEC(1U)
#define MAX_PWM 100
static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
ServoDriver::ServoDriver(){
uint8_t dir = 0U;
int ret;
if (!pwm_is_ready_dt(&pwm_led0)) {
LOG_ERR("Error: PWM device %s is not ready\n",
pwm_led0.dev->name);
}
/*
* In case the default MAX_PERIOD value cannot be set for
* some PWM hardware, decrease its value until it can.
*
* Keep its value at least MIN_PERIOD * 4 to make sure
* the sample changes frequency at least once.
*/
LOG_INF("Calibrating for channel %d...\n", pwm_led0.channel);
max_period = MAX_PERIOD;
while (pwm_set_dt(&pwm_led0, max_period, max_period / 2U) < 0) {
max_period /= 2U;
if (max_period < (4U * MIN_PERIOD)) {
LOG_INF("Error: PWM device "
"does not support a period at least %lu\n",
4U * MIN_PERIOD);
}
}
}
ServoDriver::~ServoDriver(){
}
void ServoDriver::SetPwm(uint8_t pwm){
if(pwm_set_dt(&pwm_led0, max_period, max_period * (pwm/MAX_PWM)) == 0){
period = pwm;
}
}
uint8_t ServoDriver::GetPwm(){
return period;
}
\ No newline at end of file
#pragma once
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <string>
#include <vector>
typedef struct RawBleData {
std::string addrStr;
std::vector<uint8_t> bleData;
int8_t rssi;
} RawBleData_t;
class BleScanner {
public:
......@@ -16,8 +26,24 @@ public:
static void DriverRequestListener(const struct zbus_channel *chan);
void RegisterCallback(void (*callback)(std::string, std::vector<uint8_t>, int8_t));
void SetScan(bool enable);
RawBleData_t GetLatestData();
private:
std::string currAddr;
std::vector<uint8_t> currData;
int8_t currRssi;
struct k_work bleWork;
void (*registeredCallback)(std::string, std::vector<uint8_t>, int8_t) = NULL;
static void BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad);
static void BleInterruptHandler(struct k_work *item);
};
\ No newline at end of file
#pragma once
#include <zephyr/kernel.h>
#include <vector>
......
#pragma once
#include <zephyr/kernel.h>
#include "utilities.h"
typedef struct BleSensorData {
uint8_t tempSensor;
uint8_t sensorData;
} BleSensorData_t;
class DeviceParser {
......
......@@ -2,6 +2,11 @@
#include <zephyr/kernel.h>
#include "ble-scanner.h"
#include "led-driver.h"
#include "servo-driver.h"
#include "ir-driver.h"
class DriverHook {
public:
/** Constructor.
......@@ -21,5 +26,15 @@ public:
static void DriverRequestListener(const struct zbus_channel *chan);
static void IrSensorCallback(uint8_t data);
static void BleDataCallback(std::string addr, std::vector<uint8_t> data, int8_t rssi);
private:
#ifndef CONFIG_SOC_POSIX
BleScanner *bleScanner;
LedDriver *ledDriver;
ServoDriver *servoDriver;
IrDriver *irDriver;
#endif
};
\ No newline at end of file
#pragma once
#include <zephyr/kernel.h>
#include <string>
class IrDriver {
public:
void (*registeredCallback)(uint8_t) = NULL;
struct k_work work;
/** Constructor.
*
**/
IrDriver();
/** Destructor.
*
**/
~IrDriver();
bool GetState();
void RegisterInterrupt(void (*callback)(uint8_t));
private:
static void InterruptCallback(const struct device *dev, struct gpio_callback *cb, uint32_t pins);
static void InterruptHandler(struct k_work *item);
};
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment