change VARH system/normal set func, del notify

This commit is contained in:
2021-12-17 16:58:46 +01:00
parent d85ece47b7
commit 0a2750f3b6
3 changed files with 51 additions and 156 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "Disabled"
}

View File

@ -23,8 +23,6 @@
// //
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: INCLUDES // Section: INCLUDES
// Description: List of required include files. // Description: List of required include files.
@ -43,42 +41,27 @@
// Description: Definition of local constants (visible by this module only). // Description: Definition of local constants (visible by this module only).
//================================================================================================= //=================================================================================================
// define the number of notifications according the need
#define NUMBER_OF_NOTIFICATIONS 2
//================================================================================================= //=================================================================================================
// Section: MACROS // Section: MACROS
// Description: Definition of local macros (visible by this module only). // Description: Definition of local macros (visible by this module only).
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: ENUMERATIONS // Section: ENUMERATIONS
// Description: Definition of local enumerations (visible by this module only). // Description: Definition of local enumerations (visible by this module only).
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: STRUCTURES // Section: STRUCTURES
// Description: Definition of local Structures (visible by this module only). // Description: Definition of local Structures (visible by this module only).
//================================================================================================= //=================================================================================================
typedef struct
{
VARH_pfnNotification pfnCallback;
PVOID pvArgument;
} StNotification;
//================================================================================================= //=================================================================================================
// Section: LOCAL VARIABLES // Section: LOCAL VARIABLES
// Description: Definition of local variables (visible by this module only). // Description: Definition of local variables (visible by this module only).
//================================================================================================= //=================================================================================================
LOCAL VARH_StVar m_auVariable[VARH_eNumberOfVariables]; LOCAL VARH_StVar m_auVariable[VARH_eNumberOfVariables];
LOCAL StNotification m_astNotifications[VARH_eNumberOfVariables][VARH_eNumberOfNotificationTypes][NUMBER_OF_NOTIFICATIONS] = { 0 };
LOCAL osMutexId_t m_pstMutexID = NULL; LOCAL osMutexId_t m_pstMutexID = NULL;
@ -87,14 +70,13 @@ LOCAL osMutexId_t m_pstMutexID = NULL;
// Description: Definition of local constants (visible by this module only). // Description: Definition of local constants (visible by this module only).
//================================================================================================= //=================================================================================================
LOCAL CONST VARH_StVarInfo m_astVarInfo[VARH_eNumberOfVariables] = LOCAL CONST VARH_StVarInfo m_astVarInfo[VARH_eNumberOfVariables] =
{ {
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-60.0f, (VARH_UVariable)100.0f }, // VARH_eTemp_H, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-60.0f, (VARH_UVariable)100.0f }, // VARH_eTemp_H,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-60.0f, (VARH_UVariable)100.0f }, // VARH_eTemp_C, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-60.0f, (VARH_UVariable)100.0f }, // VARH_eTemp_C,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-50.0f, (VARH_UVariable)70.0f }, // VARH_eTemp_Diff, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-50.0f, (VARH_UVariable)70.0f }, // VARH_eTemp_Diff,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-50.0f, (VARH_UVariable)70.0f }, // VARH_ePeltier_U, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-5.0f, (VARH_UVariable)14.0f }, // VARH_ePeltier_U,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-10.0f, (VARH_UVariable)10.0f }, // VARH_ePeltier_I, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-10.0f, (VARH_UVariable)10.0f }, // VARH_ePeltier_I,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)0.0f, (VARH_UVariable)5.0f }, // VARH_ePeltier_R, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)0.0f, (VARH_UVariable)5.0f }, // VARH_ePeltier_R,
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-50.0f, (VARH_UVariable)150.0f }, // VARH_ePeltier_P, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)-50.0f, (VARH_UVariable)150.0f }, // VARH_ePeltier_P,
@ -104,7 +86,6 @@ LOCAL CONST VARH_StVarInfo m_astVarInfo[VARH_eNumberOfVariables] =
{ VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)0.0f, (VARH_UVariable)150.0f }, // VARH_eSupply_P, { VARH_FLAG_READONLY | VARH_FLAG_FLOAT, (VARH_UVariable)0.0f, (VARH_UVariable)0.0f, (VARH_UVariable)150.0f }, // VARH_eSupply_P,
}; };
LOCAL CONST osMutexAttr_t m_stMutexAttr = LOCAL CONST osMutexAttr_t m_stMutexAttr =
{ {
"VARH_Mutex", // human readable mutex name "VARH_Mutex", // human readable mutex name
@ -118,11 +99,6 @@ LOCAL CONST osMutexAttr_t m_stMutexAttr =
// Description: Definition of local functions (visible by this module only). // Description: Definition of local functions (visible by this module only).
//================================================================================================= //=================================================================================================
// notification functions
PRIVATE VOID vCallNotifications( U8 u8Variable, VARH_UVariable uOldData, VARH_UVariable uNewData );
PRIVATE BOOL boNewValue( VARH_UVariable uOldData, VARH_UVariable uNewData );
// check functions // check functions
PRIVATE BOOL boCheckRange( U8 u8Variable, VARH_UVariable uNewData ); PRIVATE BOOL boCheckRange( U8 u8Variable, VARH_UVariable uNewData );
@ -136,15 +112,11 @@ PRIVATE BOOL boFloatVarCheck( U8 u8Variable, FLOAT flNewValue );
// Description: Definition of external (global) functions. // Description: Definition of external (global) functions.
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: EXTERNAL VARIABLES // Section: EXTERNAL VARIABLES
// Description: Definition of external (global) variables. // Description: Definition of external (global) variables.
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: GLOBAL FUNCTIONS // Section: GLOBAL FUNCTIONS
// Description: Definition (implementation) of global functions. // Description: Definition (implementation) of global functions.
@ -161,84 +133,51 @@ BOOL VARH_boInitializeModule( VOID )
BOOL boOK = TRUE; BOOL boOK = TRUE;
boOK &= ((m_pstMutexID = osMutexNew( &m_stMutexAttr )) == NULL) ? FALSE : TRUE; boOK &= ((m_pstMutexID = osMutexNew( &m_stMutexAttr )) == NULL) ? FALSE : TRUE;
memset( &m_astNotifications, 0, sizeof(m_astNotifications) ); // reset the notifications
VARH_vSetAllVariablesToInitData(); VARH_vSetAllVariablesToInitData();
return( boOK ); return( boOK );
} }
//-------------------------------------------------------------------------------------------------
// Function: VARH_boRegisterNotification
// Description: Registers a notification to the specified variable
// Parameters: U8 u8Variable
// VARH_EnNotification enNotification
// VARH_pfnNotification pfnCallback
// PVOID pvCallbackArgument
// Returns: Boolean TRUE if register of Callback was successful, otherwise FALSE
//-------------------------------------------------------------------------------------------------
BOOL VARH_boRegisterNotification( U8 u8Variable, VARH_EnNotification enNotification, VARH_pfnNotification pfnCallback, PVOID pvCallbackArgument )
{
// check parameters
if( enNotification >= VARH_eNumberOfNotificationTypes || pfnCallback == NULL)
{
return( FALSE );
}
for( U8 u8Notification = 0; u8Notification < NUMBER_OF_NOTIFICATIONS; u8Notification++ )
{
if( m_astNotifications[u8Variable][enNotification][u8Notification].pfnCallback == NULL )
{
m_astNotifications[u8Variable][enNotification][u8Notification].pfnCallback = pfnCallback;
m_astNotifications[u8Variable][enNotification][u8Notification].pvArgument = pvCallbackArgument;
return( TRUE );
}
}
return( FALSE ); // registration not successful
}
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
// Function: VARH_vSetVariableData // Function: VARH_vSetVariableData
// Description: Sets the Variable Data // Description: Sets the Variable Data and checks the parameters (Readonly and Min/Max)
// Parameters: U8 u8Variable // Parameters: U8 u8Variable
// VARH_UVariable uData // VARH_UVariable uData
// Returns: None // Returns: None
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
VOID VARH_vSetVariableData( U8 u8Variable, VARH_UVariable uData ) VOID VARH_vSetVariableData( U8 u8Variable, VARH_UVariable uData )
{ {
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
VARH_UVariable uOldValue = m_auVariable[u8Variable].uData; // remember old value
if( boCheckRange( u8Variable, uData ) ) { m_auVariable[u8Variable].uData = uData; } // store new value
vCallNotifications( u8Variable, uOldValue, m_auVariable[u8Variable].uData ); // call notifications
osMutexRelease( m_pstMutexID ); // release mutex
}
//-------------------------------------------------------------------------------------------------
// Function: VARH_vSetVariableDataFromMaster
// Description: Sets the Variable Data from Master Protocol.
// Only use this function from Master interpreter
// Parameters: U8 u8Variable
// VARH_UVariable uData
// Returns: None
//-------------------------------------------------------------------------------------------------
VOID VARH_vSetVariableDataFromMaster( U8 u8Variable, VARH_UVariable uData )
{
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
// check parameters // check parameters
if( m_astVarInfo[u8Variable].u8Flags & VARH_FLAG_READONLY ) if( m_astVarInfo[u8Variable].u8Flags & VARH_FLAG_READONLY )
{ {
return; return;
} }
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
VARH_UVariable uOldValue = m_auVariable[u8Variable].uData; // remember old value VARH_UVariable uOldValue = m_auVariable[u8Variable].uData; // remember old value
if( boCheckRange( u8Variable, uData ) ) { m_auVariable[u8Variable].uData = uData; } // store new value if( boCheckRange( u8Variable, uData ) ) { m_auVariable[u8Variable].uData = uData; } // store new value
vCallNotifications( u8Variable, uOldValue, m_auVariable[u8Variable].uData ); // call notifications osMutexRelease( m_pstMutexID ); // release mutex
osMutexRelease( m_pstMutexID ); // release mutex }
//-------------------------------------------------------------------------------------------------
// Function: VARH_vSetVariableDataFromSystem
// Description: Sets the Variable Data from System
// Parameters: U8 u8Variable
// VARH_UVariable uData
// Returns: None
//-------------------------------------------------------------------------------------------------
VOID VARH_vSetVariableDataFromSystem( U8 u8Variable, VARH_UVariable uData )
{
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
VARH_UVariable uOldValue = m_auVariable[u8Variable].uData; // remember old value
if( boCheckRange( u8Variable, uData ) )
{
m_auVariable[u8Variable].u8Flags |= VARH_FLAG_OUTOFRANGE; // check the value
}
m_auVariable[u8Variable].uData = uData; // store new value
osMutexRelease( m_pstMutexID ); // release mutex
} }
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
@ -255,6 +194,20 @@ VARH_UVariable VARH_uGetVariableData( U8 u8Variable )
return( uVar ); return( uVar );
} }
//-------------------------------------------------------------------------------------------------
// Function: VARH_uGetVariableFlags
// Description: Gets the Variable Flags
// Parameters: U8 u8Variable
// Returns: u8Flags
//-------------------------------------------------------------------------------------------------
U8 VARH_uGetVariableFlags( U8 u8Variable )
{
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
U8 u8Flags = m_auVariable[u8Variable].u8Flags;
osMutexRelease( m_pstMutexID ); // release mutex
return( u8Flags );
}
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
// Function: VARH_vSetVariableToInitData // Function: VARH_vSetVariableToInitData
// Description: Sets the variable to its initial data // Description: Sets the variable to its initial data
@ -264,7 +217,7 @@ VARH_UVariable VARH_uGetVariableData( U8 u8Variable )
VOID VARH_vSetVariableToInitData( U8 u8Variable ) VOID VARH_vSetVariableToInitData( U8 u8Variable )
{ {
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
VARH_vSetVariableData( u8Variable, m_astVarInfo[u8Variable].uInitData ); VARH_vSetVariableDataFromSystem( u8Variable, m_astVarInfo[u8Variable].uInitData );
osMutexRelease( m_pstMutexID ); // release mutex osMutexRelease( m_pstMutexID ); // release mutex
} }
@ -282,7 +235,6 @@ VOID VARH_vSetAllVariablesToInitData( VOID )
} }
} }
//================================================================================================= //=================================================================================================
// Section: LOCAL FUNCTIONS // Section: LOCAL FUNCTIONS
// Descriptionn: Definition (implementation) of local functions. // Descriptionn: Definition (implementation) of local functions.
@ -308,7 +260,7 @@ PRIVATE BOOL boBooleanVarCheck( U32 u32NewValue )
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
PRIVATE BOOL boU32VarCheck( U8 u8Variable, U32 u32NewValue ) PRIVATE BOOL boU32VarCheck( U8 u8Variable, U32 u32NewValue )
{ {
return( m_astVarInfo[u8Variable].uMinData.u32Val < u32NewValue < m_astVarInfo[u8Variable].uMaxData.u32Val ? TRUE : FALSE ); return( m_astVarInfo[u8Variable].uMinData.u32Val <= u32NewValue <= m_astVarInfo[u8Variable].uMaxData.u32Val ? TRUE : FALSE );
} }
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
@ -320,7 +272,7 @@ PRIVATE BOOL boU32VarCheck( U8 u8Variable, U32 u32NewValue )
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
PRIVATE BOOL boS32VarCheck( U8 u8Variable, S32 s32NewValue ) PRIVATE BOOL boS32VarCheck( U8 u8Variable, S32 s32NewValue )
{ {
return( m_astVarInfo[u8Variable].uMinData.s32Val < s32NewValue < m_astVarInfo[u8Variable].uMaxData.s32Val ? TRUE : FALSE ); return( m_astVarInfo[u8Variable].uMinData.s32Val <= s32NewValue <= m_astVarInfo[u8Variable].uMaxData.s32Val ? TRUE : FALSE );
} }
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
@ -332,37 +284,7 @@ PRIVATE BOOL boS32VarCheck( U8 u8Variable, S32 s32NewValue )
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
PRIVATE BOOL boFloatVarCheck( U8 u8Variable, FLOAT flNewValue ) PRIVATE BOOL boFloatVarCheck( U8 u8Variable, FLOAT flNewValue )
{ {
return( m_astVarInfo[u8Variable].uMinData.flVal < flNewValue < m_astVarInfo[u8Variable].uMaxData.flVal ? TRUE : FALSE ); return( m_astVarInfo[u8Variable].uMinData.flVal <= flNewValue <= m_astVarInfo[u8Variable].uMaxData.flVal ? TRUE : FALSE );
}
//-------------------------------------------------------------------------------------------------
// Function: vCallNotifications
// Description: Calls the notification callback functions
// Parameters: U8 u8Variable
// VARH_UVariable uOldData
// VARH_UVariable uNewData
// Returns: None
//-------------------------------------------------------------------------------------------------
PRIVATE VOID vCallNotifications( U8 u8Variable, VARH_UVariable uOldData, VARH_UVariable uNewData )
{
for( U8 u8Notification = 0; u8Notification < NUMBER_OF_NOTIFICATIONS; u8Notification++ )
{
if( m_astNotifications[u8Variable][VARH_eWrite][u8Notification].pfnCallback != NULL )
{
m_astNotifications[u8Variable][VARH_eWrite][u8Notification].pfnCallback( m_astNotifications[u8Variable][VARH_eWrite][u8Notification].pvArgument );
}
if( boNewValue( uOldData, uNewData ) )
{
if( m_astNotifications[u8Variable][VARH_eNewValue][u8Notification].pfnCallback != NULL )
{
m_astNotifications[u8Variable][VARH_eNewValue][u8Notification].pfnCallback( m_astNotifications[u8Variable][VARH_eNewValue][u8Notification].pvArgument );
}
}
}
} }
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
@ -390,15 +312,5 @@ PRIVATE BOOL boCheckRange( U8 u8Variable, VARH_UVariable uNewData )
} }
//-------------------------------------------------------------------------------------------------
// Function: boNewValue
// Description: returns True, if there is a new value for this variable
// Parameters: U8 u8Variable
// VARH_UVariable uOldData
// VARH_UVariable uNewData
// Returns: None
//-------------------------------------------------------------------------------------------------
PRIVATE BOOL boNewValue( VARH_UVariable uOldData, VARH_UVariable uNewData )
{
return( uNewData.u32Val != uOldData.u32Val ? TRUE : FALSE);
}

