Adafruit SSD1306 Library drawBitMap() problem

Hallo Forum,

ich benutze die OLED library und hab mehrere Bitmaps die ich ausgeben will.

Aber wie ich es auch versuche ich kriege keine Variable in die Funktion drawBitMap().

void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);

Meine Versuche sehen so aus

  String tempS;
  unsigned char* tempC;
  tempS +=tag;
  tempS +=bild;
  tempC = tempS.c_str();
  display.clearDisplay();
  display.drawBitmap(96,0,tempC,32,32,1);
  display.display();

tag und bild bilden ein String der z.B. so aussieht "day116" und ich hab mehrere bitmaps im header die auf die anspringen sollen. Es soll aber auch day176 ausgegeben werden können.

Danke im voraus!:smiley:

Bitte genauer beschreiben bevor ich anfange zu raten. Was sind die genauen Datentypen und Formate von "tag" und "bild"? z.B. String Literal und Integer.

bild und day sind einfache Strings die ich davor aus einer Json datei ziehe. Ich möchte dem jetzigem Wetter entsprechende Icons auf einem OLED Display anzeigen lassen. Ich habe alle möglichen Icons runtergeladen und in einer header datei gespeichert. Nun soll aus der kombination von bild und day das richtige Icon rausgesucht und ausgegeben werden. Ich bin alle Icons hardcoded durchgegangen und sie funktionieren.

z.B.:

char* getStr(const char* str1, const char* str2)
{
  static char buffer[13];    //muss groß genug für den längsten Namen + 1 sein
  buffer[0] = '\0';
  strlcat(buffer, str1, sizeof(buffer));
  strlcat(buffer, str2, sizeof(buffer));

  return buffer;  //Achtung! Das geht nur weil buffer static ist! Niemals Zeiger auf lokale nicht-statische Variablen zurückgeben
}

void setup()
{
  Serial.begin(9600);

  Serial.println(getStr("day", "1"));
  Serial.println(getStr("test", "100"));
  Serial.println(getStr("blah", "176"));
}

void loop()
{
}

Ansonsten zu deinem Code: c_str() liefert einen const Zeiger. Wenn man den speichern will (was hier gar nicht nötig ist) ist der korrekte Datentyp const char*

Ich nehme mal an die Buffergröße ist Zeichen*2+1 also wäre sie bei night??? ja bei 17.

Und da wie erwähnt bild und tag Strings sind gefällt das dem compiler nicht wirklich:

cannot convert 'String' to 'const char*' for argument '1' to 'char* getStr(const char*, const char*)'

Strings und C Strings sind zwei verschiedene Sachen. Ich verwende Strings generell nicht.

Wenn es unbedingt die String Klasse sein muss dann solltest du keine Probleme mit + und c_str() haben. Der Zeiger von c_str() muss wie gesagt nicht gespeichert werden.

Entschuldige das ich nochmal nachfrage aber einen mit deiner Kompetenz lass ich mir nicht entgehen :smiley:

