限制
Sketch uses 8396 bytes (26%) of program storage space. Maximum is 32256 bytes.
Global variables use 1780 bytes (86%) of dynamic memory, leaving 268 bytes for local variables. Maximum is 2048 bytes.
定义变量时const uint8_t lishun_resized_map[64][64][3],会发现arduino 全局变量的大小那一块放不下一张图,把一张图缩放到到32x48的尺寸占用了86%。
加上 PROGMEM 可将变量放到程序区(32kB大小) const PROGMEN uint8_t lishun_resized_map[64][64][3]
步骤
生利用python生成对应像素的颜色信息,在利用ucglib库的setColor和drawPixel画图片。代码如下
# 灰度化 加二值化
import cv2
import numpy as np
img = cv2.imread("C:\\Users\\cls\\OneDrive\\图片\\lishun.jpg")
img = cv2.resize(img, (48, 32))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite("C:\\Users\\cls\\OneDrive\\图片\\lishun_gray_thres.jpg", img)
w, h = img.shape[0], img.shape[1]
print(w, h)
color = np.zeros((w, h)).astype(np.uint8)
with open("C:\\Users\\cls\\OneDrive\\图片\\lishun_color_gray.txt", "w") as f:
f.write("{")
for i in range(w):
f.write("{")
for j in range(h):
color[i, j] = img[i, j]
# f.write(str(img[i, j, :]) + "\n")
f.write(str(color[i, j]) + ",")
f.write("},")
f.write("};")
# 抽取像素和颜色
import cv2
import numpy as np
img = cv2.imread("C:\\Users\\cls\\OneDrive\\图片\\lishun.jpg")
img = cv2.resize(img, (32, 32))
w, h = img.shape[0], img.shape[1]
print(w, h)
color = np.zeros((w, h, 3)).astype(np.uint8)
with open("C:\\Users\\cls\\OneDrive\\图片\\lishun_color.txt", "w") as f:
f.write("{")
for i in range(w):
f.write("{")
for j in range(h):
color[i, j, :] = img[i, j, :]
# f.write(str(img[i, j, :]) + "\n")
f.write("{" + str(color[i, j, 0]) + "," + str(color[i, j, 1]) + "," + str(color[i, j, 2]) + "},")
f.write("},")
f.write("};")
/*
HelloWorld.ino
A very simple example for Ucglib
Universal uC Color Graphics Library
Copyright (c) 2014, 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.
18 Feb 2014:
ROM Size Font Mode
9928 NONE
10942 TRANSPARENT (1014 Bytes)
11650 SOLID (1712 Bytes)
12214 TRANSPARENT+SOLID
*/
#include <SPI.h>
#include "Ucglib.h"
/*
Hardware SPI Pins:
Arduino Uno sclk=13, data=11
Arduino Due sclk=76, data=75
Arduino Mega sclk=52, data=51
>>> Please uncomment (and update) one of the following constructors. <<<
*/
//Ucglib8BitPortD ucg(ucg_dev_ili9325_18x240x320_itdb02, ucg_ext_ili9325_18, /* wr= */ 18 , /* cd= */ 19 , /* cs= */ 17, /* reset= */ 16 );
//Ucglib8Bit ucg(ucg_dev_ili9325_18x240x320_itdb02, ucg_ext_ili9325_18, 0, 1, 2, 3, 4, 5, 6, 7, /* wr= */ 18 , /* cd= */ 19 , /* cs= */ 17, /* reset= */ 16 );
//Ucglib4WireSWSPI ucg(ucg_dev_ili9325_18x240x320_itdb02, ucg_ext_ili9325_18, /*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8); // not working
//Ucglib4WireSWSPI ucg(ucg_dev_ili9325_spi_18x240x320, ucg_ext_ili9325_spi_18, /*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8); // not working
//Ucglib3WireILI9325SWSPI ucg(ucg_dev_ili9325_spi_18x240x320, ucg_ext_ili9325_spi_18, /*sclk=*/ 13, /*data=*/ 11, /*cs=*/ 10, /*reset=*/ 8); // not working
//Ucglib3WireILI9325SWSPI ucg(ucg_dev_ili9325_18x240x320_itdb02, ucg_ext_ili9325_18, /*sclk=*/ 13, /*data=*/ 11, /*cs=*/ 10, /*reset=*/ 8); // not working
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_ILI9163_18x128x128_SWSPI ucg(/*sclk=*/ 7, /*data=*/ 6, /*cd=*/ 5, /*cs=*/ 3, /*reset=*/ 4);
//Ucglib_ILI9163_18x128x128_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8); /* HW SPI Adapter */
//Ucglib_ILI9341_18x240x320_SWSPI ucg(/*sclk=*/ 7, /*data=*/ 6, /*cd=*/ 5, /*cs=*/ 3, /*reset=*/ 4);
//Ucglib_ILI9341_18x240x320_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_ILI9341_18x240x320_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_ILI9341_18x240x320_SWSPI ucg(/*sclk=*/ 4, /*data=*/ 3, /*cd=*/ 6, /*cs=*/ 7, /*reset=*/ 5); /* Elec Freaks Shield */
//Ucglib_HX8352C_18x240x400_SWSPI ucg(/*sclk=*/ 7, /*data=*/ 6, /*cd=*/ 5, /*cs=*/ 3, /*reset=*/ 4);
//Ucglib_HX8352C_18x240x400_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_ILI9486_18x320x480_SWSPI ucg(/*sclk=*/ 7, /*data=*/ 6, /*cd=*/ 5, /*cs=*/ 3, /*reset=*/ 4);
//Ucglib_ILI9486_18x320x480_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1351_18x128x128_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1351_18x128x128_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1351_18x128x128_FT_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1351_18x128x128_FT_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1351_18x128x128_FT_SWSPI ucg(/*sclk=*/ 4, /*data=*/ 17, /*cd=*/ 16, /*cs=*/ 0, /*reset=*/ 2); /* FT SSD1351 direct connect to ESP32 */
//Ucglib_PCF8833_16x132x132_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cs=*/ 9, /*reset=*/ 8); /* linksprite board */
//Ucglib_PCF8833_16x132x132_HWSPI ucg(/*cs=*/ 9, /*reset=*/ 8); /* linksprite board */
//Ucglib_LD50T6160_18x160x128_6Bit ucg( /*d0 =*/ d0, /*d1 =*/ d1, /*d2 =*/ d2, /*d3 =*/ d3, /*d4 =*/ d4, /*d5 =*/ d5, /*wr=*/ wr, /*cd=*/ cd, /*cs=*/ cs, /*reset=*/ reset);
//Ucglib_LD50T6160_18x160x128_6Bit ucg( /*d0 =*/ 16, /*d1 =*/ 17, /*d2 =*/ 18, /*d3 =*/ 19, /*d4 =*/ 20, /*d5 =*/ 21, /*wr=*/ 14, /*cd=*/ 15); /* Samsung 160x128 OLED with 6Bit minimal interface with Due */
//Ucglib_LD50T6160_18x160x128_6Bit ucg( /*d0 =*/ 5, /*d1 =*/ 4, /*d2 =*/ 3, /*d3 =*/ 2, /*d4 =*/ 1, /*d5 =*/ 0, /*wr=*/ 7, /*cd=*/ 6); /* Samsung 160x128 OLED with 6Bit minimal interface with Uno */
//Ucglib_SSD1331_18x96x64_UNIVISION_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SSD1331_18x96x64_UNIVISION_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SEPS225_16x128x128_UNIVISION_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
//Ucglib_SEPS225_16x128x128_UNIVISION_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
// 用python生成的数据 复制过来即可
const uint8_t lishun_resized_map[32][48] = {{115,114,116,225,222,222,223,224,224,221,202,198,198,209,223,221,177,120,117,116,116,116,116,116,116,116,114,116,116,116,116,116,117,117,116,117,116,116,116,116,117,117,117,116,116,116,109,110,},{117,205,227,224,219,219,221,220,223,223,223,206,193,191,191,224,221,120,119,119,118,118,117,117,117,117,117,117,117,116,116,116,116,116,116,110,118,118,117,117,118,118,118,118,119,120,122,122,},{122,218,214,221,219,219,219,221,221,219,221,195,193,189,187,187,200,128,123,122,121,121,119,119,118,118,118,118,117,117,117,117,117,117,117,117,114,108,115,118,119,119,121,121,129,189,210,219,},{218,219,213,218,219,219,221,222,224,222,222,223,221,191,189,187,186,184,195,124,122,122,121,121,121,119,119,119,119,118,118,118,118,118,118,118,117,118,118,118,118,121,121,123,133,224,222,222,},{205,193,197,219,219,221,221,189,189,223,223,224,224,223,223,222,185,208,128,126,124,123,123,123,123,122,122,122,122,122,119,122,121,121,119,116,116,122,122,123,125,126,128,127,137,223,223,221,},{217,216,217,217,216,187,182,182,182,187,189,189,188,203,189,189,221,224,224,131,128,126,126,126,124,123,124,123,123,123,123,124,124,123,124,124,126,127,127,128,129,193,223,224,226,226,222,219,},{202,191,218,182,177,177,177,179,182,181,181,182,183,183,186,184,197,220,218,223,193,129,127,126,127,127,126,127,127,126,126,126,127,126,127,128,130,132,131,131,137,222,221,223,213,223,224,200,},{179,181,187,178,176,176,177,177,178,178,178,178,178,179,184,186,200,222,223,138,97,30,28,27,25,84,147,132,131,129,130,129,129,129,132,132,133,132,131,132,137,222,220,219,221,221,216,202,},{173,174,174,174,174,173,173,172,169,162,159,158,178,179,179,167,177,60,30,28,27,27,27,27,27,27,27,27,27,103,133,134,134,133,132,134,134,135,132,133,138,199,194,206,217,205,189,183,},{161,169,172,167,162,153,171,168,168,169,171,173,174,172,163,152,29,28,28,27,27,27,26,24,26,26,26,27,27,27,28,126,138,138,138,137,141,145,149,220,225,225,222,222,216,180,177,176,},{158,153,150,151,154,162,163,162,167,156,151,149,155,156,28,29,28,28,24,75,91,98,99,101,99,96,93,74,26,27,27,29,145,138,142,142,143,146,216,223,209,199,180,178,179,177,173,172,},{159,159,152,146,146,149,159,163,163,162,159,156,135,95,29,29,26,89,100,110,121,129,133,135,134,129,123,113,103,93,27,28,31,83,143,153,228,222,219,189,184,178,174,171,170,171,172,141,},{140,143,143,143,147,144,141,139,138,143,127,123,124,33,30,85,101,112,123,131,142,150,150,153,154,154,145,136,123,115,106,29,28,36,219,182,164,170,173,172,170,168,168,167,167,168,164,140,},{146,146,141,139,139,139,138,142,125,121,123,126,83,22,32,116,117,127,138,147,155,158,162,163,164,162,157,149,142,136,129,129,23,30,153,159,162,162,162,164,164,164,166,166,166,167,167,167,},{144,146,144,136,137,137,141,138,132,124,126,129,126,27,186,126,67,145,131,118,92,148,164,164,164,122,106,136,159,159,84,157,39,29,151,151,148,152,154,161,163,164,164,164,164,165,154,157,},{141,142,125,140,124,143,144,146,148,146,131,130,126,32,214,32,139,168,170,159,44,149,158,163,165,156,34,154,175,180,175,45,201,29,154,155,154,154,154,153,154,156,154,154,155,156,157,152,},{134,140,141,141,143,143,146,145,144,146,140,133,127,75,175,163,65,186,35,33,156,151,137,155,100,162,161,60,22,199,85,218,171,36,157,157,157,157,157,157,156,151,154,156,157,157,159,159,},{102,134,137,138,135,123,138,140,141,141,142,139,123,136,87,160,167,166,158,159,160,63,137,166,156,134,159,157,162,168,176,216,51,134,166,170,222,222,190,156,156,156,157,158,159,161,164,163,},{99,101,102,110,109,131,137,137,136,118,112,112,125,122,222,165,163,162,163,166,160,140,157,168,155,171,172,169,168,168,175,219,186,140,179,198,178,188,220,198,211,166,161,161,162,164,166,200,},{98,97,99,101,102,103,108,123,129,124,104,107,106,105,214,35,166,166,165,167,108,157,160,163,204,143,39,172,172,166,142,170,209,137,105,160,160,171,177,188,176,166,166,166,167,170,171,220,},{99,95,90,95,100,99,99,99,99,102,116,99,95,149,216,159,162,165,169,171,146,142,124,131,32,155,168,172,173,172,174,184,132,179,60,113,159,173,199,211,219,199,214,210,189,194,207,212,},{110,113,111,91,87,84,83,85,94,96,96,97,62,38,149,163,159,163,169,172,171,166,165,164,174,178,180,178,176,176,177,212,179,40,33,95,105,202,197,184,193,217,216,213,188,187,191,189,},{113,114,113,114,114,114,115,98,94,88,71,67,62,34,29,217,159,164,167,169,169,167,102,123,113,178,179,178,178,179,181,213,38,40,32,79,86,91,178,179,177,175,179,181,184,177,183,182,},{97,114,101,96,97,87,84,84,86,90,67,63,57,53,39,201,161,162,166,161,97,148,164,142,167,160,48,165,178,181,204,34,32,53,57,72,78,80,101,174,174,174,174,176,173,206,203,189,},{98,99,99,97,91,88,87,86,87,72,66,59,55,48,34,28,204,161,159,158,157,142,88,61,91,153,169,171,175,177,200,27,33,40,61,69,60,75,93,171,161,158,168,171,172,165,162,158,},{91,96,96,96,98,94,87,88,77,71,60,55,47,41,32,27,26,92,156,156,154,151,154,156,156,158,164,166,172,81,27,27,32,51,57,40,73,80,87,106,156,158,162,157,153,152,153,162,},{74,74,79,76,79,80,81,95,73,67,64,56,45,41,39,31,27,26,79,159,154,156,158,157,162,164,165,173,94,112,28,27,36,42,47,70,76,83,88,85,161,161,158,158,166,167,167,160,},{74,76,75,77,144,144,144,139,70,66,63,62,51,41,38,31,28,27,95,84,70,139,142,142,145,148,87,105,118,110,28,32,37,58,65,71,76,82,56,104,168,169,158,155,156,160,158,156,},{76,77,139,144,143,142,140,71,54,68,64,63,61,59,39,36,32,27,96,109,88,94,92,94,99,105,111,115,118,98,31,35,51,55,62,67,72,76,63,125,113,91,137,169,173,131,172,165,},{77,78,100,138,80,76,69,66,53,69,65,64,61,58,54,41,33,46,89,117,98,93,91,130,113,115,120,127,114,88,34,47,49,54,57,63,70,45,67,86,57,162,169,170,171,172,173,161,},{77,85,76,75,74,72,71,68,59,94,64,59,60,54,53,46,40,35,88,117,126,107,106,113,118,123,123,128,107,86,42,45,49,52,47,67,86,42,72,79,156,159,163,166,169,169,171,171,},{84,81,78,79,72,71,69,67,42,33,37,59,49,54,49,46,36,22,93,105,132,133,116,121,123,114,133,123,100,53,41,44,45,54,57,60,41,55,72,77,80,85,92,167,163,166,168,169,},};
void setup(void)
{
delay(1000);
ucg.begin(UCG_FONT_MODE_TRANSPARENT);
//ucg.begin(UCG_FONT_MODE_SOLID);
ucg.clearScreen();
}
void loop(void)
{
//ucg.setRotate90();
for(int i=0; i<32; i++) {
for(int j=0; j<48; j++) {
ucg.setColor(lishun_resized_map[i][j] / 3, lishun_resized_map[i][j] / 3, lishun_resized_map[i][j] / 3);
ucg.drawPixel(i + 64 - 16, j + 80 - 24);
}
}
delay(1000);
}