ST7565 gLCD bargraph?

I’d like to have a bargraph function for the ST7565 library (provided by adafruit!) they have rect() & fillrect() functions but how can one modify them to make a bargraph() function? both horizontal & vertical bars would be useful for others in the community!

// fill rectangle
void ST7565::fillrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, 
		      uint8_t color) {

  // stupidest version - just pixels - but fast with internal buffer!
  for (uint8_t i=x; i<x+w; i++) {
    for (uint8_t j=y; j<y+h; j++) {
      my_setpixel(i, j, color);
    }
  }

  updateBoundingBox(x, y, x+w, y+h);
}

// draw a rectangle
void ST7565::drawrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, 
		      uint8_t color) {
  // stupidest version - just pixels - but fast with internal buffer!
  for (uint8_t i=x; i<x+w; i++) {
    my_setpixel(i, y, color);
    my_setpixel(i, y+h-1, color);
  }
  for (uint8_t i=y; i<y+h; i++) {
    my_setpixel(x, i, color);
    my_setpixel(x+w-1, i, color);
  } 

  updateBoundingBox(x, y, x+w, y+h);
}

something like this…

void ST7565::Hbargraph(…) {…} //horizontal bargraph

void ST7565::Vbargraph(…) {…}//vertical bargraph

they have rect() & fillrect() functions but how can one modify them to make a bargraph() function?

Why would you want to modify them? Use them, yes, but not modify them.

Work on one graph direction at a time. When one works, changing the direction is easy.

Define [u]what[/u] you want to have happen when you call the Vbargraph() function. Don't worry about [u]how[/u] to do it. When you can describe what, how just might be trivial.

I added the bargraph function to the ST7565 library same as fillrect() function and as figured i can only get the bar to go up.
what needs to change to make the bar go up and down?

void ST7565::bargraph(uint8_t x, uint8_t y, uint8_t w, uint8_t h, 
		      uint8_t color) {

  // stupidest version - just pixels - but fast with internal buffer!
  for (uint8_t i=x; i<x+w; i++) {
    for (uint8_t j=y; j<y+h; j++) {
      my_setpixel(i, j, color);
    }
  }
   

  updateBoundingBox(x, y, w+x, y+h);
}

I added the bargraph function to the ST7565 library same as fillrect() function

I wouldn't have done it this way. There is already a function to create a rectangle, and a function to create a filled rectangle. You should build your bar graph using those.

A bar graph has two meaning. One consists of a series of different bars (filled rectangles) adjacent to each other, like a pie chart, only square.

The other has a a filled rectangle and an empty rectangle, end to end. The total size of the two rectangles does not change, but the proportion of the filled rectangle to empty rectangle does change.

If it is the second case that you are trying to draw, What you need to do is draw a filled rectangle of one color that is the total size of the bar graph. Say you want the bar to be 200 pixels by 50 pixels. You draw a white rectangle, empty, that is 202 by 52. Then, you draw two rectangles, filled, inside that rectangle, x by 50 and y by 50. The first rectangle is red, offset by 1 in each direction. The second is white (or whatever your empty rectangle color is, offset by 1 in one direction and x+1 in the other direction.

Suppose the filled bar is to be 75 pixels long, and the bars are in the horizontal orientation. Draw a white rectangle, empty, from 0, 0 to 202, 52. Draw a red rectangle from 1,1 to 76, 51. Draw a white rectangle from 76, 1 to 201, 51.

If the filled bar length changes to a larger value, it is only necessary, then, to redraw the red rectangle. If the filled bar length changes to a smaller rectangle, it is only necessary to redraw the white rectangle.

Of course, two of the three rectangles do not need to be the same color, but you do need three of them.