This commit is contained in:
cfif 2025-06-02 13:26:40 +03:00
commit 37c9a0b908
3 changed files with 414 additions and 0 deletions

306
UserInput.c Normal file
View File

@ -0,0 +1,306 @@
//
// Created by ilya on 04.04.24.
//
#include "UserInput.h"
#include "ext_telematica.h"
#include <SystemDelayInterface.h>
#include <CmsisRtosThreadUtils.h>
#pragma GCC push_options
#pragma GCC optimize ("O0")
bool UserInputButtonWatcher_CustomEnv(tUserInputButtonWatcherCustomAdcEnv *env){
uint16_t volts = AdcGet(env->adcIo ,40);
if( (env->lowLevel<volts) && (volts < env->HightLevel) ){
return 1;
} else {
return 0;
}
}
bool currentValue = 0;
bool prevValue = 0;
bool UserInputGetAntiRattlePin(tGpioPin *pin){
prevValue = false;
currentValue = false;
prevValue = GpioPinGet(pin);
if (prevValue == true) {
uint32_t tm = SystemGetMs() + 100;
while (tm > SystemGetMs()){
currentValue = GpioPinGet(pin);
if (prevValue == currentValue) {
currentValue = true;
} else {
currentValue = false;
}
}
}
return currentValue;
}
uint32_t tm = 0;
bool UserInputButtonWatcher_CustomTestModeEnv(tUserInputButtonWatcherCustomEnvTestMode *env){
#if (AURUS_CAR_UI == 1)
bool res = false;
if(UserInputGetAntiRattlePin(&env->userInputPins->emergency)) {
res = true;
env->buttonWaitPresTime = SystemGetMs() + env->waitPresTime;
env->flIsPreset = true;
} else {
res = false;
}
if( res == true ){
env->presCounter++;
} else {
tm = SystemGetMs();
if((tm > env->buttonWaitPresTime) && (env->flIsPreset == true)){
env->flIsPreset = false;
env->presCounter = 0;
env->buttonWaitPresTime = 0;
return false;
}
}
if(env->presCounter == env->presLimit){
env->flIsPreset = false;
env->presCounter = 0;
env->buttonWaitPresTime = 0;
return true;
}
#endif
#if (AURUS_CAR_UI == 0)
bool res = false;
if(
( GpioPinGet(&env->userInputPins->additional) ) &&
( GpioPinGet(&env->userInputPins->emergency ) )
){
res = true;
} else {
res = false;
}
///===============================================================================================
if( (res) && (env->flIsPreset == false) ){
env->buttonThisPresTime = SystemGetMs() + env->buttonWaitPresTime;
env->buttonThisPresTime = env->buttonThisPresTime;
env->flIsPreset = true;
}
if(
(!res) &&
(env->buttonThisPresTime > SystemGetMs())
){
if(env->flIsPreset == true){
env->flIsPreset = false;
}
}
if(
(res) &&
(env->buttonThisPresTime < SystemGetMs()) &&
(env->flIsPreset = true)
){
env->flIsPreset = false;
return true;
} else {
return false;
}
#endif
}
bool UserInputButtonWatcher_CustomAddEnv(tUserInputButtonWatcherCustomSampleMode *env){
return UserInputGetAntiRattlePin(&env->userInputPins->additional);
}
bool UserInputButtonWatcher_CustomEmerEnv(tUserInputButtonWatcherCustomSampleMode *env){
return UserInputGetAntiRattlePin(&env->userInputPins->emergency);
}
bool res = false;
bool UserInputButtonWatcher_CustomEmerModeEnv(tUserInputButtonWatcherCustomEnvEmerMode *env){
#if (AURUS_CAR_UI == 1)
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if( (res) && (env->flIsPreset == false) ){
env->buttonThisPresTime = SystemGetMs() + env->buttonWaitPresTime;
env->buttonThisPresTime = env->buttonThisPresTime;
env->flIsPreset = true;
}
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if(
(!res) &&
(env->buttonThisPresTime > SystemGetMs())
){
if(env->flIsPreset == true){
env->flIsPreset = false;
}
}
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if(
(res) &&
(env->buttonThisPresTime < SystemGetMs()) &&
(env->flIsPreset = true)
){
env->flIsPreset = false;
return true;
} else {
return false;
}
#endif
#if (AURUS_CAR_UI == 0)
res = UserInputGetAntiRattlePin(&env->userInputPins->additional);
if(res) {
env->flIsPreset = false;
return false;
}
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if( (res) && (env->flIsPreset == false) ){
env->buttonThisPresTime = SystemGetMs() + env->buttonWaitPresTime;
env->buttonThisPresTime = env->buttonThisPresTime;
env->flIsPreset = true;
}
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if(
(!res) &&
(env->buttonThisPresTime > SystemGetMs())
){
if(env->flIsPreset == true){
env->flIsPreset = false;
}
}
res = UserInputGetAntiRattlePin(&env->userInputPins->emergency);
if(
(res) &&
(env->buttonThisPresTime < SystemGetMs()) &&
(env->flIsPreset = true)
){
env->flIsPreset = false;
return true;
} else {
return false;
}
#endif
}
void UserInput_Init(
tUserInput *env,
tUserInputPins *pins,
const uint16_t *buttonPresTimeEmerMode,
const uint16_t *buttonPresTimeTestMode
) {
UserInputButtonWatcher_InitStatic(&env->watcher, env->mem.watches, 128);
env->CustomEnvTestMode = (tUserInputButtonWatcherCustomEnvTestMode) {
.userInputPins = pins,
.buttonWaitPresTime = *buttonPresTimeTestMode,
.buttonThisPresTime = 0,
.flIsPreset = false,
.waitPresTime = 6000,
.presCounter = 0,
.presLimit = 5
};
env->CustomEnvEmerMode.buttonWaitPresTime = env->CustomEnvEmerMode.buttonWaitPresTime-1000;
UserInputButtonWatcher_AddCustom(&env->watcher, &env->CustomEnvTestMode, UserInputButtonWatcher_CustomTestModeEnv,
UI_CUSTOM_BUTTON_TEST_MODE);
env->CustomEnvEmerMode = (tUserInputButtonWatcherCustomEnvEmerMode) {
.userInputPins = pins,
.buttonWaitPresTime = *buttonPresTimeEmerMode,
.buttonThisPresTime = 0,
.flIsPreset = false,
};
env->CustomEnvEmerMode.buttonWaitPresTime = env->CustomEnvEmerMode.buttonWaitPresTime-1000;
UserInputButtonWatcher_AddCustom(&env->watcher, &env->CustomEnvEmerMode, UserInputButtonWatcher_CustomEmerModeEnv,
UI_CUSTOM_BUTTON_EMERGENCY);
env->CustomEnvSampleAdd = (tUserInputButtonWatcherCustomSampleMode) {
.userInputPins = pins,
};
UserInputButtonWatcher_AddCustom(&env->watcher, &env->CustomEnvSampleAdd, UserInputButtonWatcher_CustomAddEnv,
UI_VIRT_BUTTON_ADDITIONAL);
env->CustomEnvSampleEmer = (tUserInputButtonWatcherCustomSampleMode) {
.userInputPins = pins,
};
UserInputButtonWatcher_AddCustom(&env->watcher, &env->CustomEnvSampleEmer, UserInputButtonWatcher_CustomEmerEnv,
UI_VIRT_BUTTON_EMERGENCY);
env->buttonsInterface = UserInputButtonWatcher_GetInterface(&env->watcher);
InitThreadAtrStatic(&env->thread.attr, "UserInput", env->thread.controlBlock, env->thread.stack, osPriorityNormal);
env->thread.id = 0;
}
bool state = false;
uint32_t timing = 0;
bool isPressedEmergencyButton = false;
bool isPressedTestButton = false;
static _Noreturn void UserInput_Thread(tUserInput *env) {
for (;;) {
UserInputButtonWatcher_Check(&env->watcher);
//начало-----------------Обновление счетчиков телематики (для отладки)--------------------------------------
isPressedEmergencyButton = UserInputGetAntiRattlePin(&env->CustomEnvSampleEmer.userInputPins->emergency);
if (isPressedEmergencyButton) {
if (EXT_ENV_ADR_TELE.META_EXT_ENV_TELE) {
EXT_ENV_ADR_TELE.tele_func(env, TELE_MODULE_COUNTER_1);
}
if(state == false){
timing = SystemGetMs();
state = true;
}
} else{
if(state == true){
timing = SystemGetMs() - timing;
if (EXT_ENV_ADR_TELE.META_EXT_ENV_TELE) {
EXT_ENV_ADR_TELE.tele_func(&timing, TELE_MODULE_COUNTER_5);
}
timing = 0;
state = false;
}
}
//конец------------------Обновление счетчиков телематики (для отладки)--------------------------------------
//начало-----------------Обновление счетчиков телематики (для отладки)--------------------------------------
// bool isPressedTestButton = GpioPinGet(&env->CustomEnvSampleEmer.userInputPins->additional);
isPressedTestButton = UserInputGetAntiRattlePin(&env->CustomEnvSampleEmer.userInputPins->additional);
if (isPressedTestButton) {
if (EXT_ENV_ADR_TELE.META_EXT_ENV_TELE) {
EXT_ENV_ADR_TELE.tele_func(env, TELE_MODULE_COUNTER_3);
}
}
//конец------------------Обновление счетчиков телематики (для отладки)--------------------------------------
SystemDelayMs(100);
}
}
void UserInput_StartThread(tUserInput *env) {
if (!env->thread.id) {
env->thread.id = osThreadNew((osThreadFunc_t) (UserInput_Thread), (void *) (env), &env->thread.attr);
} else {
osThreadResume(env->thread.id);
}
}
void UserInput_Suspend(tUserInput *env) {
if (env->thread.id) {
osThreadSuspend(env->thread.id);
}
}
#pragma GCC pop_options

