feat: 全量同步 254 个常用的 Arduino 扩展库文件
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
|
||||
============ BSD License for U8g2 for Adafruit GFX Code ============
|
||||
BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2018, olikraus
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
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 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
============ X11 Fonts COUR, HELV, NCEN, TIM, SYMB ============
|
||||
|
||||
For fonts derived from the following files, the license below applies.
|
||||
COURB08.BDF COURB10.BDF COURB12.BDF COURB14.BDF COURB18.BDF
|
||||
COURB24.BDF COURR08.BDF COURR10.BDF COURR12.BDF COURR14.BDF
|
||||
COURR18.BDF COURR24.BDF HELVB08.BDF HELVB10.BDF HELVB12.BDF HELVB14.BDF
|
||||
HELVB18.BDF HELVB24.BDF HELVR08.BDF HELVR10.BDF HELVR12.BDF HELVR14.BDF
|
||||
HELVR18.BDF HELVR24.BDF NCENB08.BDF NCENB10.BDF NCENB12.BDF
|
||||
NCENB14.BDF NCENB18.BDF NCENB24.BDF NCENR08.BDF NCENR10.BDF
|
||||
NCENR12.BDF NCENR14.BDF NCENR18.BDF NCENR24.BDF SYMB08.BDF SYMB10.BDF
|
||||
SYMB12.BDF SYMB14.BDF SYMB18.BDF SYMB24.BDF TIMB08.BDF TIMB10.BDF
|
||||
TIMB12.BDF TIMB14.BDF TIMB18.BDF TIMB24.BDF TIMR08.BDF TIMR10.BDF
|
||||
TIMR12.BDF TIMR14.BDF TIMR18.BDF TIMR24.BDF
|
||||
|
||||
Copyright 1984-1989, 1994 Adobe Systems Incorporated.
|
||||
Copyright 1988, 1994 Digital Equipment Corporation.
|
||||
|
||||
Adobe is a trademark of Adobe Systems Incorporated which may be
|
||||
registered in certain jurisdictions.
|
||||
Permission to use these trademarks is hereby granted only in
|
||||
association with the images described in this file.
|
||||
|
||||
Permission to use, copy, modify, distribute and sell this software
|
||||
and its documentation for any purpose and without fee is hereby
|
||||
granted, provided that the above copyright notices appear in all
|
||||
copies and that both those copyright notices and this permission
|
||||
notice appear in supporting documentation, and that the names of
|
||||
Adobe Systems and Digital Equipment Corporation not be used in
|
||||
advertising or publicity pertaining to distribution of the software
|
||||
without specific, written prior permission. Adobe Systems and
|
||||
Digital Equipment Corporation make no representations about the
|
||||
suitability of this software for any purpose. It is provided "as
|
||||
is" without express or implied warranty.
|
||||
@@ -0,0 +1,118 @@
|
||||
|
||||
[tocstart]: # (toc start)
|
||||
|
||||
* [U8g2_for_Adafruit_GFX](#u8g2_for_adafruit_gfx)
|
||||
* [What is U8g2_for_Adafruit_GFX?](#what-is-u8g2_for_adafruit_gfx)
|
||||
* [Which Adafruit GFX Libraries are Supported?](#which-adafruit-gfx-libraries-are-supported)
|
||||
* [How to use U8g2_for_Adafruit_GFX?](#how-to-use-u8g2_for_adafruit_gfx)
|
||||
* [Reference](#reference)
|
||||
* [Command Reference](#command-reference)
|
||||
* [Differences to U8g2](#differences-to-u8g2)
|
||||
|
||||
[tocend]: # (toc end)
|
||||
|
||||
# U8g2_for_Adafruit_GFX
|
||||
|
||||
Download: [https://github.com/olikraus/U8g2_for_Adafruit_GFX/archive/master.zip](https://github.com/olikraus/U8g2_for_Adafruit_GFX/archive/master.zip)
|
||||
|
||||
## What is U8g2_for_Adafruit_GFX?
|
||||
- Arduino Library
|
||||
- Adds a the [U8g2](https://github.com/olikraus/u8g2) text drawing engine to all Adafruit GFX based Arduino librarys.
|
||||
- All [U8g2 fonts](https://github.com/olikraus/u8g2/wiki/fntlistall) can be used
|
||||
- Support for UTF-8 and Unicode
|
||||
- Support for Arduino print() command and F() Macro
|
||||
|
||||
U8g2 is a graphics library for monochrome displays. U8g2 supports many
|
||||
displays, some of them are also supported by Adafruit GFX based libraries.
|
||||
Others are supported by Adafruit GFX libraries, but are not supported by U8g2.
|
||||
`U8g2_for_Adafruit_GFX` connects to an existing Adafruit Library and adds support for
|
||||
U8g2 fonts to all Adafruit GFX based libraries.
|
||||
|
||||
## Which Adafruit GFX Libraries are Supported?
|
||||
|
||||
All libraries based on the Adafruit GFX engine are supported. This is a incomplete
|
||||
list of such libraries. Probably many more are supported:
|
||||
|
||||
- [https://github.com/adafruit/Adafruit_SSD1306](https://github.com/adafruit/Adafruit_SSD1306) (tested)
|
||||
- [https://github.com/adafruit/Adafruit-ST7735-Library](https://github.com/adafruit/Adafruit-ST7735-Library) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit-Graphic-VFD-Display-Library](https://github.com/adafruit/Adafruit-Graphic-VFD-Display-Library) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit-HX8340B](https://github.com/adafruit/Adafruit-HX8340B) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit_HX8357_Library](https://github.com/adafruit/Adafruit_HX8357_Library) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit_ILI9340](https://github.com/adafruit/Adafruit_ILI9340) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit_ILI9341](https://github.com/adafruit/Adafruit_ILI9341) (not tested)
|
||||
- [https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library](https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library) (not tested)
|
||||
|
||||
|
||||
## How to use U8g2_for_Adafruit_GFX?
|
||||
|
||||
This is a complete example for `U8g2_for_Adafruit_GFX` connected to Adafruit SSD1306
|
||||
library:
|
||||
|
||||
```
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
|
||||
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
void setup() {
|
||||
display.begin(SSD1306_SWITCHCAPVCC);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
}
|
||||
|
||||
void loop() {
|
||||
display.clearDisplay(); // clear the graphcis buffer
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
|
||||
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
u8g2_for_adafruit_gfx.setCursor(0,20); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print(F("Hello World"));
|
||||
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print(F("Umlaut ÄÖÜ")); // UTF-8 string with german umlaut chars
|
||||
display.display(); // make everything visible
|
||||
delay(2000);
|
||||
}
|
||||
```
|
||||
|
||||
# Reference
|
||||
|
||||
## Command Reference
|
||||
|
||||
- `U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx`: Constructor call. This must be
|
||||
the first command. No paramenters are required.
|
||||
- `void u8g2_for_adafruit_gfx.begin(Adafruit_GFX &gfx)`: Connect to Adafruit GFX library
|
||||
- `void u8g2_for_adafruit_gfx.setFont(const uint8_t *font)`: Assign u8g2 font, see [here](https://github.com/olikraus/u8g2/wiki/fntlistall) for a list.
|
||||
Note: The list from the u8g2 project might be a little bit newer, so maybe not allways all fonts are available for this project.
|
||||
See also: [https://github.com/olikraus/u8g2/wiki/u8g2reference#setfont](https://github.com/olikraus/u8g2/wiki/u8g2reference#setfont)
|
||||
- `void u8g2_for_adafruit_gfx.setFontMode(uint8_t is_transparent)`: Define, whether the background color should be drawn or not.
|
||||
See also: [https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontmode](https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontmode)
|
||||
- `void u8g2_for_adafruit_gfx.setFontDirection(uint8_t dir)`: Identical to the u8g2 command
|
||||
[https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontdirection](https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontdirection)
|
||||
- `void u8g2_for_adafruit_gfx.setForegroundColor(uint8_t dir)`: Set the foreground color for the text.
|
||||
- `void u8g2_for_adafruit_gfx.setBackgroundColor(uint8_t dir)`: Set the background color for the text. This is only required if the font mode is not set to transparent.
|
||||
- `void u8g2_for_adafruit_gfx.setCursor(int16_t x, int16_t y)`: Specify the x/y position for the next print() command. This x/y position is the lower left baseline of a character.
|
||||
For "Arduino" this will be the lower left corner of the A character.
|
||||
- `void u8g2_for_adafruit_gfx.print(...)`: This is the standard Arduino print function. Any text given here will be output on the display via Adafruit GFX library.
|
||||
See [here](https://www.arduino.cc/en/Serial/Print) for further description. Note: This function supports UTF-8.
|
||||
- `int8_t u8g2_for_adafruit_gfx.getFontAscent(void)`: The height of the character 'A' in the current font.
|
||||
- `int8_t u8g2_for_adafruit_gfx.getFontDescent(void)`: The extend of 'g' below baseline. This value usually is negative.
|
||||
- `int16_t u8g2_for_adafruit_gfx.getUTF8Width(const char *str)`: Return the width of the provided string.
|
||||
It is assumed, that `str` is encoded in UTF8 (this is default for Arduino IDE and gcc/g++).
|
||||
|
||||
|
||||
## Differences to U8g2
|
||||
There are some minor differences to the U8g2 library:
|
||||
- For U8g2 the color is specified with the `setDrawColor()` function.
|
||||
- The `print()` command supports UTF-8 by default. It also can not be disabled. For U8g2 the UTF-8 support must be enabled first.
|
||||
- This library does not support the change of the reference position. The reference position is the lower left baseline of a character.
|
||||
|
||||
## Release Procedure
|
||||
- Copy font file u8g2_fonts.c to "src" folder
|
||||
- Change the include file in u8g2_fonts.c from "u8g2.h" to "u8g2_fonts.h"
|
||||
- Update the list of fonts in "U8g2_for_Adafruit_GFX.h"
|
||||
- Update keywords.txt
|
||||
- Update version in library.properties
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
|
||||
Hello_Adafruit_SSD1306.ino
|
||||
|
||||
Demonstrates how to draw a fitting box around a text.
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
*/
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
|
||||
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
void setup() {
|
||||
display.begin(SSD1306_SWITCHCAPVCC);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const char s[] = "gfx LCD";
|
||||
/* width and height of the text */
|
||||
int16_t height;
|
||||
int16_t width;
|
||||
/* desired position of the text */
|
||||
int16_t x = 4;
|
||||
int16_t y = 25;
|
||||
display.clearDisplay(); // clear the graphcis buffer
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvB14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
|
||||
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
|
||||
u8g2_for_adafruit_gfx.setCursor(x, y); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print(s);
|
||||
/* calculate the size of the box into which the text will fit */
|
||||
height = u8g2_for_adafruit_gfx.getFontAscent() - u8g2_for_adafruit_gfx.getFontDescent();
|
||||
width = u8g2_for_adafruit_gfx.getUTF8Width(s);
|
||||
/* draw the box around the text*/
|
||||
display.drawRect(x, y-u8g2_for_adafruit_gfx.getFontAscent(), width, height, WHITE);
|
||||
display.display(); // make everything visible
|
||||
delay(2000);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
|
||||
Hello_Adafruit_ILI9341.ino
|
||||
|
||||
Demonstrates how to connect U8g2_for_Adafruit_GFX to Adafruit SSD1306 library.
|
||||
|
||||
U8g2_for_Adafruit_GFX:
|
||||
- Use U8g2 fonts with Adafruit GFX
|
||||
- Support for UTF-8 in print statement
|
||||
- 90, 180 and 270 degree text direction
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
*/
|
||||
#include "SPI.h"
|
||||
#include "Adafruit_GFX.h"
|
||||
#include "Adafruit_ILI9341.h"
|
||||
#include "U8g2_for_Adafruit_GFX.h"
|
||||
|
||||
#define TFT_DC 9
|
||||
#define TFT_CS 10
|
||||
|
||||
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC);
|
||||
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
void setup() {
|
||||
display.begin();
|
||||
display.setRotation(1);
|
||||
display.fillScreen(ILI9341_BLACK);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
}
|
||||
|
||||
unsigned long x = 0;
|
||||
|
||||
void loop() {
|
||||
|
||||
u8g2_for_adafruit_gfx.setFontMode(0); // use u8g2 none transparent mode
|
||||
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(ILI9341_WHITE); // apply Adafruit GFX color
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
u8g2_for_adafruit_gfx.setCursor(0,20); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("Hello World");
|
||||
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("Umlaut ÄÖÜ"); // UTF-8 string with german umlaut chars
|
||||
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_inb63_mn); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
u8g2_for_adafruit_gfx.setFontMode(0); // use u8g2 none transparent mode
|
||||
u8g2_for_adafruit_gfx.setCursor(0,220); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print(x); // UTF-8 string with german umlaut chars
|
||||
x++;
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
|
||||
Hello_Adafruit_SSD1306.ino
|
||||
|
||||
Demonstrates how to connect U8g2_for_Adafruit_GFX to Adafruit SSD1306 library.
|
||||
|
||||
U8g2_for_Adafruit_GFX:
|
||||
- Use U8g2 fonts with Adafruit GFX
|
||||
- Support for UTF-8 in print statement
|
||||
- 90, 180 and 270 degree text direction
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
*/
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
|
||||
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
void setup() {
|
||||
display.begin(SSD1306_SWITCHCAPVCC);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
}
|
||||
|
||||
void loop() {
|
||||
display.clearDisplay(); // clear the graphcis buffer
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
|
||||
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
|
||||
u8g2_for_adafruit_gfx.setCursor(0,20); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("Hello World");
|
||||
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("Umlaut ÄÖÜ"); // UTF-8 string with german umlaut chars
|
||||
display.display(); // make everything visible
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <Adafruit_GFX.h> // Core graphics library
|
||||
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
|
||||
#include <SPI.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
#define TFT_MOSI P14 //SDA,GPIO19
|
||||
#define TFT_SCLK P13 //SCL,GPIO18
|
||||
#define TFT_CS P16 //GPIO 5
|
||||
#define TFT_DC P15 //GPIO21
|
||||
#define TFT_RST -1 //
|
||||
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI,TFT_SCLK,TFT_RST);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
void setup(void) {
|
||||
tft.initR(INITR_GREENTAB); // Init ST7735S chip, black tab
|
||||
tft.setRotation(0);
|
||||
tft.fillScreen(ST77XX_WHITE);
|
||||
u8g2_for_adafruit_gfx.begin(tft);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_8x13_m_symbols); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
//u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(0x0000FF); // apply Adafruit GFX color
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 none transparent mode
|
||||
u8g2_for_adafruit_gfx.setCursor(20, 20); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("AAAbb1222");
|
||||
// u8g2_for_adafruit_gfx.drawLine(1,1,15,20);
|
||||
delay(1000);
|
||||
tft.drawLine(0, 0, 60, tft.height() - 1, 0x00FF00);
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
|
||||
Shennong.ino
|
||||
|
||||
Scroll through Chinese Short Story
|
||||
"The Farmer God Shen Nong Tastes All the Plants"
|
||||
|
||||
U8g2_for_Adafruit_GFX:
|
||||
- Use U8g2 fonts with Adafruit GFX
|
||||
- Support for UTF-8 in print statement
|
||||
- 90, 180 and 270 degree text direction
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
|
||||
English translation:
|
||||
The Farmer God Shen Nong Tastes All the Plants
|
||||
|
||||
Source:
|
||||
http://chinesereadingpractice.com/2012/08/22/the-farmer-god-shen-nong-tastes-all-the-plants/
|
||||
|
||||
All his life, Shen Nong had a crystal abdomen, and one could
|
||||
clearly see all of his internal organs. At that time, humans were
|
||||
often getting sick and even dying because they ate things
|
||||
indiscriminately [not knowing if they were good or bad]. Shen
|
||||
Nong determinedly tasted everything everywhere; the
|
||||
good-tasting things he put in a bag on his left side, those were
|
||||
for people to eat; the bad-tasting things he put in a bag on his
|
||||
right side, and those were used for medicine.
|
||||
|
||||
The first time, Shen Nong tasted a small fresh leaf. As this leaf
|
||||
fell into his stomach, it cleaned every inch of his insides so that
|
||||
every organ top and bottom was fresh and cool, as if [the leaf]
|
||||
was somehow on patrol [making the rounds], so Shen Nong
|
||||
called it “chá” [to investigate / check], and later generations of
|
||||
men called it “chá” [tea]. Shen Nong put it in the bag on the
|
||||
right. The second time, Shen Nong tasted a little light red flower
|
||||
that looked like a butterfly, which was sweet and delicious, with
|
||||
an exotic smell that filled his nostrils, so he called it “licorice”. He
|
||||
put it in the bag on the left. In this way, Shen Nong diligently
|
||||
tasted all manner of flora, and every time he was poisoned, he
|
||||
used tea to rescue himself. Before long, the bag on his left
|
||||
contained 47,000 kinds of flowers, grasses, roots and leaves,
|
||||
and the right side had 398,000 kinds.
|
||||
|
||||
But one day, Shen Nong tasted “heartbreak grass”, and this
|
||||
poison was too terrible, so there wasn’t enough time to eat the
|
||||
tea leaves to detoxify and he died. He sacrificed himself to save
|
||||
humanity, so people call him the “Bodhisattva of Medicine”, and
|
||||
people forever commemorate him through this story.
|
||||
|
||||
|
||||
*/
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
|
||||
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
|
||||
// To read the short story with all glyphs you need at least a font with _gb2312b postfix
|
||||
// However, a font with _gb2312b postfix is very large and will not always fit on
|
||||
// the target controller. For testing you can use _chinese1 extenstion, but then
|
||||
// many gylphs are skipped and not visible.
|
||||
//
|
||||
// wqy fonts are available in different sizes, here are only wqy12, wqy14 and wqy16 listed
|
||||
|
||||
//#define FONT u8g2_font_wqy12_t_chinese1
|
||||
//#define FONT u8g2_font_wqy12_t_gb2312b
|
||||
|
||||
#define FONT u8g2_font_wqy14_t_chinese1
|
||||
//#define FONT u8g2_font_wqy14_t_gb2312b
|
||||
|
||||
//#define FONT u8g2_font_wqy16_t_chinese1
|
||||
//#define FONT u8g2_font_wqy16_t_gb2312b
|
||||
|
||||
// The next two macros define the scroll speed of the short story
|
||||
#define SCROLL_DELTA 2
|
||||
#define SCROLL_DELAY 200
|
||||
|
||||
|
||||
// Chinese Short Story
|
||||
// The Farmer God Shen Nong Tastes All the Plants
|
||||
// Please excause wrong line breaks
|
||||
|
||||
const char c_str[] =
|
||||
"Shen Nong\n\n"
|
||||
"神农一生下来就是\n"
|
||||
"个水晶肚子,五脏\n"
|
||||
"六腑全都能看得一\n"
|
||||
"清二楚。那时侯,\n"
|
||||
"人们经常因为乱吃\n"
|
||||
"东西而生病,甚至\n"
|
||||
"丧命。神农决心尝\n"
|
||||
"遍所有的东西,好\n"
|
||||
"吃的放在身边左边\n"
|
||||
"的袋子里,给人吃\n"
|
||||
";\n"
|
||||
"不好吃的就放在身\n"
|
||||
"子右边的袋子里,\n"
|
||||
"作药用。\n"
|
||||
"第一次,神农尝了\n"
|
||||
"一片小嫩叶。这叶\n"
|
||||
"片一落进肚里,就\n"
|
||||
"上上下下地把里面\n"
|
||||
"各器官擦洗得清清\n"
|
||||
"爽爽,\n"
|
||||
"象巡查似的,\n"
|
||||
"神农把它叫做\n"
|
||||
"“查”,\n"
|
||||
"就是后人所称的\n"
|
||||
"“茶”。\n"
|
||||
"神农将它放进右边\n"
|
||||
"袋子里。第二次,\n"
|
||||
"神农尝了朵蝴蝶样\n"
|
||||
"的淡红小花,甜津\n"
|
||||
"津的,香味扑鼻,\n"
|
||||
"这是“甘草”。他把\n"
|
||||
"它放进了左边袋子\n"
|
||||
"里。就这样,神农\n"
|
||||
"辛苦地尝遍百草,\n"
|
||||
"每次中毒,都靠茶\n"
|
||||
"来解救。后来,\n"
|
||||
"他左边的袋子里花\n"
|
||||
"草根叶有四万七千\n"
|
||||
"种,右边有三十九\n"
|
||||
"万八千种。\n"
|
||||
"但有一天,神农尝\n"
|
||||
"到了“断肠草”,这\n"
|
||||
"种毒草太厉害了,\n"
|
||||
"他还来不及吃茶解\n"
|
||||
"毒就死了。\n"
|
||||
"他是为了拯救人们\n"
|
||||
"而牺牲的,人们称\n"
|
||||
"他为“药王菩萨”,\n"
|
||||
"人间以这个神话故\n"
|
||||
"事永远地纪念他。\n";
|
||||
|
||||
|
||||
char buf[48]; // there are at most 8 chinese glyphs per line, max buf size is 8*3 = 24
|
||||
|
||||
uint8_t total_lines; // the total number of lines in the story
|
||||
uint8_t i; // loop variable for the lines
|
||||
uint8_t line_cnt; // number of lines to draw, usually equal to lines_per_draw
|
||||
uint8_t start_line; // topmost visible line, derived from top_window_pos
|
||||
uint8_t lines_per_draw; // how many lines to draw on the screen, derived from font and display height
|
||||
uint16_t glyph_height; // height of the glyphs
|
||||
|
||||
uint16_t top_window_pos; // defines the display position in pixel within the text
|
||||
uint16_t total_height; // total height in pixel, derived from font height and total_lines
|
||||
uint16_t top_offset; // offset between the first visible line and the display
|
||||
|
||||
|
||||
uint8_t u8x8_GetStringLineCnt(const char *str)
|
||||
{
|
||||
char e;
|
||||
uint8_t line_cnt = 1;
|
||||
if ( str == NULL )
|
||||
return 0;
|
||||
for(;;)
|
||||
{
|
||||
e = *str;
|
||||
if ( e == '\0' )
|
||||
break;
|
||||
str++;
|
||||
if ( e == '\n' )
|
||||
line_cnt++;
|
||||
}
|
||||
return line_cnt;
|
||||
}
|
||||
|
||||
/*
|
||||
Assumes strings, separated by '\n' in "str".
|
||||
Returns the string at index "line_idx". First strng has line_idx = 0
|
||||
Example:
|
||||
Returns "xyz" for line_idx = 1 with str = "abc\nxyz"
|
||||
Support both UTF8 and normal strings.
|
||||
*/
|
||||
const char *u8x8_GetStringLineStart(uint8_t line_idx, const char *str )
|
||||
{
|
||||
char e;
|
||||
uint8_t line_cnt = 1;
|
||||
|
||||
if ( line_idx == 0 )
|
||||
return str;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
e = *str;
|
||||
if ( e == '\0' )
|
||||
break;
|
||||
str++;
|
||||
if ( e == '\n' )
|
||||
{
|
||||
if ( line_cnt == line_idx )
|
||||
return str;
|
||||
line_cnt++;
|
||||
}
|
||||
}
|
||||
return NULL; /* line not found */
|
||||
}
|
||||
|
||||
/* copy until first '\n' or '\0' in str */
|
||||
/* Important: There is no string overflow check, ensure */
|
||||
/* that the destination buffer is large enough */
|
||||
void u8x8_CopyStringLine(char *dest, uint8_t line_idx, const char *str)
|
||||
{
|
||||
if ( dest == NULL )
|
||||
return;
|
||||
str = u8x8_GetStringLineStart( line_idx, str );
|
||||
if ( str != NULL )
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
if ( *str == '\n' || *str == '\0' )
|
||||
break;
|
||||
*dest = *str;
|
||||
dest++;
|
||||
str++;
|
||||
}
|
||||
}
|
||||
*dest = '\0';
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
display.begin(SSD1306_SWITCHCAPVCC);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
|
||||
u8g2_for_adafruit_gfx.setFont(FONT);
|
||||
|
||||
/* calculate the length of the text in lines */
|
||||
total_lines = u8x8_GetStringLineCnt(c_str);
|
||||
|
||||
/* get the height of the glyphs */
|
||||
//glyph_height = u8g2_for_adafruit_gfx.getMaxCharHeight();
|
||||
glyph_height = u8g2_for_adafruit_gfx.u8g2.font_info.max_char_height;
|
||||
|
||||
/* calculate the height of the text in pixel */
|
||||
total_height = (uint16_t)total_lines * (uint16_t)glyph_height;
|
||||
|
||||
|
||||
/* calculate how many lines must be drawn on the screen */
|
||||
lines_per_draw = display.height() / glyph_height;
|
||||
lines_per_draw += 2;
|
||||
|
||||
/* start at the top of the text */
|
||||
top_window_pos = 0;
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
start_line = top_window_pos / glyph_height;
|
||||
top_offset = top_window_pos % glyph_height;
|
||||
|
||||
line_cnt = total_lines - start_line;
|
||||
if ( line_cnt > lines_per_draw )
|
||||
line_cnt = lines_per_draw;
|
||||
|
||||
display.clearDisplay(); // clear the graphcis buffer
|
||||
|
||||
for( i = 0; i < line_cnt; i++ )
|
||||
{
|
||||
/* copy a line of the text to the local buffer */
|
||||
u8x8_CopyStringLine(buf, i+start_line, c_str);
|
||||
|
||||
/* draw the content of the local buffer */
|
||||
u8g2_for_adafruit_gfx.drawUTF8(0, i*glyph_height-top_offset +glyph_height, buf);
|
||||
}
|
||||
|
||||
display.display(); // make everything visible
|
||||
delay(SCROLL_DELAY);
|
||||
top_window_pos += SCROLL_DELTA;
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
|
||||
Shennong_IL9341.ino
|
||||
|
||||
Scroll through Chinese Short Story
|
||||
"The Farmer God Shen Nong Tastes All the Plants"
|
||||
|
||||
U8g2_for_Adafruit_GFX:
|
||||
- Use U8g2 fonts with Adafruit GFX
|
||||
- Support for UTF-8 in print statement
|
||||
- 90, 180 and 270 degree text direction
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
|
||||
English translation:
|
||||
The Farmer God Shen Nong Tastes All the Plants
|
||||
|
||||
Source:
|
||||
http://chinesereadingpractice.com/2012/08/22/the-farmer-god-shen-nong-tastes-all-the-plants/
|
||||
|
||||
All his life, Shen Nong had a crystal abdomen, and one could
|
||||
clearly see all of his internal organs. At that time, humans were
|
||||
often getting sick and even dying because they ate things
|
||||
indiscriminately [not knowing if they were good or bad]. Shen
|
||||
Nong determinedly tasted everything everywhere; the
|
||||
good-tasting things he put in a bag on his left side, those were
|
||||
for people to eat; the bad-tasting things he put in a bag on his
|
||||
right side, and those were used for medicine.
|
||||
|
||||
The first time, Shen Nong tasted a small fresh leaf. As this leaf
|
||||
fell into his stomach, it cleaned every inch of his insides so that
|
||||
every organ top and bottom was fresh and cool, as if [the leaf]
|
||||
was somehow on patrol [making the rounds], so Shen Nong
|
||||
called it “chá” [to investigate / check], and later generations of
|
||||
men called it “chá” [tea]. Shen Nong put it in the bag on the
|
||||
right. The second time, Shen Nong tasted a little light red flower
|
||||
that looked like a butterfly, which was sweet and delicious, with
|
||||
an exotic smell that filled his nostrils, so he called it “licorice”. He
|
||||
put it in the bag on the left. In this way, Shen Nong diligently
|
||||
tasted all manner of flora, and every time he was poisoned, he
|
||||
used tea to rescue himself. Before long, the bag on his left
|
||||
contained 47,000 kinds of flowers, grasses, roots and leaves,
|
||||
and the right side had 398,000 kinds.
|
||||
|
||||
But one day, Shen Nong tasted “heartbreak grass”, and this
|
||||
poison was too terrible, so there wasn’t enough time to eat the
|
||||
tea leaves to detoxify and he died. He sacrificed himself to save
|
||||
humanity, so people call him the “Bodhisattva of Medicine”, and
|
||||
people forever commemorate him through this story.
|
||||
|
||||
|
||||
*/
|
||||
#include "SPI.h"
|
||||
#include "Adafruit_GFX.h"
|
||||
#include "Adafruit_ILI9341.h"
|
||||
#include "U8g2_for_Adafruit_GFX.h"
|
||||
|
||||
#define TFT_DC 9
|
||||
#define TFT_CS 10
|
||||
|
||||
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC);
|
||||
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
|
||||
// To read the short story with all glyphs you need at least a font with _gb2312b postfix
|
||||
// However, a font with _gb2312b postfix is very large and will not always fit on
|
||||
// the target controller. For testing you can use _chinese1 extenstion, but then
|
||||
// many gylphs are skipped and not visible.
|
||||
//
|
||||
// wqy fonts are available in different sizes, here are only wqy12, wqy14 and wqy16 listed
|
||||
|
||||
//#define FONT u8g2_font_wqy12_t_chinese1
|
||||
//#define FONT u8g2_font_wqy12_t_gb2312b
|
||||
|
||||
//#define FONT u8g2_font_wqy14_t_chinese1
|
||||
//#define FONT u8g2_font_wqy14_t_gb2312b
|
||||
|
||||
//#define FONT u8g2_font_wqy16_t_chinese1
|
||||
#define FONT u8g2_font_wqy16_t_gb2312b
|
||||
|
||||
// The next two macros define the scroll speed of the short story
|
||||
#define SCROLL_DELTA 4
|
||||
#define SCROLL_DELAY 100
|
||||
|
||||
|
||||
// Chinese Short Story
|
||||
// The Farmer God Shen Nong Tastes All the Plants
|
||||
// Please excause wrong line breaks
|
||||
|
||||
const char c_str[] =
|
||||
"Shen Nong\n\n"
|
||||
"神农一生下来就是\n"
|
||||
"个水晶肚子,五脏\n"
|
||||
"六腑全都能看得一\n"
|
||||
"清二楚。那时侯,\n"
|
||||
"人们经常因为乱吃\n"
|
||||
"东西而生病,甚至\n"
|
||||
"丧命。神农决心尝\n"
|
||||
"遍所有的东西,好\n"
|
||||
"吃的放在身边左边\n"
|
||||
"的袋子里,给人吃\n"
|
||||
";\n"
|
||||
"不好吃的就放在身\n"
|
||||
"子右边的袋子里,\n"
|
||||
"作药用。\n"
|
||||
"第一次,神农尝了\n"
|
||||
"一片小嫩叶。这叶\n"
|
||||
"片一落进肚里,就\n"
|
||||
"上上下下地把里面\n"
|
||||
"各器官擦洗得清清\n"
|
||||
"爽爽,\n"
|
||||
"象巡查似的,\n"
|
||||
"神农把它叫做\n"
|
||||
"“查”,\n"
|
||||
"就是后人所称的\n"
|
||||
"“茶”。\n"
|
||||
"神农将它放进右边\n"
|
||||
"袋子里。第二次,\n"
|
||||
"神农尝了朵蝴蝶样\n"
|
||||
"的淡红小花,甜津\n"
|
||||
"津的,香味扑鼻,\n"
|
||||
"这是“甘草”。他把\n"
|
||||
"它放进了左边袋子\n"
|
||||
"里。就这样,神农\n"
|
||||
"辛苦地尝遍百草,\n"
|
||||
"每次中毒,都靠茶\n"
|
||||
"来解救。后来,\n"
|
||||
"他左边的袋子里花\n"
|
||||
"草根叶有四万七千\n"
|
||||
"种,右边有三十九\n"
|
||||
"万八千种。\n"
|
||||
"但有一天,神农尝\n"
|
||||
"到了“断肠草”,这\n"
|
||||
"种毒草太厉害了,\n"
|
||||
"他还来不及吃茶解\n"
|
||||
"毒就死了。\n"
|
||||
"他是为了拯救人们\n"
|
||||
"而牺牲的,人们称\n"
|
||||
"他为“药王菩萨”,\n"
|
||||
"人间以这个神话故\n"
|
||||
"事永远地纪念他。\n";
|
||||
|
||||
|
||||
char buf[48]; // there are at most 8 chinese glyphs per line, max buf size is 8*3 = 24
|
||||
|
||||
uint8_t total_lines; // the total number of lines in the story
|
||||
uint8_t i; // loop variable for the lines
|
||||
uint8_t line_cnt; // number of lines to draw, usually equal to lines_per_draw
|
||||
uint8_t start_line; // topmost visible line, derived from top_window_pos
|
||||
uint8_t lines_per_draw; // how many lines to draw on the screen, derived from font and display height
|
||||
uint16_t glyph_height; // height of the glyphs
|
||||
uint16_t glyph_width;
|
||||
|
||||
uint16_t top_window_pos; // defines the display position in pixel within the text
|
||||
uint16_t total_height; // total height in pixel, derived from font height and total_lines
|
||||
uint16_t top_offset; // offset between the first visible line and the display
|
||||
|
||||
|
||||
uint8_t u8x8_GetStringLineCnt(const char *str)
|
||||
{
|
||||
char e;
|
||||
uint8_t line_cnt = 1;
|
||||
if ( str == NULL )
|
||||
return 0;
|
||||
for(;;)
|
||||
{
|
||||
e = *str;
|
||||
if ( e == '\0' )
|
||||
break;
|
||||
str++;
|
||||
if ( e == '\n' )
|
||||
line_cnt++;
|
||||
}
|
||||
return line_cnt;
|
||||
}
|
||||
|
||||
/*
|
||||
Assumes strings, separated by '\n' in "str".
|
||||
Returns the string at index "line_idx". First strng has line_idx = 0
|
||||
Example:
|
||||
Returns "xyz" for line_idx = 1 with str = "abc\nxyz"
|
||||
Support both UTF8 and normal strings.
|
||||
*/
|
||||
const char *u8x8_GetStringLineStart(uint8_t line_idx, const char *str )
|
||||
{
|
||||
char e;
|
||||
uint8_t line_cnt = 1;
|
||||
|
||||
if ( line_idx == 0 )
|
||||
return str;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
e = *str;
|
||||
if ( e == '\0' )
|
||||
break;
|
||||
str++;
|
||||
if ( e == '\n' )
|
||||
{
|
||||
if ( line_cnt == line_idx )
|
||||
return str;
|
||||
line_cnt++;
|
||||
}
|
||||
}
|
||||
return NULL; /* line not found */
|
||||
}
|
||||
|
||||
/* copy until first '\n' or '\0' in str */
|
||||
/* Important: There is no string overflow check, ensure */
|
||||
/* that the destination buffer is large enough */
|
||||
void u8x8_CopyStringLine(char *dest, uint8_t line_idx, const char *str)
|
||||
{
|
||||
if ( dest == NULL )
|
||||
return;
|
||||
str = u8x8_GetStringLineStart( line_idx, str );
|
||||
if ( str != NULL )
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
if ( *str == '\n' || *str == '\0' )
|
||||
break;
|
||||
*dest = *str;
|
||||
dest++;
|
||||
str++;
|
||||
}
|
||||
}
|
||||
*dest = '\0';
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
display.begin();
|
||||
display.setRotation(0);
|
||||
display.fillScreen(ILI9341_BLACK);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
|
||||
u8g2_for_adafruit_gfx.setFont(FONT);
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(ILI9341_WHITE); // apply Adafruit GFX color
|
||||
|
||||
/* calculate the length of the text in lines */
|
||||
total_lines = u8x8_GetStringLineCnt(c_str);
|
||||
|
||||
/* get the height of the glyphs */
|
||||
//glyph_height = u8g2_for_adafruit_gfx.getMaxCharHeight();
|
||||
glyph_height = u8g2_for_adafruit_gfx.u8g2.font_info.max_char_height;
|
||||
glyph_width = u8g2_for_adafruit_gfx.u8g2.font_info.max_char_width;
|
||||
|
||||
/* calculate the height of the text in pixel */
|
||||
total_height = (uint16_t)total_lines * (uint16_t)glyph_height;
|
||||
|
||||
|
||||
/* calculate how many lines must be drawn on the screen */
|
||||
lines_per_draw = display.height() / glyph_height;
|
||||
lines_per_draw += 2;
|
||||
|
||||
/* start at the top of the text */
|
||||
top_window_pos = 0;
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
start_line = top_window_pos / glyph_height;
|
||||
top_offset = top_window_pos % glyph_height;
|
||||
|
||||
line_cnt = total_lines - start_line;
|
||||
if ( line_cnt > lines_per_draw )
|
||||
line_cnt = lines_per_draw;
|
||||
|
||||
//display.clearDisplay(); // clear the graphcis buffer
|
||||
|
||||
for( i = 0; i < line_cnt; i++ )
|
||||
{
|
||||
/* copy a line of the text to the local buffer */
|
||||
u8x8_CopyStringLine(buf, i+start_line, c_str);
|
||||
|
||||
/* draw the content of the local buffer */
|
||||
display.fillRect(0, i*glyph_height-top_offset, line_cnt*glyph_width, glyph_height, ILI9341_BLACK);
|
||||
u8g2_for_adafruit_gfx.drawUTF8(0, i*glyph_height-top_offset +glyph_height, buf);
|
||||
|
||||
|
||||
}
|
||||
|
||||
//display.display(); // make everything visible
|
||||
delay(SCROLL_DELAY);
|
||||
top_window_pos += SCROLL_DELTA;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
|
||||
Hello_Adafruit_SSD1306.ino
|
||||
|
||||
Demonstrates how to connect U8g2_for_Adafruit_GFX to Adafruit SSD1306 library.
|
||||
|
||||
U8g2_for_Adafruit_GFX:
|
||||
- Use U8g2 fonts with Adafruit GFX
|
||||
- Support for UTF-8 in print statement
|
||||
- 90, 180 and 270 degree text direction
|
||||
|
||||
List of all U8g2 fonts: https://github.com/olikraus/u8g2/wiki/fntlistall
|
||||
|
||||
*/
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <U8g2_for_Adafruit_GFX.h>
|
||||
|
||||
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
|
||||
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
|
||||
|
||||
void setup() {
|
||||
display.begin(SSD1306_SWITCHCAPVCC);
|
||||
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
|
||||
}
|
||||
|
||||
void loop() {
|
||||
display.clearDisplay(); // clear the graphcis buffer
|
||||
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
|
||||
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
|
||||
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_siji_t_6x10); // icon font
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
|
||||
u8g2_for_adafruit_gfx.drawGlyph(0, 10, 0x0e200); // Power Supply
|
||||
u8g2_for_adafruit_gfx.drawGlyph(12, 10, 0x0e201); // Charging
|
||||
u8g2_for_adafruit_gfx.drawGlyph(24, 10, 0x0e10a); // Right Arrow
|
||||
u8g2_for_adafruit_gfx.drawGlyph(36, 10, 0x0e24b); // full Battery
|
||||
|
||||
u8g2_for_adafruit_gfx.setFont(u8g2_font_7x13_te); // extended font
|
||||
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
|
||||
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
|
||||
u8g2_for_adafruit_gfx.print("<Ȧǀʘ>"); // UTF-8 string: "<" 550 448 664 ">"
|
||||
display.display(); // make everything visible
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
https://github.com/olikraus/U8g2_for_Adafruit_GFX ChangeLog
|
||||
|
||||
2018-03-20 v1.1.0 olikraus@gmail.com
|
||||
* Fixed datatype for colors
|
||||
* Bugfix: Do not call Adafruit GFX function with zero length
|
||||
* Addes ascent, decent functions
|
||||
* Added string (UTF8) width calculation
|
||||
2018-04-05 v1.2.0 olikraus@gmail.com
|
||||
* Added Newline handling
|
||||
* Fixed another color datatype bug
|
||||
* Fixed write function
|
||||
2018-10-21 v1.3.1 olikraus@gmail.com
|
||||
* Support for new font format (unicode lookup table)
|
||||
* Takeover u8g2 fonts
|
||||
2018-11-1 v1.5.2 olikraus@gmail.com
|
||||
* Large font support
|
||||
2019-6.21 v1.6.0 olikraus@gmail.com
|
||||
* Bugfix for u8g2_DrawStr (issue 16)
|
||||
* Takeover u8g2 fonts (issue 17)
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
Developer Information
|
||||
=================
|
||||
|
||||
Takeover U8g2 fonts:
|
||||
- copy font file u8g2_fonts.c to "src" folder
|
||||
- change the include file in u8g2_fonts.c from "u8g2.h" to "u8g2_fonts.h"
|
||||
- Update the list of fonts in "U8g2_for_Adafruit_GFX.h"
|
||||
- Update keywords.txt
|
||||
- Update version in library.properties
|
||||
@@ -0,0 +1,45 @@
|
||||
#######################################
|
||||
|
||||
# Syntax Coloring Map For U8g2_for_Adafruit_GFX (uag)
|
||||
|
||||
#######################################
|
||||
|
||||
# Class (KEYWORD1)
|
||||
|
||||
#######################################
|
||||
|
||||
|
||||
|
||||
U8G2_FOR_ADAFRUIT_GFX KEYWORD1
|
||||
|
||||
|
||||
|
||||
#######################################
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
|
||||
#######################################
|
||||
|
||||
|
||||
begin KEYWORD2
|
||||
drawGlyph KEYWORD2
|
||||
drawUTF8 KEYWORD2
|
||||
getUTF8Width KEYWORD2
|
||||
home KEYWORD2
|
||||
setCursor KEYWORD2
|
||||
setFlipMode KEYWORD2
|
||||
setFont KEYWORD2
|
||||
setFontDirection KEYWORD2
|
||||
setFontMode KEYWORD2
|
||||
setBackgroundColor KEYWORD2
|
||||
setForegroundColor KEYWORD2
|
||||
getFontAscent KEYWORD2
|
||||
getFontDescent KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
|
||||
# u8g2_font_u8glib_4_tf LITERAL1
|
||||
@@ -0,0 +1,10 @@
|
||||
name=U8g2_for_Adafruit_GFX
|
||||
version=1.8.0
|
||||
author=oliver <olikraus@gmail.com>
|
||||
maintainer=oliver <olikraus@gmail.com>
|
||||
sentence=Add U8g2 fonts to any Adafruit GFX based graphics library.
|
||||
paragraph=Use our favorite Adafruit graphics library together with fonts from U8g2 project (https://github.com/olikraus/u8g2/wiki/fntlistall).
|
||||
category=Display
|
||||
url=https://github.com/olikraus/U8g2_for_Adafruit_GFX
|
||||
architectures=*
|
||||
depends=Adafruit GFX Library
|
||||
@@ -0,0 +1,737 @@
|
||||
/*
|
||||
|
||||
U8g2_for_Adafruit_GFX.cpp
|
||||
|
||||
Add unicode support and U8g2 fonts to Adafruit GFX libraries.
|
||||
|
||||
U8g2 for Adafruit GFX Lib (https://github.com/olikraus/U8g2_for_Adafruit_GFX)
|
||||
|
||||
Copyright (c) 2018, olikraus@gmail.com
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or other
|
||||
materials provided with the distribution.
|
||||
|
||||
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
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#include <Adafruit_GFX.h>
|
||||
#include "U8g2_for_Adafruit_GFX.h"
|
||||
|
||||
|
||||
//========================================================
|
||||
|
||||
|
||||
|
||||
static uint8_t u8g2_font_get_byte(const uint8_t *font, uint8_t offset)
|
||||
{
|
||||
font += offset;
|
||||
return u8x8_pgm_read( font );
|
||||
}
|
||||
|
||||
static uint16_t u8g2_font_get_word(const uint8_t *font, uint8_t offset) U8X8_NOINLINE;
|
||||
static uint16_t u8g2_font_get_word(const uint8_t *font, uint8_t offset)
|
||||
{
|
||||
uint16_t pos;
|
||||
font += offset;
|
||||
pos = u8x8_pgm_read( font );
|
||||
font++;
|
||||
pos <<= 8;
|
||||
pos += u8x8_pgm_read( font);
|
||||
return pos;
|
||||
}
|
||||
|
||||
/*========================================================================*/
|
||||
/* new font format */
|
||||
void u8g2_read_font_info(u8g2_font_info_t *font_info, const uint8_t *font)
|
||||
{
|
||||
/* offset 0 */
|
||||
font_info->glyph_cnt = u8g2_font_get_byte(font, 0);
|
||||
font_info->bbx_mode = u8g2_font_get_byte(font, 1);
|
||||
font_info->bits_per_0 = u8g2_font_get_byte(font, 2);
|
||||
font_info->bits_per_1 = u8g2_font_get_byte(font, 3);
|
||||
|
||||
/* offset 4 */
|
||||
font_info->bits_per_char_width = u8g2_font_get_byte(font, 4);
|
||||
font_info->bits_per_char_height = u8g2_font_get_byte(font, 5);
|
||||
font_info->bits_per_char_x = u8g2_font_get_byte(font, 6);
|
||||
font_info->bits_per_char_y = u8g2_font_get_byte(font, 7);
|
||||
font_info->bits_per_delta_x = u8g2_font_get_byte(font, 8);
|
||||
|
||||
/* offset 9 */
|
||||
font_info->max_char_width = u8g2_font_get_byte(font, 9);
|
||||
font_info->max_char_height = u8g2_font_get_byte(font, 10);
|
||||
font_info->x_offset = u8g2_font_get_byte(font, 11);
|
||||
font_info->y_offset = u8g2_font_get_byte(font, 12);
|
||||
|
||||
/* offset 13 */
|
||||
font_info->ascent_A = u8g2_font_get_byte(font, 13);
|
||||
font_info->descent_g = u8g2_font_get_byte(font, 14);
|
||||
font_info->ascent_para = u8g2_font_get_byte(font, 15);
|
||||
font_info->descent_para = u8g2_font_get_byte(font, 16);
|
||||
|
||||
/* offset 17 */
|
||||
font_info->start_pos_upper_A = u8g2_font_get_word(font, 17);
|
||||
font_info->start_pos_lower_a = u8g2_font_get_word(font, 19);
|
||||
|
||||
/* offset 21 */
|
||||
font_info->start_pos_unicode = u8g2_font_get_word(font, 21);
|
||||
}
|
||||
|
||||
uint8_t u8g2_GetFontBBXWidth(u8g2_font_t *u8g2)
|
||||
{
|
||||
return u8g2->font_info.max_char_width; /* new font info structure */
|
||||
}
|
||||
|
||||
uint8_t u8g2_GetFontBBXHeight(u8g2_font_t *u8g2)
|
||||
{
|
||||
return u8g2->font_info.max_char_height; /* new font info structure */
|
||||
}
|
||||
|
||||
int8_t u8g2_GetFontBBXOffX(u8g2_font_t *u8g2)
|
||||
{
|
||||
return u8g2->font_info.x_offset; /* new font info structure */
|
||||
}
|
||||
|
||||
int8_t u8g2_GetFontBBXOffY(u8g2_font_t *u8g2)
|
||||
{
|
||||
return u8g2->font_info.y_offset; /* new font info structure */
|
||||
}
|
||||
|
||||
uint8_t u8g2_GetFontCapitalAHeight(u8g2_font_t *u8g2)
|
||||
{
|
||||
return u8g2->font_info.ascent_A; /* new font info structure */
|
||||
}
|
||||
|
||||
static uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt) U8X8_NOINLINE;
|
||||
static uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt)
|
||||
{
|
||||
uint8_t val;
|
||||
uint8_t bit_pos = f->decode_bit_pos;
|
||||
uint8_t bit_pos_plus_cnt;
|
||||
|
||||
//val = *(f->decode_ptr);
|
||||
val = u8x8_pgm_read( f->decode_ptr );
|
||||
|
||||
val >>= bit_pos;
|
||||
bit_pos_plus_cnt = bit_pos;
|
||||
bit_pos_plus_cnt += cnt;
|
||||
if ( bit_pos_plus_cnt >= 8 )
|
||||
{
|
||||
uint8_t s = 8;
|
||||
s -= bit_pos;
|
||||
f->decode_ptr++;
|
||||
//val |= *(f->decode_ptr) << (8-bit_pos);
|
||||
val |= u8x8_pgm_read( f->decode_ptr ) << (s);
|
||||
//bit_pos -= 8;
|
||||
bit_pos_plus_cnt -= 8;
|
||||
}
|
||||
val &= (1U<<cnt)-1;
|
||||
//bit_pos += cnt;
|
||||
|
||||
f->decode_bit_pos = bit_pos_plus_cnt;
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
2 bit --> cnt = 2
|
||||
-2,-1,0. 1
|
||||
|
||||
3 bit --> cnt = 3
|
||||
-2,-1,0. 1
|
||||
-4,-3,-2,-1,0,1,2,3
|
||||
|
||||
if ( x < 0 )
|
||||
r = bits(x-1)+1;
|
||||
else
|
||||
r = bits(x)+1;
|
||||
|
||||
*/
|
||||
/* optimized */
|
||||
static int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt) U8X8_NOINLINE;
|
||||
static int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt)
|
||||
{
|
||||
int8_t v, d;
|
||||
v = (int8_t)u8g2_font_decode_get_unsigned_bits(f, cnt);
|
||||
d = 1;
|
||||
cnt--;
|
||||
d <<= cnt;
|
||||
v -= d;
|
||||
return v;
|
||||
//return (int8_t)u8g2_font_decode_get_unsigned_bits(f, cnt) - ((1<<cnt)>>1);
|
||||
}
|
||||
|
||||
|
||||
static int16_t u8g2_add_vector_y(int16_t dy, int8_t x, int8_t y, uint8_t dir) U8X8_NOINLINE;
|
||||
static int16_t u8g2_add_vector_y(int16_t dy, int8_t x, int8_t y, uint8_t dir)
|
||||
{
|
||||
switch(dir)
|
||||
{
|
||||
case 0:
|
||||
dy += y;
|
||||
break;
|
||||
case 1:
|
||||
dy += x;
|
||||
break;
|
||||
case 2:
|
||||
dy -= y;
|
||||
break;
|
||||
default:
|
||||
dy -= x;
|
||||
break;
|
||||
}
|
||||
return dy;
|
||||
}
|
||||
|
||||
static int16_t u8g2_add_vector_x(int16_t dx, int8_t x, int8_t y, uint8_t dir) U8X8_NOINLINE;
|
||||
static int16_t u8g2_add_vector_x(int16_t dx, int8_t x, int8_t y, uint8_t dir)
|
||||
{
|
||||
switch(dir)
|
||||
{
|
||||
case 0:
|
||||
dx += x;
|
||||
break;
|
||||
case 1:
|
||||
dx -= y;
|
||||
break;
|
||||
case 2:
|
||||
dx -= x;
|
||||
break;
|
||||
default:
|
||||
dx += y;
|
||||
break;
|
||||
}
|
||||
return dx;
|
||||
}
|
||||
|
||||
void u8g2_draw_hv_line(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color) U8X8_NOINLINE;
|
||||
void u8g2_draw_hv_line(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color)
|
||||
{
|
||||
switch(dir)
|
||||
{
|
||||
case 0:
|
||||
u8g2->gfx->drawFastHLine(x,y,len,color);
|
||||
break;
|
||||
case 1:
|
||||
u8g2->gfx->drawFastVLine(x,y,len,color);
|
||||
break;
|
||||
case 2:
|
||||
u8g2->gfx->drawFastHLine(x-len+1,y,len,color);
|
||||
break;
|
||||
case 3:
|
||||
u8g2->gfx->drawFastVLine(x,y-len+1,len,color);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Description:
|
||||
Draw a run-length area of the glyph. "len" can have any size and the line
|
||||
length has to be wrapped at the glyph border.
|
||||
Args:
|
||||
len: Length of the line
|
||||
is_foreground foreground/background?
|
||||
u8g2->font_decode.target_x X position
|
||||
u8g2->font_decode.target_y Y position
|
||||
u8g2->font_decode.is_transparent Transparent mode
|
||||
Return:
|
||||
-
|
||||
Calls:
|
||||
u8g2_Draw90Line()
|
||||
Called by:
|
||||
u8g2_font_decode_glyph()
|
||||
*/
|
||||
/* optimized */
|
||||
static void u8g2_font_decode_len(u8g2_font_t *u8g2, uint8_t len, uint8_t is_foreground)
|
||||
{
|
||||
uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
|
||||
uint8_t rem; /* remaining pixel to the right edge of the glyph */
|
||||
uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
|
||||
/* current is either equal to cnt or equal to rem */
|
||||
|
||||
/* local coordinates of the glyph */
|
||||
uint8_t lx,ly;
|
||||
|
||||
/* target position on the screen */
|
||||
int16_t x, y;
|
||||
|
||||
u8g2_font_decode_t *decode = &(u8g2->font_decode);
|
||||
|
||||
cnt = len;
|
||||
|
||||
/* get the local position */
|
||||
lx = decode->x;
|
||||
ly = decode->y;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
/* calculate the number of pixel to the right edge of the glyph */
|
||||
rem = decode->glyph_width;
|
||||
rem -= lx;
|
||||
|
||||
/* calculate how many pixel to draw. This is either to the right edge */
|
||||
/* or lesser, if not enough pixel are left */
|
||||
current = rem;
|
||||
if ( cnt < rem )
|
||||
current = cnt;
|
||||
|
||||
|
||||
/* now draw the line, but apply the rotation around the glyph target position */
|
||||
//u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
|
||||
|
||||
/* get target position */
|
||||
x = decode->target_x;
|
||||
y = decode->target_y;
|
||||
|
||||
/* apply rotation */
|
||||
x = u8g2_add_vector_x(x, lx, ly, decode->dir);
|
||||
y = u8g2_add_vector_y(y, lx, ly, decode->dir);
|
||||
|
||||
/* draw foreground and background (if required) */
|
||||
if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
|
||||
{
|
||||
if ( is_foreground )
|
||||
{
|
||||
u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, decode->fg_color);
|
||||
}
|
||||
else if ( decode->is_transparent == 0 )
|
||||
{
|
||||
u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color);
|
||||
}
|
||||
}
|
||||
|
||||
/* check, whether the end of the run length code has been reached */
|
||||
if ( cnt < rem )
|
||||
break;
|
||||
cnt -= rem;
|
||||
lx = 0;
|
||||
ly++;
|
||||
}
|
||||
lx += cnt;
|
||||
|
||||
decode->x = lx;
|
||||
decode->y = ly;
|
||||
|
||||
}
|
||||
|
||||
static void u8g2_font_setup_decode(u8g2_font_t *u8g2, const uint8_t *glyph_data)
|
||||
{
|
||||
u8g2_font_decode_t *decode = &(u8g2->font_decode);
|
||||
decode->decode_ptr = glyph_data;
|
||||
decode->decode_bit_pos = 0;
|
||||
|
||||
/* 8 Nov 2015, this is already done in the glyph data search procedure */
|
||||
/*
|
||||
decode->decode_ptr += 1;
|
||||
decode->decode_ptr += 1;
|
||||
*/
|
||||
|
||||
decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
|
||||
decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Description:
|
||||
Decode and draw a glyph.
|
||||
Args:
|
||||
glyph_data: Pointer to the compressed glyph data of the font
|
||||
u8g2->font_decode.target_x X position
|
||||
u8g2->font_decode.target_y Y position
|
||||
u8g2->font_decode.is_transparent Transparent mode
|
||||
Return:
|
||||
Width (delta x advance) of the glyph.
|
||||
Calls:
|
||||
u8g2_font_decode_len()
|
||||
*/
|
||||
/* optimized */
|
||||
static int8_t u8g2_font_decode_glyph(u8g2_font_t *u8g2, const uint8_t *glyph_data)
|
||||
{
|
||||
uint8_t a, b;
|
||||
int8_t x, y;
|
||||
int8_t d;
|
||||
int8_t h;
|
||||
u8g2_font_decode_t *decode = &(u8g2->font_decode);
|
||||
|
||||
u8g2_font_setup_decode(u8g2, glyph_data);
|
||||
h = u8g2->font_decode.glyph_height;
|
||||
|
||||
x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
|
||||
y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
|
||||
d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
|
||||
|
||||
|
||||
if ( decode->glyph_width > 0 )
|
||||
{
|
||||
decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
|
||||
decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
|
||||
//u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
|
||||
|
||||
|
||||
/* reset local x/y position */
|
||||
decode->x = 0;
|
||||
decode->y = 0;
|
||||
|
||||
/* decode glyph */
|
||||
for(;;)
|
||||
{
|
||||
a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
|
||||
b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
|
||||
do
|
||||
{
|
||||
u8g2_font_decode_len(u8g2, a, 0);
|
||||
u8g2_font_decode_len(u8g2, b, 1);
|
||||
} while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
|
||||
|
||||
if ( decode->y >= h )
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/*
|
||||
Description:
|
||||
Find the starting point of the glyph data.
|
||||
Args:
|
||||
encoding: Encoding (ASCII or Unicode) of the glyph
|
||||
Return:
|
||||
Address of the glyph data or NULL, if the encoding is not avialable in the font.
|
||||
*/
|
||||
const uint8_t *u8g2_font_get_glyph_data(u8g2_font_t *u8g2, uint16_t encoding)
|
||||
{
|
||||
const uint8_t *font = u8g2->font;
|
||||
font += 23;
|
||||
|
||||
|
||||
if ( encoding <= 255 )
|
||||
{
|
||||
if ( encoding >= 'a' )
|
||||
{
|
||||
font += u8g2->font_info.start_pos_lower_a;
|
||||
}
|
||||
else if ( encoding >= 'A' )
|
||||
{
|
||||
font += u8g2->font_info.start_pos_upper_A;
|
||||
}
|
||||
|
||||
for(;;)
|
||||
{
|
||||
if ( u8x8_pgm_read( font + 1 ) == 0 )
|
||||
break;
|
||||
if ( u8x8_pgm_read( font ) == encoding )
|
||||
{
|
||||
return font+2; /* skip encoding and glyph size */
|
||||
}
|
||||
font += u8x8_pgm_read( font + 1 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t e;
|
||||
const uint8_t *unicode_lookup_table;
|
||||
/* support for the new unicode lookup table */
|
||||
|
||||
font += u8g2->font_info.start_pos_unicode;
|
||||
unicode_lookup_table = font;
|
||||
|
||||
/* u8g2 issue 596: search for the glyph start in the unicode lookup table */
|
||||
do
|
||||
{
|
||||
font += u8g2_font_get_word(unicode_lookup_table, 0);
|
||||
e = u8g2_font_get_word(unicode_lookup_table, 2);
|
||||
unicode_lookup_table+=4;
|
||||
} while( e < encoding );
|
||||
|
||||
/* variable "font" is now updated according to the lookup table */
|
||||
|
||||
for(;;)
|
||||
{
|
||||
e = u8x8_pgm_read( font );
|
||||
e <<= 8;
|
||||
e |= u8x8_pgm_read( font + 1 );
|
||||
if ( e == 0 )
|
||||
break;
|
||||
if ( e == encoding )
|
||||
{
|
||||
return font+3; /* skip encoding and glyph size */
|
||||
}
|
||||
font += u8x8_pgm_read( font + 2 );
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int16_t u8g2_font_draw_glyph(u8g2_font_t *u8g2, int16_t x, int16_t y, uint16_t encoding)
|
||||
{
|
||||
int16_t dx = 0;
|
||||
u8g2->font_decode.target_x = x;
|
||||
u8g2->font_decode.target_y = y;
|
||||
//u8g2->font_decode.is_transparent = is_transparent; this is already set
|
||||
//u8g2->font_decode.dir = dir;
|
||||
const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
|
||||
if ( glyph_data != NULL )
|
||||
{
|
||||
dx = u8g2_font_decode_glyph(u8g2, glyph_data);
|
||||
}
|
||||
return dx;
|
||||
}
|
||||
|
||||
|
||||
//========================================================
|
||||
|
||||
uint8_t u8g2_IsGlyph(u8g2_font_t *u8g2, uint16_t requested_encoding)
|
||||
{
|
||||
/* updated to new code */
|
||||
if ( u8g2_font_get_glyph_data(u8g2, requested_encoding) != NULL )
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* side effect: updates u8g2->font_decode and u8g2->glyph_x_offset */
|
||||
/* actually u8g2_GetGlyphWidth returns the glyph delta x and glyph width itself is set as side effect */
|
||||
int8_t u8g2_GetGlyphWidth(u8g2_font_t *u8g2, uint16_t requested_encoding)
|
||||
{
|
||||
const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, requested_encoding);
|
||||
if ( glyph_data == NULL )
|
||||
return 0;
|
||||
|
||||
u8g2_font_setup_decode(u8g2, glyph_data);
|
||||
u8g2->glyph_x_offset = u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_x);
|
||||
u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_y);
|
||||
|
||||
/* glyph width is here: u8g2->font_decode.glyph_width */
|
||||
|
||||
return u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_delta_x);
|
||||
}
|
||||
|
||||
|
||||
void u8g2_SetFontMode(u8g2_font_t *u8g2, uint8_t is_transparent)
|
||||
{
|
||||
u8g2->font_decode.is_transparent = is_transparent; // new font procedures
|
||||
}
|
||||
|
||||
void u8g2_SetFontDirection(u8g2_font_t *u8g2, uint8_t dir)
|
||||
{
|
||||
u8g2->font_decode.dir = dir;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int16_t u8g2_DrawGlyph(u8g2_font_t *u8g2, int16_t x, int16_t y, uint16_t encoding)
|
||||
{
|
||||
return u8g2_font_draw_glyph(u8g2, x, y, encoding);
|
||||
}
|
||||
|
||||
int16_t u8g2_DrawStr(u8g2_font_t *u8g2, int16_t x, int16_t y, const char *s)
|
||||
{
|
||||
int16_t sum, delta;
|
||||
sum = 0;
|
||||
|
||||
while( *s != '\0' )
|
||||
{
|
||||
delta = u8g2_DrawGlyph(u8g2, x, y, *s);
|
||||
switch(u8g2->font_decode.dir)
|
||||
{
|
||||
case 0:
|
||||
x += delta;
|
||||
break;
|
||||
case 1:
|
||||
y += delta;
|
||||
break;
|
||||
case 2:
|
||||
x -= delta;
|
||||
break;
|
||||
case 3:
|
||||
y -= delta;
|
||||
break;
|
||||
}
|
||||
sum += delta;
|
||||
s++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void u8g2_SetFont(u8g2_font_t *u8g2, const uint8_t *font)
|
||||
{
|
||||
if ( u8g2->font != font )
|
||||
{
|
||||
u8g2->font = font;
|
||||
u8g2->font_decode.is_transparent = 0;
|
||||
|
||||
u8g2_read_font_info(&(u8g2->font_info), font);
|
||||
}
|
||||
}
|
||||
|
||||
void u8g2_SetForegroundColor(u8g2_font_t *u8g2, uint16_t fg)
|
||||
{
|
||||
u8g2->font_decode.fg_color = fg;
|
||||
}
|
||||
|
||||
void u8g2_SetBackgroundColor(u8g2_font_t *u8g2, uint16_t bg)
|
||||
{
|
||||
u8g2->font_decode.bg_color = bg;
|
||||
}
|
||||
|
||||
//========================================================
|
||||
|
||||
|
||||
/*
|
||||
pass a byte from an utf8 encoded string to the utf8 decoder state machine
|
||||
returns
|
||||
0x0fffe: no glyph, just continue
|
||||
0x0ffff: end of string
|
||||
anything else: The decoded encoding
|
||||
*/
|
||||
uint16_t U8G2_FOR_ADAFRUIT_GFX::utf8_next(uint8_t b)
|
||||
{
|
||||
if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
|
||||
return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
|
||||
if ( utf8_state == 0 )
|
||||
{
|
||||
if ( b >= 0xfc ) /* 6 byte sequence */
|
||||
{
|
||||
utf8_state = 5;
|
||||
b &= 1;
|
||||
}
|
||||
else if ( b >= 0xf8 )
|
||||
{
|
||||
utf8_state = 4;
|
||||
b &= 3;
|
||||
}
|
||||
else if ( b >= 0xf0 )
|
||||
{
|
||||
utf8_state = 3;
|
||||
b &= 7;
|
||||
}
|
||||
else if ( b >= 0xe0 )
|
||||
{
|
||||
utf8_state = 2;
|
||||
b &= 15;
|
||||
}
|
||||
else if ( b >= 0xc0 )
|
||||
{
|
||||
utf8_state = 1;
|
||||
b &= 0x01f;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* do nothing, just use the value as encoding */
|
||||
return b;
|
||||
}
|
||||
encoding = b;
|
||||
return 0x0fffe;
|
||||
}
|
||||
else
|
||||
{
|
||||
utf8_state--;
|
||||
/* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
|
||||
encoding<<=6;
|
||||
b &= 0x03f;
|
||||
encoding |= b;
|
||||
if ( utf8_state != 0 )
|
||||
return 0x0fffe; /* nothing to do yet */
|
||||
}
|
||||
return encoding;
|
||||
}
|
||||
|
||||
int16_t U8G2_FOR_ADAFRUIT_GFX::drawUTF8(int16_t x, int16_t y, const char *str)
|
||||
{
|
||||
uint16_t e;
|
||||
int16_t delta, sum;
|
||||
|
||||
utf8_state = 0;
|
||||
sum = 0;
|
||||
for(;;)
|
||||
{
|
||||
e = utf8_next((uint8_t)*str);
|
||||
if ( e == 0x0ffff )
|
||||
break;
|
||||
str++;
|
||||
if ( e != 0x0fffe )
|
||||
{
|
||||
delta = drawGlyph(x, y, e);
|
||||
|
||||
switch(u8g2.font_decode.dir)
|
||||
{
|
||||
case 0:
|
||||
x += delta;
|
||||
break;
|
||||
case 1:
|
||||
y += delta;
|
||||
break;
|
||||
case 2:
|
||||
x -= delta;
|
||||
break;
|
||||
case 3:
|
||||
y -= delta;
|
||||
break;
|
||||
}
|
||||
|
||||
sum += delta;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int16_t U8G2_FOR_ADAFRUIT_GFX::getUTF8Width(const char *str)
|
||||
{
|
||||
uint16_t e;
|
||||
int16_t dx, w;
|
||||
|
||||
u8g2.font_decode.glyph_width = 0;
|
||||
utf8_state = 0;
|
||||
w = 0;
|
||||
dx = 0;
|
||||
for(;;)
|
||||
{
|
||||
e = utf8_next((uint8_t)*str);
|
||||
if ( e == 0x0ffff )
|
||||
break;
|
||||
str++;
|
||||
if ( e != 0x0fffe )
|
||||
{
|
||||
dx = u8g2_GetGlyphWidth(&u8g2, e);
|
||||
w += dx;
|
||||
}
|
||||
}
|
||||
/* adjust the last glyph, check for issue #16: do not adjust if width is 0 */
|
||||
if ( u8g2.font_decode.glyph_width != 0 )
|
||||
{
|
||||
w -= dx;
|
||||
w += u8g2.font_decode.glyph_width; /* the real pixel width of the glyph, sideeffect of GetGlyphWidth */
|
||||
/* issue #46: we have to add the x offset also */
|
||||
w += u8g2.glyph_x_offset; /* this value is set as a side effect of u8g2_GetGlyphWidth() */
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
343732
arduino-libs/arduino-cli/libraries/U8g2_for_Adafruit_GFX/src/u8g2_fonts.c
Normal file
343732
arduino-libs/arduino-cli/libraries/U8g2_for_Adafruit_GFX/src/u8g2_fonts.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
|
||||
u8g2_fonts.h
|
||||
|
||||
Several definitions for the fonts, included and required for u8g2_fonts.c.
|
||||
If the u8g2_fonts.c file is copied from u8g2 project, replace the include
|
||||
from "u8g2.h" to "u8g2_fonts.h"
|
||||
|
||||
U8g2 for Adafruit GFX Lib (https://github.com/olikraus/U8g2_for_Adafruit_GFX)
|
||||
|
||||
Copyright (c) 2018, olikraus@gmail.com
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or other
|
||||
materials provided with the distribution.
|
||||
|
||||
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
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _u8g2_fonts_h
|
||||
#define _u8g2_fonts_h
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __GNUC__
|
||||
# define U8X8_NOINLINE __attribute__((noinline))
|
||||
# define U8X8_SECTION(name) __attribute__ ((section (name)))
|
||||
# define U8X8_UNUSED __attribute__((unused))
|
||||
#else
|
||||
# define U8X8_SECTION(name)
|
||||
# define U8X8_NOINLINE
|
||||
# define U8X8_UNUSED
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && defined(__AVR__)
|
||||
# define U8X8_FONT_SECTION(name) U8X8_SECTION(".progmem." name)
|
||||
# define u8x8_pgm_read(adr) pgm_read_byte_near(adr)
|
||||
# define U8X8_PROGMEM PROGMEM
|
||||
#endif
|
||||
|
||||
#ifndef U8X8_FONT_SECTION
|
||||
# define U8X8_FONT_SECTION(name)
|
||||
#endif
|
||||
|
||||
#ifndef u8x8_pgm_read
|
||||
# define u8x8_pgm_read(adr) (*(const uint8_t *)(adr))
|
||||
#endif
|
||||
|
||||
#ifndef U8X8_PROGMEM
|
||||
# define U8X8_PROGMEM
|
||||
#endif
|
||||
|
||||
#define U8G2_FONT_SECTION(name) U8X8_FONT_SECTION(name)
|
||||
|
||||
/* the macro U8G2_USE_LARGE_FONTS enables large fonts (>32K) */
|
||||
/* it can be enabled for those uC supporting larger arrays */
|
||||
#if defined(unix) || defined(__arm__) || defined(__arc__) || defined(ESP8266) || defined(ESP_PLATFORM)
|
||||
#ifndef U8G2_USE_LARGE_FONTS
|
||||
#define U8G2_USE_LARGE_FONTS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _u8g2_fonts_h */
|
||||
|
||||
Reference in New Issue
Block a user