Difference between revisions of "C Code to Flowchart Conversion"
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | Flowcode can convert C code icons to standard flowchart icons. This feature is similar to the [[C_Code_Simulation|C Code Simulation]] feature, and shares many of the same abilities and characteristics. Note, in order to use this feature you will need to have purchased the C Simulation feature pack. | |
− | |||
If you are someone who prefers to type out your programs using C code while developing, but enjoys the benefits of a graphical representation, you can use this feature to turn the final result into a flowchart. This feature can also be great for understanding the effect that custom C code can have within your project, as such it is also invaluable as a teaching tool, allowing you to teach students the effects that certain C language statements have in a program. The resulting icons can be viewed in any of the four (Flowchart, Blocks, C code, Pseudocode) views. | If you are someone who prefers to type out your programs using C code while developing, but enjoys the benefits of a graphical representation, you can use this feature to turn the final result into a flowchart. This feature can also be great for understanding the effect that custom C code can have within your project, as such it is also invaluable as a teaching tool, allowing you to teach students the effects that certain C language statements have in a program. The resulting icons can be viewed in any of the four (Flowchart, Blocks, C code, Pseudocode) views. |
Latest revision as of 10:34, 5 October 2023
Flowcode can convert C code icons to standard flowchart icons. This feature is similar to the C Code Simulation feature, and shares many of the same abilities and characteristics. Note, in order to use this feature you will need to have purchased the C Simulation feature pack.
If you are someone who prefers to type out your programs using C code while developing, but enjoys the benefits of a graphical representation, you can use this feature to turn the final result into a flowchart. This feature can also be great for understanding the effect that custom C code can have within your project, as such it is also invaluable as a teaching tool, allowing you to teach students the effects that certain C language statements have in a program. The resulting icons can be viewed in any of the four (Flowchart, Blocks, C code, Pseudocode) views.
Contents
How to convert C code to Flowchart icons
Flowcode can convert C Code into Flowchart icons by using the Convert Custom C to Icons, right click menu item.
Multiple C Code icons can be converted at once by dragging the mouse or by holding control and left clicking on icons to select multiple icons.
Any warnings or user messages will be displayed in the Conversion messages window that also appears when simulating C code.
Converting multiple blocks in one operation
You can also convert multiple C code icons in a single operation by selecting them all before clicking the Convert Custom C to Icons button. This is required if you write code in one block which refers to variables that are declared in other blocks. Converting multiple icons in a single operation ensures that these "dependencies" are carried between blocks during the conversion process. Note that any variables which are added to the flowchart during the conversion process (e.g. x), would appear in the resulting C code generated from the new icons under a different name (e.g. FCL_X). Hence, converting multiple blocks of this nature one-at-a-time would render references to these variables in other blocks invalid, following the conversion of the original block containing the declaration.
The Conversion Messages window
When converting from C code to Flowchart icons, a helpful Conversion Messages window will appear.
Errors
The Conversion Messages window will detail any problems that Flowcode has encountered when trying to parse your C code. Certain C language features such as pointers, and advanced modifiers such as const and typedef are not supported. If Flowcode encounters a statement within your C code that it cannot convert, this information will be displayed in the Conversion Window, and Flowcode will simply leave that statement in a C code icon. Flowcode will still attempt to convert the rest of the code within the C code icon.
Variable renaming
The Conversion Messages window will also detail any information that is useful regarding the names of variables. Flowcode may rename variables within your C code when converting to icons. This due to the fact that multiple variables in C may use the same name, as long as they exist at different scopes (e.g. a name can be used to define a variable at the top of a function, as well as a variable inside the curly braces of a while-loop), but this is not supported in Flowcode. In an educational context, this renaming has the advantage of conveying clearly to the user that variables at different scopes in C are in fact distinct and cannot affect each other. No variable renaming performed by Flowcode will alter the semantics (behavior) of the program. If a variable is renamed by Flowcode during conversion, then this information, including the name that should now be used, will appear in the Conversion Messages window. For example, we might declare "int x" inside a while-loop, which Flowcode will rename to "x2" during conversion, hence it will now appear in the Project Explorer as "x2". Any variables declared in C code are become Local variables in Flowcode during conversion.
Interacting with Flowcode variables in C code
We recommend viewing your project using the C code view in order to get a better understanding of how the C code created by Flowcode relates to the icons, especially regarding the declaration of Local variables.
Variable data types
When variables are normally created in Flowcode, you specify a given type (e.g. Byte). The actual types as they appear in C code can be understood using the following mapping:
Flowcode type | In C code |
---|---|
Bool | MX_BOOL |
Byte | MX_UINT8 |
Int | MX_SINT16 |
UInt | MX_UINT16 |
Long | MX_SINT32 |
ULong | MX_UINT32 |
String | MX_CHAR (array) |
Float | MX_FLOAT |
Object handle | MX_UINT32 |
The conversion engine also currently supports the following native C types:
int, signed int, char, float, long, signed long, unsigned int, unsigned long
Any of the Flowcode types or native C types above, are automatically converted to the appropriate type when Local variables are added to the project during conversion (e.g. "signed int" becomes SInt).
Accessing variables created in Flowcode's UI
You can easily modify and read the values of variables that you have created in the user-interface (for example, using the Project Explorer) with the C code that you write. Variables are referred to using purely uppercase, and with a special prefix depending on whether they are global or local variables.
Variable type | C code prefix | Example |
---|---|---|
Global | FCV_ | Global variable "var1" can be accessed with "FCV_VAR1" |
Local | FCL_ | Local variable "var1" can be accessed with "FCL_VAR1" |
Variable accesses of this type (e.g. FCL_VAR1) are automatically converted back to the standard on-screen representation during conversion (e.g. FCL_VAR1 becomes .var1 in the resulting icons)
Calling Flowcode functions and your own Macros
Flowcode functions
Currently, there is a limited support for converting Flowcode-generated function calls back from C code into icons.
Flowcode is able to understand, and generate icons corresponding to the effects of the following function calls:
Function call | Parameter explanation | Effect of function |
---|---|---|
FCI_DELAYBYTE_MS(x) | x is a byte | Causes a delay of x milliseconds |
FCI_DELAYBYTE_US(x) | x is a byte | Causes a delay of x microseconds |
FCI_DELAYBYTE_S(x) | x is a byte | Causes a delay of x seconds |
FCI_DELAYINT_MS(x) | x is an int up to 2000 | Causes a delay of x milliseconds |
FCI_DELAYINT_US(x) | x is an int up to 2000 | Causes a delay of x microseconds |
FCI_DELAYINT_S(x) | x is an int up to 2000 | Causes a delay of x seconds |
GET_PORT(x) | x is a port letter (A, B, C...) | Returns the value of port x |
GET_PORT_PIN(x, y) | x is a port letter (A, B, C...), y is a pin index (0, 1, 2...) | Returns the value of pin y of port x |
GET_PORT_MASK(x, y) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00) | Returns the value of the pins of port x given by the mask y |
GET_PORT_SHIFT(x, y) | x is a port letter (A, B, C...), y is a shift value (e.g. 3) | Returns the value of port x after right-shifting it by y positions |
GET_PORT_MASK_SHIFT(x, y, z) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a shift value (e.g. 3) | Returns the value of the pins of port x given by the mask y, after right-shifting it by z positions |
SET_PORT(x, y) | x is a port letter (A, B, C...), y is a numerical value | Sets the value of port x to y |
SET_PORT_PIN(x, y, z) | x is a port letter (A, B, C...), y is a pin index (0, 1, 2...), z is a numerical value | Sets the value of pin y of port x to z |
SET_PORT_MASK(x, y, z) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a numerical value | Sets the value of the pins of port x given by the mask y, to z |
SET_PORT_SHIFT(x, y, z) | x is a port letter (A, B, C...), y is a shift value (e.g. 3), z is a numerical value | Sets the value of port x, to z, after left-shifting z by y positions |
SET_PORT_MASK_SHIFT(w, x, y, z) | w is a port letter (A, B, C...), x is a mask value (e.g. 0xFF00), y is a shift value (e.g. 3), z is a numerical value | Sets the function sets the value of the pins of port w given by the mask x, to z, after left-shifting z by y positions |
Function calls of the type FCI_DELAY... become Delay icons, and function calls of the type GET_PORT... and SET_PORT... become Calculation icons.
User-defined macros
Flowcode is also able to generate icons corresponding to the effects of calling your own user-defined macros. In C code, user-macros are referred to using the prefix "FCM_". For example, if we defined a user-macro called "Func", which returned a byte, and required an integer as a parameter, then we could write the following C code.
MX_UINT8 x = FCM_Func(4);
The above code would become a Calculation icon containing the line ".x = Func(4)". Note that we do not have to make the "Func" part of the expression uppercase in the C code, unlike when referring to variables.
Example
Here is an example program that counts up and down in binary using Port A and a Combo Board component to show the LED signals.
Here is the file before conversion to Flowchart icons.
Here is the same file after converting the C Code into Flowchart icons.
The C Code to Flowchare icon feature is similar to the C Simulation feature that allows custom C Code to be simulated.