Skip to content

Commit

Permalink
Upd Tanks
Browse files Browse the repository at this point in the history
  • Loading branch information
RogueMaster committed Jul 15, 2023
1 parent a20de9c commit b5a877a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 32 deletions.
1 change: 1 addition & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This software is for experimental purposes only and is not meant for any illegal

- Last Synced/Checked Unleashed, changes in [changelog](https://github.com/RogueMaster/flipperzero-firmware-wPlugins/blob/420/CHANGELOG.md) and in [commits](https://github.com/DarkFlippers/unleashed-firmware/commits/dev): `2023-07-14 16:51 EST`
- Last Synced/Checked OFW, changes in [commits](https://github.com/flipperdevices/flipperzero-firmware/commits/dev): `2023-07-14 16:51 EST`
- Updated: [Tanks (By Alexgr13)](https://github.com/alexgr13/flipperzero-firmware/tree/fork/dev/applications/tanks-game) (By Sil333033)

<a name="release">

Expand Down
64 changes: 64 additions & 0 deletions applications/external/tanksgame/helpers/radio_device_loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "radio_device_loader.h"

#include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
#include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>

static void radio_device_loader_power_on() {
uint8_t attempts = 0;
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
//CC1101 power-up time
furi_delay_ms(10);
}
}

static void radio_device_loader_power_off() {
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
}

bool radio_device_loader_is_connect_external(const char* name) {
bool is_connect = false;
bool is_otg_enabled = furi_hal_power_is_otg_enabled();

if(!is_otg_enabled) {
radio_device_loader_power_on();
}

const SubGhzDevice* device = subghz_devices_get_by_name(name);
if(device) {
is_connect = subghz_devices_is_connect(device);
}

if(!is_otg_enabled) {
radio_device_loader_power_off();
}
return is_connect;
}

const SubGhzDevice* radio_device_loader_set(
const SubGhzDevice* current_radio_device,
SubGhzRadioDeviceType radio_device_type) {
const SubGhzDevice* radio_device;

if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
radio_device_loader_is_connect_external(SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
radio_device_loader_power_on();
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
subghz_devices_begin(radio_device);
} else if(current_radio_device == NULL) {
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
} else {
radio_device_loader_end(current_radio_device);
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
}

return radio_device;
}

void radio_device_loader_end(const SubGhzDevice* radio_device) {
furi_assert(radio_device);
radio_device_loader_power_off();
if(radio_device != subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME)) {
subghz_devices_end(radio_device);
}
}
15 changes: 15 additions & 0 deletions applications/external/tanksgame/helpers/radio_device_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <lib/subghz/devices/devices.h>

/** SubGhzRadioDeviceType */
typedef enum {
SubGhzRadioDeviceTypeInternal,
SubGhzRadioDeviceTypeExternalCC1101,
} SubGhzRadioDeviceType;

const SubGhzDevice* radio_device_loader_set(
const SubGhzDevice* current_radio_device,
SubGhzRadioDeviceType radio_device_type);

void radio_device_loader_end(const SubGhzDevice* radio_device);
79 changes: 47 additions & 32 deletions applications/external/tanksgame/tanks_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <lib/subghz/protocols/princeton.h>
#include <lib/subghz/subghz_tx_rx_worker.h>
#include "tanks_icons.h"
#include "helpers/radio_device_loader.h"

#include "constants.h"

Expand Down Expand Up @@ -1215,7 +1216,15 @@ int32_t tanks_game_app(void* p) {
size_t message_max_len = 180;
uint8_t incomingMessage[180] = {0};
SubGhzTxRxWorker* subghz_txrx = subghz_tx_rx_worker_alloc();
// subghz_tx_rx_worker_start(subghz_txrx, frequency);

subghz_devices_init();
const SubGhzDevice* subghz_device =
radio_device_loader_set(NULL, SubGhzRadioDeviceTypeExternalCC1101);

subghz_devices_reset(subghz_device);
subghz_devices_load_preset(subghz_device, FuriHalSubGhzPresetOok650Async, NULL);

subghz_tx_rx_worker_start(subghz_txrx, subghz_device, frequency);
furi_hal_power_suppress_charge_enter();

for(bool processing = true; processing;) {
Expand Down Expand Up @@ -1277,39 +1286,43 @@ int32_t tanks_game_app(void* p) {
}
break;
case InputKeyRight:
if(tanks_state->state == GameStateCooperativeClient) {
FuriString* goesRight = furi_string_alloc();
char arr[2];
arr[0] = GoesRight;
arr[1] = 0;
furi_string_set(goesRight, (char*)&arr);

subghz_tx_rx_worker_write(
subghz_txrx,
(uint8_t*)furi_string_get_cstr(goesRight),
strlen(furi_string_get_cstr(goesRight)));
furi_string_free(goesRight);
} else {
tanks_state->p1->moving = true;
tanks_state->p1->direction = DirectionRight;
if(!(tanks_state->state == GameStateMenu)) {
if(tanks_state->state == GameStateCooperativeClient) {
FuriString* goesRight = furi_string_alloc();
char arr[2];
arr[0] = GoesRight;
arr[1] = 0;
furi_string_set(goesRight, (char*)&arr);

subghz_tx_rx_worker_write(
subghz_txrx,
(uint8_t*)furi_string_get_cstr(goesRight),
strlen(furi_string_get_cstr(goesRight)));
furi_string_free(goesRight);
} else {
tanks_state->p1->moving = true;
tanks_state->p1->direction = DirectionRight;
}
}
break;
case InputKeyLeft:
if(tanks_state->state == GameStateCooperativeClient) {
FuriString* goesLeft = furi_string_alloc();
char arr[2];
arr[0] = GoesLeft;
arr[1] = 0;
furi_string_set(goesLeft, (char*)&arr);

subghz_tx_rx_worker_write(
subghz_txrx,
(uint8_t*)furi_string_get_cstr(goesLeft),
strlen(furi_string_get_cstr(goesLeft)));
furi_string_free(goesLeft);
} else {
tanks_state->p1->moving = true;
tanks_state->p1->direction = DirectionLeft;
if(!(tanks_state->state == GameStateMenu)) {
if(tanks_state->state == GameStateCooperativeClient) {
FuriString* goesLeft = furi_string_alloc();
char arr[2];
arr[0] = GoesLeft;
arr[1] = 0;
furi_string_set(goesLeft, (char*)&arr);

subghz_tx_rx_worker_write(
subghz_txrx,
(uint8_t*)furi_string_get_cstr(goesLeft),
strlen(furi_string_get_cstr(goesLeft)));
furi_string_free(goesLeft);
} else {
tanks_state->p1->moving = true;
tanks_state->p1->direction = DirectionLeft;
}
}
break;
case InputKeyOk:
Expand Down Expand Up @@ -1438,6 +1451,8 @@ int32_t tanks_game_app(void* p) {
subghz_tx_rx_worker_free(subghz_txrx);
}

subghz_devices_deinit();

furi_timer_free(timer);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
Expand All @@ -1457,4 +1472,4 @@ int32_t tanks_game_app(void* p) {
free(tanks_state);

return 0;
}
}

0 comments on commit b5a877a

Please sign in to comment.