A:\Program Files (x86)\Arduino\libraries\Adafruit-GFX-Library-master/Adafruit_GFX.h:71:5: note: void Adafruit_GFX::drawBitmap(int16_t, int16_t, uint8_t*, int16_t, int16_t, uint16_t) <near match>

     drawBitmap(int16_t x, int16_t y, uint8_t *bitmap,

     ^

A:\Program Files (x86)\Arduino\libraries\Adafruit-GFX-Library-master/Adafruit_GFX.h:71:5: note:   no known conversion for argument 3 from 'char*' to 'uint8_t* {aka unsigned char*}'

exit status 1
call of overloaded 'drawBitmap(int, int, char*, int, int, int)' is ambiguous

Ich habe mehr aus dem Fehler entnommen da Sie evtl was damit anfangen können.

Jetzige Code

char* getStr(const char* str1, const char* str2)
{
  static char buffer[17];    //muss groß genug für den längsten Namen + 1 sein
  buffer[0] = '\0';
  strlcat(buffer, str1, sizeof(buffer));
  strlcat(buffer, str2, sizeof(buffer));

  return buffer;  //Achtung! Das geht nur weil buffer static ist! Niemals Zeiger auf lokale nicht-statische Variablen zurückgeben
}
void iconMan()
{
  
  display.clearDisplay();
  display.drawBitmap(96,0,getStr(tag.c_str(),bild.c_str()),32,32,1);
  display.display();
}

Du bist generell auf dem falschen Weg. Das fällt mir leider erst jetzt auf. Die Funktion will nicht den Namen der Bitmap als String, sondern den Namen des Arrays. Der Datentyp ist uint8_t*. Damit ist ein Array aus Bytes gemeint.

Also wenn du sowas hast:

uint8_t bild[] PROGMEM ....

Dann kannst du bild übergeben, aber nicht "bild"

Jaja sowas habe ich sogar sehr viele :smiley:

const unsigned char day113 [] PROGMEM = {
0x00, 0x01, 0x80, 0x00, 0x01, 0x81, 0x81, 0x80, 0x01, 0x81, 0x81, 0x80, 0x00, 0xc0, 0x03, 0x00, 
0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x60, 0x1f, 0xf8, 0x06, 
0x78, 0x3f, 0xfc, 0x1e, 0x18, 0x7f, 0xfe, 0x18, 0x00, 0xff, 0xff, 0x00, 0x01, 0xff, 0xff, 0x00, 
0x01, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0x80, 0xe3, 0xff, 0xff, 0xc7, 
0xe3, 0xff, 0xff, 0xc7, 0x03, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0x80, 
0x01, 0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0x00, 0x18, 0x7f, 0xfe, 0x18, 0x78, 0x3f, 0xfc, 0x1e, 
0x60, 0x1f, 0xf8, 0x06, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 
0x00, 0xc0, 0x03, 0x00, 0x01, 0x81, 0x81, 0x80, 0x01, 0x81, 0x81, 0x80, 0x00, 0x01, 0x80, 0x00,
};
const unsigned char day116 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 
0x00, 0x00, 0x08, 0x00, 0x08, 0x7c, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xc0, 
0x01, 0xff, 0xff, 0xc0, 0x11, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xe0, 
0x00, 0xff, 0xff, 0xf0, 0x09, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 
0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char day119 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 
0x00, 0x00, 0x08, 0x00, 0x08, 0x7c, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xc0, 
0x01, 0xff, 0xff, 0xc0, 0x11, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xe0, 
0x00, 0xff, 0xff, 0xf0, 0x09, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 
0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char day122 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 
0x00, 0x00, 0x08, 0x00, 0x08, 0x7c, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xc0, 
0x01, 0xff, 0xff, 0xc0, 0x11, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xe0, 
0x00, 0xff, 0xff, 0xf0, 0x09, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 
0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char day143 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 
0x00, 0x00, 0x08, 0x00, 0x08, 0x7c, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xc0, 
0x01, 0xff, 0xff, 0xc0, 0x11, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xe0, 
0x00, 0xff, 0xff, 0xf0, 0x09, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 
0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char day176 [] PROGMEM = {

};
const unsigned char day320 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 
0x00, 0x00, 0x08, 0x00, 0x08, 0x7c, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xc0, 
0x01, 0xff, 0xff, 0xc0, 0x11, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xe0, 
0x00, 0xff, 0xff, 0xf0, 0x09, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf8, 
0x01, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

...................................
};

und die beiden Strings sollen halt sagen welches Bild angezeigt werden soll

Genau das geht aber nicht. Ein Variablenname ist kein String

Also sagst du mein Vorhaben ist unmöglich :(.

Oder ich muss es halt dreckig schreiben mit String temp; temp+=day; temp+=bild; if(temp=="day116"){display116();} und das 96 mal :smiley:

Geht auch einfacher. Die Bitmaps in ein Array (Also ein zwei-dimensionales Array statt alle einzeln). Dann ein Array aus Strings mit den Namen. Und das am besten auch gleich auch ins PROGMEM, damit man nicht hunderte Bytes RAM damit verschwendet. Dann kann man über das String Array iterieren, vergleichen und darüber den Index des Bildes bekommen.

Oder benenne das so dass die Zahlen direkt dem Index entsprechen. Dann muss man gar nichts umsetzen.

Weil ich nur die hälfte von dem verstehe, heißt das für mich übersetzt wohl dreckig schreiben :slight_smile:

Besser: Lerne es zu verstehen.

Gruß Tommy

Er meint sowas in der Art:
Du hast Deine ganzen Bilder als char-Arrays eingespeichert, und legst dann ein weiteres Array an, welches sozusagen Deine Bilder beinhaltet:

const unsigned char *day[]={day113, day116, day119, day122, day143, day176, day320};

zur Anzeige kannst Du dann z.B. das hier machen:

for(int i=0; i<=6; i++){
  display.clearDisplay();                                                                  //Display leeren
  display.drawBitmap(96, 0,  day[i], 32, 32, WHITE);                     //Bild zeichnen
  display.display();                                                                         //Bild anzeigen
  delay(1000);                                                                               //Warten
}

(jetzt mal grob auf die Schnelle zusammengefrickelt ;))