3 Replies Latest reply on Aug 1, 2016 10:01 AM by jayls

    GPIO C code convert to C++

    jayls

      Hi,

       

      I am attempting to convert some C code from here to C++

       

      My code:

       

      #include "mraa.hpp"

      #include <stdio.h>

      #include <unistd.h>

      #include <iostream>

      using namespace std;

       

      void switchChange(void* args);

       

       

      int main(){

         mraa::Gpio* gpio36 = new mraa::Gpio(36);

          gpio36->dir(mraa::DIR_IN);

          gpio36->isr(mraa::EDGE_FALLING, &switchChange, gpio36);

      for (;;) {};

      return MRAA_SUCCESS;

      }

      void switchChange(void* args){

      int s = args->read  ;

      cout << "switch" << s << endl;

      }

       

      error message is:

       

      make temp2

      i586-poky-linux-g++ -m32 -march=core2 -mtune=core2 -msse3 -mfpmath=sse -mstackrealign -fno-omit-frame-pointer --sysroot=/opt/poky-edison/1.7.2/sysroots/core2-32-poky-linux -lmraa -o temp2 temp.cpp

      temp.cpp: In function 'void switchChange(void*)':

      temp.cpp:17:14: error: 'void*' is not a pointer-to-object type

        int s = args->read  ;

                    ^

      make: *** [temp2] Error 1

       

       

      I.e. I am not sure how to get the read info from args in the interrupt handler?

       

      I realise this is a bit of a coding newby question, but I have a number of others that follow on from this, once this is cracked

        • 1. Re: GPIO C code convert to C++
          0andriy

          In C (C++ follows that as well) you have to declare structure before usage.

          For example:

          struct mystruct {

          int read;

          };

           

          void myfunc(void *args) {

          struct mystruct *ms = args;

           

          printf("Args->read: %d\n", args->read);

          }

          Otherwise compiler doesn't know what to do with undefined variables.

          • 2. Re: GPIO C code convert to C++
            jayls

            I'm not sure I totally follow this. Isn't gpio36 a pointer to a Gpio object, in which case shouldn't the struct definition reflect the structure of a Gpio?

             

            Code wise are you suggesting the following? Becuase this still produces error as shown below

             

            #include "mraa.hpp"

            #include <stdio.h>

            #include <unistd.h>

            #include <iostream>

            using namespace std;

             

            void switchChange(void* args);

            struct mystruct {

              int read;

            };

             

            int main(){

               mraa::Gpio* gpio36 = new mraa::Gpio(36);

                gpio36->dir(mraa::DIR_IN);

                gpio36->isr(mraa::EDGE_FALLING, &switchChange, gpio36);

            for (;;) {};

            return MRAA_SUCCESS;

            }

            void switchChange(void* args){

             

            struct mystruct *ms = args;

            int s = args->read;

            cout << "switch" << s << endl;

            }

             

            because this gives error:

            make temp2

            i586-poky-linux-g++ -m32 -march=core2 -mtune=core2 -msse3 -mfpmath=sse -mstackrealign -fno-omit-frame-pointer --sysroot=/opt/poky-edison/1.7.2/sysroots/core2-32-poky-linux -lmraa -o temp2 temp.cpp

            temp.cpp: In function 'void switchChange(void*)':

            temp.cpp:22:23: error: invalid conversion from 'void*' to 'mystruct*' [-fpermissive]

            struct mystruct *ms = args;

                                   ^

            temp.cpp:23:13: error: 'void*' is not a pointer-to-object type

            int s = args->read;

                         ^

            make: *** [temp2] Error 1

            • 3. Re: GPIO C code convert to C++
              jayls

              Because we are passing a void pointer to the interrupt function we need to convert this to some more meaningful form within the function.

               

              So, in this case we are passing a pointer to a gpio pin. So we need to cast the void pointer to a gpio pointer. Then we can access it. See commented code.

               

              #include "mraa.hpp"

              #include <stdio.h>

              #include <unistd.h>

              #include <iostream>

              using namespace std;

               

               

              void switchChange(void* args);

              int main() {

                  mraa::Gpio* gpio36 = new mraa::Gpio(36);

                  gpio36->dir(mraa::DIR_IN);

                  gpio36->isr(mraa::EDGE_FALLING, &switchChange, gpio36);

                  for (;;) {};

                  return MRAA_SUCCESS;

              }

              void switchChange(void* args) {

                // args is a void pointer and needs to be cast to a pointer to a gpio

                  mraa::Gpio* myArgs = (mraa::Gpio*) args;

                  // then we can get value

                  int s = myArgs->read();

                  cout << "switch" << s << endl;

              }