Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy file descriptors using cpp version of lib not working #10

Open
karlo922 opened this issue Apr 18, 2024 · 2 comments
Open

Copy file descriptors using cpp version of lib not working #10

karlo922 opened this issue Apr 18, 2024 · 2 comments

Comments

@karlo922
Copy link

karlo922 commented Apr 18, 2024

Hi,

you helped me solving the issue with me master/slave display by suggesting following:

u8g2_t u8g2l;
u8g2_t u8g2r;

u8g2_Setup_s1d15300_100x32i_f(&u8g2l, U8G2_R0, u8x8_byte_4wire_sw_spi,u8x8_arm_linux_gpio_and_delay);
init_spi_sw(&u8g2l, GPIO_CHIP_NUM, PIN_DISPA0, U8X8_PIN_NONE,PIN_DISPMOSI, PIN_DISPCLK, PIN_DISPCS, 0);
u8g2_Setup_s1d15300_97x32_1(&u8g2r, U8G2_R0, u8x8_byte_4wire_sw_spi,u8x8_arm_linux_gpio_and_delay);
init_spi_sw(&u8g2r, GPIO_CHIP_NUM, PIN_DISPA02, U8X8_PIN_NONE,PIN_DISPMOSI2, PIN_DISPCLK2, PIN_DISPCS2, 0);
u8g2_InitDisplay(&u8g2l);
u8g2_ClearDisplay(&u8g2l); 
u8g2_SetPowerSave(&u8g2l, 0);

  // Get GPIO file descriptors
  user_data_t *user_data_r = u8g2_GetUserPtr(&u8g2r);
  user_data_t *user_data_l = u8g2_GetUserPtr(&u8g2l);
  
  // Copy file descriptors from u8g2 to u8g2l
  if ( (user_data_r != NULL) && (user_data_l != NULL) ){
    for (int i = 0; i < U8X8_PIN_CNT; ++i) {
      if (user_data_l->pins[i] != NULL) {
      user_data_r->pins[i] = user_data_l->pins[i];    // Copy the file descriptor
      }
    }
  }

  // Now, u8g2 and u8g2l share the same file descriptors, so that we don't need to repeatedly close and reopen gpio ports
  // At the end of the program, please be careful with `done_user_data(u8g2)` to avoid double free.
  u8g2_InitDisplay(&u8g2r);
  u8g2_ClearDisplay(&u8g2r); 
  u8g2_SetPowerSave(&u8g2r, 0);

This works for the "C" version, but does not for "CPP". As I need to switch to C++ for my project - could you help me how I could achieve the same with C++?

@karlo922 karlo922 changed the title Copy file descriptors using cpp version of lib not workin Copy file descriptors using cpp version of lib not working Apr 18, 2024
@karlo922
Copy link
Author

I now got it working with just compiling everything u8g2 related as pure C and only doing the rest of my program in C++. So you can still answer if you may help others searching it, or we can close it :)

@wuhanstudio
Copy link
Owner

wuhanstudio commented Apr 21, 2024

Sorry for the late reply. I'm writing my Ph.D. thesis recently.

It's great that the solution worked for the pure C code. The C++ wrapper is automatically generated from the tool codebuild

https://github.com/wuhanstudio/u8g2-arm-linux/tree/master/tools/codebuild

It looks like the C++ code does not work because the variable u8g2_t u8g2 is defined as a protected member. Thus, the reference to the pins cannot be modified from the outside of the class.

class U8G2: public Print {
protected:
	u8g2_t u8g2;
	u8x8_char_cb cpp_next_cb; /*  the cpp interface has its own decoding function for the Arduino print command */

Besides, the u8g2_t is a struct defined in:

https://github.com/wuhanstudio/u8g2-arm-linux/blob/master/csrc/u8g2.h

typedef struct u8g2_struct u8g2_t;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants