element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Safe and Sound
  • Challenges & Projects
  • Design Challenges
  • Safe and Sound
  • More
  • Cancel
Safe and Sound
Blog Safe and Sound Wearables- Hearing Guard System #7 CCS and TI-RTOS with Class code
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jomoenginer
  • Date Created: 2 Apr 2017 11:37 AM Date Created
  • Views 472 views
  • Likes 4 likes
  • Comments 1 comment
  • c++ class
  • safe & sound
  • ti-rtos
  • sharp_lcd_booster_pack
Related
Recommended

Safe and Sound Wearables- Hearing Guard System #7 CCS and TI-RTOS with Class code

jomoenginer
jomoenginer
2 Apr 2017

In my last post, I outlined how to set up Code Composer Studio to support C++11 in TI-RTOS. In this post, I will show some of the basic code I have come up with to show how to use C++ Classes in a TI-RTOS project.

 

For this example, I am using a simple menu display that was created from a Virtual Base Class and includes the use of vectors to add items to a menu list. I wanted something that was reusable for each of the menu selections that will be displayed in my project, so I came up with the Base Class which can be instantiated by multiple menu objects allowing for code reuse in the project. This same concept I intend to use through out the project and the Menu is just a start.

 

One thing that I did find was that it worked best if the Classes as well as the grlib calls were in separate files.  In the case of the non-c++ function calls, I had to add 'extern  "c" to the files.

 

I had tried various variable types for use with the menu header and the menu items, and found that 'char const *' was the best option when having to interface with code that is implemented in C such as the grlib and LCD libraries.  Also, I tried to use the C++ 'string' type, but this caused too many issues.

 

For a great example of how to added the grlib and LcdDriver to a MSP432 TI-RTOS project:

https://www.element14.com/community/community/design-challenges/texas-instruments-safe-sound-wearables-design-challenge/blog/2017/01/11/msp432-and-ti-rtos-sharp-lcd-boosterpack

 

 

BaseMenu.h

 

In the BaseMenu class, I have included 2 overloaded constructors where one takes individual items for the Menu Items and Menu Header and the other can take a char const array of menu items and add these to m_menuItems  menu vector variable as well as the Menu Header item. A vector was used since it has built-in functionality to manipulate the items in the list sort of like a linked list.   There are 4 virtual methods which can be redefined in derived classes and still keep their original properties.  The other non-virtual methods are defined by the Base Class by callable by the derived classes.

 

/*
 * baseMenu.h
 *
 *  Created on: Mar 19, 2017
 *      Author: Jon Morss
 */
//#include <iostream>
//#include <string>


#ifndef BASEMENU_H_
#define BASEMENU_H_


>
#include <vector>


using std::vector;


class BaseMenu
{
protected:


  char const * m_menuHeader;
  vector<char const *> m_menuItems;
public:
  // Constructor
  BaseMenu();
  BaseMenu(char const * m_Item, char const * m_Header);
  BaseMenu(char const * m_Item[], char const * m_Header, unsigned int num_I);
    ~BaseMenu();  // Virtual BaseMenu destructor


    virtual vector<char const *> menu(void) = 0;
    virtual vector<char const *> getMenuItems(void) = 0;
    virtual char const * getHeader(void) = 0;
    virtual char const * getItem(unsigned int m_I) = 0;
    void displayMenu();
    void setMenuItem(char const * m);
    void setMenuHeader(char const * h);
    void setMenuItemList(char const *m_IList[], unsigned int num_I);


};




#endif /* BASEMENU_H_ */

 

 

 

BaseMenu.cpp

The BaseMenu.cpp has the definition of the BaseMenu class non virtual methods.

 

/*
 * baseMenu.cpp
 *
 *  Created on: Mar 19, 2017
 *      Author: Jon Morss
 */
//#include <iostream>
//#include <string>


#include <vector>
#include "baseMenu.h"


using std::vector;
//using std::cout;


BaseMenu::BaseMenu(char const * m_Item, char const * m_Header) : m_menuHeader(m_Header)
{
  //m_menuHeader.push_back(m_Header);
  m_menuItems.push_back(m_Item);
}


