Variable Types

From Flowcode Help
Jump to navigationJump to search
Gen Create a New Variable Window.png

A variable or constant type defines the range of values what can be represented, as well as the amount of memory the variable or constant will occupy.


Integer types

BOOL

Icon: Image
Range: 0 or 1
Bit Depth: 1-bit unsigned integer

A Boolean value is either false or true. In Flowcode, this means 0 or 1, respectively. On download to an embedded target, this may take up 1 byte or a single bit of a byte, depending on the compiler and the optimisation level.

Boolean values are used typically as flags to indicate something needs to happen, has happened, or to control a function.


BYTE

Icon: Image
Range: 0 to 255
Bit Depth: 8-bit unsigned integer

A byte is the smallest storage unit most processors are able to read from in a single instruction.

A byte can be used to store a single value or its bits may be used as flags, almost like a compact array of BOOLs.

The value stored in a byte can only be positive.


INT

Icon: Image
Range: -32768 to 32767
Bit Depth: 16-bit signed integer

A signed int uses the top most bit of the binary value to indicate a positive or negative value, 0b0XXXXXXXXXXXXXXX being a positive value and 0b1XXXXXXXXXXXXXXX being a negative value.

The negative values are stored in 2's compliment form with 0b1000000000000000 = -32768 and 0b11111111111111111 = -1


UINT

Icon: Image
Range: 0 to 65535
Bit Depth: 16-bit unsigned integer

An unsigned value allows for the use of all of the bits allowing for a positive value with twice the range as the signed counterpart.


LONG

Icon: Image
Range: -21474836648 to 21474836647
Bit Depth: 32-bit signed integer

A 32-bit integer is useful for processing larger values and most integer-arithmetic uses found in embedded devices.

A signed long uses the top most bit of the binary value to indicate a positive or negative value. See INT for details.

32-bit calculations require additional maths libraries, by default calculations will only be 16-bit. By using a 32-bit variable the additional libraries will be used.


ULONG

Icon: Image
Range: 0 to 4294967295
Bit Depth: 32-bit unsigned integer

An unsigned value allows for the use of all of the bits allowing for a positive value with twice the range as the signed counterpart.

32-bit calculations require additional maths libraries, by default calculations will only be 16-bit. By using a 32-bit variable the additional libraries will be used.

Character String types

STRING

Icon: Image
Range: 0 to 255
Bit Depth: 8-bit array of integers. Default size of the array is 20 but can be adjusted as required.

A string consists of an array of 8-bit values, usually containing values with ASCII encoding which maps alphanumeric characters to the numeric values.

For example "Hello" is the same as {72,101,108,108,111,0}

Strings are automatically terminated with a 0 or null byte, this indicates the end of valid data when using string functions.

String is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.

Numeric values and variables can easily be converted to a string by using the keyword sting before the variable.

e.g. StringVar1 = "Temperature: " + string IntVar1

Details on the string functions are available here.

Real Number types

FLOAT

Icon: Image
Range: ±1.175494 × 10−38 to ±3.402824 × 1038
Bit Depth: 32-bit floating point

The range of a single-precision floating-point number is approximately ±1.175494 × 10−38 to ±3.402824 × 1038.

Floating point single-precision numbers are 32-bit approximations of real numbers which can represent a much wider range of values than an integer can, but at a loss of accuracy over large ranges.

Floating point calculations require additional maths libraries, by default calculations will only be 16-bit integer. By using a floating point variable the additional libraries will be used.

When defining floating point constants e.g. 1, it is important to let the C compiler know that the value is intended for use as a floating point value otherwise an integer calculation will take precedent.

To tell the C compiler the constant value is floating point simply use for example 1.0 as your constant.

Float is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.


Mathematical operations such as a = b + c will be processed as an integer calculation if a, b or c are integer values.

for example you would expect 2.5 * 2 to equal 5 but in the world of integers the calculation only sees 2.5 * 2 = 4, the real portion of the number is lost.

To fix this you must instead use 2.5 * 2.0 = 5


The keyword float can be used to force a calculation to be done using floating point maths instead of integer maths.

e.g. FloatVar1 = FloatVar2 + float IntVar1

Using the float keyword or changing the constant value to for example 35.0 would force the value to be recognised as a floating point type variable.

Additional types

HANDLE

Icon: Image
Range: Undefined
Bit Depth: 32-bit unsigned value

An object handle is used to reference a more complicated piece of data (such as a file, Flowcode component or block of text) whose internal format is not known. Flowcode provides many simulation macros that use handles to easily pass data around.

In an embedded context, a handle can be thought of as a pointer to memory, except Flowcode manages the handles so referencing a non-existent handle will not cause any fatal errors.

A handle value of zero is considered null and invalid to Flowcode.


Global vs Local

Global Variables

A global variable can be used throughout the program and remains in memory for the entire runtime. Every macro can access the variable for read and write operations.

It is recommended to use global variables only where needed to reduce overall memory usage and to make the program more segmented and easier to maintain.


Local Variables

A local variable will only consume memory while you are inside the function that contains it and are only accessable from the macro that creates them.

This allows for much better organisation and make it more obvious what is the variable for and how is it being used.

Local variables for the Main macro act like globals in that they will always exist in memory but they have the benefit of only being accessible from the main macro itself.


Variables of different types

Often you need to pass variables into macros as parameters or copy the value of one variable into another variable. When the two variables are of the same type or bit depth then there will be a like for like bit assignment.

However when the two variables are of differing types and bit depths then it's good to know what the rules are.


Low to High bit depth

A variable of a low bit depth e.g. an 8-bit variable being assigned to a variable of a higher bit depth e.g. 16-bit will simply populate the bits of the low bit depth variable and the remaining bits will be assigned 0.

BYTE B

UINT UI

B = 255

UI = B

UI == 255


High to Low bit depth

A variable of a high bit depth e.g. a 16-bit variable being assigned to a variable of a lower bit depth e.g. 8-bit will be truncated and the higher bits will be lost.

UINT UI

BYTE B

UI = 65535

B = UI

B == 255


Unsigned and Signed

Take care when transferring a signed value to an unsigned value of the same bit depth and visa versa as the bits will be copied on a one to one bases. This could have the effect of changing the value or flipping the state if the most significant bit is set.

When copying a signed value into another signed value of greater bit depth the sign will be retained allowing the negative value to be retained and unaffected.


Integer to Float

An integer variable being assigned to a floating point variable will be represented by a floating point number approximating the integer value.

UINT UI

FLOAT F

UI = 5

F = UI

F == 5.0


Float to Integer

A floating point variable being assigned to an integer variable will loose the real portion of the value.

FLOAT F

UINT UI

F = 5.999

UI = F

UI == 5


In the above case the real portion of the floating point is lost and the integer value is returned as 5. In a lot of cases you might want the floating point value to be rounded to the nearest integer.

To do this the round function can be used.

Converting to and from a String

A special routine is required to convert an ASCII string to or from a numeric variable or constant.

See the string functions for more information on how to do this.