0

What I'm trying to achieve is very simple, I'm using ArduinoJson lib to load a JSON from SPIFFS and trying to inject the image values in an Array to feed the small Oled display of the ESP32:

// This is the format the library expects
static const unsigned char image[] U8X8_PROGMEM  = {
0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x17 }

This is the JSON File: https://github.com/martinberlin/esp/blob/master/oled-128x64-xbm/data/1.json

The goal is to be able to generate little thumbnails after uploading an image for example, and display them in this Monochrome small display.

The problem is to convert this Json array into the format the library expects. And I tried many different ways and readed about it, without finding the right way to do it. So I though to share this here as a challenge and see if someone is also interested to get dynamic image thumbnails working in this display.

This is my sketch: (Needs /data upload since json example is there)

https://github.com/martinberlin/esp/tree/master/oled-128x64-xbm

#include "FS.h"
#include "SPIFFS.h"
#include <U8g2lib.h>
#include <ArduinoJson.h>
#include <Wire.h>


/*
  U8glib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.

*/
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);


static const unsigned char image[] U8X8_PROGMEM  = {
  0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x17, 
  0xFC, 0xFF, 0xFF, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 
  0xFF, 0xFF, 0xFF, 0x14, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xF0, 0xFF, 0xFF, 
  0xFF, 0x7F, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x14, 0xFD, 0xFF, 0xFF, 0xFF, 
  0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0x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xFF, 
  0xFF, 0x79, 0x40, 0x18, 0x00, 0x1C, 0x9E, 0xFC, 0xFF, 0x03, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1C, 0x00, 0x0C, 0x00, 0x0C, 0x4E, 0xFE, 
  0xFF, 0x33, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3C, 0x00, 0x06, 
  0x00, 0x0C, 0x77, 0xFF, 0xFF, 0x33, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0x7F, 0x7F, 0x00, 0x23, 0x00, 0xCE, 0xFF, 0xFF, 0xFF, 0x16, 0xF8, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x85, 0x01, 0x00, 0x1E, 0xFE, 0xFF, 
  0x7F, 0x10, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC7, 0x01, 
  0x80, 0x0F, 0xFF, 0xFF, 0x3F, 0x10, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0x67, 0x00, 0xD8, 0x1F, 0xFF, 0xFF, 0x7F, 0x10, 0xFE, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF};

const char* xtemp[1024]; // Temporary array to store JSON incoming Hex as string
const unsigned char* xbm[1024] U8X8_PROGMEM;


void setup(void) {
  Serial.begin(115200);
  // Read xbm from  json
  SPIFFS.begin();

   if (SPIFFS.exists("/1.json")) {
    Serial.println("1.json found in SPIFFS");
    File configFile = SPIFFS.open("/1.json", FILE_READ);
      if (configFile) {
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        //json.printTo(Serial);

        if (json.success()) {          
          Serial.println("json.success");
          JsonArray& arr = json["xbm"];

          // using C++11 syntax (preferred):
          int c=0;

          for (auto value : arr) {
            // Assign the json array to xtemp
            xtemp[c] = value.as<char*>(); 

            //Serial.println(String(c)+":"+value.as<char*>()); // Right value
            c++;      
          }

        } else {
          Serial.println("ERR load config");
        }

        configFile.close();
      }
   }
  u8g2.begin();
  u8g2.setDrawColor(0);
  u8g2.clearBuffer();

  Serial.println("cross_block_bits size:");
  Serial.print(sizeof(image));
  Serial.println(image[1]);

  //xbm = xtemp;

  Serial.println("xbm:");
  Serial.println(xtemp[1]);

 // Should be xbm (coming from json)   xbm
 // u8g2.drawXBM( 0, 0, 128, 64, xtemp); // Does not work since requires  'const uint8_t* {aka const unsigned char*} 
  u8g2.drawXBM( 0, 0, 128, 64, image);

  u8g2.sendBuffer();
}

void loop(void) {

}
  • post the code directly in your question, don't link it. – MichaelT Nov 5 '18 at 9:34
  • Done, thanks! I though that an alternative way to do this would be to make my own loop and draw pixel per pixel, but anyways would be interesting to know if I can read a JSON array and give it to the library in the expected format. – Martin Fasani Nov 5 '18 at 9:59
  • u8g2.drawXBM( 0, 0, 128, 64, (const uint8_t *)xtemp); – Majenko Nov 5 '18 at 10:32
  • 1
    uint8_t val = strtol(stringval, NULL, 16) – Majenko Nov 5 '18 at 11:55
  • 1
    const char* xtemp[1024]; is array of 1024 pointers to char. const char xtemp[1024]; is array of 1024 chars – Juraj Nov 6 '18 at 10:12
