Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzholzer committed Jun 3, 2024
1 parent c2e1c93 commit b90bdf0
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 89 deletions.
9 changes: 3 additions & 6 deletions node/code/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APPLICATION = teamagotchi
BOARD = feather-nrf52840-sense
BOARD ?= feather-nrf52840-sense

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/RIOT
Expand All @@ -15,22 +15,19 @@ DEVELHELP ?= 1

# FEATURES_REQUIRED += cpp
# FEATURES_REQUIRED += libstdcpp
FEATURES_REQUIRED += periph_gpio_irq

# Internal Modules
# Teamagotchi MODULES
USEMODULE += io_handler
USEMODULE += display_handler
USEMODULE += lwm2m_handler
# USEMODULE += lwm2m_handler
EXTERNAL_MODULE_DIRS += modules

# External modules
# USEMODULE += cpp11-compat
USEMODULE += ztimer
USEMODULE += ztimer_msec
USEMODULE += shell
USEMODULE += xtimer

# As there is an 'Kconfig' we want to explicitly disable Kconfig by setting
# the variable to empty
#SHOULD_RUN_KCONFIG ?=
Expand All @@ -55,7 +52,7 @@ BOARD_BLACKLIST += native64

## DEBUG
CFLAGS += -DDEBUG_ASSERT_VERBOSE

FEATURES_BLACKLIST += tinyusb
##### LWM2M #####
LWM2M_SERVER_SHORT_ID ?= 1
CFLAGS += -DEVENT_THREAD_MEDIUM_STACKSIZE='(3*1024)'
Expand Down
2 changes: 1 addition & 1 deletion node/code/RIOT
Submodule RIOT updated 41 files
+1 −1 .murdock
+3 −2 Makefile.include
+28 −5 core/include/thread.h
+6 −3 core/thread.c
+1 −0 cpu/cortexm_common/vectors_cortexm.c
+1 −1 cpu/esp_common/freertos/task.c
+3 −4 cpu/esp_common/thread_arch.c
+7 −0 cpu/native/Makefile.include
+1 −1 cpu/native/include/native_internal.h
+12 −9 cpu/native/irq_cpu.c
+23 −15 cpu/native/native_cpu.c
+59 −12 cpu/native/startup.c
+1 −1 cpu/native/syscalls.c
+7 −8 cpu/saml1x/cpu.c
+2 −2 cpu/saml1x/periph/pm.c
+22 −18 cpu/saml21/cpu.c
+2 −2 cpu/saml21/periph/pm.c
+4 −0 dist/tools/tapsetup/tapsetup
+1 −1 examples/rust-gcoap/Cargo.lock
+1 −1 examples/rust-hello-world/Cargo.lock
+1 −2 pkg/flashdb/mtd/fal_mtd_port.c
+6 −0 sys/Makefile.dep
+67 −0 sys/include/net/nanocoap/fs.h
+19 −0 sys/include/net/nanocoap_sock.h
+70 −0 sys/include/net/sock/config.h
+1 −43 sys/include/net/sock/util.h
+34 −22 sys/include/vfs.h
+12 −1 sys/net/application_layer/nanocoap/fileserver.c
+322 −0 sys/net/application_layer/nanocoap/fs.c
+101 −0 sys/net/application_layer/nanocoap/sock.c
+13 −3 sys/net/gnrc/sock/gnrc_sock.c
+1 −1 sys/ps/ps.c
+1 −1 sys/rust_riotmodules_standalone/Cargo.lock
+7 −4 sys/shell/cmds/vfs.c
+1 −1 sys/test_utils/print_stack_usage/print_stack_usage.c
+22 −0 tests/net/nanocoap_fs/Makefile
+38 −0 tests/net/nanocoap_fs/Makefile.ci
+11 −0 tests/net/nanocoap_fs/README.md
+89 −0 tests/net/nanocoap_fs/main.c
+4 −4 tests/periph/hwrng/main.c
+1 −1 tests/rust_minimal/Cargo.lock
41 changes: 21 additions & 20 deletions node/code/events.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
#include "events.h"
#include "event.h"

static event_queue_t queues[2];
static event_handler_t handler_fn;
#define ENABLE_DEBUG 1
#include "debug.h"

event_queue_t * get_queue_standard(void){
return &queues[1];
// return &queues[1];
}

void trigger_event_input(EVENT_T event){
team_event_t t_event = {.super.handler = handler_fn, .event = event};
event_post(&queues[0],&t_event.super);
}
#define RCV_QUEUE_SIZE (8)

kernel_pid_t t_events_pid;
static msg_t rcv_queue[RCV_QUEUE_SIZE];

