# Library of General Functions (LGF) for SIMATIC S7-1200 / S7-1500

## Overview

The SIMATIC S7-1200 / S7-1500 General Functions Library (LGF) is a meticulously curated collection of function blocks that enhance the programming experience within the TIA Portal for SIMATIC S7-1200 and S7-1500 controllers. This library encompasses a variety of operations, including bit logic, timing, counting, comparison, mathematical computations, data handling, signal generation, and technology functions.

## Key Features

- **Bit Logic Operations:** Functions for bit manipulation such as setting, resetting, toggling, and testing bits.
- **Date and Timer Operations:** Functions to handle calendar days, weeks, holidays, and astronomical clocks.
- **Counter Operations:** Functions to count bits, edges, and elements in arrays.
- **Comparator Operations:** Functions to compare binary masks, real numbers, strings, and variants.
- **Math Operations:** Functions for distance calculations, factorials, random number generation, and more.
- **Matrix Operations:** Functions for matrix addition, subtraction, multiplication, and inversion.
- **Data Handling:** Functions for CRC calculations, data logging, FIFO, LIFO, and sorting.
- **Converter Operations:** Functions to convert between different data types and formats.
- **Signal Generators:** Functions to generate various signal waveforms.
- **Technology Operations:** Functions for rate of change limitation, interpolation, and ramp generation.
- **Measurement Operations:** Functions for averaging, smoothing, and regression analysis.
- **System Operations:** Functions to check endianness and activate/deactivate devices.

## Detailed Functions

### Bit Logic Operations

- **LGF_BitReset**: Resets a specified bit within a DWORD variable.
- **LGF_BitSet**: Sets a specified bit within a DWORD variable.  
- **LGF_BitSetTo**: Sets a specified bit within a DWORD variable to a specified value (TRUE or FALSE).  
- **LGF_BitTest**: Checks the status of a specified bit within a DWORD variable.  
- **LGF_BitToggle**: Toggles a specified bit within a DWORD variable.  
- **LGF_IecTimerOnOff**: Implements an IEC Timer with both TON and TOF functionalities.  
- **LGF_PulseRelay**: Simulates a pulse relay or toggle flip-flop with set and reset inputs.

### Date and Timer Operations

- **LGF_GetCalendarDay**: Calculates the number of days passed since the beginning of the year.  
- **LGF_GetCalendarWeek_ISO**: Determines the ISO week number for a given date.  
- **LGF_GetCalendarWeek_US**: Determines the US week number for a given date.  
- **LGF_IsGermanHoliday**: Checks if a given date is a German public holiday.  
- **LGF_AstroClock**: Provides astronomical clock functionalities.  
- **LGF_SetTime**: Sets the system time on a controller.  
- **LGF_TimerSwitch**: Offers a timer switch functionality with multiple modes.
- **LGF_DelayProgram**: Introduces a delay mechanism for program execution.
- **LGF_GetClockState**: Reads the local time, the system time and returns the NTP status.

### Counter Operations

- **LGF_BitCount**: Counts the number of set bits within a DWORD.  
- **LGF_CountArrayElements**: Counts the number of elements in an array that meet certain criteria.  
- **LGF_CountBooleanEdges**: Counts the rising or falling edges of a boolean signal.  
- **LGF_CountFalInDWord**: Counts falling edges within a DWORD.  
- **LGF_CountRisInDWord**: Counts rising edges within a DWORD.  
- **LGF_GetBitStates**: Retrieves the state of each bit within a DWORD.  

### Comparator Operations

- **LGF_BinaryMaskCompare**: Compares binary data against a mask.  
- **LGF_CompareLReal**: Compares two LREAL values.  
- **LGF_CompareLRealByPrecision**: Compares two LREAL values with specified precision.  
- **LGF_CompareString**: Compares two strings.  
- **LGF_CompareVariant**: Compares two VARIANT data types.

### Math Operations

- **LGF_CalcDistance_2D**: Calculates the 2D distance between two points.  
- **LGF_CalcDistance_3D**: Calculates the 3D distance between two points.  
- **LGF_GetFactorial**: Computes the factorial of a number.  
- **LGF_IsValueInLimits**: Checks if a value is within specified limits.  
- **LGF_IsValueInRange**: Determines if a value falls within a specified range.  
- **LGF_IsValueInTolerance**: Checks if a value is within a tolerance range.  
- **LGF_NthRoot**: Calculates the nth root of a number.  
- **LGF_Random_DInt**: Generates a random DINT.  
- **LGF_Random_Real**: Generates a random REAL value.  
- **LGF_Random_UDInt**: Generates a random UDINT.  
- **LGF_RandomRange_DInt**: Generates a random DINT within a specified range.  
- **LGF_RandomRange_Real**: Generates a random REAL within a specified range.  
- **LGF_RandomRange_UDInt**: Generates a random UDINT within a specified range.  
- **LGF_ScaleLinear**: Performs a linear scaling operation.  
- **LGF_SearchMinMax**: Searches for the minimum and maximum values in an array.
- **LGF_SimpleAveraging**: Calculates a moving arithmetic mean from LReal values. The method can be used to smooth data series; the values can be read in cyclically or triggered.
- **LGF_RoundByPrecision**: Rounds a floating-point number (LReal) to a specified number of decimal places.
- **LGF_IsNumber**: Proves a LReal datatype for a valid number and numeric range.

### Data Handling