0

Thanks and credits to Majenko. Sorted it out, still I don't know why addressing an existing image works but targeting a new array doesn't. Anyways as proof-of-concept that an XBM Icon can be loaded from JSON it works. For anyone wanting to achieve this, here is the code, make sure to upload some JSON to Spiffs first:

/*
  GraphicsTest.ino  
  Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
*/
#include "FS.h"
#include "SPIFFS.h"
#include <U8g2lib.h>
#include <ArduinoJson.h>
#include <Wire.h>


/*
  U8glib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.

*/
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);


static unsigned char image[] U8X8_PROGMEM  = {
  0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x17, 
  0xFC, 0xFF, 0xFF, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 
  0xFF, 0xFF, 0xFF, 0x14, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xF0, 0xFF, 0xFF, 
  0xFF, 0x7F, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x14, 0xFD, 0xFF, 0xFF, 0xFF, 
  0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x8F, 0xFF, 0x10, 
  0x7E, 0xFF, 0xFF, 0xFF, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 
  0xFE, 0x8F, 0xFF, 0x90, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0xFE, 0xFF, 0xFF, 
  0xFF, 0xFF, 0x03, 0x00, 0xFC, 0x0F, 0xFF, 0x90, 0xFF, 0xEE, 0xFF, 0xFF, 
  0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x00, 0xFF, 0x0F, 0xFF, 0x90, 
  0xFF, 0xCC, 0xFF, 0xFF, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 
  0xFF, 0x0F, 0xFF, 0x92, 0xFF, 0xC0, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0x1F, 0x00, 0xFF, 0x1F, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF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 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0x67, 0x00, 0xD8, 0x1F, 0xFF, 0xFF, 0x7F, 0x10, 0xFE, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF};

// Using this new array I could not get it work. Rewriting image works
long unsigned int xbm[1024];


void setup(void) {
  Serial.begin(115200);
  // Read xbm from  json
  SPIFFS.begin();

   if (SPIFFS.exists("/1.json")) {
    Serial.println("1.json found in SPIFFS");
    File configFile = SPIFFS.open("/2.json", FILE_READ);
      if (configFile) {
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        //json.printTo(Serial);

        if (json.success()) {          
          Serial.println("json.success Size of xbm array");

          JsonArray& arr = json["xbm"];

        Serial.print(sizeof(arr));
          // using C++11 syntax (preferred):
          int c=0;
          const char* tempx;
          for (auto value : arr) {
            // Assign the json array to xtemp
            tempx = value.as<char*>();

            image[c] = strtol(tempx, NULL, 16);

              //Serial.print(strtol(tempx, NULL, 16)); //Thx. Majenko
              if (c<10) {
              Serial.print("i:");Serial.print(image[c]);Serial.print(" ");
              }
            c++;      
          }
          Serial.print("Final c count:"+String(c));


        } else {
          Serial.println("ERR loading JSON");
        }

        configFile.close();
      }
   }
  u8g2.begin();
  u8g2.setDrawColor(0);
  u8g2.clearBuffer();

  Serial.print("image size:");
  Serial.print(sizeof(image));Serial.println();  // Here is 4096 also 4 times bigger, if I use a new array (WHY?)
  // Send the JSON array to oled display
  u8g2.drawXBM( 0, 0, 128, 64, (const uint8_t *)image);
  u8g2.sendBuffer();
}

void loop(void) {

}

XBM picture from Json: Proof of concept

  • why long unsigned int xbm[1024]; and not byte xbm[1024];, which would be the same as new char[size] for the existing file? – Juraj Nov 6 '18 at 10:14
  • Yes that will work. But the issue is that I'm not even using this new one, but filling the already declared image. Using the new one, at the end of the 1024 pixels loop, you end up having 4x more (4096) and I still don't know why. – Martin Fasani Nov 7 '18 at 7:38
  • Also it sounds to me that using this: long unsigned int xbm[1024] It's reading somehow the 4 chars from every pixel ( 0x00 ) but that would be impossible since we are transforming this HEX into an integer. But well cool thing is that it works just overwriting a previous image – Martin Fasani Nov 7 '18 at 8:05
  • a hex number with two hex digits fits into one byte. FF is in binary 11111111. 0F hex is 00001111 binary. that is way hex is used – Juraj Nov 8 '18 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.