2016-01-09 00:58:32 +01:00
# ucg Module
2016-03-05 10:47:01 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2015-08-05 | [Oli Kraus ](https://github.com/olikraus/ucglib ), [Arnim Läuger ](https://github.com/devsaurus ) | [Arnim Läuger ](https://github.com/devsaurus ) | [ucglib ](../../app/ucglib/ )|
2016-03-05 10:47:01 +01:00
2018-10-19 22:18:50 +02:00
Ucglib is a graphics library developed at [olikraus/ucglib ](https://github.com/olikraus/ucglib ) with support for color TFT displays.
2016-01-09 00:58:32 +01:00
2018-10-19 22:18:50 +02:00
!!! note "BSD License for Ucglib Code"
Universal 8bit Graphics Library (http://code.google.com/p/u8glib/)
Copyright (c) 2014, olikraus@gmail.com
All rights reserved.
2019-02-17 19:26:29 +01:00
Redistribution and use in source and binary forms, with or without modification,
2018-10-19 22:18:50 +02:00
are permitted provided that the following conditions are met:
2019-02-17 19:26:29 +01:00
* Redistributions of source code must retain the above copyright notice, this list
2018-10-19 22:18:50 +02:00
of conditions and the following disclaimer.
2019-02-17 19:26:29 +01:00
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
2018-10-19 22:18:50 +02:00
materials provided with the distribution.
2019-02-17 19:26:29 +01:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2018-10-19 22:18:50 +02:00
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The NodeMCU firmware supports a subset of these:
- HX8352C
2016-01-09 00:58:32 +01:00
- ILI9163
- ILI9341
2018-10-19 22:18:50 +02:00
- ILI9486
2016-01-09 00:58:32 +01:00
- PCF8833
- SEPS225
- SSD1331
- SSD1351
- ST7735
2018-12-28 23:33:26 +01:00
This integration is based on version [1.5.2 ](https://github.com/olikraus/Ucglib_Arduino/releases/tag/1.5.2 ).
2016-01-09 00:58:32 +01:00
## Overview
### SPI Connection
2018-12-28 23:33:26 +01:00
The HSPI module is used ([more information](https://web.archive.org/web/20180425221055/http://d.av.id.au:80/blog/esp8266-hardware-spi-hspi-general-info-and-pinout/)), so certain pins are fixed:
2016-01-09 00:58:32 +01:00
* HSPI CLK = GPIO14
* HSPI MOSI = GPIO13
* HSPI MISO = GPIO12 (not used)
All other pins can be assigned to any available GPIO:
* CS
* D/C
* RES (optional for some displays)
2016-05-01 15:42:20 +02:00
Also refer to the initialization sequence eg in [GraphicsTest.lua ](https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/ucglib/GraphicsTest.lua ):
2016-01-09 00:58:32 +01:00
```lua
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
```
### Library Usage
The Lua bindings for this library closely follow ucglib's object oriented C++ API. Based on the ucg class, you create an object for your display type.
ILI9341 via SPI:
```lua
cs = 8 -- GPIO15, pull-down 10k to GND
dc = 4 -- GPIO2
res = 0 -- GPIO16, RES is optional YMMV
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
```
This object provides all of ucglib's methods to control the display.
Again, refer to [GraphicsTest.lua ](https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/ucglib/GraphicsTest.lua ) to get an impression how this is achieved with Lua code. Visit the [ucglib homepage ](https://github.com/olikraus/ucglib ) for technical details.
2018-10-19 22:18:50 +02:00
### Display selection
HW SPI based displays with support in u8g2 can be enabled.
The procedure is different for ESP8266 and ESP32 platforms.
#### ESP8266
2019-01-13 22:01:57 +01:00
Add the desired entries to the display table in [app/include/ucg_config.h ](../../app/include/ucg_config.h ):
2016-01-09 00:58:32 +01:00
```c
#define UCG_DISPLAY_TABLE \
UCG_DISPLAY_TABLE_ENTRY(ili9341_18x240x320_hw_spi, ucg_dev_ili9341_18x240x320, ucg_ext_ili9341_18) \
UCG_DISPLAY_TABLE_ENTRY(st7735_18x128x160_hw_spi, ucg_dev_st7735_18x128x160, ucg_ext_st7735_18) \
```
2018-10-19 22:18:50 +02:00
#### ESP32
Enable the desired entries for SPI displays in ucg's sub-menu (run `make menuconfig` ).
2016-01-09 00:58:32 +01:00
### Fonts
2018-10-19 22:18:50 +02:00
ucglib comes with a wide range of fonts for small displays. Since they need to be compiled into the firmware image.
The procedure is different for ESP8266 and ESP32 platforms.
#### ESP8266
2019-01-13 22:01:57 +01:00
Add the desired fonts to the font table in [app/include/ucg_config.h ](../../app/include/ucg_config.h ):
2016-01-09 00:58:32 +01:00
```c
#define UCG_FONT_TABLE \
UCG_FONT_TABLE_ENTRY(font_7x13B_tr) \
UCG_FONT_TABLE_ENTRY(font_helvB12_hr) \
UCG_FONT_TABLE_ENTRY(font_helvB18_hr) \
UCG_FONT_TABLE_ENTRY(font_ncenR12_tr) \
UCG_FONT_TABLE_ENTRY(font_ncenR14_hr)
```
2018-10-19 22:18:50 +02:00
They will be available as `ucg.<font_name>` in Lua.
#### ESP32
Add the desired fonts to the font selection sub-entry via `make menuconfig` .
2016-01-09 00:58:32 +01:00
## Display Drivers
Initialize a display via Hardware SPI.
2018-10-19 22:18:50 +02:00
- `hx8352c_18x240x400_hw_spi()`
2016-01-09 00:58:32 +01:00
- `ili9163_18x128x128_hw_spi()`
- `ili9341_18x240x320_hw_spi()`
2018-10-19 22:18:50 +02:00
- `ili9486_18x320x480_hw_spi()`
2016-01-09 00:58:32 +01:00
- `pcf8833_16x132x132_hw_spi()`
2016-03-25 23:49:22 +01:00
- `seps225_16x128x128_uvis_hw_spi()`
2016-01-09 00:58:32 +01:00
- `ssd1351_18x128x128_hw_spi()`
- `ssd1351_18x128x128_ft_hw_spi()`
2016-03-25 23:49:22 +01:00
- `ssd1331_18x96x64_uvis_hw_spi()`
2016-01-09 00:58:32 +01:00
- `st7735_18x128x160_hw_spi()`
#### Syntax
2018-10-19 22:18:50 +02:00
`ucg.st7735_18x128x160_hw_spi(bus, cs, dc[, res])`
2016-01-09 00:58:32 +01:00
#### Parameters
2018-10-19 22:18:50 +02:00
- `bus` SPI master bus
2016-01-09 12:37:56 +01:00
- `cs` GPIO pin for /CS
- `dc` GPIO pin for DC
2018-10-19 22:18:50 +02:00
- `res` GPIO pin for /RES, none if omitted
2016-01-09 00:58:32 +01:00
#### Returns
2016-01-09 01:10:27 +01:00
ucg display object
2016-01-09 00:58:32 +01:00
2018-10-19 22:18:50 +02:00
#### Example for ESP8266
2016-01-09 00:58:32 +01:00
```lua
2018-10-19 22:18:50 +02:00
-- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used)
-- Hardware SPI /CS = GPIO15 (not used)
2016-01-09 00:58:32 +01:00
cs = 8 -- GPIO15, pull-down 10k to GND
dc = 4 -- GPIO2
res = 0 -- GPIO16, RES is optional YMMV
2018-10-19 22:18:50 +02:00
bus = 1
spi.setup(bus, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
-- we won't be using the HSPI /CS line, so disable it again
gpio.mode(8, gpio.INPUT, gpio.PULLUP)
disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res)
```
#### Example for ESP32
```lua
sclk = 19
mosi = 23
cs = 22
dc = 16
res = 17
bus = spi.master(spi.HSPI, {sclk=sclk, mosi=mosi})
disp = ucg.st7735_18x128x160_hw_spi(bus, cs, dc, res)
2016-01-09 00:58:32 +01:00
```
## Constants
2016-01-16 13:18:31 +01:00
Constants for various functions.
2016-01-09 00:58:32 +01:00
`ucg.FONT_MODE_TRANSPARENT` , `ucg.FONT_MODE_SOLID` , `ucg.DRAW_UPPER_RIGHT` ,
`ucg.DRAW_UPPER_LEFT` , `ucg.DRAW_LOWER_RIGHT` , `ucg.DRAW_LOWER_LEFT` , `ucg.DRAW_ALL`
`ucg.font_7x13B_tr` , ...
# ucg.disp Sub-Module
2016-01-16 13:15:14 +01:00
## ucg.disp:begin()
See [ucglib begin() ](https://github.com/olikraus/ucglib/wiki/reference#begin ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:clearScreen()
See [ucglib clearScreen() ](https://github.com/olikraus/ucglib/wiki/reference#clearscreen ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:draw90Line()
See [ucglib draw90Line() ](https://github.com/olikraus/ucglib/wiki/reference#draw90line ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawBox()
See [ucglib drawBox() ](https://github.com/olikraus/ucglib/wiki/reference#drawbox ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawCircle()
See [ucglib drawCircle() ](https://github.com/olikraus/ucglib/wiki/reference#drawcircle ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawDisc()
See [ucglib drawDisc() ](https://github.com/olikraus/ucglib/wiki/reference#drawdisc ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawFrame()
See [ucglib drawFrame() ](https://github.com/olikraus/ucglib/wiki/reference#drawframe ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawGlyph()
See [ucglib drawGlyph() ](https://github.com/olikraus/ucglib/wiki/reference#drawglyph ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawGradientBox()
See [ucglib drawGradientBox() ](https://github.com/olikraus/ucglib/wiki/reference#drawgradientbox ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawGradientLine()
See [ucglib drawGradientLine() ](https://github.com/olikraus/ucglib/wiki/reference#drawgradientline ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawHLine()
See [ucglib drawHLine() ](https://github.com/olikraus/ucglib/wiki/reference#drawhline ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawLine()
See [ucglib drawLine() ](https://github.com/olikraus/ucglib/wiki/reference#drawline ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawPixel()
See [ucglib drawPixel() ](https://github.com/olikraus/ucglib/wiki/reference#drawpixel ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawRBox()
See [ucglib drawRBox() ](https://github.com/olikraus/ucglib/wiki/reference#drawrbox ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawRFrame()
See [ucglib drawRFrame() ](https://github.com/olikraus/ucglib/wiki/reference#drawrframe ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawString()
See [ucglib drawString() ](https://github.com/olikraus/ucglib/wiki/reference#drawstring ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawTetragon()
See [ucglib drawTetragon() ](https://github.com/olikraus/ucglib/wiki/reference#drawtetragon ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawTriangle()
See [ucglib drawTriangle() ](https://github.com/olikraus/ucglib/wiki/reference#drawrtiangle ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:drawVLine()
See [ucglib drawVline() ](https://github.com/olikraus/ucglib/wiki/reference#drawvline ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:getFontAscent()
See [ucglib getFontAscent() ](https://github.com/olikraus/ucglib/wiki/reference#getfontascent ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:getFontDescent()
See [ucglib getFontDescent() ](https://github.com/olikraus/ucglib/wiki/reference#getfontdescent ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:getHeight()
See [ucglib getHeight() ](https://github.com/olikraus/ucglib/wiki/reference#getheight ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:getStrWidth()
See [ucglib getStrWidth() ](https://github.com/olikraus/ucglib/wiki/reference#getstrwidth ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:getWidth()
See [ucglib getWidth() ](https://github.com/olikraus/ucglib/wiki/reference#getwidth ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:print()
See [ucglib print() ](https://github.com/olikraus/ucglib/wiki/reference#print ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setClipRange()
See [ucglib setClipRange() ](https://github.com/olikraus/ucglib/wiki/reference#setcliprange ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setColor()
See [ucglib setColor() ](https://github.com/olikraus/ucglib/wiki/reference#setcolor ).
2016-01-09 00:58:32 +01:00
## ucg.disp:setFont()
2018-10-19 22:18:50 +02:00
Define a ucg font for the glyph and string drawing functions. They are available as `ucg.<font_name>` in Lua.
2016-01-09 00:58:32 +01:00
#### Syntax
`disp:setFont(font)`
#### Parameters
2016-01-09 12:37:56 +01:00
`font` constant to identify pre-compiled font
2016-01-09 00:58:32 +01:00
#### Returns
`nil`
#### Example
```lua
disp:setFont(ucg.font_7x13B_tr)
```
#### See also
2016-01-09 01:10:27 +01:00
[ucglib setFont() ](https://github.com/olikraus/ucglib/wiki/reference#setfont )
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setFontMode()
See [ucglib setFontMode() ](https://github.com/olikraus/ucglib/wiki/reference#setfontmode ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setFontPosBaseline()
See [ucglib setFontPosBaseline() ](https://github.com/olikraus/ucglib/wiki/reference#setfontposbaseline ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setFontPosBottom()
See [ucglib setFontPosBottom() ](https://github.com/olikraus/ucglib/wiki/reference#setfontposbottom ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setFontPosCenter()
See [ucglib setFontPosCenter() ](https://github.com/olikraus/ucglib/wiki/reference#setfontposcenter ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setFontPosTop()
See [ucglib setFontPosTop() ](https://github.com/olikraus/ucglib/wiki/reference#setfontpostop ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setMaxClipRange()
See [ucglib setMaxClipRange() ](https://github.com/olikraus/ucglib/wiki/reference#setmaxcliprange ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setPrintDir()
See [ucglib setPrintDir() ](https://github.com/olikraus/ucglib/wiki/reference#setprintdir ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setPrintPos()
See [ucglib setPrintPos() ](https://github.com/olikraus/ucglib/wiki/reference#setprintpos ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setRotate90()
See [ucglib setRotate90() ](https://github.com/olikraus/ucglib/wiki/reference#setrotate90 ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setRotate180()
See [ucglib setRotate180() ](https://github.com/olikraus/ucglib/wiki/reference#setrotate180 ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setRotate270()
See [ucglib setRotate270() ](https://github.com/olikraus/ucglib/wiki/reference#setrotate270 ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:setScale2x2()
See [ucglib setScale2x2() ](https://github.com/olikraus/ucglib/wiki/reference#setscale2x2 ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:undoClipRange()
See [ucglib undoClipRange() ](https://github.com/olikraus/ucglib/wiki/reference#undocliprange ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:undoRotate()
See [ucglib undoRotate() ](https://github.com/olikraus/ucglib/wiki/reference#undorotate ).
2016-01-09 00:58:32 +01:00
2016-01-16 13:15:14 +01:00
## ucg.disp:undoScale()
See [ucglib undoScale() ](https://github.com/olikraus/ucglib/wiki/reference#undoscale ).