Skip to content

Commit

Permalink
Add code files
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Jul 16, 2023
0 parents commit 81cbbe0
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
10 changes: 10 additions & 0 deletions application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
App(
appid="rps",
name="Rock Paper Scissor",
apptype=FlipperAppType.EXTERNAL,
entry_point="rps_app",
requires=["gui"],
stack_size=2 * 1024,
fap_category="Games",
fap_icon_assets="assets",
)
Binary file added assets/paper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/rock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/scissor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions rps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdlib.h>

#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>

#include "rps.h"
#include "rps_icons.h"

#define QUEUE_SIZE 8
#define LOG_TAG "RPS"

void input_callback(InputEvent* input_event, void* ctx) {
FURI_LOG_D(LOG_TAG, "Input callback fired!");
RPSApp_t* app = ctx;

switch (input_event->type)
{
case InputTypeShort:
furi_message_queue_put(app->event_queue, input_event, 0);
break;

default:
break;
}
}

void render_callback(Canvas* canvas, void* ctx) {
FURI_LOG_T(LOG_TAG, "Render callback fired!");
RPSApp_t* app = ctx;
UNUSED(app);
canvas_clear(canvas);
canvas_draw_icon(canvas, 0, 0, &I_rock);
canvas_draw_icon(canvas, 30, 0, &I_paper);
canvas_draw_icon(canvas, 60, 0, &I_scissor);
}

RPSApp_t* state_init() {
FURI_LOG_D(LOG_TAG, "Initing app...");
RPSApp_t* app = malloc(sizeof(RPSApp_t));

app->event_queue = furi_message_queue_alloc(QUEUE_SIZE, sizeof(InputEvent));
app->view_port = view_port_alloc();
app->gui = furi_record_open(RECORD_GUI);
app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);

view_port_input_callback_set(app->view_port, input_callback, app);
view_port_draw_callback_set(app->view_port, render_callback, app);
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);

return app;
}

void state_free(RPSApp_t* app) {
FURI_LOG_D(LOG_TAG, "Freeing app...");
furi_message_queue_free(app->event_queue);
gui_remove_view_port(app->gui, app->view_port);
furi_record_close(RECORD_GUI);
view_port_free(app->view_port);
furi_mutex_free(app->mutex);
free(app);
}

bool main_loop(RPSApp_t* app) {
InputEvent event;
FuriStatus status = FuriStatusErrorTimeout;
while ((status = furi_message_queue_get(app->event_queue, &event, 60000)) == FuriStatusErrorTimeout) ;

furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);

FURI_LOG_D(LOG_TAG, "mainnnnn");
switch (event.key) {
case InputKeyBack:
FURI_LOG_D(LOG_TAG, "exit");
furi_mutex_release(app->mutex);
return false;

default:
FURI_LOG_D(LOG_TAG, "%d", event.key);
break;
}

furi_mutex_release(app->mutex);
return true;
}

int32_t rps_app(void) {
RPSApp_t* app = state_init();
while (main_loop(app)) ;
state_free(app);
return 0;
}
14 changes: 14 additions & 0 deletions rps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef RPS_H_
#define RPS_H_

typedef struct RPSApp {
// Flipper Firmware Variables
FuriMessageQueue* event_queue;
ViewPort* view_port;
Gui* gui;
FuriMutex** mutex;

// App Variables
} RPSApp_t;

#endif // RPS_H_
8 changes: 8 additions & 0 deletions rps_icons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <furi.h>
#include <gui/icon.h>

extern Icon I_rock;
extern Icon I_paper;
extern Icon I_scissor;

0 comments on commit 81cbbe0

Please sign in to comment.