Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Message Q with Data Structure help

Good day. I am a little rusty on C so would like some basic help. I would like to define a structure to hold Character LCD data such as the text string, the position, and the style. I would like to use the structure to pass messages to the LCD display task which will then write to the LCD.

I have defined

typedef struct message{

char text[16];

int xpos;

int ypos;

enum { STEADY, BLINK } textstyle;

} LCDMessage

LCDMessage LCDMessages[10];

Now what is the proper method of initializing the Q for MicroC because all the examples show the void type for pointer. So I'm guessing I need to map an array of pointers to the data structures but not sure. Can someone please straighten me out and show me a simple example using a structure for messages. Is this possible?

Thanks

JT

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I do it like this:

    
    # define L_CMD_BUFFER_SIZE       0x1000 // Only  Hexnumbers like 0x10, 0x20 ......
    # define L_MSG_QUEUE_MASK        (L_CMD_BUFFER_SIZE - 1) 
          
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    LCDMessage m_t_msg_buffer;
    unsigned int  m_ui_message_write_pointer;
    OS_EVENT *    m_message_queue;
    ......
    void init_message_system
       (
       void
       )
       {
       m_ui_message_write_pointer = 0;
       if(!(m_message_queue = OSQCreate(&m_CommMsg, 
                                        L_CMD_BUFFER_SIZE)))
          {
          puts("OSQCreate() failed\r\n");
          return;
          }
       }
    void send_message
       (
       const LCDMessage  * para_t_command
       )
       {
      # if OS_CRITICAL_METHOD == 3
          OS_CPU_SR cpu_sr;
      # endif
       OS_ENTER_CRITICAL();
       memcpy(&m_t_cmd_buffer, 
              para_t_command,
              sizeof(LCDMessage));
       OS_EXIT_CRITICAL();
       switch(OSQPost(m_message_queue,
                      (void *)&m_t_cmd_buffer))
          {
          case OS_NO_ERR:
             {
             break;   
             }   
          case OS_Q_FULL:
             {
             // error handling
             ....
             break;            
             }
          case OS_ERR_EVENT_TYPE:
             {
             // error handling
             ....
             break;
             }
          case OS_ERR_PEVENT_NULL:
             {
             // error handling
             ....
             break;
             }
          case OS_ERR_POST_NULL_PTR:
             {
             // error handling
              ....
              break;
              }
          default:
             {
             // error handling
              ....
              break;
              }
          }
       OS_ENTER_CRITICAL();
       m_ui_message_write_pointer = (m_ui_message_write_pointer + 1) & L_MSG_QUEUE_MASK;
       OS_EXIT_CRITICAL(); 
       }
    bool get_message
       (
       LCDMessage   * para_ptr_command_buffer, 
       INT16U          para_ui_timeout
       )
       { 
       LCDMessage  * proc_ptr_command;
       INT8U           proc_os_error;
      # if OS_CRITICAL_METHOD == 3
          OS_CPU_SR cpu_sr;
      # endif
       
       proc_ptr_command = 
             (t_command_typ *)OSQPend(m_message_queue,
                                      para_ui_timeout,
                                      &proc_os_error);
       switch(proc_os_error)
          {
          case OS_NO_ERR:
             {
             OS_ENTER_CRITICAL();
             memcpy(para_ptr_command_buffer, 
                    proc_ptr_command,
                    sizeof(LCDMessage  ));
             OS_EXIT_CRITICAL(); 
             return(true); // msg received
             }   
          case OS_TIMEOUT:
             {
             return(false);
             }   
          case OS_ERR_EVENT_TYPE:
             {
             // error handling
             ...
             return(false);
             }   
          case OS_ERR_PEVENT_NULL:
             {
             // error handling
             ...
             return(false);
             }   
          case OS_ERR_PEND_ISR:
             {
             // error handling
             ...
             return(false);
             }   
         default:
             {
             // error handling
             ...
             return(false);
             }   
          }
       }
    
    I used C++ therefore the bool type

    karsten
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    OS_EVENT * some_queue;

    void* some_queue_tbl[16];

    some_queue = OSQCreate(&some_queue_tbl[0], 16);

    task1

    {

    OSQPost(some_queue, (void *)pointer_to_your_struct);

    }

    task2

    {

    your_struct_type * pointer_to_my_struct_type;

    pointer_to_my_struct_type = (your_struct_type *)OSQPend(some_que, 0, &err);

    }