BaseMenu::BaseMenu(char const * m_Item[], char const * m_Header, unsigned int num_I) : m_menuHeader(m_Header)
{
  m_menuItems.assign(m_Item, m_Item + num_I);
}


BaseMenu::~BaseMenu() {};


void BaseMenu::displayMenu()
{
    // Future work
  //cout << this->menu() << this->getHeader() << endl;
  /*
  for(unsigned int i =0; i <this->m_menuItems.size(); i++)
  {
  cout << "MenuItem[" << i << "] = " << this->m_menuItems[i] << endl;
  }
  cout << endl;
  */
}


void BaseMenu::setMenuItem(char const * m_I)
{
  //m_menuItems = m_I;
  m_menuItems.push_back(m_I);
}


void BaseMenu::setMenuHeader(char const * m_H)
{
  //m_menuHeader = m_H;
  m_menuHeader = m_H;
}


void BaseMenu::setMenuItemList(char const *m_IList[], unsigned int num_I)
{
  m_menuItems.insert(m_menuItems.begin(), m_IList, m_IList + num_I);
}

 

 

DBMenu.h

 

The DBMenu class will instantiate the BaseMenu class and redefine the virtual methods.  The DBMenu class interface will be used to create the menu options for DB related functionality.

 

/*
 * dbMenu.h
 *
 *  Created on: Mar 24, 2017
 *      Author: jomodev
 */


#ifndef DBMENU_H_
#define DBMENU_H_
#include <vector>
#include "BaseMenu.h"
//#include <string>


//using std::string;
using std::vector;


class DBMenu: public BaseMenu {
  public:
    DBMenu(char const * m_Item, char const * m_Header);
    DBMenu(char const * m_Item[], char const * m_Header, unsigned int num_Items);
    ~DBMenu();
    vector<char const *> menu();
    vector<char const *> getMenuItems();
    char const * getHeader();
    char const * getItem(unsigned int m_I);
};




#endif /* DBMENU_H_ */

 

DBMenu.cpp

 

The DBMenu.cpp redefines the BaseMenu virtual methods and adds it's own implementation. Note, the DBMenu class has direct access to the protected BaseMenu data members.

 

 

/*
 * dbMenu.cpp
 *
 *  Created on: Mar 24, 2017
 *      Author: jomodev
 */
//#include <iostream>
#include <vector>
#include <stdexcept>


using std::vector;


#include "DBMenu.h"




DBMenu::DBMenu(char const * m_Item, char const * m_Header) : BaseMenu(m_Item, m_Header)
{
    //cout << "Create a DBMenu from MainMenu!" << endl;
}


// Overloaded constructor to create Menu object with item array
DBMenu::DBMenu(char const * m_Item[], char const * m_Header, unsigned int num_Items) : BaseMenu(m_Item, m_Header, num_Items)
{
  //cout << "Create a DBMenu from MainMenu!" << endl;
}
vector<char const *> DBMenu::menu()
{
    return m_menuItems;
}


char const * DBMenu::getHeader()
{
    return m_menuHeader;
}


// Get the menu items
vector<char const *> DBMenu::getMenuItems() {


  return m_menuItems;
}


// Retrieve a menu item, return error if value is out of range
char const * DBMenu::getItem(unsigned int m_I)
{
    //return m_menuItems[m_I];
  char const * w_I = "ERROR";
  try {
  w_I = m_menuItems.at(m_I);
  }
  catch (const std::out_of_range& orr){
  //cerr << "Out of Range Error" << orr.what() << '\n';
  w_I = "ERROR";
  }


  return w_I;
}


DBMenu::~DBMenu()
{
    //cout << "Destroy a DBMenu!" << endl;
}

 

 

 

printmessage.h

 

The printMessage is used to call the Graphics libs to display items on the Sharp98 LCD as pure 'C' calls to preserve the 'C' nature of the grlib implementation. Note the use of the 'extern "C" which tells to the compiler/linked to treat what follows as pure 'C' code.

 

/*
 * printMessage.h
 *
 *  Created on: Mar 22, 2017
 *      Author: Jon Morss
 */


