What is the right way to write the code for two different objects that need to interact with eachother?

I am working on a project for an STM32-based device that has multiple peripherals connected to it. For the most simple version of my problem, these peripherals are one:

  • stepper motor that is connected to a driver, which is driven by the GPIO pins of the STM32
  • touch-screen display that communicates with the STM32 via UART

The display is used to display and manipulate the settings that the stepper is running on, like speed, slow-down characteristic (none/gradual), distance to reverse direction, etc.

So far I have written all my code in C, but when I want to expand the peripherals (for example: with another instance of a stepper motor, which is required) it would make a mess. So far I had used volatile global variables for maintaining all the settings for the stepper, and everything works, but I feel like it would be best to instead have an object for the stepper motor and maintain all the variables it uses to track its current status parameters as object variables. So I set out to rewrite the code to C++ and write a classes for a stepper motor, which went well so far, but now I’m running into an issue.

The current structure of my program:

  • main.cpp: here all objects are instantiated, timers and UART are initialized, and FreeRTOS stuff
  • display.hpp: header file for display objects
  • display.cpp: display object code
  • stepper.hpp: header file for stepper motor objects
  • stepper.cpp: stepper motor code

The display both uses and manipulates data that is stored in the stepper object. Before, when I was using C, I could simply include the stepper.h file in the display.h file and vice versa, and they could call eachothers functions, like:

display.c:

int display_process_integer_data(uint8_t *buf) // buf = message stream received from display over UART

    int retval = 0;
    switch (buf[5]) // buf[5] defines the type of data is received from the display, i.e. start/stop, change speed etc. 
    {
    case 0x00: // Start/stop
        if (buf[8] == 0) { stepper_stop(); break; }

        if (buf[8] == 1) { stepper_start(); break; }

    case 0x01: // Change speed
        if ( stepper_set_speed( buf[7]*256 + buf[8] ) ) { retval = -1; break; }
        break;

    default: // No valid case
        retval = -1;
        break;
    }
    return retval;

Ideally I would now change this to this:

display.cpp:

int Display::process_integer_data(uint8_t *buf)

    int retval = 0;
    switch (buf[5]) 
    {
    case 0x00: // Start/stop
        if (buf[8] == 0) { motor1.start(); break; }

        if (buf[8] == 1) { motor1.stop(); break; }

    case 0x01: // Change speed
        if ( motor1.setSpeed( buf[7]*256 + buf[8] ) ) { retval = -1; break; }
        break;

    default: // No valid case
        retval = -1;
        break;
    }
    return retval;

But this is not possible. Now that the stepper is an instantiated object in main.cpp, I can no longer call this function as this object doesn’t exist in this scope.

Now usually I’d say ‘make the function give back the data to main.cpp and then do the rest of the work there’, but there’s more information that this function does than could fit in one return value: The function both figures out the type of instruction that is received (stored in buf[5]) and the data contained in that instruction (stored in buf[7] and buf[8] as an int16). So that is not possible (I think?).

In other situations I got around this by making an object a variable inside the Display class, like:

display.hpp:

class Display
{
protected:
    Stepper __motor = Stepper(uint8_t instantiating_var);
};

But this is more a hack than a proper solution I think, and it would only work if the display only interacts with __motor. I also need the Stepper object to call functions in the Display object, so this would not work. like:

stepper.cpp:

int Stepper::init()
{
    while (0 != display.inform_speed(__speed)) {}

    while (0 != display.inform_startstop(__running_FLAG)) {}

    while (0 != display.inform_slowdown_mode(__mode)) {}
    __initialized_FLAG = 1;

    return 0;
}

(above may not be the best example, but I can imagine that alarm states detected by the stepper motor, such as running into an end-stop, will need to be reported to the display. I haven’t programmed this yet)

So my question ultimately is: What is the right way to have two objects in main.cpp have access to other object methods? I feel like I’m missing a very obvious solution to this problem but I can’t wrap my head around it atm.

New contributor

Joppe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật