Skip to content

Commit

Permalink
Update readme + minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizoku-oh committed Oct 29, 2023
1 parent df73098 commit b488691
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 36 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ Once the app is flashed open a serial monitor like PuTTY and reset your board.

- [ ] Add lambda callback to Button class

- [x] Add lambda callback to Serial class

- [ ] Add Network manager class

- [ ] Add Local class for managing local time and geo-location

- [ ] Add cloud client class

- [ ] Add NVS config class

- [ ] Add an out-of-tree driver

## 💳 Credits
This project is generated from the [zephyr-vscode-example](https://github.com/beriberikix/zephyr-vscode-example) template by [Jonathan Beri](https://github.com/beriberikix).

Expand Down
2 changes: 1 addition & 1 deletion include/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Button {
bool isPressed();

private:
const struct gpio_dt_spec *_gpioDevice;
const struct gpio_dt_spec *_device;

};

Expand Down
2 changes: 1 addition & 1 deletion include/Led.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Led {

private:
int _dutyCycle;
const struct gpio_dt_spec *_gpioDevice;
const struct gpio_dt_spec *_device;

};

Expand Down
4 changes: 2 additions & 2 deletions src/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Button::Button(const struct gpio_dt_spec *gpio) {
return;
}

this->_gpioDevice = gpio;
this->_device = gpio;

if (!gpio_is_ready_dt(gpio)) {
printk("Error: button device %s is not ready\n", gpio->port->name);
Expand All @@ -28,5 +28,5 @@ Button::~Button() {
}

bool Button::isPressed() {
return (gpio_pin_get_dt(this->_gpioDevice) == 0) ? true : false;
return (gpio_pin_get_dt(this->_device) == 0) ? true : false;
}
8 changes: 4 additions & 4 deletions src/Led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Led::Led(const struct gpio_dt_spec *gpio) {
return;
}

this->_gpioDevice = gpio;
this->_device = gpio;

if (!gpio_is_ready_dt(gpio)) {
printk("Error: Led device %s is not ready\n", gpio->port->name);
Expand All @@ -27,17 +27,17 @@ Led::~Led() {
}

void Led::on() {
gpio_pin_set_dt(this->_gpioDevice, 1);
gpio_pin_set_dt(this->_device, 1);
}

void Led::off() {
gpio_pin_set_dt(this->_gpioDevice, 0);
gpio_pin_set_dt(this->_device, 0);
}

void Led::toggle() {
int ret = 0;

ret = gpio_pin_toggle_dt(this->_gpioDevice);
ret = gpio_pin_toggle_dt(this->_device);
if (ret < 0) {
printk("Failed to toggle LED pin!\r\n");
return;
Expand Down
58 changes: 40 additions & 18 deletions src/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Serial.h"

static void serial_callback(const struct device *dev, void *userData);
static void serialCallback(const struct device *dev, void *userData);

Serial::Serial(const struct device *device) {
if (device == NULL) {
Expand All @@ -27,8 +27,10 @@ Serial::~Serial() {
}

void Serial::write(uint8_t *data, uint32_t length) {
size_t index;

if (data && length) {
for (size_t index = 0; index < length; index++) {
for (index = 0; index < length; index++) {
uart_poll_out(this->device, *data++);
}
}
Expand All @@ -38,13 +40,16 @@ void Serial::read(uint8_t *data, uint32_t *length) {
}

void Serial::onReceive(std::function<void(uint8_t*, uint32_t)> callback) {
int ret;

if (callback == nullptr) {
printk("Failed to register callback\r\n");
return;
}

this->callback = callback;

int ret = uart_irq_callback_user_data_set(this->device, serial_callback, this);
ret = uart_irq_callback_user_data_set(this->device, serialCallback, this);
if (ret < 0) {
if (ret == -ENOTSUP) {
printk("Interrupt-driven UART API support not enabled\r\n");
Expand All @@ -57,35 +62,52 @@ void Serial::onReceive(std::function<void(uint8_t*, uint32_t)> callback) {
}
}

static void serial_callback(const struct device *dev, void *userData) {
Serial *serialInstance = static_cast<Serial *>(userData);
static void serialCallback(const struct device *dev, void *userData) {
Serial *serialInstance = nullptr;
uint8_t rxByte = 0;
int length = 0;
int ret = 0;

if ((dev == nullptr) || (userData == nullptr)) {
printk("Invalid callback parameters\r\n");
return;
}

if (!uart_irq_update(dev)) {
serialInstance = static_cast<Serial *>(userData);

ret = uart_irq_update(dev);
if (ret < 0) {
if (ret == -ENOSYS) {
printk("uart_irq_update() function is not implemented\r\n");
} else if (ret == -ENOTSUP) {
printk("UART API is not enabled");
}
return;
}

if (!uart_irq_rx_ready(dev)) {
ret = uart_irq_rx_ready(dev);
if (ret < 0) {
if (ret == -ENOSYS) {
printk("uart_irq_rx_ready() function is not implemented\r\n");
} else if (ret == -ENOTSUP) {
printk("UART API is not enabled");
}
return;
}

length = uart_fifo_read(dev, &rxByte, sizeof(rxByte));
if (length == 1) {
ret = uart_fifo_read(dev, &rxByte, sizeof(rxByte));
if (ret == 1) {
if (serialInstance->callback) {
serialInstance->callback(&rxByte, length);
serialInstance->callback(&rxByte, ret);
}
} else if (length == 0) {
} else if (ret == 0) {
printk("Got a UART RX interrupt but FIFO is empty!\r\n");
} else if (length > 1) {
} else if (ret > 1) {
printk("Didn't expect to find more than 1 byte in FIFO!\r\n");
} else if (length == -ENOSYS) {
} else if (ret == -ENOSYS) {
printk("uart_fifo_read() function is not implemented\r\n");
} else if (length == -ENOTSUP) {
} else if (ret == -ENOTSUP) {
printk("UART API is not enabled");
} else {
printk("Unknown error: %d\r\n", length);
printk("Unknown error: %d\r\n", ret);
}

printk("serial_callback()\r\n");
}
17 changes: 7 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,32 @@

#define MAIN_THREAD_SLEEP_TIME_MS (100)

uint8_t rxBuffer[8] = {0};

int main(void) {
// Reference devices from device tree
const struct gpio_dt_spec buttonGpio = GPIO_DT_SPEC_GET_OR(DT_ALIAS(sw0), gpios, {0});

const struct gpio_dt_spec greenLedGpio = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0});
const struct gpio_dt_spec blueLedGpio = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led1), gpios, {0});
const struct gpio_dt_spec redLedGpio = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led2), gpios, {0});

const struct device *temperatureDevice = DEVICE_DT_GET(DT_NODELABEL(die_temp));

const struct device *serialDevice = DEVICE_DT_GET(DT_NODELABEL(usart2));

// Create objects using the device tree devices
Button button(&buttonGpio);

Led greenLed(&greenLedGpio);
Led blueLed(&blueLedGpio);
Led redLed(&redLedGpio);

Temperature temperature(temperatureDevice);

Serial serial(serialDevice);

// Register serial callback as a lambda function
serial.onReceive([](uint8_t *data, uint32_t length) {
printk("Received length: %" PRIu32 "\r\n", length);
printk("Received byte: %c\r\n", *data);
printk("Received %d byte%s: %.*s\r\n", length, (length == 1)?"":"s", length, data);
});

// Write a string over serial
serial.write((uint8_t *)"Hello world!\r\n", (sizeof("Hello world!\r\n") - 1));

// Continuously check if a button is pressed, if so read temperature and toggle LEDs
while (true) {
if (button.isPressed()) {
printk("Button is pressed\r\n");
Expand Down

0 comments on commit b488691

Please sign in to comment.