|
I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to declare delegates inside structures. What's the closest thing I can achieve the same thing in my ARM-project? For example, it would great if I could write a state machine like this:
typedef void (*readDataResultCallback_t)(uint8_t* readData, uint16_t sizeOfReadData);
typedef enum {
STATE1 = 0,
STATE2 = 1,
STATE3 = 2,
} stateMachineState_e;
typedef struct __attribute__((__packed__)) {
stateMachineState_e state;
uint32_t startAddr;
uint16_t numRegsToRead;
readDataResultCallback_t readDataResultCallback;
} stateMachineStep_s;
static stateMachineState_e state = STATE1;
stateMachineStep_s stateMachineSteps[3] = {
{STATE1, 10, 2, { (uint8_t* readData, uint16_t sizeOfReadData) {
if (stringCompare(readData, "AB") {
... Do stuff here...
state = STATE_2;
} else {
... Do stuff here...
state = STATE_3;
}}},
{STATE2, 20, 3, { (uint8_t* readData, uint16_t sizeOfReadData) {
if (stringCompare(readData, "ABC") {
... Do stuff here...
state = STATE_1;
} else {
... Do stuff here...
state = STATE_3;
}}},
{STATE3, 30, 4, { (uint8_t* readData, uint16_t sizeOfReadData) {
if (stringCompare(readData, "ABCD") {
... Do stuff here...
state = STATE_3;
} else {
... Do stuff here...
state = STATE_2;
}}},
}; Do I need to write my own pre-compile code interpreter/generator script that would place the anonymous code in functions outside the stateMachineStep_s array or is there something clever I can do (perhaps with macros or C++)?
modified 10-Jul-20 7:38am.
|
|
|
|
|
the
readDataResultCallback member of your structure is just a fancy pointer, either 32 or 64 bits long depending on your architecture, so it can only be initialised with the memory address of a function.
So you could have something like the following...
void OnState1(uint8_t* readData, uint16_t sizeOfReadData)
{
...
}
stateMachineStep_s stateMachineSteps[3] = {
{STATE1, 10, 2, &OnState1}
...
}
which, to be honest, is not significantly more typing. You could also use macros to generate some of the code but that comes with it's own issues.
C++11 and greater support lambda expressions which will allow you to initialise your structs with a similar syntax to your example but in that case you would have to swap your function pointer to an std::function<> type. If you don't have c++11 support the boost library offers a similar function pointer but without the nicer lambda syntax.
modified 10-Jul-20 10:24am.
|
|
|
|
|
Josh Gray2 wrote: which, to be honest, is not significantly more typing It's not a matter of typing less, it's a matter of having code that is easy to read (I expect to have arount 50-60 states).Josh Gray2 wrote: If you don't have c++11 support the boost library offers a similar function pointer but without the nicer lambda syntax. When I googled it, it seems GCC has terrific support for C++.Josh Gray2 wrote: C++11 and greater support lambda expressions which will allow you to initialise your structs with a similar syntax to your example but in that case you would have to swap your function pointer to an std::function<> type. Could someone knowledgeable in C++ please help with the syntax for the example I provided? Do I need to put it inside a C++ file or it is possible have sections of a C-file containing C++ code?
|
|
|
|
|
arnold_w wrote: Could someone knowledgeable in C++ please help with the syntax for the example I provided? Do I need to put it inside a C++ file or it is possible have sections of a C-file containing C++ code? |
You can't put C++ code inside a C file, but since C++ is (mostly) a superset of C, most C code will compile without issue. About the only thing you can't do is use a C identifier that is a C++ keyword (e.g. int new; will not compile in C++).
Depending on what you're running on your ARM, you might be able to get C# (mono) up and running on your ARM device (e.g. raspberry pi or similar).
Keep Calm and Carry On
|
|
|
|
|
There is no syntax for your example, as neither C nor C++ have such a feature. A function pointer needs to contain the address of the function to be called.
|
|
|
|
|
If you are allowed to use (a modern version of) the C++ compiler (g++ ), then something like this
#include <functional>
#include <iostream>
using namespace std;
using MyCallback = function <void(const char * p, const size_t size)>;
struct State
{
int i;
MyCallback mc;
};
State s[] =
{
{ 10, [](const char * p, size_t size ){ for (size_t n=0; n<size; ++n) cout << p[n];}}
};
int main()
{
cout << s[0].i << endl;
s[0].mc( "foo", 3);
cout << endl;
}
would work.
|
|
|
|
|
I converted my project to a C++ project (I could see that the line <nature>org.eclipse.cdt.core.ccnature got added inside my .project file) and added your code, but I get the following error message when I try to build the project:
cannot open linker script file -Wl,-Map=output.map: No such file or directory MyProject C/C++ Problem
Do you know what I'm doing wrong?
|
|
|
|
|
|
So I went to Project Properties -> C/C++ Build -> Settings -> MCU G++ Linker and replaced the following Command line pattern:
${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} with
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -T"" -Wl,-Map,output.map -Wl,--gc-sections -fno-exceptions -fno-rtti -o "MyProject.elf" @"objects.list" -lm When I build the project and look inside the console window, I see the following:
Invoking: MCU G++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -T"" -Wl,-Map,output.map -Wl,--gc-sections -fno-exceptions -fno-rtti -o "MyProject.elf" @"objects.list" -lm -lm
c:/ac6/systemworkbench/plugins/fr.ac6.mcu.externaltools.arm-none.win32_1.17.0.201812190825/tools/compiler/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/bin/ld.exe: cannot open linker script file -Wl,-Map,output.map: No such file or directory What did I do wrong?
|
|
|
|
|
arnold_w wrote: What did I do wrong? Nothing that I can see. I just did a build with -Wl,-Map,output.map and it worked fine. But the error message you have suggests that the linker is trying to read -Wl,-Map,output.map as a script file for some reason. I am not sure whether the MCU g++ linker is significantly different from the standard ld linker , but you may want to check the documentation.
|
|
|
|
|
It's probably a stupid question, but is there any way to make anonymous function usage like that compile inside a file with .c (not .cpp) extension? I know I'm allowed to put snippets of C-code inside a C++ file, but I suppose the opposite isn't possible (not even with clever macros)?
|
|
|
|
|
i'm just testing to run prog using parameters, but why i got error when debugging?
screenshot[^]
this is my code:
#include <iostream>
int main (int argc, char * argv []) {
if (argc > 0) {
std::cout << *argv[1] << std::endl;
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
|
|
|
|
|
If you call the program via:
myprog.exe
then argc will equal 1 , and argv[1] will not exist, hence the exception. Remember, array indexes start from zero, not one.
|
|
|
|
|
i know, but how to respond users if there's argument(s) submitted?
|
|
|
|
|
You have two choices:
1. Your program needs specific argument values which you must check by iterating over the items in argv . The actual number and type is for you to decide.
2. Your program does not accept command line arguments in which case you just ignore them. You may wish to display a warning message if argc is greater than 1.
|
|
|
|
|
1. you mean like loop? i tried this but still error:
#include <iostream>
int main (int argc, char * argv []) {
int itr = 0;
if (argc > 0) {
while (itr <= argc) {
std::cout << argv[itr] << std::endl;
++itr;
}
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
2. so how to make my program accept argument(s)?
|
|
|
|
|
You did not mention what error you received, but I assume it is because you are still trying to access an item which does not exist. The values for argc and argv are as follows:
1. argv is an array of strings, which will always contain at least one item: the name of the executable that is used to initiate the program.
2. argc contains the number of items in the argv array. So it will always be at least 1.
Let's look at a couple of examples:
1. A simple command line call to run my program, which is called Test.exe produces the following:
C:\Users\user1\Documents\Code\C++>Test.exe
argc = 1
argv[0] = Test.exe
There are no input parameters, argc equals 1 as there is only one item in argv , and that is argv[0] which contains the program name. If I use the full path to run the program the output will be:
C:\Users\user1\Documents\Code\C++>C:\Users\user1\Documents\Code\C++\Test
argc = 1
argv[0] = C:\Users\user1\Documents\Code\C++\Test
If we add some parameters to the command line we will get something like
C:\Users\user1\Documents\Code\C++>Test.exe one two "three and a half"
argc = 4
argv[0] = Test.exe
argv[1] = one
argv[2] = two
argv[3] = three and a half
So argv now contains 4 items, the program name followed by each item that is separated by a space or tab. Not that argv[3] contains four words, since they were delineated by double quotes in the command line.
The code to list these values is as follows:
std::cout << "argc = " << argc << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}
Generally you are not interested in the program name so you can start the above loop from 1 rather than 0.
The actual values in each parameter are for you to decide. You can use simple strings in a specific or random order, option letters or names preceded by single or double dashes or forward slashes:
Test check \foo\bar\filename.txt
Test -c C:\user1\Documents\file.jpg
Test --check somefilename
... etc.
|
|
|
|
|
this is really embarrassing! this was my mistake...
thank you very much! i just fix the code and now it's working![^]
this is the final test code:
#include <iostream>
int main (int argc, char * argv []) {
if (argc > 1) {
std::cout << argv[1] << std::endl;
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
really appreciate your reply!!
|
|
|
|
|
|
what is best resource to learn python language
|
|
|
|
|
you know that this is C & C++ thread, right?
|
|
|
|
|
|
Try to Google with Python and tutorial.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
The C/C++/MFC forum, of course.
|
|
|
|
|
Can i Use C++ Progam to develop a Appliciton for Andriod?
|
|
|
|