Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. what extra changes i need to do when i make my local variable to global variable ?
Forum Updated to NodeBB v4.3 + New Features

what extra changes i need to do when i make my local variable to global variable ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 475 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    typedef struct
    {
    int nI2C;
    unsigned char nAddress;
    }TI2C;



    TI2C i2c;
    void *pHandle = (void*) & i2c;
    
    qDebug("gauge test\n\r");
    
    if((i2c.nI2C= open("/dev/i2c-2", O_RDWR))<0)
    {
        qDebug("can not open I2C bus \n\r");
        return bStatus;
    }
    
    qDebug("openend I2C bus\n\r");
    
    unsigned char nAddress=GAUGE_DEVICE_ADDRESS;
    
    TI2C *pI2C= (TI2C*) pHandle;
    
    if(nAddress!= pI2C->nAddress)
        ioctl(pI2C->nI2C,I2C_SLAVE,nAddress>>1);
    else
    {
        qDebug("Battery fuel gauge not found\n");
        return bStatus;
    }
    bStatus= true;
    pI2C->nAddress= nAddress;
    return bStatus;
    

    i need below questions answer regarding above code.

    1] what extra changes i need to do to make pI2C to make global structure pointer so that application not going to crash ?

    2] what extra changes i need to do to make pHandle to make global structure pointer so that application not going to crash ?

    jsulmJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      typedef struct
      {
      int nI2C;
      unsigned char nAddress;
      }TI2C;



      TI2C i2c;
      void *pHandle = (void*) & i2c;
      
      qDebug("gauge test\n\r");
      
      if((i2c.nI2C= open("/dev/i2c-2", O_RDWR))<0)
      {
          qDebug("can not open I2C bus \n\r");
          return bStatus;
      }
      
      qDebug("openend I2C bus\n\r");
      
      unsigned char nAddress=GAUGE_DEVICE_ADDRESS;
      
      TI2C *pI2C= (TI2C*) pHandle;
      
      if(nAddress!= pI2C->nAddress)
          ioctl(pI2C->nI2C,I2C_SLAVE,nAddress>>1);
      else
      {
          qDebug("Battery fuel gauge not found\n");
          return bStatus;
      }
      bStatus= true;
      pI2C->nAddress= nAddress;
      return bStatus;
      

      i need below questions answer regarding above code.

      1] what extra changes i need to do to make pI2C to make global structure pointer so that application not going to crash ?

      2] what extra changes i need to do to make pHandle to make global structure pointer so that application not going to crash ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-embedded-developer Basics:

      1. Make sure it is initialised (a pointer to valid instance assigned) before you use it
      2. Same here

      You should rething your design as global variables are usually a sign for bad design...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Qt-embedded-developer Basics:

        1. Make sure it is initialised (a pointer to valid instance assigned) before you use it
        2. Same here

        You should rething your design as global variables are usually a sign for bad design...

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #3

        @jsulm instead this which concept i need to use so i can make it to good design?

        jsulmJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @jsulm instead this which concept i need to use so i can make it to good design?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Qt-embedded-developer You should think about where these variables are really needed and keep them there (for example in a class which manages the access to them).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Qt-embedded-developer You should think about where these variables are really needed and keep them there (for example in a class which manages the access to them).

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #5

            @jsulm thanks !!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved