element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
Code Exchange
  • Technologies
  • More
Code Exchange
Blog embedded C++: manage a resource with a tiny* object
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 5 Apr 2025 12:58 PM Date Created
  • Views 714 views
  • Likes 6 likes
  • Comments 11 comments
  • Modern C++
  • c++
Related
Recommended

embedded C++: manage a resource with a tiny* object

Jan Cumps
Jan Cumps
5 Apr 2025

*tiny like in: 0 bits of data, 0 code instructions added, 0 clock ticks

scenario: you have a resource that needs to be initialised before use, and deactivated when done. We usually do that by calling some code before the actions, and some code after we're done. 

// ... code before you want to activate stepper driver

activate_stepper_driver(true);

// code where you use the stepper
step(15, left);
step(185, right);

activate_stepper_driver(false);

// ... code after you deactivated the driver

We do the bookkeeping ourselves, and take care that we call both functions.

There's a C++ design pattern (RAII) that can handle this for us. You create an object when you need to activate the resource, and it 'll deactivate when it gets out of scope.

Example class - a real world case where my motor burns 0.6 A when the stepper driver is active (motor "hold" current):

class wakeup_drv8711 { // driver out of sleep as long as object in scope
public:    
    inline wakeup_drv8711() { gpio_put(nsleep, 1); }
    inline ~wakeup_drv8711() { gpio_put(nsleep, 0); }
};

... and its use:

    // ... code before you want to activate stepper driver

    { // enter scope. w constructor gets called
        wakeup_drv8711 w;
        // code where you use the stepper
        step(15, left);
        step(185, right);
        
    } // leave scope, w destructor gets called

    // ... code after you deactivated the driver

The driver is activated at line 4 (3 actually), and deactivated at line 8

Thoughts are welcome.

  • Sign in to reply
Parents
  • balajivan1995
    balajivan1995 5 months ago

    Even in my company we are advised to use SBRM (Scope based/bound resource management, as explained in original post example). Lifetime of the variables/object created inside the scope will be cleared once it goes out of scope.

    It does make code look ugly though.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 5 months ago in reply to balajivan1995

    How do you think it makes the code look ugly?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balajivan1995
    balajivan1995 5 months ago in reply to Jan Cumps

    Ok, imagine a function that has a switch case that uses a const,

    void func()
    {
        const int a = getSomeValue();
        
        switch( a )
        {
            case 0:
                func01();
            break;
            
            case 1:
                func02();
            break;
            
            default:
                errorMsg();
            break;
        }
        
        someFuction();
        someOtherFunction();
    }

    using SBRM ( one of RAII rules), it needs to be rewritten like this so as to limit the scope of that const.

    void func()
    {
        {
            const int a = getSomeValue();
            
            switch( a )
            {
                case 0:
                    func01();
                break;
                
                case 1:
                    func02();
                break;
                
                default:
                    errorMsg();
                break;
            }
        }
        
        someFuction();
        someOtherFunction();
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 5 months ago in reply to balajivan1995

    got it

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Jan Cumps
    Jan Cumps 5 months ago in reply to balajivan1995

    got it

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
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