- **LGF_CalcCRC16**: Calculates a 16-bit CRC for a given data block.  
- **LGF_CalcCRC16Advanced**: Calculates an advanced 16-bit CRC with customizable parameters.  
- **LGF_CalcCRC32**: Computes a 32-bit CRC for data integrity verification.  
- **LGF_CalcCRC32Advanced**: Provides advanced 32-bit CRC calculation with customizable polynomial and initial value.  
- **LGF_CalcCRC8**: Calculates an 8-bit CRC for smaller data sets.  
- **LGF_CalcCRC8Advanced**: Offers an advanced 8-bit CRC calculation with additional options.  
- **LGF_CalcCRC8For1Byte**: Specialized function to calculate an 8-bit CRC for a single byte.  
- **LGF_IsParityEven**: Checks if the parity of a given number is even.  
- **LGF_IsParityOdd**: Checks if the parity of a given number is odd.  
- **LGF_DataLogC**: Logs data into a user-defined structure.  
- **LGF_FIFO**: Implements a First In, First Out data structure.  
- **LGF_FileRead**: Reads data from a file.  
- **LGF_FileWrite**: Writes data to a file.  
- **LGF_LIFO**: Implements a Last In, First Out data structure.  
- **LGF_ShellSort_DInt**: Sorts an array of DINTs using the Shell sort algorithm.  
- **LGF_ShellSort_LReal**: Sorts an array of LREALs using the Shell sort algorithm.  
- **LGF_ShellSort_UDInt**: Sorts an array of UDINTs using the Shell sort algorithm.  
- **LGF_ShiftRegister**: Implements a shift register for data serialization.
- **LGF_StartValueReadWrite**: Read or write start value(s) from variables or complete data blocks.
- **LGF_FileReadWriteDelete**: Reads, writes, or deletes a file to/from the SIMATIC Memory Card (SMC).
- **LGF_FileDelete**: Deletes a file from the SIMATIC Memory Card (SMC).

### Converter Operations  

- **LGF_BinaryToGray**: Converts binary numbers to Gray code.
- **LGF_ConvertSpeed**: Converts a given speed value in another unit.
- **LGF_DecodeUtf8**: Decodes UTF-8 encoded data to strings.  
- **LGF_DTLToJulianDate**: Converts date and time to Julian date format.  
- **LGF_DTLToString_DE**: Converts date and time to a German date string.  
- **LGF_DTLToString_ISO**: Converts date and time to an ISO 8601 compliant string.  
- **LGF_DTLToUnixTime**: Converts date and time to Unix time (seconds since epoch).  
- **LGF_EncodeUtf8**: Encodes strings to UTF-8 format.  
- **LGF_GpsDDToGps**: Converts GPS coordinates from decimal degrees to GPS format.  
- **LGF_GpsToGpsDD**: Converts GPS coordinates from GPS format to decimal degrees.  
- **LGF_GrayToBinary**: Converts Gray code to binary numbers.  
- **LGF_IntToString**: Converts integer values to string representation.  
- **LGF_JulianTimeToDTL**: Converts Julian date to date and time format.  
- **LGF_StringToDTL_DE**: Parses a German date string to date and time format.  
- **LGF_StringToDTL_ISO**: Parses an ISO 8601 date string to date and time format.  
- **LGF_StringToInt**: Parses a string to produce an integer value.  
- **LGF_StringToTaddr**: Converts a string to a TIA Portal address format.  
- **LGF_StringToTime**: Parses a string to produce a time value.  
- **LGF_TaddrToString**: Converts a TIA Portal address format to a string.  
- **LGF_TimeToString**: Converts a time value to its string representation.  
- **LGF_UnixTimeToDTL**: Converts Unix time to date and time format.  
- **LGF_StringToDTL_US**: Parses a US date string to date and time format.
- **LGF_DTLToString_US**: Converts date and time to a US date string (MM DD YYYY…).

### Signal Generators

- **LGF_CosinusCI**: Generates a cosine wave signal.  
- **LGF_Frequency**: Produces a frequency signal for testing or control purposes.  
- **LGF_Impulse**: Generates an impulse signal.  
- **LGF_RectangleCI**: Creates a rectangular wave signal.  
- **LGF_SawToothCI**: Produces a sawtooth wave signal.  
- **LGF_SinusCI**: Generates a sinusoidal signal.  
- **LGF_TriangleCI**: Creates a triangle wave signal.
- **LGF_ClockGen**: Generates the same clock signals as the system clock memory.

### Technology Operations

- **LGF_LimRateOfChangeAdvancedCI**: Limits the rate of change of a signal with advanced configurations.  
- **LGF_LimRateOfChangeCI**: Standard rate of change limiter for signal processing.  
- **LGF_NonLinearInterpolation**: Provides non-linear interpolation between data points.  
- **LGF_RampCI**: Generates a ramp signal with configurable slope.  

### Measurement Operations

- **LGF_AverageAndDeviation**: Calculates the average and standard deviation of a data set.  
- **LGF_DifferenceQuotientFC**: Computes the difference quotient for control and analysis.  
- **LGF_RegressionLine**: Determines the regression line for a set of data points.  
- **LGF_SimpleSmoothingFC**: Applies simple smoothing to a signal.  
- **LGF_SmoothByPolynomFC**: Smooths a signal using polynomial functions.  

### System Operations

- **LGF_IsBigEndian**: Checks if the system uses big-endian byte order.  
- **LGF_IsLittleEndian**: Checks if the system uses little-endian byte order.  
- **LGF_ActDeactDevice**: Activates or deactivates a device connected to the PLC.
- **LGF_ReadPnInterfaceParameter**: Reads parameters from a Profinet interface.
- **LGF_MeasureCpuLoad**: Measurement and calculation of PLC CPU load for different OB types.
