mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-08 23:08:10 -07:00
fmt
This commit is contained in:
@@ -16,72 +16,70 @@
|
||||
|
||||
#define DEALER_MAX 17
|
||||
|
||||
void start_round(GameState *game_state);
|
||||
void start_round(GameState* game_state);
|
||||
|
||||
void init(GameState *game_state);
|
||||
|
||||
static void draw_ui(Canvas *const canvas, const GameState *game_state) {
|
||||
void init(GameState* game_state);
|
||||
|
||||
static void draw_ui(Canvas* const canvas, const GameState* game_state) {
|
||||
draw_money(canvas, game_state->player_score);
|
||||
|
||||
draw_score(canvas, true, hand_count(game_state->player_cards, game_state->player_card_count));
|
||||
|
||||
if (!game_state->queue_state.running && game_state->state == GameStatePlay) {
|
||||
render_menu(game_state->menu,canvas, 2, 47);
|
||||
if(!game_state->queue_state.running && game_state->state == GameStatePlay) {
|
||||
render_menu(game_state->menu, canvas, 2, 47);
|
||||
}
|
||||
}
|
||||
|
||||
static void render_callback(Canvas *const canvas, void *ctx) {
|
||||
const GameState *game_state = acquire_mutex((ValueMutex *) ctx, 25);
|
||||
static void render_callback(Canvas* const canvas, void* ctx) {
|
||||
const GameState* game_state = acquire_mutex((ValueMutex*)ctx, 25);
|
||||
|
||||
if (game_state == NULL) {
|
||||
if(game_state == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_frame(canvas, 0, 0, 128, 64);
|
||||
|
||||
if (game_state->state == GameStateStart) {
|
||||
if(game_state->state == GameStateStart) {
|
||||
canvas_draw_icon(canvas, 0, 0, &I_blackjack);
|
||||
}
|
||||
if (game_state->state == GameStateGameOver) {
|
||||
if(game_state->state == GameStateGameOver) {
|
||||
canvas_draw_icon(canvas, 0, 0, &I_endscreen);
|
||||
}
|
||||
|
||||
if (game_state->state == GameStatePlay || game_state->state == GameStateDealer) {
|
||||
if (game_state->state == GameStatePlay)
|
||||
if(game_state->state == GameStatePlay || game_state->state == GameStateDealer) {
|
||||
if(game_state->state == GameStatePlay)
|
||||
draw_player_scene(canvas, game_state);
|
||||
else
|
||||
draw_dealer_scene(canvas, game_state);
|
||||
render_queue(&(game_state->queue_state), game_state, canvas);
|
||||
draw_ui(canvas, game_state);
|
||||
} else if (game_state->state == GameStateSettings) {
|
||||
} else if(game_state->state == GameStateSettings) {
|
||||
settings_page(canvas, game_state);
|
||||
}
|
||||
|
||||
release_mutex((ValueMutex *) ctx, game_state);
|
||||
release_mutex((ValueMutex*)ctx, game_state);
|
||||
}
|
||||
|
||||
//region card draw
|
||||
Card draw_card(GameState *game_state) {
|
||||
Card draw_card(GameState* game_state) {
|
||||
Card c = game_state->deck.cards[game_state->deck.index];
|
||||
game_state->deck.index++;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void drawPlayerCard(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void drawPlayerCard(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
Card c = draw_card(game_state);
|
||||
game_state->player_cards[game_state->player_card_count] = c;
|
||||
game_state->player_card_count++;
|
||||
if(game_state->player_score < game_state->settings.round_price || game_state->doubled){
|
||||
if(game_state->player_score < game_state->settings.round_price || game_state->doubled) {
|
||||
set_menu_state(game_state->menu, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
void drawDealerCard(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void drawDealerCard(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
Card c = draw_card(game_state);
|
||||
game_state->dealer_cards[game_state->dealer_card_count] = c;
|
||||
game_state->dealer_card_count++;
|
||||
@@ -89,319 +87,363 @@ void drawDealerCard(void *ctx) {
|
||||
//endregion
|
||||
|
||||
//region queue callbacks
|
||||
void to_lose_state(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_lose_state(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You lost");
|
||||
}
|
||||
|
||||
void to_bust_state(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_bust_state(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Busted!");
|
||||
}
|
||||
|
||||
void to_draw_state(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_draw_state(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Draw");
|
||||
}
|
||||
|
||||
void to_dealer_turn(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_dealer_turn(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Dealers turn");
|
||||
}
|
||||
|
||||
void to_win_state(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_win_state(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You win");
|
||||
}
|
||||
|
||||
void to_start(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
if (game_state->settings.message_duration == 0)
|
||||
return;
|
||||
void to_start(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
if(game_state->settings.message_duration == 0) return;
|
||||
popup_frame(canvas);
|
||||
elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Round started");
|
||||
}
|
||||
|
||||
void before_start(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void before_start(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->dealer_card_count = 0;
|
||||
game_state->player_card_count = 0;
|
||||
}
|
||||
|
||||
|
||||
void start(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void start(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
start_round(game_state);
|
||||
}
|
||||
|
||||
void draw(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void draw(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->player_score += game_state->bet;
|
||||
game_state->bet = 0;
|
||||
enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
|
||||
game_state->settings.message_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
start,
|
||||
before_start,
|
||||
to_start,
|
||||
game_state->settings.message_duration);
|
||||
}
|
||||
|
||||
void game_over(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void game_over(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->state = GameStateGameOver;
|
||||
}
|
||||
|
||||
void lose(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void lose(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->state = GameStatePlay;
|
||||
game_state->bet = 0;
|
||||
if (game_state->player_score >= game_state->settings.round_price) {
|
||||
enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
|
||||
game_state->settings.message_duration);
|
||||
if(game_state->player_score >= game_state->settings.round_price) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
start,
|
||||
before_start,
|
||||
to_start,
|
||||
game_state->settings.message_duration);
|
||||
} else {
|
||||
enqueue(&(game_state->queue_state), game_state, game_over, NULL, NULL,
|
||||
0);
|
||||
enqueue(&(game_state->queue_state), game_state, game_over, NULL, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void win(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void win(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->state = GameStatePlay;
|
||||
game_state->player_score += game_state->bet * 2;
|
||||
game_state->bet = 0;
|
||||
enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
|
||||
game_state->settings.message_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
start,
|
||||
before_start,
|
||||
to_start,
|
||||
game_state->settings.message_duration);
|
||||
}
|
||||
|
||||
|
||||
void dealerTurn(void *ctx) {
|
||||
GameState *game_state = ctx;
|
||||
void dealerTurn(void* ctx) {
|
||||
GameState* game_state = ctx;
|
||||
game_state->state = GameStateDealer;
|
||||
}
|
||||
|
||||
float animationTime(const GameState *game_state){
|
||||
return (float) (furi_get_tick() - game_state->queue_state.start) /
|
||||
(float) (game_state->settings.animation_duration);
|
||||
float animationTime(const GameState* game_state) {
|
||||
return (float)(furi_get_tick() - game_state->queue_state.start) /
|
||||
(float)(game_state->settings.animation_duration);
|
||||
}
|
||||
|
||||
void dealer_card_animation(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
void dealer_card_animation(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
float t = animationTime(game_state);
|
||||
|
||||
Card animatingCard = game_state->deck.cards[game_state->deck.index];
|
||||
if (game_state->dealer_card_count > 1) {
|
||||
if(game_state->dealer_card_count > 1) {
|
||||
Vector end = card_pos_at_index(game_state->dealer_card_count);
|
||||
draw_card_animation(animatingCard,
|
||||
(Vector) {0, 64},
|
||||
(Vector) {0, 32},
|
||||
end,
|
||||
t,
|
||||
true,
|
||||
canvas);
|
||||
draw_card_animation(animatingCard, (Vector){0, 64}, (Vector){0, 32}, end, t, true, canvas);
|
||||
} else {
|
||||
draw_card_animation(animatingCard,
|
||||
(Vector) {32, -CARD_HEIGHT},
|
||||
(Vector) {64, 32},
|
||||
(Vector) {2, 2},
|
||||
t,
|
||||
false,
|
||||
canvas);
|
||||
// draw_deck(game_state->dealer_cards, game_state->dealer_card_count, canvas);
|
||||
draw_card_animation(
|
||||
animatingCard,
|
||||
(Vector){32, -CARD_HEIGHT},
|
||||
(Vector){64, 32},
|
||||
(Vector){2, 2},
|
||||
t,
|
||||
false,
|
||||
canvas);
|
||||
// draw_deck(game_state->dealer_cards, game_state->dealer_card_count, canvas);
|
||||
}
|
||||
}
|
||||
|
||||
void dealer_back_card_animation(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
void dealer_back_card_animation(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
float t = animationTime(game_state);
|
||||
|
||||
Vector currentPos = quadratic_2d((Vector) {32, -CARD_HEIGHT}, (Vector) {64, 32}, (Vector) {13, 5}, t);
|
||||
Vector currentPos =
|
||||
quadratic_2d((Vector){32, -CARD_HEIGHT}, (Vector){64, 32}, (Vector){13, 5}, t);
|
||||
draw_card_back_at(currentPos.x, currentPos.y, canvas);
|
||||
}
|
||||
|
||||
void player_card_animation(const void *ctx, Canvas *const canvas) {
|
||||
const GameState *game_state = ctx;
|
||||
void player_card_animation(const void* ctx, Canvas* const canvas) {
|
||||
const GameState* game_state = ctx;
|
||||
float t = animationTime(game_state);
|
||||
|
||||
Card animatingCard = game_state->deck.cards[game_state->deck.index];
|
||||
Vector end = card_pos_at_index(game_state->player_card_count);
|
||||
|
||||
draw_card_animation(animatingCard,
|
||||
(Vector) {32, -CARD_HEIGHT},
|
||||
(Vector) {0, 32},
|
||||
end,
|
||||
t,
|
||||
true,
|
||||
canvas);
|
||||
// draw_deck(game_state->dealer_cards, game_state->player_card_count, canvas);
|
||||
draw_card_animation(
|
||||
animatingCard, (Vector){32, -CARD_HEIGHT}, (Vector){0, 32}, end, t, true, canvas);
|
||||
// draw_deck(game_state->dealer_cards, game_state->player_card_count, canvas);
|
||||
}
|
||||
//endregion
|
||||
|
||||
void player_tick(GameState *game_state) {
|
||||
void player_tick(GameState* game_state) {
|
||||
uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count);
|
||||
if ((game_state->doubled && score <= 21) || score == 21) {
|
||||
enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
} else if (score > 21) {
|
||||
enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
|
||||
game_state->settings.message_duration);
|
||||
if((game_state->doubled && score <= 21) || score == 21) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
dealerTurn,
|
||||
NULL,
|
||||
to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
} else if(score > 21) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
lose,
|
||||
NULL,
|
||||
to_bust_state,
|
||||
game_state->settings.message_duration);
|
||||
} else {
|
||||
if(game_state->selectDirection == DirectionUp || game_state->selectDirection == DirectionDown){
|
||||
if(game_state->selectDirection == DirectionUp ||
|
||||
game_state->selectDirection == DirectionDown) {
|
||||
move_menu(game_state->menu, game_state->selectDirection == DirectionUp ? -1 : 1);
|
||||
}
|
||||
|
||||
if (game_state->selectDirection == Select){
|
||||
if(game_state->selectDirection == Select) {
|
||||
activate_menu(game_state->menu, game_state);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dealer_tick(GameState *game_state) {
|
||||
void dealer_tick(GameState* game_state) {
|
||||
uint8_t dealer_score = hand_count(game_state->dealer_cards, game_state->dealer_card_count);
|
||||
uint8_t player_score = hand_count(game_state->player_cards, game_state->player_card_count);
|
||||
|
||||
if (dealer_score >= DEALER_MAX) {
|
||||
if (dealer_score > 21 || dealer_score < player_score) {
|
||||
enqueue(&(game_state->queue_state), game_state, win, NULL, to_win_state,
|
||||
game_state->settings.message_duration);
|
||||
} else if (dealer_score > player_score) {
|
||||
enqueue(&(game_state->queue_state), game_state, lose, NULL, to_lose_state,
|
||||
game_state->settings.message_duration);
|
||||
} else if (dealer_score == player_score) {
|
||||
enqueue(&(game_state->queue_state), game_state, draw, NULL, to_draw_state,
|
||||
game_state->settings.message_duration);
|
||||
if(dealer_score >= DEALER_MAX) {
|
||||
if(dealer_score > 21 || dealer_score < player_score) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
win,
|
||||
NULL,
|
||||
to_win_state,
|
||||
game_state->settings.message_duration);
|
||||
} else if(dealer_score > player_score) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
lose,
|
||||
NULL,
|
||||
to_lose_state,
|
||||
game_state->settings.message_duration);
|
||||
} else if(dealer_score == player_score) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
draw,
|
||||
NULL,
|
||||
to_draw_state,
|
||||
game_state->settings.message_duration);
|
||||
}
|
||||
} else {
|
||||
enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawDealerCard,
|
||||
NULL,
|
||||
dealer_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
}
|
||||
}
|
||||
|
||||
void settings_tick(GameState *game_state) {
|
||||
if (game_state->selectDirection == DirectionDown && game_state->selectedMenu < 4) {
|
||||
void settings_tick(GameState* game_state) {
|
||||
if(game_state->selectDirection == DirectionDown && game_state->selectedMenu < 4) {
|
||||
game_state->selectedMenu++;
|
||||
}
|
||||
if (game_state->selectDirection == DirectionUp && game_state->selectedMenu > 0) {
|
||||
if(game_state->selectDirection == DirectionUp && game_state->selectedMenu > 0) {
|
||||
game_state->selectedMenu--;
|
||||
}
|
||||
|
||||
if (game_state->selectDirection == DirectionLeft || game_state->selectDirection == DirectionRight) {
|
||||
if(game_state->selectDirection == DirectionLeft ||
|
||||
game_state->selectDirection == DirectionRight) {
|
||||
int nextScore = 0;
|
||||
switch (game_state->selectedMenu) {
|
||||
case 0:
|
||||
nextScore = game_state->settings.starting_money;
|
||||
if (game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 10;
|
||||
else
|
||||
nextScore += 10;
|
||||
if (nextScore >= (int) game_state->settings.round_price && nextScore < 400)
|
||||
game_state->settings.starting_money = nextScore;
|
||||
break;
|
||||
case 1:
|
||||
nextScore = game_state->settings.round_price;
|
||||
if (game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 10;
|
||||
else
|
||||
nextScore += 10;
|
||||
if (nextScore >= 5 && nextScore <= (int) game_state->settings.starting_money)
|
||||
game_state->settings.round_price = nextScore;
|
||||
break;
|
||||
case 2:
|
||||
nextScore = game_state->settings.animation_duration;
|
||||
if (game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 100;
|
||||
else
|
||||
nextScore += 100;
|
||||
if (nextScore >= 0 && nextScore < 2000)
|
||||
game_state->settings.animation_duration = nextScore;
|
||||
break;
|
||||
case 3:
|
||||
nextScore = game_state->settings.message_duration;
|
||||
if (game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 100;
|
||||
else
|
||||
nextScore += 100;
|
||||
if (nextScore >= 0 && nextScore < 2000)
|
||||
game_state->settings.message_duration = nextScore;
|
||||
break;
|
||||
case 4:
|
||||
game_state->settings.sound_effects = !game_state->settings.sound_effects;
|
||||
default:
|
||||
break;
|
||||
switch(game_state->selectedMenu) {
|
||||
case 0:
|
||||
nextScore = game_state->settings.starting_money;
|
||||
if(game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 10;
|
||||
else
|
||||
nextScore += 10;
|
||||
if(nextScore >= (int)game_state->settings.round_price && nextScore < 400)
|
||||
game_state->settings.starting_money = nextScore;
|
||||
break;
|
||||
case 1:
|
||||
nextScore = game_state->settings.round_price;
|
||||
if(game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 10;
|
||||
else
|
||||
nextScore += 10;
|
||||
if(nextScore >= 5 && nextScore <= (int)game_state->settings.starting_money)
|
||||
game_state->settings.round_price = nextScore;
|
||||
break;
|
||||
case 2:
|
||||
nextScore = game_state->settings.animation_duration;
|
||||
if(game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 100;
|
||||
else
|
||||
nextScore += 100;
|
||||
if(nextScore >= 0 && nextScore < 2000)
|
||||
game_state->settings.animation_duration = nextScore;
|
||||
break;
|
||||
case 3:
|
||||
nextScore = game_state->settings.message_duration;
|
||||
if(game_state->selectDirection == DirectionLeft)
|
||||
nextScore -= 100;
|
||||
else
|
||||
nextScore += 100;
|
||||
if(nextScore >= 0 && nextScore < 2000)
|
||||
game_state->settings.message_duration = nextScore;
|
||||
break;
|
||||
case 4:
|
||||
game_state->settings.sound_effects = !game_state->settings.sound_effects;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void tick(GameState *game_state) {
|
||||
void tick(GameState* game_state) {
|
||||
game_state->last_tick = furi_get_tick();
|
||||
bool queue_ran = run_queue(&(game_state->queue_state), game_state);
|
||||
|
||||
switch (game_state->state) {
|
||||
case GameStateGameOver:
|
||||
case GameStateStart:
|
||||
if (game_state->selectDirection == Select)
|
||||
init(game_state);
|
||||
else if (game_state->selectDirection == DirectionRight) {
|
||||
game_state->selectedMenu = 0;
|
||||
game_state->state = GameStateSettings;
|
||||
}
|
||||
break;
|
||||
case GameStatePlay:
|
||||
if (!game_state->started) {
|
||||
game_state->selectedMenu = 0;
|
||||
game_state->started = true;
|
||||
enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_back_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
}
|
||||
if (!queue_ran)
|
||||
player_tick(game_state);
|
||||
break;
|
||||
case GameStateDealer:
|
||||
if (!queue_ran)
|
||||
dealer_tick(game_state);
|
||||
break;
|
||||
case GameStateSettings:
|
||||
settings_tick(game_state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
switch(game_state->state) {
|
||||
case GameStateGameOver:
|
||||
case GameStateStart:
|
||||
if(game_state->selectDirection == Select)
|
||||
init(game_state);
|
||||
else if(game_state->selectDirection == DirectionRight) {
|
||||
game_state->selectedMenu = 0;
|
||||
game_state->state = GameStateSettings;
|
||||
}
|
||||
break;
|
||||
case GameStatePlay:
|
||||
if(!game_state->started) {
|
||||
game_state->selectedMenu = 0;
|
||||
game_state->started = true;
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawDealerCard,
|
||||
NULL,
|
||||
dealer_back_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawPlayerCard,
|
||||
NULL,
|
||||
player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawDealerCard,
|
||||
NULL,
|
||||
dealer_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawPlayerCard,
|
||||
NULL,
|
||||
player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
}
|
||||
if(!queue_ran) player_tick(game_state);
|
||||
break;
|
||||
case GameStateDealer:
|
||||
if(!queue_ran) dealer_tick(game_state);
|
||||
break;
|
||||
case GameStateSettings:
|
||||
settings_tick(game_state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
game_state->selectDirection = None;
|
||||
|
||||
}
|
||||
|
||||
void start_round(GameState *game_state) {
|
||||
game_state->menu->current_menu=1;
|
||||
void start_round(GameState* game_state) {
|
||||
game_state->menu->current_menu = 1;
|
||||
game_state->player_card_count = 0;
|
||||
game_state->dealer_card_count = 0;
|
||||
set_menu_state(game_state->menu, 0, true);
|
||||
game_state->menu->enabled=true;
|
||||
game_state->menu->enabled = true;
|
||||
game_state->started = false;
|
||||
game_state->doubled = false;
|
||||
game_state->queue_state.running = true;
|
||||
shuffle_deck(&(game_state->deck));
|
||||
game_state->doubled = false;
|
||||
game_state->bet = game_state->settings.round_price;
|
||||
if (game_state->player_score < game_state->settings.round_price) {
|
||||
if(game_state->player_score < game_state->settings.round_price) {
|
||||
game_state->state = GameStateGameOver;
|
||||
} else {
|
||||
game_state->player_score -= game_state->settings.round_price;
|
||||
@@ -409,10 +451,10 @@ void start_round(GameState *game_state) {
|
||||
game_state->state = GameStatePlay;
|
||||
}
|
||||
|
||||
void init(GameState *game_state) {
|
||||
void init(GameState* game_state) {
|
||||
set_menu_state(game_state->menu, 0, true);
|
||||
game_state->menu->enabled=true;
|
||||
game_state->menu->current_menu=1;
|
||||
game_state->menu->enabled = true;
|
||||
game_state->menu->current_menu = 1;
|
||||
game_state->settings = load_settings();
|
||||
game_state->last_tick = 0;
|
||||
game_state->processing = true;
|
||||
@@ -422,60 +464,86 @@ void init(GameState *game_state) {
|
||||
start_round(game_state);
|
||||
}
|
||||
|
||||
static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
|
||||
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
AppEvent event = {.type = EventTypeKey, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
static void update_timer_callback(FuriMessageQueue *event_queue) {
|
||||
static void update_timer_callback(FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
AppEvent event = {.type = EventTypeTick};
|
||||
furi_message_queue_put(event_queue, &event, 0);
|
||||
}
|
||||
|
||||
void doubleAction(void *state){
|
||||
GameState *game_state = state;
|
||||
if (!game_state->doubled && game_state->player_score >= game_state->settings.round_price) {
|
||||
void doubleAction(void* state) {
|
||||
GameState* game_state = state;
|
||||
if(!game_state->doubled && game_state->player_score >= game_state->settings.round_price) {
|
||||
game_state->player_score -= game_state->settings.round_price;
|
||||
game_state->bet += game_state->settings.round_price;
|
||||
game_state->doubled = true;
|
||||
enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
game_state->player_cards[game_state->player_card_count] = game_state->deck.cards[game_state->deck.index];
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawPlayerCard,
|
||||
NULL,
|
||||
player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
game_state->player_cards[game_state->player_card_count] =
|
||||
game_state->deck.cards[game_state->deck.index];
|
||||
uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count + 1);
|
||||
if (score > 21) {
|
||||
enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
|
||||
game_state->settings.message_duration);
|
||||
if(score > 21) {
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
lose,
|
||||
NULL,
|
||||
to_bust_state,
|
||||
game_state->settings.message_duration);
|
||||
} else {
|
||||
enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
dealerTurn,
|
||||
NULL,
|
||||
to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
}
|
||||
set_menu_state(game_state->menu, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
void hitAction(void *state){
|
||||
GameState *game_state = state;
|
||||
enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
void hitAction(void* state) {
|
||||
GameState* game_state = state;
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
drawPlayerCard,
|
||||
NULL,
|
||||
player_card_animation,
|
||||
game_state->settings.animation_duration);
|
||||
}
|
||||
void stayAction(void *state){
|
||||
GameState *game_state = state;
|
||||
enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
void stayAction(void* state) {
|
||||
GameState* game_state = state;
|
||||
enqueue(
|
||||
&(game_state->queue_state),
|
||||
game_state,
|
||||
dealerTurn,
|
||||
NULL,
|
||||
to_dealer_turn,
|
||||
game_state->settings.message_duration);
|
||||
}
|
||||
|
||||
int32_t blackjack_app(void *p) {
|
||||
int32_t blackjack_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
int32_t return_code = 0;
|
||||
|
||||
FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
|
||||
|
||||
GameState *game_state = malloc(sizeof(GameState));
|
||||
game_state->menu= malloc(sizeof(Menu));
|
||||
game_state->menu->menu_width=40;
|
||||
GameState* game_state = malloc(sizeof(GameState));
|
||||
game_state->menu = malloc(sizeof(Menu));
|
||||
game_state->menu->menu_width = 40;
|
||||
init(game_state);
|
||||
add_menu(game_state->menu, "Double", doubleAction);
|
||||
add_menu(game_state->menu, "Hit", hitAction);
|
||||
@@ -484,58 +552,56 @@ int32_t blackjack_app(void *p) {
|
||||
game_state->state = GameStateStart;
|
||||
|
||||
ValueMutex state_mutex;
|
||||
if (!init_mutex(&state_mutex, game_state, sizeof(GameState))) {
|
||||
if(!init_mutex(&state_mutex, game_state, sizeof(GameState))) {
|
||||
FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
|
||||
return_code = 255;
|
||||
goto free_and_exit;
|
||||
}
|
||||
|
||||
ViewPort *view_port = view_port_alloc();
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, render_callback, &state_mutex);
|
||||
view_port_input_callback_set(view_port, input_callback, event_queue);
|
||||
|
||||
FuriTimer *timer =
|
||||
furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||
FuriTimer* timer = furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
|
||||
|
||||
Gui *gui = furi_record_open("gui");
|
||||
Gui* gui = furi_record_open("gui");
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
AppEvent event;
|
||||
|
||||
for (bool processing = true; processing;) {
|
||||
for(bool processing = true; processing;) {
|
||||
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
||||
GameState *localstate = (GameState *) acquire_mutex_block(&state_mutex);
|
||||
if (event_status == FuriStatusOk) {
|
||||
if (event.type == EventTypeKey) {
|
||||
|
||||
if (event.input.type == InputTypePress) {
|
||||
switch (event.input.key) {
|
||||
case InputKeyUp:
|
||||
localstate->selectDirection = DirectionUp;
|
||||
break;
|
||||
case InputKeyDown:
|
||||
localstate->selectDirection = DirectionDown;
|
||||
break;
|
||||
case InputKeyRight:
|
||||
localstate->selectDirection = DirectionRight;
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
localstate->selectDirection = DirectionLeft;
|
||||
break;
|
||||
case InputKeyBack:
|
||||
if (localstate->state == GameStateSettings) {
|
||||
localstate->state = GameStateStart;
|
||||
save_settings(localstate->settings);
|
||||
} else
|
||||
processing = false;
|
||||
break;
|
||||
case InputKeyOk:
|
||||
localstate->selectDirection = Select;
|
||||
break;
|
||||
GameState* localstate = (GameState*)acquire_mutex_block(&state_mutex);
|
||||
if(event_status == FuriStatusOk) {
|
||||
if(event.type == EventTypeKey) {
|
||||
if(event.input.type == InputTypePress) {
|
||||
switch(event.input.key) {
|
||||
case InputKeyUp:
|
||||
localstate->selectDirection = DirectionUp;
|
||||
break;
|
||||
case InputKeyDown:
|
||||
localstate->selectDirection = DirectionDown;
|
||||
break;
|
||||
case InputKeyRight:
|
||||
localstate->selectDirection = DirectionRight;
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
localstate->selectDirection = DirectionLeft;
|
||||
break;
|
||||
case InputKeyBack:
|
||||
if(localstate->state == GameStateSettings) {
|
||||
localstate->state = GameStateStart;
|
||||
save_settings(localstate->settings);
|
||||
} else
|
||||
processing = false;
|
||||
break;
|
||||
case InputKeyOk:
|
||||
localstate->selectDirection = Select;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (event.type == EventTypeTick) {
|
||||
} else if(event.type == EventTypeTick) {
|
||||
tick(localstate);
|
||||
processing = localstate->processing;
|
||||
}
|
||||
@@ -547,7 +613,6 @@ int32_t blackjack_app(void *p) {
|
||||
release_mutex(&state_mutex, localstate);
|
||||
}
|
||||
|
||||
|
||||
furi_timer_free(timer);
|
||||
view_port_enabled_set(view_port, false);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
@@ -555,7 +620,7 @@ int32_t blackjack_app(void *p) {
|
||||
view_port_free(view_port);
|
||||
delete_mutex(&state_mutex);
|
||||
|
||||
free_and_exit:
|
||||
free_and_exit:
|
||||
free_menu(game_state->menu);
|
||||
queue_clear(&(game_state->queue_state));
|
||||
free(game_state);
|
||||
|
||||
@@ -22,7 +22,7 @@ typedef enum {
|
||||
EventTypeKey,
|
||||
} EventType;
|
||||
|
||||
typedef struct{
|
||||
typedef struct {
|
||||
uint32_t animation_duration;
|
||||
uint32_t message_duration;
|
||||
uint32_t starting_money;
|
||||
@@ -43,14 +43,7 @@ typedef enum {
|
||||
GameStateDealer,
|
||||
} PlayState;
|
||||
|
||||
typedef enum {
|
||||
DirectionUp,
|
||||
DirectionRight,
|
||||
DirectionDown,
|
||||
DirectionLeft,
|
||||
Select,
|
||||
None
|
||||
} Direction;
|
||||
typedef enum { DirectionUp, DirectionRight, DirectionDown, DirectionLeft, Select, None } Direction;
|
||||
|
||||
typedef struct {
|
||||
Card player_cards[21];
|
||||
@@ -70,7 +63,6 @@ typedef struct {
|
||||
Deck deck;
|
||||
PlayState state;
|
||||
QueueState queue_state;
|
||||
Menu *menu;
|
||||
Menu* menu;
|
||||
unsigned int last_tick;
|
||||
} GameState;
|
||||
|
||||
|
||||
@@ -6,32 +6,28 @@
|
||||
#define LINE_HEIGHT 16
|
||||
#define ITEM_PADDING 4
|
||||
|
||||
const char MoneyMul[4] = {
|
||||
'K', 'B', 'T', 'S'
|
||||
};
|
||||
const char MoneyMul[4] = {'K', 'B', 'T', 'S'};
|
||||
|
||||
void draw_player_scene(Canvas *const canvas, const GameState *game_state) {
|
||||
void draw_player_scene(Canvas* const canvas, const GameState* game_state) {
|
||||
int max_card = game_state->player_card_count;
|
||||
|
||||
if (max_card > 0)
|
||||
draw_deck((game_state->player_cards), max_card, canvas);
|
||||
if(max_card > 0) draw_deck((game_state->player_cards), max_card, canvas);
|
||||
|
||||
if (game_state->dealer_card_count > 0)
|
||||
draw_card_back_at(13, 5, canvas);
|
||||
if(game_state->dealer_card_count > 0) draw_card_back_at(13, 5, canvas);
|
||||
|
||||
max_card = game_state->dealer_card_count;
|
||||
if (max_card > 1) {
|
||||
draw_card_at(2, 2, game_state->dealer_cards[1].pip, game_state->dealer_cards[1].character,
|
||||
canvas);
|
||||
if(max_card > 1) {
|
||||
draw_card_at(
|
||||
2, 2, game_state->dealer_cards[1].pip, game_state->dealer_cards[1].character, canvas);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_dealer_scene(Canvas *const canvas, const GameState *game_state) {
|
||||
void draw_dealer_scene(Canvas* const canvas, const GameState* game_state) {
|
||||
uint8_t max_card = game_state->dealer_card_count;
|
||||
draw_deck((game_state->dealer_cards), max_card, canvas);
|
||||
}
|
||||
|
||||
void popup_frame(Canvas *const canvas) {
|
||||
void popup_frame(Canvas* const canvas) {
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_box(canvas, 32, 15, 66, 13);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
@@ -39,15 +35,16 @@ void popup_frame(Canvas *const canvas) {
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
}
|
||||
|
||||
|
||||
void draw_play_menu(Canvas *const canvas, const GameState *game_state) {
|
||||
const char *menus[3] = {"Double", "Hit", "Stay"};
|
||||
for (uint8_t m = 0; m < 3; m++) {
|
||||
if (m == 0 && (game_state->doubled || game_state->player_score < game_state->settings.round_price)) continue;
|
||||
void draw_play_menu(Canvas* const canvas, const GameState* game_state) {
|
||||
const char* menus[3] = {"Double", "Hit", "Stay"};
|
||||
for(uint8_t m = 0; m < 3; m++) {
|
||||
if(m == 0 &&
|
||||
(game_state->doubled || game_state->player_score < game_state->settings.round_price))
|
||||
continue;
|
||||
int y = m * 13 + 25;
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
|
||||
if (game_state->selectedMenu == m) {
|
||||
if(game_state->selectedMenu == m) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_box(canvas, 1, y, 31, 12);
|
||||
} else {
|
||||
@@ -57,7 +54,7 @@ void draw_play_menu(Canvas *const canvas, const GameState *game_state) {
|
||||
canvas_draw_frame(canvas, 1, y, 31, 12);
|
||||
}
|
||||
|
||||
if (game_state->selectedMenu == m)
|
||||
if(game_state->selectedMenu == m)
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
else
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
@@ -65,35 +62,34 @@ void draw_play_menu(Canvas *const canvas, const GameState *game_state) {
|
||||
}
|
||||
}
|
||||
|
||||
void draw_screen(Canvas *const canvas, const bool *points) {
|
||||
for (uint8_t x = 0; x < 128; x++) {
|
||||
for (uint8_t y = 0; y < 64; y++) {
|
||||
if (points[y * 128 + x])
|
||||
canvas_draw_dot(canvas, x, y);
|
||||
void draw_screen(Canvas* const canvas, const bool* points) {
|
||||
for(uint8_t x = 0; x < 128; x++) {
|
||||
for(uint8_t y = 0; y < 64; y++) {
|
||||
if(points[y * 128 + x]) canvas_draw_dot(canvas, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_score(Canvas *const canvas, bool top, uint8_t amount) {
|
||||
void draw_score(Canvas* const canvas, bool top, uint8_t amount) {
|
||||
char drawChar[20];
|
||||
snprintf(drawChar, sizeof(drawChar), "Player score: %i", amount);
|
||||
if (top)
|
||||
if(top)
|
||||
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, drawChar);
|
||||
else
|
||||
canvas_draw_str_aligned(canvas, 64, 62, AlignCenter, AlignBottom, drawChar);
|
||||
}
|
||||
|
||||
void draw_money(Canvas *const canvas, uint32_t score) {
|
||||
void draw_money(Canvas* const canvas, uint32_t score) {
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
char drawChar[10];
|
||||
uint32_t currAmount = score;
|
||||
if (currAmount < 1000) {
|
||||
if(currAmount < 1000) {
|
||||
snprintf(drawChar, sizeof(drawChar), "$%lu", currAmount);
|
||||
} else {
|
||||
char c = 'K';
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
for(uint8_t i = 0; i < 4; i++) {
|
||||
currAmount = currAmount / 1000;
|
||||
if (currAmount < 1000) {
|
||||
if(currAmount < 1000) {
|
||||
c = MoneyMul[i];
|
||||
break;
|
||||
}
|
||||
@@ -104,77 +100,87 @@ void draw_money(Canvas *const canvas, uint32_t score) {
|
||||
canvas_draw_str_aligned(canvas, 126, 2, AlignRight, AlignTop, drawChar);
|
||||
}
|
||||
|
||||
|
||||
void draw_menu(Canvas *const canvas, const char *text, const char *value, int8_t y, bool left_caret, bool right_caret,
|
||||
bool selected) {
|
||||
void draw_menu(
|
||||
Canvas* const canvas,
|
||||
const char* text,
|
||||
const char* value,
|
||||
int8_t y,
|
||||
bool left_caret,
|
||||
bool right_caret,
|
||||
bool selected) {
|
||||
UNUSED(selected);
|
||||
if (y < 0 || y >= 64) return;
|
||||
if(y < 0 || y >= 64) return;
|
||||
|
||||
if (selected) {
|
||||
if(selected) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_box(canvas, 0, y, 122, LINE_HEIGHT);
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
}
|
||||
|
||||
canvas_draw_str_aligned(canvas, 4, y + ITEM_PADDING, AlignLeft, AlignTop, text);
|
||||
if (left_caret)
|
||||
canvas_draw_str_aligned(canvas, 80, y + ITEM_PADDING, AlignLeft, AlignTop, "<");
|
||||
if(left_caret) canvas_draw_str_aligned(canvas, 80, y + ITEM_PADDING, AlignLeft, AlignTop, "<");
|
||||
canvas_draw_str_aligned(canvas, 100, y + ITEM_PADDING, AlignCenter, AlignTop, value);
|
||||
if (right_caret)
|
||||
if(right_caret)
|
||||
canvas_draw_str_aligned(canvas, 120, y + ITEM_PADDING, AlignRight, AlignTop, ">");
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
}
|
||||
|
||||
void settings_page(Canvas *const canvas, const GameState *gameState) {
|
||||
void settings_page(Canvas* const canvas, const GameState* gameState) {
|
||||
char drawChar[10];
|
||||
int startY = 0;
|
||||
if (LINE_HEIGHT * (gameState->selectedMenu + 1) >= 64) {
|
||||
if(LINE_HEIGHT * (gameState->selectedMenu + 1) >= 64) {
|
||||
startY -= (LINE_HEIGHT * (gameState->selectedMenu + 1)) - 64;
|
||||
}
|
||||
|
||||
int scrollHeight = round(64 / 6.0) + ITEM_PADDING * 2;
|
||||
int scrollPos = 64 / (6.0 / (gameState->selectedMenu + 1)) - ITEM_PADDING * 2;
|
||||
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_box(canvas, 123, scrollPos, 4, scrollHeight);
|
||||
canvas_draw_box(canvas, 125, 0, 1, 64);
|
||||
|
||||
snprintf(drawChar, sizeof(drawChar), "%li", gameState->settings.starting_money);
|
||||
draw_menu(canvas, "Start money", drawChar,
|
||||
0 * LINE_HEIGHT + startY,
|
||||
gameState->settings.starting_money > gameState->settings.round_price,
|
||||
gameState->settings.starting_money < 400,
|
||||
gameState->selectedMenu == 0
|
||||
);
|
||||
draw_menu(
|
||||
canvas,
|
||||
"Start money",
|
||||
drawChar,
|
||||
0 * LINE_HEIGHT + startY,
|
||||
gameState->settings.starting_money > gameState->settings.round_price,
|
||||
gameState->settings.starting_money < 400,
|
||||
gameState->selectedMenu == 0);
|
||||
snprintf(drawChar, sizeof(drawChar), "%li", gameState->settings.round_price);
|
||||
draw_menu(canvas, "Round price", drawChar,
|
||||
1 * LINE_HEIGHT + startY,
|
||||
gameState->settings.round_price > 10,
|
||||
gameState->settings.round_price < gameState->settings.starting_money,
|
||||
gameState->selectedMenu == 1
|
||||
);
|
||||
draw_menu(
|
||||
canvas,
|
||||
"Round price",
|
||||
drawChar,
|
||||
1 * LINE_HEIGHT + startY,
|
||||
gameState->settings.round_price > 10,
|
||||
gameState->settings.round_price < gameState->settings.starting_money,
|
||||
gameState->selectedMenu == 1);
|
||||
|
||||
snprintf(drawChar, sizeof(drawChar), "%li", gameState->settings.animation_duration);
|
||||
draw_menu(canvas, "Anim. length", drawChar,
|
||||
2 * LINE_HEIGHT + startY,
|
||||
gameState->settings.animation_duration > 0,
|
||||
gameState->settings.animation_duration < 2000,
|
||||
gameState->selectedMenu == 2
|
||||
);
|
||||
draw_menu(
|
||||
canvas,
|
||||
"Anim. length",
|
||||
drawChar,
|
||||
2 * LINE_HEIGHT + startY,
|
||||
gameState->settings.animation_duration > 0,
|
||||
gameState->settings.animation_duration < 2000,
|
||||
gameState->selectedMenu == 2);
|
||||
snprintf(drawChar, sizeof(drawChar), "%li", gameState->settings.message_duration);
|
||||
draw_menu(canvas, "Popup time", drawChar,
|
||||
3 * LINE_HEIGHT + startY,
|
||||
gameState->settings.message_duration > 0,
|
||||
gameState->settings.message_duration < 2000,
|
||||
gameState->selectedMenu == 3
|
||||
);
|
||||
// draw_menu(canvas, "Sound", gameState->settings.sound_effects ? "Yes" : "No",
|
||||
// 5 * LINE_HEIGHT + startY,
|
||||
// true,
|
||||
// true,
|
||||
// gameState->selectedMenu == 5
|
||||
// );
|
||||
|
||||
draw_menu(
|
||||
canvas,
|
||||
"Popup time",
|
||||
drawChar,
|
||||
3 * LINE_HEIGHT + startY,
|
||||
gameState->settings.message_duration > 0,
|
||||
gameState->settings.message_duration < 2000,
|
||||
gameState->selectedMenu == 3);
|
||||
// draw_menu(canvas, "Sound", gameState->settings.sound_effects ? "Yes" : "No",
|
||||
// 5 * LINE_HEIGHT + startY,
|
||||
// true,
|
||||
// true,
|
||||
// gameState->selectedMenu == 5
|
||||
// );
|
||||
}
|
||||
@@ -3,16 +3,16 @@
|
||||
#include "defines.h"
|
||||
#include <gui/gui.h>
|
||||
|
||||
void draw_player_scene(Canvas *const canvas, const GameState *game_state);
|
||||
void draw_player_scene(Canvas* const canvas, const GameState* game_state);
|
||||
|
||||
void draw_dealer_scene(Canvas *const canvas, const GameState *game_state);
|
||||
void draw_dealer_scene(Canvas* const canvas, const GameState* game_state);
|
||||
|
||||
void draw_play_menu(Canvas *const canvas, const GameState *game_state);
|
||||
void draw_play_menu(Canvas* const canvas, const GameState* game_state);
|
||||
|
||||
void draw_score(Canvas *const canvas, bool top, uint8_t amount);
|
||||
void draw_score(Canvas* const canvas, bool top, uint8_t amount);
|
||||
|
||||
void draw_money(Canvas *const canvas, uint32_t score);
|
||||
void settings_page(Canvas *const canvas, const GameState * gameState);
|
||||
void draw_money(Canvas* const canvas, uint32_t score);
|
||||
void settings_page(Canvas* const canvas, const GameState* gameState);
|
||||
|
||||
void popup_frame(Canvas *const canvas);
|
||||
void draw_screen(Canvas *const canvas, const bool* points);
|
||||
void popup_frame(Canvas* const canvas);
|
||||
void draw_screen(Canvas* const canvas, const bool* points);
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#include <storage/storage.h>
|
||||
#include "util.h"
|
||||
|
||||
const char *CONFIG_FILE_PATH = EXT_PATH(".blackjack.settings");
|
||||
const char* CONFIG_FILE_PATH = EXT_PATH(".blackjack.settings");
|
||||
|
||||
void save_settings(Settings settings) {
|
||||
Storage *storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat *file = flipper_format_file_alloc(storage);
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
FURI_LOG_D(APP_NAME, "Saving config");
|
||||
if (flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
|
||||
FURI_LOG_D(APP_NAME, "Saving %s: %ld", CONF_ANIMATION_DURATION, settings.animation_duration);
|
||||
flipper_format_update_uint32(file, CONF_ANIMATION_DURATION, &(settings.animation_duration), 1);
|
||||
|
||||
if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
|
||||
FURI_LOG_D(
|
||||
APP_NAME, "Saving %s: %ld", CONF_ANIMATION_DURATION, settings.animation_duration);
|
||||
flipper_format_update_uint32(
|
||||
file, CONF_ANIMATION_DURATION, &(settings.animation_duration), 1);
|
||||
|
||||
FURI_LOG_D(APP_NAME, "Saving %s: %ld", CONF_MESSAGE_DURATION, settings.message_duration);
|
||||
flipper_format_update_uint32(file, CONF_MESSAGE_DURATION, &(settings.message_duration), 1);
|
||||
@@ -21,10 +22,10 @@ void save_settings(Settings settings) {
|
||||
FURI_LOG_D(APP_NAME, "Saving %s: %ld", CONF_ROUND_PRICE, settings.round_price);
|
||||
flipper_format_update_uint32(file, CONF_ROUND_PRICE, &(settings.round_price), 1);
|
||||
|
||||
FURI_LOG_D(APP_NAME, "Saving %s: %i", CONF_SOUND_EFFECTS, settings.sound_effects?1:0);
|
||||
FURI_LOG_D(APP_NAME, "Saving %s: %i", CONF_SOUND_EFFECTS, settings.sound_effects ? 1 : 0);
|
||||
flipper_format_update_bool(file, CONF_SOUND_EFFECTS, &(settings.sound_effects), 1);
|
||||
FURI_LOG_D(APP_NAME, "Config saved");
|
||||
}else{
|
||||
} else {
|
||||
FURI_LOG_E(APP_NAME, "Save error");
|
||||
}
|
||||
flipper_format_file_close(file);
|
||||
@@ -32,7 +33,7 @@ void save_settings(Settings settings) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
void save_settings_file(FlipperFormat *file, Settings *settings) {
|
||||
void save_settings_file(FlipperFormat* file, Settings* settings) {
|
||||
flipper_format_write_header_cstr(file, CONFIG_FILE_HEADER, CONFIG_FILE_VERSION);
|
||||
flipper_format_write_comment_cstr(file, "Card animation duration in ms");
|
||||
flipper_format_write_uint32(file, CONF_ANIMATION_DURATION, &(settings->animation_duration), 1);
|
||||
@@ -57,58 +58,57 @@ Settings load_settings() {
|
||||
settings.sound_effects = true;
|
||||
|
||||
FURI_LOG_D(APP_NAME, "Opening storage");
|
||||
Storage *storage = furi_record_open(RECORD_STORAGE);
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FURI_LOG_D(APP_NAME, "Allocating file");
|
||||
FlipperFormat *file = flipper_format_file_alloc(storage);
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
FURI_LOG_D(APP_NAME, "Allocating string");
|
||||
FuriString *string_value;
|
||||
FuriString* string_value;
|
||||
string_value = furi_string_alloc();
|
||||
|
||||
if (storage_common_stat(storage, CONFIG_FILE_PATH, NULL) != FSE_OK) {
|
||||
if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) != FSE_OK) {
|
||||
FURI_LOG_D(APP_NAME, "Config file %s not found, creating new one...", CONFIG_FILE_PATH);
|
||||
if (!flipper_format_file_open_new(file, CONFIG_FILE_PATH)) {
|
||||
if(!flipper_format_file_open_new(file, CONFIG_FILE_PATH)) {
|
||||
FURI_LOG_E(APP_NAME, "Error creating new file %s", CONFIG_FILE_PATH);
|
||||
flipper_format_file_close(file);
|
||||
} else {
|
||||
save_settings_file(file, &settings);
|
||||
}
|
||||
} else {
|
||||
if (!flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
|
||||
if(!flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
|
||||
FURI_LOG_E(APP_NAME, "Error opening existing file %s", CONFIG_FILE_PATH);
|
||||
flipper_format_file_close(file);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
uint32_t value;
|
||||
bool valueBool;
|
||||
FURI_LOG_D(APP_NAME, "Checking version");
|
||||
if (!flipper_format_read_header(file, string_value, &value)) {
|
||||
if(!flipper_format_read_header(file, string_value, &value)) {
|
||||
FURI_LOG_E(APP_NAME, "Config file mismatch");
|
||||
} else {
|
||||
FURI_LOG_D(APP_NAME, "Loading %s", CONF_ANIMATION_DURATION);
|
||||
if (flipper_format_read_uint32(file, CONF_ANIMATION_DURATION, &value, 1)) {
|
||||
if(flipper_format_read_uint32(file, CONF_ANIMATION_DURATION, &value, 1)) {
|
||||
settings.animation_duration = value;
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %ld", CONF_ANIMATION_DURATION, value);
|
||||
}
|
||||
FURI_LOG_D(APP_NAME, "Loading %s", CONF_MESSAGE_DURATION);
|
||||
if (flipper_format_read_uint32(file, CONF_MESSAGE_DURATION, &value, 1)) {
|
||||
if(flipper_format_read_uint32(file, CONF_MESSAGE_DURATION, &value, 1)) {
|
||||
settings.message_duration = value;
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %ld", CONF_MESSAGE_DURATION, value);
|
||||
}
|
||||
FURI_LOG_D(APP_NAME, "Loading %s", CONF_STARTING_MONEY);
|
||||
if (flipper_format_read_uint32(file, CONF_STARTING_MONEY, &value, 1)) {
|
||||
if(flipper_format_read_uint32(file, CONF_STARTING_MONEY, &value, 1)) {
|
||||
settings.starting_money = value;
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %ld", CONF_STARTING_MONEY, value);
|
||||
}
|
||||
FURI_LOG_D(APP_NAME, "Loading %s", CONF_ROUND_PRICE);
|
||||
if (flipper_format_read_uint32(file, CONF_ROUND_PRICE, &value, 1)) {
|
||||
if(flipper_format_read_uint32(file, CONF_ROUND_PRICE, &value, 1)) {
|
||||
settings.round_price = value;
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %ld", CONF_ROUND_PRICE, value);
|
||||
}
|
||||
FURI_LOG_D(APP_NAME, "Loading %s", CONF_SOUND_EFFECTS);
|
||||
if (flipper_format_read_bool(file, CONF_SOUND_EFFECTS, &valueBool, 1)) {
|
||||
if(flipper_format_read_bool(file, CONF_SOUND_EFFECTS, &valueBool, 1)) {
|
||||
settings.sound_effects = valueBool;
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %i", CONF_ROUND_PRICE, valueBool?1:0);
|
||||
FURI_LOG_D(APP_NAME, "Loaded %s: %i", CONF_ROUND_PRICE, valueBool ? 1 : 0);
|
||||
}
|
||||
}
|
||||
flipper_format_file_close(file);
|
||||
@@ -116,7 +116,7 @@ Settings load_settings() {
|
||||
}
|
||||
|
||||
furi_string_free(string_value);
|
||||
// flipper_format_file_close(file);
|
||||
// flipper_format_file_close(file);
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
return settings;
|
||||
|
||||
@@ -12,12 +12,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define TS_DEFAULT_VALUE 0xFFFF
|
||||
#define TS_DEFAULT_VALUE 0xFFFF
|
||||
|
||||
#define HTU21D_ADDRESS (0x40 << 1)
|
||||
#define HTU21D_ADDRESS (0x40 << 1)
|
||||
|
||||
#define HTU21D_CMD_TEMPERATURE 0xE3
|
||||
#define HTU21D_CMD_HUMIDITY 0xE5
|
||||
#define HTU21D_CMD_TEMPERATURE 0xE3
|
||||
#define HTU21D_CMD_HUMIDITY 0xE5
|
||||
|
||||
#define DATA_BUFFER_SIZE 8
|
||||
|
||||
@@ -25,19 +25,19 @@
|
||||
#define I2C_BUS &furi_hal_i2c_handle_external
|
||||
|
||||
typedef enum {
|
||||
TSSInitializing,
|
||||
TSSNoSensor,
|
||||
TSSPendingUpdate,
|
||||
TSSInitializing,
|
||||
TSSNoSensor,
|
||||
TSSPendingUpdate,
|
||||
} TSStatus;
|
||||
|
||||
typedef enum {
|
||||
TSEventTypeTick,
|
||||
TSEventTypeInput,
|
||||
TSEventTypeTick,
|
||||
TSEventTypeInput,
|
||||
} TSEventType;
|
||||
|
||||
typedef struct {
|
||||
TSEventType type;
|
||||
InputEvent input;
|
||||
TSEventType type;
|
||||
InputEvent input;
|
||||
} TSEvent;
|
||||
|
||||
extern const NotificationSequence sequence_blink_red_100;
|
||||
@@ -58,36 +58,32 @@ char ts_data_buffer_absolute_humidity[DATA_BUFFER_SIZE];
|
||||
// true if fetch was successful, false otherwise
|
||||
// </returns>
|
||||
static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
|
||||
uint32_t timeout = furi_ms_to_ticks(100);
|
||||
bool ret = false;
|
||||
|
||||
uint32_t timeout = furi_ms_to_ticks(100);
|
||||
bool ret = false;
|
||||
// Aquire I2C and check if device is ready, then release
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
if(furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout)) {
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
|
||||
// Aquire I2C and check if device is ready, then release
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
if (furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout))
|
||||
{
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
// Transmit given command
|
||||
ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout);
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
// Transmit given command
|
||||
ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout);
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
if(ret) {
|
||||
uint32_t wait_ticks = furi_ms_to_ticks(50);
|
||||
furi_delay_tick(wait_ticks);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
uint32_t wait_ticks = furi_ms_to_ticks(50);
|
||||
furi_delay_tick(wait_ticks);
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
// Receive data
|
||||
ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout);
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
}
|
||||
} else
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
// Receive data
|
||||
ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout);
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
}
|
||||
}
|
||||
else
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// <sumary>
|
||||
@@ -101,212 +97,199 @@ static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
|
||||
// true if fetch was successful, false otherwise
|
||||
// </returns>
|
||||
static bool temperature_sensor_fetch_data(double* temperature, double* humidity) {
|
||||
bool ret = false;
|
||||
bool ret = false;
|
||||
|
||||
uint16_t adc_raw;
|
||||
uint16_t adc_raw;
|
||||
|
||||
uint8_t buffer[2] = { 0x00 };
|
||||
uint8_t buffer[2] = {0x00};
|
||||
|
||||
// Fetch temperature
|
||||
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
|
||||
// Fetch temperature
|
||||
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
// Calculate temperature
|
||||
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
|
||||
*temperature = (float)(adc_raw * 175.72 / 65536.00) - 46.85;
|
||||
if(ret) {
|
||||
// Calculate temperature
|
||||
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
|
||||
*temperature = (float)(adc_raw * 175.72 / 65536.00) - 46.85;
|
||||
|
||||
// Fetch humidity
|
||||
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
|
||||
// Fetch humidity
|
||||
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
// Calculate humidity
|
||||
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
|
||||
*humidity = (float)(adc_raw * 125.0 / 65536.00) - 6.0;
|
||||
}
|
||||
}
|
||||
if(ret) {
|
||||
// Calculate humidity
|
||||
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
|
||||
*humidity = (float)(adc_raw * 125.0 / 65536.00) - 6.0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// <sumary>
|
||||
// Draw callback
|
||||
// </sumary>
|
||||
static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
|
||||
UNUSED(ctx);
|
||||
|
||||
UNUSED(ctx);
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 2, 10, "HTU21D/Si7021 Sensor");
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 2, 10, "HTU21D/Si7021 Sensor");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 2, 62, "Press back to exit.");
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 2, 62, "Press back to exit.");
|
||||
switch(temperature_sensor_current_status) {
|
||||
case TSSInitializing:
|
||||
canvas_draw_str(canvas, 2, 30, "Initializing..");
|
||||
break;
|
||||
case TSSNoSensor:
|
||||
canvas_draw_str(canvas, 2, 30, "No sensor found!");
|
||||
break;
|
||||
case TSSPendingUpdate: {
|
||||
canvas_draw_str(canvas, 3, 24, "Temperature");
|
||||
canvas_draw_str(canvas, 68, 24, "Humidity");
|
||||
|
||||
switch (temperature_sensor_current_status) {
|
||||
case TSSInitializing:
|
||||
canvas_draw_str(canvas, 2, 30, "Initializing..");
|
||||
break;
|
||||
case TSSNoSensor:
|
||||
canvas_draw_str(canvas, 2, 30, "No sensor found!");
|
||||
break;
|
||||
case TSSPendingUpdate: {
|
||||
// Draw vertical lines
|
||||
canvas_draw_line(canvas, 61, 16, 61, 50);
|
||||
canvas_draw_line(canvas, 62, 16, 62, 50);
|
||||
|
||||
canvas_draw_str(canvas, 3, 24, "Temperature");
|
||||
canvas_draw_str(canvas, 68, 24, "Humidity");
|
||||
// Draw horizontal line
|
||||
canvas_draw_line(canvas, 2, 27, 122, 27);
|
||||
|
||||
// Draw vertical lines
|
||||
canvas_draw_line(canvas, 61, 16, 61, 50);
|
||||
canvas_draw_line(canvas, 62, 16, 62, 50);
|
||||
|
||||
// Draw horizontal line
|
||||
canvas_draw_line(canvas, 2, 27, 122, 27);
|
||||
|
||||
// Draw temperature and humidity values
|
||||
canvas_draw_str(canvas, 8, 38, ts_data_buffer_temperature_c);
|
||||
canvas_draw_str(canvas, 42, 38, "C");
|
||||
canvas_draw_str(canvas, 8, 48, ts_data_buffer_temperature_f);
|
||||
canvas_draw_str(canvas, 42, 48, "F");
|
||||
canvas_draw_str(canvas, 68, 38, ts_data_buffer_relative_humidity);
|
||||
canvas_draw_str(canvas, 100, 38, "%");
|
||||
canvas_draw_str(canvas, 68, 48, ts_data_buffer_absolute_humidity);
|
||||
canvas_draw_str(canvas, 100, 48, "g/m3");
|
||||
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// Draw temperature and humidity values
|
||||
canvas_draw_str(canvas, 8, 38, ts_data_buffer_temperature_c);
|
||||
canvas_draw_str(canvas, 42, 38, "C");
|
||||
canvas_draw_str(canvas, 8, 48, ts_data_buffer_temperature_f);
|
||||
canvas_draw_str(canvas, 42, 48, "F");
|
||||
canvas_draw_str(canvas, 68, 38, ts_data_buffer_relative_humidity);
|
||||
canvas_draw_str(canvas, 100, 38, "%");
|
||||
canvas_draw_str(canvas, 68, 48, ts_data_buffer_absolute_humidity);
|
||||
canvas_draw_str(canvas, 100, 48, "g/m3");
|
||||
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// <sumary>
|
||||
// Input callback
|
||||
// </sumary>
|
||||
static void temperature_sensor_input_callback(InputEvent* input_event, void* ctx) {
|
||||
furi_assert(ctx);
|
||||
FuriMessageQueue* event_queue = ctx;
|
||||
|
||||
furi_assert(ctx);
|
||||
FuriMessageQueue* event_queue = ctx;
|
||||
|
||||
TSEvent event = { .type = TSEventTypeInput, .input = *input_event };
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
|
||||
TSEvent event = {.type = TSEventTypeInput, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
// <sumary>
|
||||
// Timer callback
|
||||
// </sumary>
|
||||
static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
|
||||
furi_assert(event_queue);
|
||||
|
||||
TSEvent event = { .type = TSEventTypeTick };
|
||||
furi_message_queue_put(event_queue, &event, 0);
|
||||
|
||||
TSEvent event = {.type = TSEventTypeTick};
|
||||
furi_message_queue_put(event_queue, &event, 0);
|
||||
}
|
||||
|
||||
// <sumary>
|
||||
// App entry point
|
||||
// </sumary>
|
||||
int32_t temperature_sensor_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
UNUSED(p);
|
||||
// Declare our variables and assign variables a default value
|
||||
TSEvent tsEvent;
|
||||
bool sensorFound = false;
|
||||
double celsius, fahrenheit, rel_humidity, abs_humidity = TS_DEFAULT_VALUE;
|
||||
|
||||
// Declare our variables and assign variables a default value
|
||||
TSEvent tsEvent;
|
||||
bool sensorFound = false;
|
||||
double celsius, fahrenheit, rel_humidity, abs_humidity = TS_DEFAULT_VALUE;
|
||||
// Used for absolute humidity calculation
|
||||
double vapour_pressure = 0;
|
||||
|
||||
// Used for absolute humidity calculation
|
||||
double vapour_pressure = 0;
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent));
|
||||
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent));
|
||||
// Register callbacks
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL);
|
||||
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
|
||||
|
||||
// Register callbacks
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL);
|
||||
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
|
||||
// Create timer and register its callback
|
||||
FuriTimer* timer =
|
||||
furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
||||
|
||||
// Create timer and register its callback
|
||||
FuriTimer* timer = furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
||||
// Register viewport
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
// Register viewport
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
// Used to notify the user by blinking red (error) or blue (fetch successful)
|
||||
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
// Used to notify the user by blinking red (error) or blue (fetch successful)
|
||||
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||
while(1) {
|
||||
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
while (1) {
|
||||
// Handle events
|
||||
if(tsEvent.type == TSEventTypeInput) {
|
||||
// Exit on back key
|
||||
if(tsEvent.input.key ==
|
||||
InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort)
|
||||
break;
|
||||
|
||||
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
|
||||
} else if(tsEvent.type == TSEventTypeTick) {
|
||||
// Update sensor data
|
||||
// Fetch data and set the sensor current status accordingly
|
||||
sensorFound = temperature_sensor_fetch_data(&celsius, &rel_humidity);
|
||||
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
|
||||
|
||||
// Handle events
|
||||
if (tsEvent.type == TSEventTypeInput) {
|
||||
if(sensorFound) {
|
||||
// Blink blue
|
||||
notification_message(notifications, &sequence_blink_blue_100);
|
||||
|
||||
// Exit on back key
|
||||
if (tsEvent.input.key == InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort)
|
||||
break;
|
||||
if(celsius != TS_DEFAULT_VALUE && rel_humidity != TS_DEFAULT_VALUE) {
|
||||
// Convert celsius to fahrenheit
|
||||
fahrenheit = (celsius * 9 / 5) + 32;
|
||||
|
||||
}
|
||||
else if (tsEvent.type == TSEventTypeTick) {
|
||||
|
||||
// Update sensor data
|
||||
// Fetch data and set the sensor current status accordingly
|
||||
sensorFound = temperature_sensor_fetch_data(&celsius, &rel_humidity);
|
||||
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
|
||||
|
||||
if (sensorFound) {
|
||||
|
||||
// Blink blue
|
||||
notification_message(notifications, &sequence_blink_blue_100);
|
||||
|
||||
if (celsius != TS_DEFAULT_VALUE && rel_humidity != TS_DEFAULT_VALUE) {
|
||||
|
||||
// Convert celsius to fahrenheit
|
||||
fahrenheit = (celsius * 9 / 5) + 32;
|
||||
|
||||
// Calculate absolute humidity - For more info refer to https://github.com/Mywk/FlipperTemperatureSensor/issues/1
|
||||
// Calculate saturation vapour pressure first
|
||||
vapour_pressure = (double)6.11 * pow(10, (double)(((double)7.5 * celsius) / ((double)237.3 + celsius)));
|
||||
// Then the vapour pressure in Pa
|
||||
// Calculate absolute humidity - For more info refer to https://github.com/Mywk/FlipperTemperatureSensor/issues/1
|
||||
// Calculate saturation vapour pressure first
|
||||
vapour_pressure =
|
||||
(double)6.11 *
|
||||
pow(10, (double)(((double)7.5 * celsius) / ((double)237.3 + celsius)));
|
||||
// Then the vapour pressure in Pa
|
||||
vapour_pressure = vapour_pressure * rel_humidity;
|
||||
// Calculate absolute humidity
|
||||
abs_humidity = (double)2.16679 * (double)(vapour_pressure / ((double)273.15 + celsius));
|
||||
// Calculate absolute humidity
|
||||
abs_humidity =
|
||||
(double)2.16679 * (double)(vapour_pressure / ((double)273.15 + celsius));
|
||||
|
||||
// Fill our buffers here, not on the canvas draw callback
|
||||
snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius);
|
||||
snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit);
|
||||
snprintf(ts_data_buffer_relative_humidity, DATA_BUFFER_SIZE, "%.2f", rel_humidity);
|
||||
snprintf(ts_data_buffer_absolute_humidity, DATA_BUFFER_SIZE, "%.2f", abs_humidity);
|
||||
}
|
||||
// Fill our buffers here, not on the canvas draw callback
|
||||
snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius);
|
||||
snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit);
|
||||
snprintf(
|
||||
ts_data_buffer_relative_humidity, DATA_BUFFER_SIZE, "%.2f", rel_humidity);
|
||||
snprintf(
|
||||
ts_data_buffer_absolute_humidity, DATA_BUFFER_SIZE, "%.2f", abs_humidity);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Reset our variables to their default values
|
||||
celsius = fahrenheit = rel_humidity = abs_humidity = TS_DEFAULT_VALUE;
|
||||
|
||||
// Reset our variables to their default values
|
||||
celsius = fahrenheit = rel_humidity = abs_humidity = TS_DEFAULT_VALUE;
|
||||
// Blink red
|
||||
notification_message(notifications, &sequence_blink_red_100);
|
||||
}
|
||||
}
|
||||
|
||||
// Blink red
|
||||
notification_message(notifications, &sequence_blink_red_100);
|
||||
uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500);
|
||||
furi_delay_tick(wait_ticks);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// Dobby is freee (free our variables, Flipper will crash if we don't do this!)
|
||||
furi_timer_free(timer);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
|
||||
uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500);
|
||||
furi_delay_tick(wait_ticks);
|
||||
}
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
// Dobby is freee (free our variables, Flipper will crash if we don't do this!)
|
||||
furi_timer_free(timer);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user