95
UserInput.h Normal file
View File

@ -0,0 +1,95 @@
//
// Created by ilya on 04.04.24.
//
#ifndef SMART_COMPONENTS_USERINPUT_H
#define SMART_COMPONENTS_USERINPUT_H
#include "UserInputButtonWatcher.h"
//#define AURUS_CAR_UI 1
//Кнопки БИП
typedef struct {
tGpioPin additional;
tGpioPin emergency;
} tUserInputPins;
/**
* Минимальное время нажатия для перехода
* в сервисный режим
*/
#define SERVICE_MODE_MIN 7000
/**
* Максимальное время нажатия для перехода
* в сервисный режим
*/
#define SERVICE_MODE_MAX 0xFFFF
typedef enum {
// UI_BUTTON_EMERGENCY,
// UI_BUTTON_ADDITIONAL,
UI_VIRT_BUTTON_EMERGENCY,
UI_VIRT_BUTTON_ADDITIONAL,
UI_CUSTOM_BUTTON_TEST_MODE,
UI_CUSTOM_BUTTON_EMERGENCY,
} eUserInputButtonIds;
typedef struct {
tAdcIO *adcIo;
uint16_t lowLevel;
uint16_t HightLevel;
} tUserInputButtonWatcherCustomAdcEnv;
typedef struct {
tUserInputPins *userInputPins;
uint64_t buttonThisPresTime;
uint64_t buttonWaitPresTime;
uint64_t waitPresTime;
uint16_t presCounter;
uint16_t presLimit;
bool flIsPreset;
} tUserInputButtonWatcherCustomEnvTestMode;
typedef struct {
tUserInputPins *userInputPins;
} tUserInputButtonWatcherCustomSampleMode;
typedef struct {
tUserInputPins *userInputPins;
uint64_t buttonThisPresTime;
uint16_t buttonWaitPresTime;
bool flIsPreset;
} tUserInputButtonWatcherCustomEnvEmerMode;
typedef struct {
struct {
tUserInputButtonWatch watches[8];
} mem;
tUserInputButtonWatcher watcher;
tUserButtonsInterface buttonsInterface;
struct {
osThreadId_t id;
uint32_t stack[512];
StaticTask_t controlBlock;
osThreadAttr_t attr;
} thread;
tUserInputButtonWatcherCustomSampleMode CustomEnvSampleEmer;
tUserInputButtonWatcherCustomSampleMode CustomEnvSampleAdd;
tUserInputButtonWatcherCustomEnvTestMode CustomEnvTestMode;
tUserInputButtonWatcherCustomEnvEmerMode CustomEnvEmerMode;
} tUserInput;
void UserInput_Init(
tUserInput *env,
tUserInputPins *pins,
const uint16_t *buttonPresTimeEmerMode,
const uint16_t *buttonPresTimeTestMode
);
void UserInput_StartThread(tUserInput *env);
#endif //SMART_COMPONENTS_USERINPUT_H

13
modular.json Normal file
View File

@ -0,0 +1,13 @@
{
"dep": [
],
"cmake": {
"inc_dirs": [
"./"
],
"srcs": [
"./**.c"
]
}
}