#ifndef PRINTMESSAGE_H_
#define PRINTMESSAGE_H_
//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C" {
#endif


#include <lcddriver/sharp96x96.h>


void printMessage (int8_t *theItem, int8_t *theItem2,  int8_t *theHeader);


//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endifc
#endif /* PRINTMESSAGE_H_ */

 

printmessage.c

Definition for the interfaces listed in printmessage and prints out a message to the string. .

 

 

/*
 * printMessage.c
 *
 *  Created on: Mar 22, 2017
 *      Author: Jon Morss
 */
#include "printMessage.h"


int8_t menuMessage[] = "Test";
int8_t menuMessage2[] = "Mess";
Graphics_Context g_sContext;




void printMessage (int8_t *theItem, int8_t *theItem2, int8_t *theHeader)
{
  Sharp96x96_LCDInit();
  GrContextInit(&g_sContext, &g_sharp96x96LCD);
  GrContextForegroundSet(&g_sContext, ClrBlack);
  GrContextBackgroundSet(&g_sContext, ClrWhite);


  GrClearDisplay(&g_sContext);
  GrContextFontSet(&g_sContext, &g_sFontCm20b);


  //GPIO_toggle(Board_LED0);


  GrStringDraw(&g_sContext,  theHeader , -1, 5, 5, 0);
  Graphics_drawLineH(&g_sContext, 10, 1, 95);
  GrContextFontSet(&g_sContext, &g_sFontCm14b);


  Graphics_drawLine(&g_sContext, 1, 22, Graphics_getDisplayWidth(&g_sContext) - 1, 22);
  GrStringDraw(&g_sContext, theItem, -1, 10, 30, 0);
  GrStringDraw(&g_sContext, theItem2, -1, 10, 50, 0);
  //GrStringDraw(&g_sContext, menuMessage , -1, 22, 60, 0);


  GrFlush(&g_sContext);
}

 

 

 

classSample.cpp

 

To implement the DBMenu class and get something displayed on the sharpLCD, the following modifications were made to the empty.c file.

First, include the prrintMessage.h and DBMenu. header files.

 

 

/* Board Header file */
#include "Board.h"
#include "printMessage.h"
#include "dbMenu.h"
//#include <string>


//using std::string;



#define TASKSTA

KSIZE   512

 

 

Second, I defined a macro that handles the the size of the array being passed . Also, I create an instance of the DBMenu class (NOTE: The  derived method is of BaseMenu value).  Also, an array containing the list of menu items is defined.

 

#define noOfElements(v) sizeof(v)/sizeof(v[0])


char const *menuList[] {"Set Level", "View Level", "Set Thresh" };


BaseMenu *dbmenu = new DBMenu(menuList, "DB Menu", noOfElements(menuList));

 

 

 

Third, I change the heartBeatFxn method so it calls the printMessage function passing two items from the DBMenu class items of menu and the Header. NOTE: I am type casting the values passed to 'int8_t' to match the interfaces in the .grlib and LCD libraries . Also, I added the 'extern"C"' typedefs to the grlib.h header to ensure it does not get compiled as a C++ file.

 

Void heartBeatFxn(UArg arg0, UArg arg1)
{
  //int8_t thisMessage[] = "What!";
  char const *thisItem = dbmenu->getItem(0);
  char const * thisHeader = dbmenu->getHeader();
  dbmenu->setMenuItem("Item2");
  //int8_t *thisItem2 = (int8_t *)"Item2";
  //dbmenu->setMenuItem("Item2");
  char const * thisItem2  = dbmenu->getItem(1);
  printMessage((int8_t *)thisItem, (int8_t *)thisItem2, (int8_t *)thisHeader);
  while (1) {
     Task_sleep((UInt)arg0);
     GPIO_toggle(Board_LED0);
  }
}

 

 

That pretty much covers a basic outline of one way to add classes to a TI_RTOS project.  This might not be the cleanest implementation but it is a start, and for me I will expand the usage of this into to other areas of my project.

  • Sign in to reply
  • DAB
    DAB over 8 years ago

    Nice post and overview of the software.

     

    I look forward to seeing the software evolve into your system.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube