mcu_hi3321_watch/tjd/ui/app/motion_sensing_game/TjdUiAppMSGamePresenter.cpp
2025-05-26 20:15:20 +08:00

142 lines
4.7 KiB
C++

#include "TjdUiAppMSGamePresenter.h"
#include "NativeAbility.h"
#include "TjdUiAppIds.h"
#include "TjdUiAppMSGameModel.h"
#include "TjdUiAppMSGameView.h"
#include "TjdUiSettingCenter.h"
#include "dock/input_device.h"
#include "graphic_service.h"
#include "sys_config.h"
#include "TjdUiRegisterManager.h"
#include "bts_br_gap.h"
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
namespace TJD {
TJD_REGIST_NATIVE_MENU(TJD_APP_VIEW_MS_GAME, TjdUiAppMSGameView, TjdUiAppMSGamePresenter, IMG_MENU_LIST_MENU_SOMATOSENSORY, STR_ID_31);
static TjdUiAppMSGamePresenter *g_msGamePresenter = nullptr;
TjdUiAppMSGamePresenter::TjdUiAppMSGamePresenter() { g_msGamePresenter = this; }
TjdUiAppMSGamePresenter::~TjdUiAppMSGamePresenter() { g_msGamePresenter = nullptr; }
TjdUiAppMSGamePresenter *TjdUiAppMSGamePresenter::GetInstance(void) { return g_msGamePresenter; }
#define MAXFLOWLIMIT 10240
#define MINFLOWLIMIT 3072
#define INTERVAL 1024
static void ms_game_task(void *argument)
{
float acc[3] = {0};
game_data_struct output;
TjdUiAppMSGameModel &model = TjdUiAppMSGameModel::GetInstance();
errcode_t ret = gap_bt_set_flow_limit(MINFLOWLIMIT);
static_print_info("star flow_limit:%d", ret);
while (1) {
lis2doc_read_data_polling_fun();
GsensorData &data = model.GetGsensorData();
acc[0] = (data.x/4096.0)*0.98;
acc[1] = (data.y/4096.0)*0.98;
acc[2] = (data.z/4096.0)*0.98;
IMU_10M_DataHandle(NULL,acc,&output,0.8);
// static_print_info("Primitive x:%d, y:%d, z:%d", data.x, data.y, data.z);
// static_print_info("transform x:%f, y:%f, z:%f", acc[0], acc[1], acc[2]);
// static_print_info("X_Move_Distance:%d, Y_Move_Distance:%d, Speed_Throw:%d", output.X_Move_Distance, output.Y_Move_Distance, output.Speed_Throw);
model.UploadGsensorData(output);
osDelay(50);
}
}
void TjdUiAppMSGamePresenter::OnStart()
{
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(this, KeyModelType::APP_KEY_TYPE);
TjdUiAppMSGameModel &model = TjdUiAppMSGameModel::GetInstance();
TjdUiAppMSGameView *msGmaeView = TjdUiAppMSGameView::GetInstance();
if (msGmaeView == nullptr) {
return;
}
bool isConnect = TjdUiSettingCenter::GetInstance().IsConnectApp();
if (isConnect == false) {
msGmaeView->ShowView(MSGameViewIndex::MS_GAME_DISCONNECT);
return;
}
msGmaeView->ShowView(MSGameViewIndex::MS_GAME_MAIN);
osThreadAttr_t task_attr = {"mg_game_task", 0, NULL, 0, NULL, 0xF00, (osPriority_t)17, 0, 0};
task_attr.stack_mem = memalign(16, task_attr.stack_size); // add
ms_game_task_id = osThreadNew(ms_game_task, NULL, &task_attr);
if (ms_game_task_id == NULL) {
static_print_error("create ms_game task failed");
return;
}
model.EnterMsGameEvent();
}
void TjdUiAppMSGamePresenter::OnStop()
{
TjdUiCommonOnKeyListener::GetInstance()->ClearOnKeyActListener(KeyModelType::APP_KEY_TYPE);
TjdUiAppMSGameModel &model = TjdUiAppMSGameModel::GetInstance();
errcode_t ret = gap_bt_set_flow_limit(MAXFLOWLIMIT);
static_print_info("end flow_limit:%d", ret);
osThreadTerminate(ms_game_task_id);
model.ExitMsGameEvent();
}
bool TjdUiAppMSGamePresenter::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
{
if(!TjdUiCommonOnKeyListener::CheckIsExitEvent(event)) {
return true;
}
ViewExitEvent(false);
return false;
}
static bool DataIsChanged(GsensorData &data)
{
// TODO: 某个轴的数据大于某个阈值时,上传数据
static GsensorData lastData = {0};
const int16_t threshold = 100;
if (abs(data.x - lastData.x) > threshold || abs(data.y - lastData.y) > threshold ||
abs(data.z - lastData.z) > threshold) {
lastData = data;
return true;
}
return false;
}
void TjdUiAppMSGamePresenter::ViewExitEvent(bool isSwipe)
{
TjdUiAppMSGameView *gameView = TjdUiAppMSGameView::GetInstance();
if (gameView == nullptr) {
return;
}
switch (TjdUiAppMSGameView::currentViewIndex_) {
case MSGameViewIndex::MS_GAME_MAIN:
case MSGameViewIndex::MS_GAME_DISCONNECT:
OHOS::NativeAbility::GetInstance().ChangePreSlice();
break;
default:
break;
}
}
} // namespace TJD