View File

@ -26,8 +26,6 @@
extern "C" { extern "C" {
#endif #endif
//================================================================================================= //=================================================================================================
// Section: INCLUDES // Section: INCLUDES
// Description: List of required include files (visible by all modules). // Description: List of required include files (visible by all modules).
@ -59,22 +57,11 @@ extern "C" {
// Description: Definition of global macros (visible by all modules). // Description: Definition of global macros (visible by all modules).
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: ENUMERATIONS // Section: ENUMERATIONS
// Description: Definition of global enumerations (visible by all modules). // Description: Definition of global enumerations (visible by all modules).
//================================================================================================= //=================================================================================================
typedef enum
{
VARH_eWrite = 0, // everytime someone writes
VARH_eNewValue, // only if there is a new value
VARH_eNumberOfNotificationTypes, // Must be last entry
} VARH_EnNotification;
// variables // variables
typedef enum typedef enum
{ {
@ -94,8 +81,6 @@ typedef enum
VARH_eNumberOfVariables, // Must be last entry VARH_eNumberOfVariables, // Must be last entry
} VARH_EnVariables; } VARH_EnVariables;
//================================================================================================= //=================================================================================================
// Section: STRUCTURES // Section: STRUCTURES
// Description: Definition of global Structures (visible by all modules). // Description: Definition of global Structures (visible by all modules).
@ -123,25 +108,20 @@ typedef struct
U8 u8Flags; // flags U8 u8Flags; // flags
} VARH_StVar; } VARH_StVar;
//================================================================================================= //=================================================================================================
// Section: GLOBAL VARIABLES // Section: GLOBAL VARIABLES
// Description: Definition of global variables (visible by all modules). // Description: Definition of global variables (visible by all modules).
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: GLOBAL CONSTANTS // Section: GLOBAL CONSTANTS
// Description: Definition of global constants (visible by all modules). // Description: Definition of global constants (visible by all modules).
//================================================================================================= //=================================================================================================
//================================================================================================= //=================================================================================================
// Section: FUNCTION TYPES // Section: FUNCTION TYPES
// Description: Definition of functions // Description: Definition of functions
//================================================================================================= //=================================================================================================
typedef VOID (*VARH_pfnNotification)( PVOID pvData );
//================================================================================================= //=================================================================================================
// Section: GLOBAL FUNCTIONS (PROTOTYPES) // Section: GLOBAL FUNCTIONS (PROTOTYPES)
@ -149,14 +129,14 @@ typedef VOID (*VARH_pfnNotification)( PVOID pvData );
//================================================================================================= //=================================================================================================
BOOL VARH_boInitializeModule( VOID ); BOOL VARH_boInitializeModule( VOID );
BOOL VARH_boRegisterNotification( U8 u8Variable, VARH_EnNotification enNotification, VARH_pfnNotification pfnCallback, PVOID pvCallbackArgument );
// set data functions // set data functions
VOID VARH_vSetVariableData( U8 u8Variable, VARH_UVariable uData ); VOID VARH_vSetVariableData( U8 u8Variable, VARH_UVariable uData );
VOID VARH_vSetVariableDataFromMaster( U8 u8Variable, VARH_UVariable uData ); VOID VARH_vSetVariableDataFromSystem( U8 u8Variable, VARH_UVariable uData );
// get data function // get data function
VARH_UVariable VARH_uGetVariableData( U8 u8Variable ); VARH_UVariable VARH_uGetVariableData( U8 u8Variable );
U8 VARH_uGetVariableFlags( U8 u8Variable );
VOID VARH_vSetVariableToInitData( U8 u8Variable ); VOID VARH_vSetVariableToInitData( U8 u8Variable );
VOID VARH_vSetAllVariablesToInitData( VOID ); VOID VARH_vSetAllVariablesToInitData( VOID );