Skip to content
Snippets Groups Projects
Select Git revision
  • b7de9736dc8a91e09c98f3759b95bf167c93ccf9
  • main default protected
  • 1.1.0
  • 1.0
4 results

helpers.h

Blame
  • helpers.h 898 B
    #ifndef Helpers_h
    #define Helpers_h
    
    #include "Adafruit_SH110X.h"
    #include "Adafruit_GFX.h"
    extern Adafruit_SH1106G display;
    
    int centeredText(const char *buf, int x, int y, int color, int lineheight=8) {
      int16_t x1, y1;
      uint16_t w, h;
      char *line_pointer = strchr(buf, '\n');
      display.setTextColor(color);
      if (!line_pointer) {
        display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
        display.setCursor(x - (w / 2), y - (h / 2));
        display.print(buf);
      }else {
        char *tmp = strdup(buf);
        char* d = strtok(tmp, "\n");
        int line = 0;
        while (d != NULL) {
            display.getTextBounds(d, 0, 0, &x1, &y1, &w, &h); //calc width of new string
            display.setCursor(x - (w / 2), y - (h / 2)-lineheight/2 + (line*lineheight));
            display.print(d);
            d = strtok(NULL, ",");
            line++;
        }
        free(tmp);
      }
      return w;
    }
    
    
    
    #endif