Code Snipes

Code Snipes

Extremely Useful Things

Code like that i reuse quite often,
but at the moment its quite some demo content to test prismjs ;-)

Edge detection

IF bInput AND NOT _bInput THEN
    // Rising Edge
ELSIF NOT bInput AND _bInput Then
    // Falling Edge
END_IF
_bIngput:= bInput;

Init something

IF bInit THEN
    bInit:= TRUE;
    // Do some stuff
END_IF
_bIngput:= bInput;

Check range

IF fInput <= fLowerLimet OR fInput >= fUpperLimit THEN
    // Out of range
END_IF

For Loop

FOR nIndex := start TO end BY step DO
    // Code block to be repeated
END_FOR

While Loop

WHILE nIndex < 10 DO
    // Code block to be repeated
    nIndex := nIndex + 1; // Increment counter
END_WHILE

Simple debounce

IF bInput AND NOT bOutput THEN
    IF nCounter >= 10 THEN
        bOutput:= TRUE;
    END_IF
    nCounter:= nCounter + 1;
ELSIF NOT bInput AND bOutput THEN
    IF nCounter >= 10 THEN
        bOutput:= FALSE;
    END_IF
    nCounter:= nCounter + 1;
ELSE
    nCounter:= 0:
END_IF

BYTE_TO_ARRAY_OF_BOOL

FUNCTION BYTE_TO_BOOL_ARRAY : ARRAY[0..7] OF BOOL
VAR_INPUT
    nValue : BYTE;  // Input byte
END_VAR

// Looks bad => performance ist the best
BYTE_TO_BOOL_ARRAY[0]:= nValue.0;
BYTE_TO_BOOL_ARRAY[1]:= nValue.1;
BYTE_TO_BOOL_ARRAY[2]:= nValue.2;
BYTE_TO_BOOL_ARRAY[3]:= nValue.3;
BYTE_TO_BOOL_ARRAY[4]:= nValue.4;
BYTE_TO_BOOL_ARRAY[5]:= nValue.5;
BYTE_TO_BOOL_ARRAY[6]:= nValue.6;
BYTE_TO_BOOL_ARRAY[7]:= nValue.7;


// FOR i := 0 TO 7 DO
//    BYTE_TO_BOOL_ARRAY[i] := (byteValue AND (1 SHL i)) <> 0;
// END_FOR