Skip to content

Commit

Permalink
Format examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdietrich committed Aug 29, 2024
1 parent 1798977 commit 8b75d00
Show file tree
Hide file tree
Showing 75 changed files with 443 additions and 523 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ run_qemu:

format:
find src -iname *.h -o -iname *.c -o -iname *.cpp | xargs clang-format -i
find examples -iname *.h -o -iname *.c -o -iname *.cpp -o -iname *.ino | xargs clang-format -i

clean:
rm -rf build
Expand Down
3 changes: 2 additions & 1 deletion examples/ArduinoBlinkingLed/ArduinoBlinkingLed.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <avrtos.h>
#include <avrtos/avrtos.h>
#include <avrtos/misc/led.h>

#include <avrtos.h>

void setup(void)
{
pinMode(LED_BUILTIN, OUTPUT);
Expand Down
61 changes: 27 additions & 34 deletions examples/ArduinoDiningPhilisophers/ArduinoDiningPhilisophers.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <avrtos.h>

#include <avrtos/logging.h>

#include <avrtos.h>
// Change the log level (LOG_LEVEL_DBG, LOG_LEVEL_INF, LOG_LEVEL_WRN or LOG_LEVEL_ERR)
#define LOG_LEVEL LOG_LEVEL_WRN

Expand All @@ -18,8 +18,8 @@
#define FORKS MUTEX

#define PHIL_THINKING_DURATION_MAX_MS 5000u
#define PHIL_EATING_DURATION_MAX_MS 5000u
#define PHIL_DURATION_TO_STARVE_MS 15000u
#define PHIL_EATING_DURATION_MAX_MS 5000u
#define PHIL_DURATION_TO_STARVE_MS 15000u

typedef enum {
PHIL_STATE_NONE = 0u,
Expand All @@ -33,12 +33,12 @@ typedef enum {
const char *phil_state_to_string(phil_state_t state)
{
static const char *states_char[] = {
[PHIL_STATE_NONE] = "-",
[PHIL_STATE_THINKING] = "THINKING ",
[PHIL_STATE_NONE] = "-",
[PHIL_STATE_THINKING] = "THINKING ",
[PHIL_STATE_PENDING_LEFT_FORK] = "PEND_LEFT ",
[PHIL_STATE_PENDING_RIGHT_FORK] = "PEND_RIGHT",
[PHIL_STATE_EATING] = "EATING ",
[PHIL_STATE_STARVING] = "STARVING ",
[PHIL_STATE_EATING] = "EATING ",
[PHIL_STATE_STARVING] = "STARVING ",
};

if (state >= ARRAY_SIZE(states_char)) {
Expand Down Expand Up @@ -67,11 +67,11 @@ static uint16_t get_random_duration_ms(uint16_t min_duration_ms, uint16_t max_du

class Fork
{
public:
public:
Fork() = default;

virtual bool Lock(k_timeout_t timeout) = 0;
virtual void Unlock(void) = 0;
virtual void Unlock(void) = 0;
};

#if FORKS == MUTEX
Expand All @@ -80,7 +80,7 @@ public:

class MutexFork : public Fork
{
public:
public:
MutexFork(void)
{
/* Create a mutex for the fork */
Expand All @@ -97,7 +97,7 @@ public:
k_mutex_unlock(&m_mutex);
}

private:
private:
/* Mutex for the fork */
struct k_mutex m_mutex;
};
Expand All @@ -108,7 +108,7 @@ private:

class SemaphoreFork : public Fork
{
public:
public:
SemaphoreFork(void)
{
/* Create a semaphore for the fork */
Expand All @@ -125,7 +125,7 @@ public:
k_sem_give(&m_sem);
}

private:
private:
/* Mutex for the fork */
struct k_sem m_sem;
};
Expand All @@ -136,7 +136,7 @@ private:

class FifoFork : public Fork
{
public:
public:
FifoFork(void)
{
/* Create a fifo for the fork */
Expand All @@ -156,7 +156,7 @@ public:
k_fifo_put(&m_fifo, &m_node);
}

private:
private:
/* Mutex for the fork */
struct k_fifo m_fifo;
struct snode m_node;
Expand All @@ -166,17 +166,12 @@ private:

class Philosopher
{
public:
public:
Philosopher(Fork &left, Fork &right, const char *name)
: m_left_fork(left), m_right_fork(right), m_name(name)
{
k_thread_create(&m_thread,
reinterpret_cast<k_thread_entry_t>(Philosopher::Live),
m_stack,
sizeof(m_stack),
K_COOPERATIVE,
this,
m_name[0u]);
k_thread_create(&m_thread, reinterpret_cast<k_thread_entry_t>(Philosopher::Live),
m_stack, sizeof(m_stack), K_COOPERATIVE, this, m_name[0u]);

m_state = PHIL_STATE_NONE;

Expand All @@ -199,7 +194,7 @@ public:
return m_state;
}

private:
private:
void Think(void)
{
m_state = PHIL_STATE_THINKING;
Expand All @@ -212,14 +207,14 @@ private:

bool GetForks(void)
{
bool has_left_fork = false;
bool has_left_fork = false;
bool has_right_fork = false;
uint16_t time_left = PHIL_DURATION_TO_STARVE_MS;
uint32_t now = k_uptime_get_ms32();
uint16_t time_left = PHIL_DURATION_TO_STARVE_MS;
uint32_t now = k_uptime_get_ms32();

LOG_INF("%s is trying to get forks", m_name);

m_state = PHIL_STATE_PENDING_LEFT_FORK;
m_state = PHIL_STATE_PENDING_LEFT_FORK;
has_left_fork = m_left_fork.Lock(K_MSEC(time_left));

if (has_left_fork) {
Expand All @@ -229,7 +224,7 @@ private:
goto exit;
}

m_state = PHIL_STATE_PENDING_RIGHT_FORK;
m_state = PHIL_STATE_PENDING_RIGHT_FORK;
has_right_fork = m_right_fork.Lock(K_MSEC(time_left));

if (has_right_fork) {
Expand All @@ -246,9 +241,8 @@ private:

void Eat(void)
{
m_state = PHIL_STATE_EATING;
const uint16_t duration =
get_random_duration_ms(0u, PHIL_EATING_DURATION_MAX_MS);
m_state = PHIL_STATE_EATING;
const uint16_t duration = get_random_duration_ms(0u, PHIL_EATING_DURATION_MAX_MS);
LOG_INF("%s is eating for %u ms", m_name, duration);
k_sleep(K_MSEC(duration));

Expand Down Expand Up @@ -321,7 +315,6 @@ int timer_handler(struct k_timer *timer)
return 0; // continue
}


void setup(void)
{
/* LED initialisation */
Expand Down
41 changes: 20 additions & 21 deletions examples/ArduinoDrivers/ArduinoDrivers.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <avrtos.h>
#include <avrtos/drivers/usart.h>
#include <avrtos/drivers/timer.h>
#include <avrtos/drivers/gpio.h>
#include <avrtos/drivers/exti.h>
#include <avrtos/drivers/gpio.h>
#include <avrtos/drivers/timer.h>
#include <avrtos/drivers/usart.h>

#include <avrtos.h>

#if defined(__AVR_ATmega2560__)
# define INT0_PORT GPIOD
# define INT0_PIN 0u
#define INT0_PORT GPIOD
#define INT0_PIN 0u
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__)
# define INT0_PORT GPIOD
# define INT0_PIN 2u
#define INT0_PORT GPIOD
#define INT0_PIN 2u
#else
# error "Unsupported MCU for this example"
#endif
#error "Unsupported MCU for this example"
#endif

ISR(TIMER1_COMPA_vect)
{
Expand Down Expand Up @@ -50,14 +51,14 @@ void setup(void)

/* UART initialisation */
const struct usart_config usart_config = {
.baudrate = CONFIG_SERIAL_USART_BAUDRATE,
.receiver = 1u,
.baudrate = CONFIG_SERIAL_USART_BAUDRATE,
.receiver = 1u,
.transmitter = 1u,
.mode = USART_MODE_ASYNCHRONOUS,
.parity = USART_PARITY_NONE,
.stopbits = USART_STOP_BITS_1,
.databits = USART_DATA_BITS_8,
.speed_mode = USART_SPEED_MODE_NORMAL,
.mode = USART_MODE_ASYNCHRONOUS,
.parity = USART_PARITY_NONE,
.stopbits = USART_STOP_BITS_1,
.databits = USART_DATA_BITS_8,
.speed_mode = USART_SPEED_MODE_NORMAL,
};
usart_init(USART0_DEVICE, &usart_config);

Expand All @@ -66,10 +67,8 @@ void setup(void)

/* Timer initialisation */
const struct timer_config timer_config {
.mode = TIMER_MODE_CTC,
.prescaler = TIMER_PRESCALER_1024,
.counter = TIMER_CALC_COUNTER_VALUE(1000000lu, 1024lu),
.timsk = BIT(OCIEnA),
.mode = TIMER_MODE_CTC, .prescaler = TIMER_PRESCALER_1024,
.counter = TIMER_CALC_COUNTER_VALUE(1000000lu, 1024lu), .timsk = BIT(OCIEnA),
};
timer16_init(TIMER1_DEVICE, &timer_config);

Expand Down
2 changes: 1 addition & 1 deletion examples/ArduinoEasy/ArduinoEasy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void setup(void)

/* Create thread_led handling LED, then start it */
k_thread_create(&thread_led, thread_led_task, thread_led_stack,
sizeof(thread_led_stack), K_PREEMPTIVE, NULL, 'L');
sizeof(thread_led_stack), K_PREEMPTIVE, NULL, 'L');
k_thread_start(&thread_led);

/* Make main thread_led preemptive, so that the LED thread_led can run
Expand Down
31 changes: 16 additions & 15 deletions examples/ArduinoMinimalExample/ArduinoMinimalExample.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <avrtos.h>
#include <avrtos/debug.h>
#include <avrtos/drivers/gpio.h>
#include <avrtos/drivers/usart.h>
#include <avrtos/logging.h>
#include <avrtos/misc/led.h>

#include <avrtos.h>
#define LOG_LEVEL LOG_LEVEL_DBG

K_MSGQ_DEFINE(usart_msgq, 1u, 16u);
Expand All @@ -32,24 +33,24 @@ ISR(USART0_RX_vect)
void setup(void)
{
const struct usart_config usart_config = {
.baudrate = USART_BAUD_9600,
.receiver = 1u,
.baudrate = USART_BAUD_9600,
.receiver = 1u,
.transmitter = 1u,
.mode = USART_MODE_ASYNCHRONOUS,
.parity = USART_PARITY_NONE,
.stopbits = USART_STOP_BITS_1,
.databits = USART_DATA_BITS_8,
.speed_mode = USART_SPEED_MODE_NORMAL,
.mode = USART_MODE_ASYNCHRONOUS,
.parity = USART_PARITY_NONE,
.stopbits = USART_STOP_BITS_1,
.databits = USART_DATA_BITS_8,
.speed_mode = USART_SPEED_MODE_NORMAL,
};
ll_usart_init(USART0_DEVICE, &usart_config);
ll_usart_enable_rx_isr(USART0_DEVICE);

led_init();

k_thread_create(&thread_usart, thread_usart_task, thread_usart_stack,
sizeof(thread_usart_stack), K_COOPERATIVE, NULL, 'X');
sizeof(thread_usart_stack), K_COOPERATIVE, NULL, 'X');
k_thread_create(&thread_led, thread_led_task, thread_led_stack,
sizeof(thread_led_stack), K_COOPERATIVE, NULL, 'L');
sizeof(thread_led_stack), K_COOPERATIVE, NULL, 'L');

LOG_INF("Application started");

Expand All @@ -62,8 +63,8 @@ void setup(void)

void loop(void)
{
/* loop() is executed in the context of the main thread which is cooperative,
* therefore we need to give the other threads a chance to run, this is done
/* loop() is executed in the context of the main thread which is cooperative,
* therefore we need to give the other threads a chance to run, this is done
* through the k_idle() function.
*/
k_idle();
Expand All @@ -89,8 +90,8 @@ static void thread_led_task(void *arg)

/* As serial baudrate is very slow, the thread will take a lot of time
* to print the uptime and debug message, therefore the LED will blink
* slower than 2Hz.
*
* slower than 2Hz.
*
* I advise to use a faster baudrate (e.g. 115200) or comment out the
* k_show_uptime() and LOG_DBG() calls.
*/
Expand Down
6 changes: 3 additions & 3 deletions examples/ArduinoPIOBlinkingLed/BlinkingLed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <Arduino.h>

#include <avrtos.h>
#include <avrtos/avrtos.h>
#include <avrtos/misc/led.h>

#include <Arduino.h>
#include <avrtos.h>

void setup(void)
{
pinMode(LED_BUILTIN, OUTPUT);
Expand Down
Loading

0 comments on commit 8b75d00

Please sign in to comment.