void trigger_event(EVENT_T event){
team_event_t t_event = {.super.handler = handler_fn, .event = event};
event_post(&queues[1],&t_event.super);
void set_t_events_pid(kernel_pid_t pid){
t_events_pid = pid;
}


void events_handler_init(event_handler_t handlerfn){
handler_fn = handlerfn;
event_queues_init(queues,2);
void trigger_event(EVENT_T _event){
DEBUG("event input \n");
msg_t msg;
msg.type = _event;
if (msg_try_send(&msg, t_events_pid) == 0) {
printf("Receiver queue full.\n");
}
}


void events_start(void){
event_loop_multi(queues,2);
void events_start(void (*fsm_callback)(EVENT_T event)){
msg_t msg;
msg_init_queue(rcv_queue, RCV_QUEUE_SIZE);
while (1) {
msg_receive(&msg);
fsm_callback(msg.type);
}
}
80 changes: 49 additions & 31 deletions node/code/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

#include "event.h"
#include "events.h"
#include "lwm2m_handler.h"
#include "msg.h"
// #include "lwm2m_handler.h"
#include "display_handler.h"
#include "io_handler.h"

#define ENABLE_DEBUG 1
#include "debug.h"

char fsm_thread_stack[THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF];

typedef struct hierarchical_state state_t;

struct hierarchical_state {
Expand All @@ -27,6 +30,8 @@ struct hierarchical_state {
handler_result_t testHandler(EVENT_T event);
void main_state_init_entry_handler(void);
handler_result_t main_state_init_handler(EVENT_T event);
void fsm_handle(EVENT_T event);
void *fsm_thread(void * arg);
// // This is an array of root (top most) states .
// static const state_t Top_Level[] = {{
// top_level_awake_handler, // state handler
Expand Down Expand Up @@ -142,34 +147,45 @@ static const state_t RunningState[] = {{

static const state_t *currentState = &MainState[0];

void fsm_start_thread(void){
set_t_events_pid(thread_create(fsm_thread_stack, sizeof(fsm_thread_stack),
THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
fsm_thread, NULL, "fsm_thread"));
}

void *fsm_thread(void *arg) {
(void)arg;
DEBUG("[FSM:thread]: start\n");
currentState->Entry();
events_handler_init(fsm_handle);
events_start();
// events_handler_init(fsm_handle);
events_start(fsm_handle);

return NULL;
}

void fsm_handle(event_t *event) {
puts("handle\n");
team_event_t *t_event = container_of(event, team_event_t, super);

handler_result_t result = currentState->Handler(t_event->event);
if (result != EVENT_HANDLED) {
const state_t *pState = currentState;
do {
// check if state has parent state.
if (pState->Parent == NULL) // Is Node reached top
{
// This is a fatal error. terminate state machine.
return;
}

pState = pState->Parent; // traverse to parent state
} while (pState->Handler ==
NULL); // repeat again if parent state doesn't have handler
pState->Handler(t_event->event);
}
void fsm_handle(EVENT_T event) {
DEBUG("[FSM:fsm_handle]: handle\n");
// team_event_t *t_event = container_of(event, team_event_t, super_event);
// DEBUG("[FSM:fsm_handle]: event defined: %d\n",t_event->event);
handler_result_t result = currentState->Handler(event);
if (result != EVENT_HANDLED) {
const state_t *pState = currentState;
do {
// check if state has parent state.
if (pState->Parent == NULL) // Is Node reached top
{
DEBUG("[FSM:fsm_handle]: Fatal error, terminating.\n");
// This is a fatal error. terminate state machine.
return;
}

pState = pState->Parent; // traverse to parent state
} while (pState->Handler ==
NULL); // repeat again if parent state doesn't have handler
pState->Handler(event);
}else{
DEBUG("[FSM:fsm_handle]: Event handled\n");
}
}

void traverse_state(state_t target_state) {
Expand Down Expand Up @@ -201,17 +217,19 @@ handler_result_t testHandler(EVENT_T event) {
}

void main_state_init_entry_handler(void) {
lwm2m_handler_init();
lwm2m_handler_start();
io_init();
display_init();
startDisplayThread();
DEBUG("[FSM:init_entry]\n");
// lwm2m_handler_init();
// lwm2m_handler_start();
io_init();
display_init();
startDisplayThread();
}

handler_result_t main_state_init_handler(EVENT_T event) {
DEBUG("[FSM:main_state_init_handler]\n");
lwm2m_handleEvent(event);
displayHandler_handleEvent(event);
DEBUG("[FSM:main_state_init_handler]: lwm2m handle\n");
// lwm2m_handleEvent(event);
ioHandler_handleEvent(event);
displayHandler_handleEvent(event);

return EVENT_HANDLED;
}
9 changes: 4 additions & 5 deletions node/code/inc/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C" {
#endif

#include "event.h"
#include "thread.h"

typedef enum {
UNDEFINED,
Expand Down Expand Up @@ -52,7 +53,7 @@ typedef enum {
}EVENT_T;

typedef struct {
event_t super;
event_t super_event;
EVENT_T event;
} team_event_t;

Expand All @@ -65,11 +66,9 @@ typedef enum
TRIGGERED_TO_SELF,
}handler_result_t;

void trigger_event_input(EVENT_T event);
void trigger_event(EVENT_T event);
void events_handler_init(event_handler_t handlerfn);
void events_start(void);
event_queue_t * get_queue_standard(void);
void events_start(void (*fsm_callback)(EVENT_T event));
void set_t_events_pid(kernel_pid_t pid);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions node/code/inc/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ extern "C" {
#include "event.h"
#include "events.h"

void *fsm_thread(void * arg);
void fsm_handle(event_t *event);
void fsm_start_thread(void);

/**
* @brief event handler type definition
*/
Expand Down
9 changes: 4 additions & 5 deletions node/code/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

#include "events.h"
#include "fsm.h"
#include "thread.h"

#define SHELL_QUEUE_SIZE (8)
static msg_t _shell_queue[SHELL_QUEUE_SIZE];
char fsm_thread_stack[THREAD_STACKSIZE_MAIN];


int main(void)
{
thread_create(fsm_thread_stack, sizeof(fsm_thread_stack),
THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
fsm_thread, NULL, "fsm_thread");
{
fsm_start_thread();
msg_init_queue(_shell_queue, SHELL_QUEUE_SIZE);
shell_loop();
}
1 change: 0 additions & 1 deletion node/code/modules/display_handler/Makefile.include
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# # Use an immediate variable to evaluate `MAKEFILE_LIST` now
# USEMODULE_INCLUDES_display_handler += $(LAST_MAKEFILEDIR)/include
# #USEMODULE_INCLUDES_external_module += $(LAST_MAKEFILEDIR)/image
# USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_display_handler)


Expand Down
33 changes: 16 additions & 17 deletions node/code/modules/io_handler/init_buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#define ENABLE_DEBUG 1
#include "debug.h"

//TODO: change the actual pin layout according to PCB Design
gpio_t button_ok = GPIO_PIN(0, 5); //PIN A1
gpio_t button_right = GPIO_PIN(0, 30); //PIN sA2
gpio_t button_up = GPIO_PIN(0, 28); //PIN A3
Expand Down Expand Up @@ -59,11 +58,11 @@ void button_up_callback (void *arg)
(void) arg; /* the argument is not used */
if (!gpio_read(button_up)) {
DEBUG("Button up pressed!\n");
trigger_event_input(BUTTON_UP_PRESSED);
trigger_event(BUTTON_UP_PRESSED);
}
else {
DEBUG("Button up released!\n");
trigger_event_input(BUTTON_UP_RELEASED);
trigger_event(BUTTON_UP_RELEASED);
}
}

Expand All @@ -72,11 +71,11 @@ void button_left_callback (void *arg)
(void) arg; /* the argument is not used */
if (!gpio_read(button_left)) {
DEBUG("Button left pressed!\n");
trigger_event_input(BUTTON_LEFT_PRESSED);
trigger_event(BUTTON_LEFT_PRESSED);
}
else {
DEBUG("Button left released!\n");
trigger_event_input(BUTTON_LEFT_RELEASED);
trigger_event(BUTTON_LEFT_RELEASED);
}
}

Expand All @@ -85,37 +84,37 @@ void button_down_callback (void *arg)
(void) arg; /* the argument is not used */
if (!gpio_read(button_down)) {
DEBUG("Button down pressed!\n");
trigger_event_input(BUTTON_DOWN_PRESSED);
trigger_event(BUTTON_DOWN_PRESSED);
}
else {
DEBUG("Button down released!\n");
trigger_event_input(BUTTON_DOWN_RELEASED);
trigger_event(BUTTON_DOWN_RELEASED);
}
}

void button_right_callback (void *arg)
{
(void) arg; /* the argument is not used */
// if (!gpio_read(button_right)) {
// DEBUG("Button right pressed!\n");
// trigger_event_input(BUTTON_RIGHT_PRESSED);
// }
// else {
// DEBUG("Button right released!\n");
// trigger_event_input(BUTTON_RIGHT_RELEASED);
// }
if (!gpio_read(button_right)) {
DEBUG("Button right pressed!\n");
trigger_event(BUTTON_RIGHT_PRESSED);
}
else {
DEBUG("Button right released!\n");
trigger_event(BUTTON_RIGHT_RELEASED);
}
}

void button_ok_callback (void *arg)
{
(void) arg; /* the argument is not used */
if (!gpio_read(button_ok)) {
DEBUG("Button ok pressed!\n");
trigger_event_input(BUTTON_OK_PRESSED);
trigger_event(BUTTON_OK_PRESSED);
}
else {
DEBUG("Button ok released!\n");
trigger_event_input(BUTTON_OK_RELEASED);
trigger_event(BUTTON_OK_RELEASED);
}
}

Expand Down
2 changes: 1 addition & 1 deletion node/code/modules/io_handler/io_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @}
*/

#define ENABLE_DEBUG 0
#define ENABLE_DEBUG 1
#include "debug.h"

#include "init_buttons.h"
Expand Down

0 comments on commit b90bdf0

Please sign in to comment.