Arduino网络编程实战-从SD卡加载中文字模数据并显示 从SD卡加载中文字模数据并显示 JSON(JavaScript Object Notation)是一种开放的标准文件格式和数据交换格式,它使用人类可读的文本来存储和传输由属性-值对和数组(或其他可序列化值)组成的数据对象。 它是一种常见的数据格式,在电子数据交换中具有多种用途,包括带有服务器的 Web 应用程序。
JSON 是一种独立于语言的数据格式。 它源自 JavaScript,但许多现代编程语言都包含生成和解析 JSON 格式数据的代码。 JSON 文件名使用扩展名 .json。当数据从服务器发送到网页时,通常使用 JSON。JSON是“自我描述的”并且易于理解。
ArduinoJson 库为嵌入式系统提供了高性能的JSON数据序列化和反序列化支持。
前面的文章对Arduino中JSON数据解析做出详细的介绍,请参考:
SD卡模块对于需要数据记录的项目特别有用。前面的文章对Arduino如何使用SD卡做了详细的介绍,请参考:
OLED显示中文字符请参考:
本次实例将演示如何从SD卡加载中文字模数据并使用SSD1306 OLED显示。
1、硬件准备
Arduino Mega2560 开发板一块
SD卡模块一个及SD卡一个
SSD1306 OLED屏幕一块
数据线一条
杜邦线若干
硬件接线,请关于前面相关文章,在这里不再做描述。
2、软件准备
3、JSON储存中文字模数据说明 本次实例储存字模数据的JSON格式如下(在这里文件名为:cn_huang.json,并保存在SD卡根目录的fonts下):
1 2 3 4 5 6 { width:16, height:16, datas:[0,128,0,128,252,128,4,252,5,4,73,8,42,64,20,64,16,64,40,160,36,160,69,16,129,16,2,8,4,4,8,2], }
由于ArduinoJson库不支持解析十六进制数组数据,因此在生成中文字模数据时,请将Pctolcd2002设置为:
生成十进制格式数据,并将生成的数分别保存到文件中。
4、代码实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 #include <SPI.h> #include <Wire.h> #include <Adafruit\_GFX.h> #include <Adafruit\_SSD1306.h> #include <SdFat.h> #include <ArduinoJson.h> #define SCREEN\_WIDTH 128 // OLED display width, in pixels #define SCREEN\_HEIGHT 64 // OLED display height, in pixels #define LOGO\_HEIGHT 32 #define LOGO\_WIDTH 32 #define OLED\_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN\_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // SD\_FAT\_TYPE = 0 for SdFat/File as defined in SdFatConfig.h, // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT. // SD卡文件系统格式类型 #define SD\_FAT\_TYPE 0 #define SD\_CS\_PIN 4 #define SPI\_CLOCK SD\_SCK\_MHZ(50) #define SD\_CONFIG SdSpiConfig(SD\_CS\_PIN, DEDICATED\_SPI, SPI\_QUARTER\_SPEED) #if SD\_FAT\_TYPE == 0 SdFat sd; File file; #elif SD\_FAT\_TYPE == 1 SdFat32 sd; File32 file; #elif SD\_FAT\_TYPE == 2 SdExFat sd; ExFile file; #elif SD\_FAT\_TYPE == 3 SdFs sd; FsFile file; #else // SD\_FAT\_TYPE #error Invalid SD\_FAT\_TYPE #endif // SD\_FAT\_TYPE unsigned char img_datas [1024] = {0}; const String font_files[] = { "fonts/cn\_huan.json", "fonts/cn\_ying.json", "fonts/cn\_shi.json", "fonts/cn\_yong.json", "fonts/cn\_tian.json", "fonts/cn\_qi.json", "fonts/cn\_yu.json", "fonts/cn\_bao.json"}; StaticJsonDocument<2048> doc; void setup() { Serial.begin(9600); // SSD1306\_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. display.display(); // Clear the buffer display.clearDisplay(); Serial.println("Initializing SD card..."); // Initialize the SD. if (!sd.begin(SD_CONFIG)) { sd.initErrorHalt(&Serial); return; } Serial.println("Initialized SD card..."); delay(2000); for(int j = 0;j < 8;j++){ doc.clear(); Serial.println(font_files[j].c\_str()); if (!file.open(font_files[j].c\_str(), O_RDWR)) { Serial.println("open failed"); while(true); } DeserializationError error = deserializeJson(doc, file); int width = doc["width"].as<int>(); int height = doc["height"].as<int>(); int len; //serializeJson(doc, Serial); memset(img_datas,0,1024); Serial.print("data len:"); Serial.println(doc["datas"].as<JsonArray>().size()); len = doc["datas"].as<JsonArray>().size(); for(int i = 0;i < len;i++){ img_datas[i] = doc["datas"][i].as<unsigned char>(); } file.close(); // 绘制字符 display.drawBitmap(j \* 16, 32, img_datas, 16, 16, 1); display.display(); delay(500); } } void loop() { }
5、运行结果
文章来源: https://iotsmart.blog.csdn.net/article/details/123028237
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!