This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:esp32:emb7_1 [2024/04/05 14:44] – [Steps] pczekalski | en:iot-open:practical:hardware:sut:esp32:emb7_1 [2024/06/29 18:54] (current) – [Prerequisites] pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== EMB7: Using OLED display ===== | ||
| + | This scenario presents how to use the OLED display. Our OLED display is an RGB (16bit colour, 64k colours) 1.5in, 128x128 pixels. The OLED chip is SSD1351, and it is controlled over the SPI interface using the following pin configuration: | ||
| + | * SPI_MOSI=15, | ||
| + | * SPI_CLK=18, | ||
| + | * SPI_DC=13, | ||
| + | * SPI_CS=11, | ||
| + | * SPI_RST=12. | ||
| + | ===== Prerequisites ===== | ||
| + | As usual, there is no need to program SPI directly; instead, it should be handled by a dedicated library. In addition to the protocol communication library and display library, we will use a graphic abstraction layer for drawing primitives such as lines, images, text, circles, and so on: | ||
| + | <code ini> | ||
| + | lib_deps = adafruit/ | ||
| + | </ | ||
| + | Note that the graphics abstraction library (Adafruit GFX) is loaded automatically because of the | ||
| + | <code ini> | ||
| + | <code ini> | ||
| + | lib_deps = | ||
| + | adafruit/ | ||
| + | adafruit/ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | To generate an array of bytes representing an image in 565 format, it is easiest to use an online tool, e.g.: | ||
| + | * [[https:// | ||
| + | <note important> | ||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | |||
| + | ==== Task to be implemented ==== | ||
| + | Draw a text on the OLED display and an image of your choice (small, to fit both text and image). | ||
| + | |||
| + | ==== Start ==== | ||
| + | Perhaps you will need to use an external tool to preprocess an image to the desired size (we suggest something no bigger than 100x100 pixels) and another tool (see hint above) to convert an image to an array of bytes. | ||
| + | <note important> | ||
| + | |||
| + | Check if you can see a full OLED Display in your video stream. Book a device and create a dummy Arduino file with '' | ||
| + | |||
| + | Prepare a small bitmap and convert it to the byte array for 16-bit colour settings.\\ | ||
| + | Sample project favicon you can use is present in Figure {{ref> | ||
| + | <figure iotopenfavicon> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | ==== Steps ==== | ||
| + | Remember to include the source array in the code when drawing an image. | ||
| + | The corresponding generated C array for the logo in Figure {{ref> | ||
| + | <code c> | ||
| + | const uint16_t epd_bitmap_logo_60 [] PROGMEM = { | ||
| + | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | ||
| + | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf7be, 0xbdd7, 0x8430, 0x5aeb, 0x39c7, 0x2104, 0x1082, 0x0020, 0x0020, 0x1082, | ||
| + | 0x2104, 0x39c7, 0x5aeb, 0x8430, 0xbdd7, 0xf7be, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | ||
| + | .... | ||
| + | |||
| + | .... | ||
| + | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 | ||
| + | }; | ||
| + | |||
| + | // Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 3616) | ||
| + | const int epd_bitmap_allArray_LEN = 1; | ||
| + | const uint16_t* epd_bitmap_allArray[1] = { | ||
| + | epd_bitmap_logo_60 | ||
| + | }; | ||
| + | </ | ||
| + | === Step 1 === | ||
| + | Include necessary libraries: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | //Fonts | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | The code above also includes a font to draw text on the OLED Display. There are many fonts one can use, and a non-exhaustive list is present below (files are located in the '' | ||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === Step 2 === | ||
| + | Add declarations for GPIOs, colours (to ease programming and use names instead of hexadecimal values) and screen height and width. To recall, the OLED display in our lab is square: 128x128 pixels, 16k colours (16-bit 565: RRRRRGGGGGGBBBBB colour model): | ||
| + | <code c> | ||
| + | //Test configuration of the SPI | ||
| + | #define OLED_SPI_MOSI_PIN 15 //DIN | ||
| + | #define OLED_SPI_SCLK_PIN 18 //CLK | ||
| + | #define OLED_SPI_CS_PIN 11 | ||
| + | #define OLED_SPI_DC_PIN 13 | ||
| + | #define OLED_SPI_RST_PIN 12 | ||
| + | #define SCREEN_WIDTH 128 | ||
| + | #define SCREEN_HEIGHT 128 | ||
| + | |||
| + | // Color definitions | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | #define CYAN 0x07FF | ||
| + | #define MAGENTA | ||
| + | #define YELLOW | ||
| + | #define WHITE | ||
| + | </ | ||
| + | |||
| + | === Step 3 === | ||
| + | Declare an SPI communication and OLED controller objects: | ||
| + | <code c> | ||
| + | static SPIClass hspi(HSPI); | ||
| + | static Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, | ||
| + | </ | ||
| + | |||
| + | === Step 4 === | ||
| + | Initialise the SPI communication object and the OLED controller object. Then clear the screen (write all black): | ||
| + | <code c> | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === Step 5 === | ||
| + | Draw a bitmap around the centre part of the screen (screen is 128x128px); please mind that '' | ||
| + | <code c> | ||
| + | tft.drawRGBBitmap(48, | ||
| + | </ | ||
| + | === Step 6 === | ||
| + | Drop some additional text on the screen: | ||
| + | <code c> | ||
| + | tft.setFont(& | ||
| + | tft.setTextSize(1); | ||
| + | tft.setTextColor(WHITE); | ||
| + | tft.setCursor(0, | ||
| + | tft.println(" | ||
| + | </ | ||
| + | Some remarks regarding coordinates: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | <note tip>To speed up screen updating and avoid flickering, you may use a trick to clear the afore-written text: instead of clearing the whole or partial screen, write the same text in the same location but in the background colour.</ | ||
| + | |||
| + | <note tip> | ||
| + | |||
| + | Besides the functions presented above, the controller class has several other handy functions (among others): | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | ==== Result validation ==== | ||
| + | You should see the image and the text in the video stream. | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | **The screen is black even if I write to it. What to do?**: Check if you have initialised an SPI communication object and pulled the "chip select" | ||
| + | |||
| + | |||
| + | <WRAP noprint> | ||
| + | ===== Project information ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | </ | ||