Programming in C/C++?



  • If it may help someone, here is a working piece of code to make a stepper engine rotate. The unipolar, 4-phase stepper engine is connected to pins 0, 1, 6, 7.

    The program expects one argument being the microseconds to wait between 2 half-steps. In my case, below 600 it's too fast for the engine to follow, but any value higher is fine.

    Thanks again KitBishop for this nice GPIO API!

    Cheers

    #include <cstdlib>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include "GPIOPin.h"
    #include "GPIOAccess.h"

    int main(int argc, char** argv) {
    int d = atoi(argv[1]);
    printf("delay=%d\n", d);

    GPIOPin* pins[4];
    pins[0] = new GPIOPin(0);
    pins[1] = new GPIOPin(1);
    pins[2] = new GPIOPin(6);
    pins[3] = new GPIOPin(7);
    for (int i = 0; i < 4; i++) {
    pins[i]->setDirection(GPIO_OUTPUT);
    }
    int i = 0, j = 3;
    for (;;) {
    pins[i]->set(1);
    usleep(d);
    pins[j]->set(0);
    usleep(d);
    j = i++;
    i = i % 4;
    }
    return 0;
    }



  • @Frederic-Baumann Cool 🙂 Glad my code is of use.
    Your code is not dissimilar to what I have used on Arduino for driving a stepper motor.



  • @Kit-Bishop help please
    i want to cross compile C++ to omega 2 but failed
    i do every thing i know !
    most of tutorials not clear
    they just say some words
    im beginner in Linux
    please help !
    ** i download the tool chain for 32,64 , change the path , write simple hello word file , compiled it
    transferred to onion by winscp , change mode , execute it !! get error like below !!
    ./asd: line 1: syntax error: unexpected "&" (expecting ")")
    what can i do ???? **



  • @anglo-marc the onion tutorial is for Omega 1, not Omega2. Did you use the MIPSel (little-endian) compiler?

    In my docker project you can find a long conversation between another apprentice in compiling for Omega2 and me, and in the OP a little introduction to How to compile C code (right before the Edit 1)

    If you have a toolchain already set, you don't need Docker. Everything said in the post that must be executed inside the docker container is what you must do with your own toolchain, if it is the right one, the MIPSel 24kc.



  • @JosĂ©-Luis-Cánovas said in Programming in C/C++?:

    @anglo-marc the onion tutorial is for Omega 1, not Omega2. Did you use the MIPSel (little-endian) compiler?

    In my docker project you can find a long conversation between another apprentice in compiling for Omega2 and me, and in the OP a little introduction to How to compile C code (right before the Edit 1)

    If you have a toolchain already set, you don't need Docker. Everything said in the post that must be executed inside the docker container is what you must do with your own toolchain, if it is the right one, the MIPSel 24kc.

    thanks dear for replaying
    actually im beginner in this field
    and unfortunately there are no Clear tutorials to explain that !
    the Onion did not release enough resources !



  • Wow, I have actually managed to get this to work!

    The biggest problem was getting the environment variables to remain valid over a reboot! Thanks very much for the pointers.



  • It looks like i can compile and run fairly simple command line code at the moment. if i try and compile ogps (for example) then i get a number of errors which relate to additional libraries and header files and so on.

    I have managed to get as far as

    CLEAN SUCCESSFUL (total time: 60ms)
    cd '/home/ianm/ogps'
    /usr/bin/make -f Makefile
    [ 33%] Building C object CMakeFiles/ugps.dir/main.c.o
    [ 66%] Building C object CMakeFiles/ugps.dir/nmea.c.o
    [100%] Linking C executable ugps
    /home/ianm/toolchain/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.15/bin/../lib/gcc/mipsel-openwrt-linux-musl/5.4.0/../../../../mipsel-openwrt-linux-musl/bin/ld: cannot find -lubox
    /home/ianm/toolchain/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.15/bin/../lib/gcc/mipsel-openwrt-linux-musl/5.4.0/../../../../mipsel-openwrt-linux-musl/bin/ld: cannot find -lubus
    collect2: error: ld returned 1 exit status
    CMakeFiles/ugps.dir/build.make:120: recipe for target 'ugps' failed
    make[2]: *** [ugps] Error 1
    CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ugps.dir/all' failed
    make[1]: *** [CMakeFiles/ugps.dir/all] Error 2
    Makefile:127: recipe for target 'all' failed
    make: *** [all] Error 2
    

    has anyone else tried this yet? I think it is upset about libraries but I am not 100% on this as the whole linux things is a bit new to me at present.



  • @Ian-Marchant you have to select in make menuconfig ubos and ubus, compile with make.
    Check this post in my docker project about getting onion libraries, it explains where the library files will appear. You don't need to add onion feeds, but the thread is full with problems and solutions.



  • while the onion site not provide good support !
    ex.. the omega2+ dist. is lede ..
    in lede site i found some info about the supported distrib. of linux that can use it ! to cross compile !

    so >> why this ?

    check link below !
    https://lede-project.org/docs/guide-developer/install-buildsystem
    my os . Ubuntu 14
    not supported !

    hope some serious support from official onion .



  • @anglo-marc said in Programming in C/C++?:

    hope some serious support from official onion .

    There have already been a lot of threads here explaining how to build C programs, both via building a LEDE cross toolchain as part of a "close enough" LEDE build, or by a ready-to-go docker virtual machine image that has been offered. For someone in a position to really do native development, these work fine, and are the standard approach for an Embedded Linux system of this category.

    It might be important to recognize that C development isn't really a primary goal of the product, compared to scripting languages and web widgets. Rather than chasing every varied user request (and especially those such as this that are already well covered by the community), the onion team's time would be far better spent catching up on their overdue obligations for corresponding source releases - the thing that only they can do.

    In comparison to something that some might call a success, like the Raspberry Pi, the key takeway has to be that the system vendor there is only providing the Hardware and the directly required GPL sources - most of the resources people think of in the pi ecosystem, including Raspbian itself, do not come from the hardware vendor. It's a rich ecosystem precisely because they have enabled their community, and not restricted it to the glacial pace of doing everything themselves.



  • @Chris-Stratton said in Programming in C/C++?:

    @anglo-marc said in Programming in C/C++?:

    hope some serious support from official onion .

    There have already been a lot of threads here explaining how to build C programs, both via building a LEDE cross toolchain as part of a "close enough" LEDE build, or by a ready-to-go docker virtual machine image that has been offered. For someone in a position to really do native development, these work fine, and are the standard approach for an Embedded Linux system of this category.

    It might be important to recognize that C development isn't really a primary goal of the product, compared to scripting languages and web widgets. Rather than chasing every varied user request (and especially those such as this that are already well covered by the community), the onion team's time would be far better spent catching up on their overdue obligations for corresponding source releases - the thing that only they can do.

    In comparison to something that some might call a success, like the Raspberry Pi, the key takeway has to be that the system vendor there is only providing the Hardware and the directly required GPL sources - most of the resources people think of in the pi ecosystem, including Raspbian itself, do not come from the hardware vendor. It's a rich ecosystem precisely because they have enabled their community, and not restricted it to the glacial pace of doing everything themselves.

    thanks for interesting
    but .. realy !!
    the steps not clear !



  • @anglo-marc said in Programming in C/C++?:

    the steps not clear !

    The topic is an advanced one, but the necessary information has been provided.

    You'll need to spend more time reading and learning. In particular, handling any uniqueness of your particular development machine's Linux installation is uniquely your task, not anyone else's.

    The intent was that those wanting a simple path would be using pre-built capabilities and scripting languages, not C.



  • @Frederic-Baumann hi sir
    can u give me the gpio library of your version ?
    and why i cannot compile it in C++ compiler even though i already switch it from C compiler to C++
    Here's my error
    $ gccmips --std=gnu++11 sm_rotate.c -I../../libnewgpio/

    gccmips: warning: environment variable 'STAGING_DIR' not defined
    cc1: warning: command line option '-std=gnu++11' is valid for C++/ObjC++ but not for C [enabled by default]
    In file included from ../libnewgpio/hdr/GPIOPin.h:5:0,
    from sm_rotate.c:5:
    ../libnewgpio/hdr/GPIOTypes.h:37:24: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    GPIO_Irq_Handler_Object{
    ^
    ../libnewgpio/hdr/GPIOTypes.h:44:28: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    GPIO_PulseIn_Handler_Object{
    ^
    In file included from sm_rotate.c:5:0:
    ../libnewgpio/hdr/GPIOPin.h:7:1: error: unknown type name 'class'
    class GPIOPin {
    ^
    ../libnewgpio/hdr/GPIOPin.h:7:15: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    class GPIOPin {
    ^
    In file included from ../libnewgpio/hdr/GPIOAccess.h:13:0,
    from sm_rotate.c:6:
    ../libnewgpio/hdr/GPIOPwmPin.h:10:1: error: unknown type name 'class'
    class GPIOPwmPin {
    ^
    ../libnewgpio/hdr/GPIOPwmPin.h:10:18: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    class GPIOPwmPin {
    ^
    In file included from ../libnewgpio/hdr/GPIOAccess.h:14:0,
    from sm_rotate.c:6:
    ../libnewgpio/hdr/GPIOIrqInfo.h:7:5: error: unknown type name 'GPIO_Irq_Type'
    GPIO_Irq_Type type;
    ^
    ../libnewgpio/hdr/GPIOIrqInfo.h:9:5: error: unknown type name 'GPIO_Irq_Handler_Object'
    GPIO_Irq_Handler_Object * handlerObj;
    ^
    ../libnewgpio/hdr/GPIOIrqInfo.h:10:5: error: unknown type name 'bool'
    bool enabled;
    ^
    In file included from ../libnewgpio/hdr/GPIOAccess.h:15:0,
    from sm_rotate.c:6:
    ../libnewgpio/hdr/GPIOPulseInPin.h:7:1: error: unknown type name 'class'
    class GPIOPulseInPin {
    ^
    ../libnewgpio/hdr/GPIOPulseInPin.h:7:22: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    class GPIOPulseInPin {
    ^
    In file included from sm_rotate.c:6:0:
    ../libnewgpio/hdr/GPIOAccess.h:21:1: error: unknown type name 'class'
    class GPIOAccess {
    ^
    ../libnewgpio/hdr/GPIOAccess.h:21:18: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
    class GPIOAccess {
    ^
    sm_rotate.c: In function 'main':
    sm_rotate.c:14:2: error: unknown type name 'GPIOPin'
    GPIOPin* pins[4];
    ^
    sm_rotate.c:15:12: error: 'new' undeclared (first use in this function)
    pins[0] = new GPIOPin(0);
    ^
    sm_rotate.c:15:12: note: each undeclared identifier is reported only once for each function it appears in
    sm_rotate.c:15:16: error: expected ';' before 'GPIOPin'
    pins[0] = new GPIOPin(0);
    ^
    sm_rotate.c:16:16: error: expected ';' before 'GPIOPin'
    pins[1] = new GPIOPin(1);
    ^
    sm_rotate.c:17:16: error: expected ';' before 'GPIOPin'
    pins[2] = new GPIOPin(6);
    ^
    sm_rotate.c:18:16: error: expected ';' before 'GPIOPin'
    pins[3] = new GPIOPin(7);
    ^
    sm_rotate.c:20:2: error: 'for' loop initial declarations are only allowed in C99 mode
    for (int i = 0; i < 4; i++)
    ^
    sm_rotate.c:20:2: note: use option -std=c99 or -std=gnu99 to compile your code
    sm_rotate.c:22:10: error: request for member 'setDirection' in something not a structure or union
    pins[i]->setDirection(GPIO_OUTPUT);
    ^
    sm_rotate.c:25:6: error: redefinition of 'i'
    int i = 0, j = 3;
    ^
    sm_rotate.c:20:11: note: previous definition of 'i' was here
    for (int i = 0; i < 4; i++)
    ^
    sm_rotate.c:28:10: error: request for member 'set' in something not a structure or union
    pins[i]->set(1);
    ^
    sm_rotate.c:30:10: error: request for member 'set' in something not a structure or union
    pins[j]->set(0);



  • @Raymond-Cahyadi "gccmips: warning: environment variable 'STAGING_DIR' not defined" - fix that first & the others will probably fix themselves. See https://docs.onion.io/omega2-docs/cross-compiling.html


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.