From 564e99a78dee339a68bf8022d12357959e6b6cab Mon Sep 17 00:00:00 2001 From: cfif Date: Mon, 2 Jun 2025 13:26:41 +0300 Subject: [PATCH] Init --- ControllableSwitch.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ ControllableSwitch.h | 37 ++++++++++++++++++++++++++++ modular.json | 12 ++++++++++ 3 files changed, 106 insertions(+) create mode 100644 ControllableSwitch.c create mode 100644 ControllableSwitch.h create mode 100644 modular.json diff --git a/ControllableSwitch.c b/ControllableSwitch.c new file mode 100644 index 0000000..857a50b --- /dev/null +++ b/ControllableSwitch.c @@ -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); +} diff --git a/ControllableSwitch.h b/ControllableSwitch.h new file mode 100644 index 0000000..8331fde --- /dev/null +++ b/ControllableSwitch.h @@ -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 diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..0f166c1 --- /dev/null +++ b/modular.json @@ -0,0 +1,12 @@ +{ + "dep": [ + ], + "cmake": { + "inc_dirs": [ + "./" + ], + "srcs": [ + "*.c" + ] + } +} \ No newline at end of file