This commit is contained in:
cfif 2025-06-02 13:26:41 +03:00
commit 564e99a78d
3 changed files with 106 additions and 0 deletions

57
ControllableSwitch.c Normal file
View File

@ -0,0 +1,57 @@
//
// Created by xemon on 15.12.22.
//
#include "ControllableSwitch.h"
#include "stdint.h"
void ControllableSwitch_Init(
tControllableSwitch *env,
void *invalidatorEnv,
void *invalidator
) {
env->env = invalidatorEnv;
env->invalidator = invalidator;
env->hasCurrentState = false;
env->hasHardTarget = false;
env->hasSoftTarget = false;
}
#define ControllableSwitch_Invalidate(STATE, TARGET) (STATE)->invalidator((STATE)->env,(STATE)->currentState,TARGET)
uint32_t ControllableSwitch_Check(tControllableSwitch *env) {
if (env->hasHardTarget) {
if ((env->hardTarget != env->currentState) || !env->hasCurrentState) {
return ControllableSwitch_Invalidate(env, env->hardTarget);
}
} else if (env->hasSoftTarget) {
if ((env->softTarget != env->currentState) || !env->hasCurrentState) {
return ControllableSwitch_Invalidate(env, env->softTarget);
}
}
return env->currentState;
}
void ControllableSwitch_Set(tControllableSwitch *env, bool value) {
env->softTarget = value;
env->hasSoftTarget = true;
env->currentState = ControllableSwitch_Check(env);
env->hasCurrentState = true;
}
void ControllableSwitch_SetHard(tControllableSwitch *env, int32_t value) {
env->hardTarget = value;
env->hasHardTarget = true;
env->currentState = ControllableSwitch_Check(env);
env->hasCurrentState = true;
}
void ControllableSwitch_ClearHard(tControllableSwitch *env) {
env->hasHardTarget = false;
env->currentState = ControllableSwitch_Check(env);
}

37
ControllableSwitch.h Normal file
View File

@ -0,0 +1,37 @@
//
// Created by xemon on 15.12.22.
//
#ifndef UVEOS_ON_NATION_CONTROLLABLESWITCH_H
#define UVEOS_ON_NATION_CONTROLLABLESWITCH_H
#include "stdbool.h"
#include "stdint.h"
typedef struct {
bool hasCurrentState;
uint32_t currentState;
bool hasSoftTarget;
uint32_t softTarget;
bool hasHardTarget;
uint32_t hardTarget;
void *env;
uint32_t (*invalidator)(void *env, uint32_t currentState, bool targetState);
} tControllableSwitch;
void ControllableSwitch_Init(
tControllableSwitch *env,
void *invalidatorEnv,
void *invalidator
);
void ControllableSwitch_Set(tControllableSwitch *env, bool value);
void ControllableSwitch_SetHard(tControllableSwitch *env, int32_t value);
void ControllableSwitch_ClearHard(tControllableSwitch *env);
#endif //UVEOS_ON_NATION_CONTROLLABLESWITCH_H

12
modular.json Normal file
View File

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