c++ - How to vertically scroll an LCD display - Stack Overflow

admin2025-04-17  2

I have a 4 line by 20 character oled (NHD0420cw-ab3) and a structure in c++ that contains 8 different lines to be displayed. Every time a button is pressed I would like the display to move each line up 1 position and the next array in the structure added to the bottom line. After the last line in the structure is placed on the bottom of the display, the next button press places the first array at the bottom, like a circular buffer. I would appreciate any input as to how I should index these lines.

struct menus {
  char line0[21] = {"This is a menu 12351"};
  char line1[21] = {"This is a menu 12352"};
  char line2[21] = {"This is a menu 12353"};
  char line3[21] = {"This is a menu 12354"};
  char line4[21] = {"This is a menu 12355"};
  char line5[21] = {"This is a menu 12356"};
  char line6[21] = {"This is a menu 12357"};
  char line7[21] = {"This is a menu 12358"};
  u_int8_t top_line = 0; // place holder for which line is at the top of the display
} menu;

// Function to update the display  sendString([text to display],[column],[row])
void updateDisplay() {
  LCD.sendString(menu.line0, 0, 0);
  LCD.sendString(menu.line1, 0, 1);
  LCD.sendString(menu.line2, 0, 2);
  LCD.sendString(menu.line3, 0, 3);
}

Is there a way to do something like: LCD.sendString(menu.(line+variable), 0,0) ??

as a test I tried

// Function to scroll the display
void scrollDisplay() 
 {
   menu.top_line++;
   if (menu.top_line > 7) 
   {
    menu.top_line = 0;
   }

  // Calculate the indices of the lines to display
  int line0_index = menu.top_line % 8;
  int line1_index = (menu.top_line + 1) % 8;
  int line2_index = (menu.top_line + 2) % 8;
  int line3_index = (menu.top_line + 3) % 8;

  // Update the display with the new lines
  LCD.sendString(menu.line0 + line0_index, 0, 0);
  LCD.sendString(menu.line1 + line1_index, 0, 1);
  LCD.sendString(menu.line2 + line2_index, 0, 2);
  LCD.sendString(menu.line3 + line3_index, 0, 3);
}

But the line only scrolls to the right not up.

I have a 4 line by 20 character oled (NHD0420cw-ab3) and a structure in c++ that contains 8 different lines to be displayed. Every time a button is pressed I would like the display to move each line up 1 position and the next array in the structure added to the bottom line. After the last line in the structure is placed on the bottom of the display, the next button press places the first array at the bottom, like a circular buffer. I would appreciate any input as to how I should index these lines.

struct menus {
  char line0[21] = {"This is a menu 12351"};
  char line1[21] = {"This is a menu 12352"};
  char line2[21] = {"This is a menu 12353"};
  char line3[21] = {"This is a menu 12354"};
  char line4[21] = {"This is a menu 12355"};
  char line5[21] = {"This is a menu 12356"};
  char line6[21] = {"This is a menu 12357"};
  char line7[21] = {"This is a menu 12358"};
  u_int8_t top_line = 0; // place holder for which line is at the top of the display
} menu;

// Function to update the display  sendString([text to display],[column],[row])
void updateDisplay() {
  LCD.sendString(menu.line0, 0, 0);
  LCD.sendString(menu.line1, 0, 1);
  LCD.sendString(menu.line2, 0, 2);
  LCD.sendString(menu.line3, 0, 3);
}

Is there a way to do something like: LCD.sendString(menu.(line+variable), 0,0) ??

as a test I tried

// Function to scroll the display
void scrollDisplay() 
 {
   menu.top_line++;
   if (menu.top_line > 7) 
   {
    menu.top_line = 0;
   }

  // Calculate the indices of the lines to display
  int line0_index = menu.top_line % 8;
  int line1_index = (menu.top_line + 1) % 8;
  int line2_index = (menu.top_line + 2) % 8;
  int line3_index = (menu.top_line + 3) % 8;

  // Update the display with the new lines
  LCD.sendString(menu.line0 + line0_index, 0, 0);
  LCD.sendString(menu.line1 + line1_index, 0, 1);
  LCD.sendString(menu.line2 + line2_index, 0, 2);
  LCD.sendString(menu.line3 + line3_index, 0, 3);
}

But the line only scrolls to the right not up.

Share Improve this question edited Jan 30 at 19:06 bdrmachine asked Jan 30 at 18:22 bdrmachinebdrmachine 112 bronze badges 2
  • Could you provide the document piece describing just LCD.sendString? Your code looks all wrong, but it doesn't give us enough information. To start with, you need to develop a more reasonable menu object. For example, it could be simply an array of strings. As it is C++ and not C, is it possible that you used std and std::string instead (but this is not a requirement, just convenience)? – Sergey A Kryukov Commented Jan 30 at 18:38
  • Displays like this sometimes don't allow overwriting. You may have to clear the line or clear the entire display and rewrite all four lines. Explain more about what you see when it scrolls right. – Rud48 Commented Jan 31 at 0:28
Add a comment  | 

1 Answer 1

Reset to default 2

instead of using names for each member line0, line1, etc... use an array that you can index into, a 2D array.

struct menus {
  char lines[8][21] ={
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
    {"This is a menu 12351"},
  };
  uint8_t top_line = 0; // place holder for which line is at the top of the display
} menu;

and now you can index which line you want to use.

LCD.sendString(menu.lines[line0_index], 0, 0);
LCD.sendString(menu.lines[line1_index], 0, 1);
LCD.sendString(menu.lines[line2_index], 0, 2);
LCD.sendString(menu.lines[line3_index], 0, 3);
转载请注明原文地址:http://anycun.com/QandA/1744898425a89185.html