wuhanstudio / u8g2-arm-linux

U8g2 for arm linux - a monochrome graphics library
https://github.com/wuhanstudio/u8g2-arm-linux/wiki
Other
51 stars 24 forks source link

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

Open karlo922 opened 7 months ago

karlo922 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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;