Skip to content

Commit

Permalink
Fix Registration Flow & Snake (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnsAnns authored Jul 8, 2024
2 parents 039a555 + 070376b commit 8cf6a34
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 28 deletions.
2 changes: 1 addition & 1 deletion node/code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ifndef CONFIG_LWM2M_SERVER_URI

endif
##### /LWM2M #####
CFLAGS += -DCONFIG_LWM2M_DEVICE_NAME=\""urn:t8i:dev:1"\"
CFLAGS += -DCONFIG_LWM2M_DEVICE_NAME=\""urn:t8i:dev:3"\"

include $(RIOTBASE)/Makefile.include

Expand Down
2 changes: 1 addition & 1 deletion node/code/RIOT
Submodule RIOT updated 67 files
+1 −1 .github/workflows/check-labels.yml
+0 −9 Makefile.features
+0 −1 boards/b-l072z-lrwan1/Makefile.features
+0 −16 boards/b-l072z-lrwan1/include/periph_conf.h
+1 −1 boards/esp32-ethernet-kit-v1_0/Makefile.features
+1 −1 boards/esp32-olimex-evb/Makefile.features
+2 −1 boards/native/common_features.inc.mk
+2 −0 boards/nucleo-f207zg/Makefile.features
+2 −4 boards/nucleo-f303ze/doc.txt
+2 −0 boards/nucleo-f429zi/Makefile.features
+2 −0 boards/nucleo-f439zi/Makefile.features
+2 −0 boards/nucleo-f767zi/Makefile.features
+2 −0 boards/same54-xpro/Makefile.features
+0 −4 boards/sodaq-sara-sff/board.c
+0 −2 boards/sodaq-sara-sff/include/board.h
+2 −0 boards/stm32f746g-disco/features-shared.mk
+2 −5 boards/stm32l476g-disco/Makefile.include
+0 −60 boards/stm32l476g-disco/doc.txt
+2 −1 boards/stm32l476g-disco/include/board.h
+2 −20 core/thread.c
+1 −1 cpu/esp32/Makefile.dep
+7 −48 cpu/esp_common/syscalls.c
+1 −1 cpu/native/startup.c
+2 −2 cpu/nrf5x_common/periph/gpio.c
+10 −8 cpu/samd21/cpu.c
+34 −44 dist/tools/buildsystem_sanity_check/check.sh
+0 −91 dist/tools/buildsystem_sanity_check/get_dockerhub_digests.py
+0 −13,673 doc/doxygen/src/pinouts/nucleo-f303ze.svg
+52 −0 drivers/w5500/Kconfig
+14 −7 drivers/w5500/w5500.c
+2 −1 drivers/ws281x/ws281x_saul.c
+4 −0 features.yaml
+5 −42 makefiles/docker.inc.mk
+1 −0 makefiles/features_existing.inc.mk
+0 −25 pkg/esp32_sdk/patches/0034-components-efuse-fix-incorrect-forward-declaration.patch
+0 −24 pkg/esp32_sdk/patches/0035-components-wpa_supplicant-add-missing-include.patch
+0 −105 pkg/esp32_sdk/patches/0036-components-fix-calls-to-calloc.patch
+2 −2 pkg/wakaama/Kconfig
+0 −25 pkg/wakaama/Makefile.dep
+0 −5 pkg/wakaama/contrib/objects/Kconfig
+0 −62 pkg/wakaama/contrib/objects/Kconfig.ipso
+0 −68 pkg/wakaama/contrib/objects/barometer.c
+0 −68 pkg/wakaama/contrib/objects/current.c
+0 −69 pkg/wakaama/contrib/objects/humidity.c
+0 −70 pkg/wakaama/contrib/objects/illuminance.c
+0 −417 pkg/wakaama/contrib/objects/ipso_sensor_base.c
+0 −71 pkg/wakaama/contrib/objects/temperature.c
+0 −69 pkg/wakaama/contrib/objects/voltage.c
+0 −161 pkg/wakaama/include/objects/barometer.h
+0 −159 pkg/wakaama/include/objects/current.h
+0 −162 pkg/wakaama/include/objects/humidity.h
+0 −158 pkg/wakaama/include/objects/illuminance.h
+0 −202 pkg/wakaama/include/objects/ipso_sensor_base.h
+0 −161 pkg/wakaama/include/objects/temperature.h
+0 −163 pkg/wakaama/include/objects/voltage.h
+1 −1 sys/hashes/sha2xx_common.c
+1 −1 sys/include/random.h
+2 −2 sys/include/xtimer.h
+21 −26 sys/net/application_layer/nanocoap/sock.c
+11 −33 sys/net/gnrc/network_layer/ipv6/nib/_nib-internal.c
+8 −9 sys/net/gnrc/network_layer/ipv6/nib/nib.c
+2 −2 sys/random/shaxprng.c
+0 −1 tests/core/thread_stack_alignment/tests/01-run.py
+2 −0 tests/drivers/w5500/app.config.test
+12 −127 tests/net/gnrc_ipv6_nib/main.c
+11 −25 tests/periph/flashpage/main.c
+1 −1 tests/sys/prng_sha1prng/main.c
13 changes: 11 additions & 2 deletions node/code/modules/display_handler/display_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

char display_thread_stack [DISPLAY_STACKSIZE];

char game_thread_stack [DISPLAY_STACKSIZE];

handler_result_t displayHandler_handleEvent(EVENT_T event){
char buf[100];
DEBUG("[DisplayHandler:handleEvent]\n");
Expand All @@ -49,7 +51,6 @@ handler_result_t displayHandler_handleEvent(EVENT_T event){
break;
case BUTTON_DOWN_RELEASED:
down_released();
init_registered_pet();
break;
case BUTTON_LEFT_PRESSED:
left_pressed();
Expand All @@ -65,7 +66,6 @@ handler_result_t displayHandler_handleEvent(EVENT_T event){
break;
case REGISTER_CODE:
init_not_registered_code(get_register_code());
//init_not_registered_code("Hallo Tom");
break;
case REGISTERED:
init_registered_no_pet();
Expand All @@ -79,6 +79,15 @@ handler_result_t displayHandler_handleEvent(EVENT_T event){
case INFO_PRESSED:
get_pet_stats((char*)&buf);
init_pet_stats((char*)&buf);
break;
case DEAD:
showDeadScreen();
break;
case GAME_START:
DEBUG("[DisplayHandler:handleEvent]: GAME_START\n");
thread_create(game_thread_stack, sizeof(game_thread_stack),
THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_WOUT_YIELD, game_loop, NULL, "Snake Game");
break;
default:
break;
}
Expand Down
3 changes: 3 additions & 0 deletions node/code/modules/display_handler/include/display_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ void startDisplayThread(void);
*/
void display_init(void);


void direction_handler(EVENT_T event);

#endif /* DISPLAY_HANDLER_T */
3 changes: 3 additions & 0 deletions node/code/modules/display_handler/include/init_lvgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void change_top_bar_text(char* top_bar_text);
void init_registered_no_pet(void);
void init_registered_pet(void);
void init_pet_stats(char* stats);
void showDeadScreen(void);

void * game_loop(void * arg);

#ifdef __cplusplus
}
Expand Down
217 changes: 211 additions & 6 deletions node/code/modules/display_handler/init_lvgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,23 @@
#include "lvgl/src/core/lv_indev.h"
#include "lvgl/src/hal/lv_hal_indev.h"
#include "disp_dev.h"
#include "ztimer.h"
#include "init_lvgl.h"
#include "events.h"

#define ENABLE_DEBUG 1
#include "debug.h"

lv_obj_t * roller1;
typedef enum {
UP,
DOWN,
LEFT,
RIGHT
} Direction;

Direction direction;

lv_obj_t * screen;

lv_obj_t * top_bar;
lv_obj_t * center;
Expand Down Expand Up @@ -86,7 +97,7 @@ void init_menu(void);
//main here


static void menu_cb(lv_event_t * e){
static void menu_cb(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_KEY) {

Expand Down Expand Up @@ -167,6 +178,7 @@ void enter_released(void){
}

void up_pressed(void){
direction = UP;
buttons[1].state = true;
}

Expand All @@ -175,6 +187,7 @@ void up_released(void){
}

void down_pressed(void){
direction = DOWN;
buttons[2].state = true;
}

Expand All @@ -183,6 +196,7 @@ void down_released(void){
}

void left_pressed(void){
direction = LEFT;
buttons[3].state = true;
}

Expand All @@ -191,6 +205,7 @@ void left_released(void){
}

void right_pressed(void){
direction = RIGHT;
buttons[4].state = true;
}

Expand Down Expand Up @@ -220,7 +235,7 @@ void init_default_screen(char* top_bar_text){
lv_obj_t * screen = lv_obj_create(lv_scr_act());
lv_obj_clear_flag(screen,LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_size(screen, 320, 240);
lv_obj_add_style(screen,&style_base, LV_PART_MAIN);
lv_obj_add_style(screen, &style_base, LV_PART_MAIN);

/* Style for top bar */
static lv_style_t style_top_bar;
Expand Down Expand Up @@ -311,8 +326,6 @@ void init_not_registered(void){
wakeup_task = lv_timer_create(timer_cb, WAKEUP_TIME, NULL);
}



void init_not_registered_code(char *code){
// timer_deactivate();
lv_obj_clean(center);
Expand Down Expand Up @@ -340,12 +353,14 @@ void init_not_registered_code(char *code){
}

void init_registered_no_pet(void){
init_default_screen("Pet Room");
lv_obj_clean(center);
init_menu();
}

void init_registered_pet(void){
// timer_deactivate();
lv_obj_clean(center);
lv_obj_clean(center);

/* Style of the align */
static lv_style_t style_align;
Expand Down Expand Up @@ -403,6 +418,8 @@ void init_pet_stats(char* stats){
}

void init_menu(void){
lv_obj_clean(bottom_bar);
current_img_index = 0;
static lv_style_t style;
lv_style_init(&style);
lv_style_set_bg_opa(&style,LV_OPA_TRANSP);
Expand Down Expand Up @@ -475,3 +492,191 @@ int init_lvgl(void)
init_not_registered();
return 0;
}

void showDeadScreen(void){
lv_obj_clean(center);

lv_obj_t * dead_label = lv_label_create(center);
static lv_style_t style_label;
lv_style_init(&style_label);
lv_style_set_text_font(&style_label, &lv_font_montserrat_24);
lv_obj_add_style(dead_label, &style_label, LV_PART_MAIN);

lv_label_set_text(dead_label, "Your pet is dead!");
lv_obj_align(dead_label, LV_ALIGN_CENTER, 0, 0);
}


/******
for snake game
*******/

#define SNAKE_MAX_LENGTH 10
#define SNAKE_START_LENGTH 3
#define GRID_SIZE 30
#define GRID_WIDTH (320 / GRID_SIZE)
#define GRID_HEIGHT (240 / GRID_SIZE)
#define SNAKE_SPEED 200 // Milliseconds

int snake_speed = SNAKE_SPEED;


typedef struct {
int x;
int y;
} Point;

static Point snake[SNAKE_MAX_LENGTH];
static int snake_length;
static Point food;

static lv_obj_t *snake_objs[SNAKE_MAX_LENGTH];
static lv_obj_t *food_obj;

void game_won(void) {
lv_obj_t *label = lv_label_create(screen);
snake_speed = SNAKE_SPEED;
lv_label_set_text(label, "You Win!");
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
ztimer_sleep(ZTIMER_SEC,3);
init_default_screen("Initializing ...");
init_registered_pet();
init_menu();
trigger_event(GAME_FINISHED);
}

void init_game(void) {
snake_length = SNAKE_START_LENGTH;
snake_speed = SNAKE_SPEED;
lv_obj_clean(lv_scr_act());
screen = lv_obj_create(lv_scr_act());
static lv_style_t style_base;
lv_style_init(&style_base);
lv_style_set_border_width(&style_base,0);
lv_style_set_pad_all(&style_base,0);
lv_obj_clear_flag(screen,LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_size(screen, 320, 240);
lv_obj_add_style(screen, &style_base, LV_PART_MAIN);
lv_obj_align(screen, LV_ALIGN_CENTER, 0, 0);

static lv_style_t snake_style;
lv_style_init(&snake_style);
lv_style_set_bg_color(&snake_style, lv_color_hex(0x00FF00)); // Grün für die Schlange

static lv_style_t food_style;
lv_style_init(&food_style);
lv_style_set_bg_color(&food_style, lv_color_hex(0xFF0000)); // Rot für das Essen


for (int i = 0; i < snake_length; i++) {
snake[i].x = GRID_WIDTH / 2 - i;
snake[i].y = GRID_HEIGHT / 2;
snake_objs[i] = lv_obj_create(screen);
lv_obj_set_size(snake_objs[i], GRID_SIZE, GRID_SIZE);
lv_obj_add_style(snake_objs[i], &snake_style, LV_PART_MAIN);
lv_obj_set_pos(snake_objs[i], snake[i].x * GRID_SIZE, snake[i].y * GRID_SIZE);
}
direction = RIGHT;

// Place food
food.x = lv_rand(1, GRID_WIDTH-1);
food.y = lv_rand(1, GRID_HEIGHT-1);
food_obj = lv_obj_create(screen);
lv_obj_set_size(food_obj, GRID_SIZE, GRID_SIZE);
lv_obj_add_style(food_obj, &food_style, LV_PART_MAIN);
lv_obj_set_pos(food_obj, food.x * GRID_SIZE, food.y * GRID_SIZE);
}

bool update_game(void) {
// Move snake body
for (int i = snake_length - 1; i > 0; i--) {
snake[i] = snake[i - 1];
lv_obj_set_pos(snake_objs[i], snake[i].x * GRID_SIZE, snake[i].y * GRID_SIZE);
}

// Move snake head
switch (direction) {
case UP: snake[0].y--; break;
case DOWN: snake[0].y++; break;
case LEFT: snake[0].x--; break;
case RIGHT: snake[0].x++; break;
}

lv_obj_set_pos(snake_objs[0], snake[0].x * GRID_SIZE, snake[0].y * GRID_SIZE);
// DEBUG("x: %d, y: %d\n",snake[0].x * GRID_SIZE,snake[0].y * GRID_SIZE);

// Check food collision
if (snake[0].x == food.x && snake[0].y == food.y) {
if (snake_length < SNAKE_MAX_LENGTH) {
snake_length++;
lv_obj_t *new_body_part = lv_obj_create(screen);
lv_obj_set_size(new_body_part, GRID_SIZE, GRID_SIZE);
static lv_style_t style;
lv_style_init(&style);
lv_style_set_bg_opa(&style, LV_OPA_50);
lv_style_set_bg_color(&style, lv_color_hex(0x00FF00)); // Grün für die Schlange
lv_obj_add_style(new_body_part, &style, LV_PART_MAIN);

snake_objs[snake_length - 1] = new_body_part;
}

food.x = lv_rand(0, GRID_WIDTH-1);
food.y = lv_rand(0, GRID_HEIGHT-1);
lv_obj_set_pos(food_obj, food.x * GRID_SIZE, food.y * GRID_SIZE);
snake_speed -= 10;
}

// Check win condition
if (snake_length >= 10) {
game_won();
return true; // Stop further updates if the game is won
}

// Check wall collision
if (snake[0].x < 0 || snake[0].x >= GRID_WIDTH || snake[0].y < 0 || snake[0].y >= GRID_HEIGHT) {
// Game Over
init_game();
}

// Check self collision
for (int i = 1; i < snake_length; i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
// Game Over
init_game();
}
}
return false;
}

void lv_tick_task(void *arg) {
(void)arg;
//lv_tick_inc(1);
}



void *game_loop(void * arg) {
(void) arg;
init_game();

//Start game loop
while (1) {
if (update_game()) {
break;
}
lv_task_handler();
ztimer_sleep(ZTIMER_MSEC, snake_speed);
}
game_won();
return NULL;
}

/******
for snake game
*******/
Loading

0 comments on commit 8cf6a34

Please sign in to comment.