Thread: struct in MPLAB Harmony

  1. #1
    Registered User
    Join Date
    May 2016
    Location
    Northeast US
    Posts
    5

    struct in MPLAB Harmony

    Hello,

    I am new here and trying to understand some C programming examples used in MPLAB Harmony. I have limited C programming experience. I have gone through two online tutorials and read a couple of books. Most of my programming experience has been with QuickBASIC (yes, I'm getting old) and assembly language. I'm looking for some help.

    In the following:
    Code:
    typedef struct _DRV_GFX_SSD1926_OBJ
    {
       bool                               inUse;
       SYS_MODULE_INDEX         drvIndex;
       DRV_GFX_STATES            state;
       SYS_STATUS                  status;
       uint32_t                         nClients;
       DRV_GFX_CLIENT_OBJ *    pDrvSSD1926ClientObj;
       DRV_GFX_SSD1926_OBJECT_TASK task;
       DRV_GFX_INIT *              initData;
       uint16_t                         maxY;
       uint16_t                         maxX;
    } DRV_GFX_SSD1926_OBJ;
    
    static     DRV_GFX_SSD1926_OBJ drvSSD1926Obj;
    static     DRV_GFX_CLIENT_OBJ drvSSD1926Clients;
    Why is _DRV_GFX_SSD1926_OBJ in the first line?
    What does it do?
    Why does it have a leading underscore?
    What does static DRV_GFX_SSD1926_OBJ drvSSD1926Obj; do?
    Is it declaring two static variables?

    And in the following:

    Code:
    DRV_GFX_HANDLE DRV_GFX_SSD1926_Open( const SYS_MODULE_INDEX index, const DRV_IO_INTENT intent )
    {
        DRV_GFX_CLIENT_OBJ * client = (DRV_GFX_CLIENT_OBJ *)DRV_HANDLE_INVALID;
        if(drvSSD1926Obj.status != SYS_STATUS_READY)
        {
        }
        else if(intent != DRV_IO_INTENT_EXCLUSIVE)
        {
              }
        else if(drvSSD1926Obj.nClients > 0)
        {
             }
        else
        {
            client = &drvSSD1926Clients;
            client->inUse = true;
            client->drvObj = &drvSSD1926Obj;
            drvSSD1926Obj.nClients++;
        }
         return ((DRV_HANDLE)client);
    }
    Where do drvSSD1926Obj.status and drvSSD1926Obj.nClients come from? Are they declared in the code above? If so, how? Can someone explain the logic?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In C, you declare a struct (an aggregate data type) like this:
    Code:
    struct StructName {
        int aVar;
        int anotherVar;
    };
    And when you delcare a variable of that type, you say:
    Code:
    struct StructName stName;  // or whatever you want to call it
    Note that you need to use the keyword struct when declaring a struct variable.

    C has a facility for creating synonyms for types, called a typedef:
    Code:
    typedef int AnInt;
    
    AnInt i;   // i is really an int
    So you can typedef a struct like this:
    Code:
    typedef struct StructName {
        int aVar;
        int anotherVar;
    } StructName;
    
    // Now we can declare variables of this struct type like this:
    StructName stName;
    The only advantage is that we don't have to use the struct keyword anymore.

    In the typedef you can make the typedef name the same as the struct name as I did above, but some people would rather make them different, perhaps by adding an underscore as they have done in your code.


    static is a keyword that, when used globally (outside a function), makes the variable invisible to other code files (by leaving it out of the symbol table of the object file). Otherwise it would be possible for another code file to access it. That's all the keyword is doing in your case.

    drvSSD1926Obj is one of those global variables.

  3. #3
    Registered User
    Join Date
    May 2016
    Location
    Northeast US
    Posts
    5
    Thanks for the quick reply!

    So then does drvSSD1926Obj.status represent DRV_GFX_SSD1926_OBJ.SYS_STATUS?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In a sense, yes. But SYS_STATUS is the type. The struct member variable is called status. SYS_STATUS is presumably a typedef. You could search the code (including the headers) for SYS_STATUS to see what the underlying type is. My money's on it being a simple int.

    An example of accessing struct member variables:
    Code:
    typedef struct StructName {
         int aVar;
         int anotherVar;
    } StructName;
     
    StructName stName;
    
    void aFunction(void) {
        stName.aVar = 1;
        stName.anotherVar = 2;
    }

  5. #5
    Registered User
    Join Date
    May 2016
    Location
    Northeast US
    Posts
    5
    Thanks but I'm still having trouble understanding...

    Code:
    typedef struct _DRV_GFX_SSD1926_OBJ
    {    bool                       inUse;
       SYS_MODULE_INDEX         drvIndex;
       DRV_GFX_STATES            state;
       SYS_STATUS                  status;
       uint32_t                         nClients;
       DRV_GFX_CLIENT_OBJ *    pDrvSSD1926ClientObj;
       DRV_GFX_SSD1926_OBJECT_TASK task;
       DRV_GFX_INIT *              initData;
       uint16_t                         maxY;
       uint16_t                         maxX;
    } DRV_GFX_SSD1926_OBJ;
    
    static     DRV_GFX_SSD1926_OBJ drvSSD1926Obj;
    static     DRV_GFX_CLIENT_OBJ drvSSD1926Clients;
    Is drvSSD1926Obj being declared as a type DRV_GFX_SSD1926_OBJ as defined in the struct _DRV_GFX_SSD1926_Obj? The declaration being static, ie. is invisible to other code files?

    Is drvSSD1926Clients being declared as type DRV_GFX_CLIENT_OBJ? The declaration also being static.

    And another question; there are numerous occurrences of "->" in the code. For example:

    Code:
    client->inUse = true;
    client->drvObj = &drvSSD1926Obj;
    drvObj->nClients--;
    void DRV_GFX_SSD1926_InterfaceSet( DRV_HANDLE handle, DRV_GFX_INTERFACE * interface )
    {
        interface->BarFill = DRV_GFX_SSD1926_BarFill;
        interface->PixelArrayPut = DRV_GFX_SSD1926_PixelArrayPut;
        interface->PixelArrayGet = DRV_GFX_SSD1926_PixelArrayGet;
        interface->PixelPut = DRV_GFX_SSD1926_PixelPut;
        interface->ColorSet = DRV_GFX_SSD1926_SetColor;
        interface->InstanceSet = DRV_GFX_SSD1926_SetInstance;
        interface->MaxXGet = DRV_GFX_SSD1926_MaxXGet;
        interface->MaxYGet = DRV_GFX_SSD1926_MaxYGet;
    }
    Is something->somethingelse a valid variable name or does -> indicate an operation of some sort? (it's tough to search the code for -> with the tools I have).

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is drvSSD1926Obj being declared as a type DRV_GFX_SSD1926_OBJ
    No it is an instance of a DRV_GFX_SSD1926_OBJ. Remember DRV_GFX_SSD1926_OBJ is a type of variable just like int and double are types of variables.

    Is drvSSD1926Clients being declared as type DRV_GFX_CLIENT_OBJ?
    No it is being declared as an instance of DRV_GFX_CLIENT_OBJ, but yes it is being declared static.

    Is something->somethingelse a valid variable name or does -> indicate an operation of some sort?
    Code:
    client->drvObj = &drvSSD1926Obj;
    drvObj->nClients--;
    The operator-> is used when using pointers to structures, note that in the above ode drvObj is a pointer to drvSSD1962OBj, to access the structure member variables.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hyperterminal and MPLAB in C
    By tzzr101 in forum C Programming
    Replies: 48
    Last Post: 03-04-2013, 01:18 PM
  2. Replies: 8
    Last Post: 08-02-2012, 06:15 PM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. mplab ide
    By russ4789 in forum C Programming
    Replies: 0
    Last Post: 03-29-2009, 09:05 AM
  5. C30 Compiler for MPLAB
    By Dukefrukem in forum C Programming
    Replies: 1
    Last Post: 11-08-2007, 07:40 PM

Tags for this Thread