完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嘿,我在使用这个 Wemos D1 迷你 ESP8266 控制器的 SPIFFS 时遇到了一些问题。
这是我的 Arduino 草图: 代码:全选#include #include struct RGBLA { uint8_t R; uint8_t G; uint8_t B; uint8_t L; uint8_t A; }; void setup() { if (!SPIFFS.begin()) { Serial.println("error while mounting filesystem!"); } else { // put your setup code here, to run once: RGBLA returnedVars = readSPIFFS(); Serial.begin(115200); Serial.println(); Serial.println("Starting..."); Serial.println("readSPIFFS 1"); Serial.print("R: "); Serial.println(returnedVars.R); Serial.print("G: "); Serial.println(returnedVars.G); Serial.print("B: "); Serial.println(returnedVars.B); Serial.print("L: "); Serial.println(returnedVars.L); Serial.print("AmbientLight: "); Serial.println(returnedVars.A); Serial.println("=================================="); returnedVars = saveToSPIFFS(25,77, 107, 250, 155, false); Serial.println("saveToSPIFFS #1"); Serial.print("R: "); Serial.println(returnedVars.R); Serial.print("G: "); Serial.println(returnedVars.G); Serial.print("B: "); Serial.println(returnedVars.B); Serial.print("L: "); Serial.println(returnedVars.L); Serial.print("AmbientLight: "); Serial.println(returnedVars.A); Serial.println("=================================="); returnedVars = saveToSPIFFS(205,17, 68, 50, 15, true); Serial.println("saveToSPIFFS #2"); Serial.print("R: "); Serial.println(returnedVars.R); Serial.print("G: "); Serial.println(returnedVars.G); Serial.print("B: "); Serial.println(returnedVars.B); Serial.print("L: "); Serial.println(returnedVars.L); Serial.print("AmbientLight: "); Serial.println(returnedVars.A); returnedVars = readSPIFFS(); Serial.println("readSPIFFS 2"); Serial.print("R: "); Serial.println(returnedVars.R); Serial.print("G: "); Serial.println(returnedVars.G); Serial.print("B: "); Serial.println(returnedVars.B); Serial.print("L: "); Serial.println(returnedVars.L); Serial.print("AmbientLight: "); Serial.println(returnedVars.A); Serial.println(); Serial.println("END!"); } } void loop() { // put your main code here, to run repeatedly: } RGBLA saveToSPIFFS(uint8_t R, uint8_t G, uint8_t B, uint8_t L, uint8_t A, bool saveA) { File configFile = SPIFFS.open("/config.json", "w+"); if (configFile.size() > 3072) { Serial.println("Config file size is too large."); } else { Serial.print("Writing json to Config file..."); StaticJsonBuffer<200> jsonBuffer; JsonObject& json = jsonBuffer.createObject(); //Get the old value and save it RGBLA returnedVars = readSPIFFS(); Serial.println("Old Values?"); Serial.print("R: "); Serial.println(returnedVars.R); Serial.print("G: "); Serial.println(returnedVars.G); Serial.print("B: "); Serial.println(returnedVars.B); Serial.print("L: "); Serial.println(returnedVars.L); Serial.print("AmbientLight: "); Serial.println(returnedVars.A); if (saveA) { //Save Ambient Light data json["R"] = returnedVars.R; json["G"] = returnedVars.G; json["B"] = returnedVars.B; json["L"] = returnedVars.L; json["A"] = A; //Now save the new data returnedVars.A = A; } else { //Save RGBL data json["R"] = R; json["G"] = G; json["B"] = B; json["L"] = L; json["A"] = returnedVars.A; //Now save the new data returnedVars.R = R; returnedVars.G = G; returnedVars.B = B; returnedVars.L = L; } json.printTo(configFile); configFile.close(); //Read new saved values into struct //returnedVars = readSPIFFS(); return returnedVars; } } RGBLA readSPIFFS() { bool exist = SPIFFS.exists("/config.json"); if (exist) { Serial.println("YAY!"); } else { Serial.println("Boo!"); } File configFile = SPIFFS.open("/config.json", "r"); std::unique_ptr configFile.readBytes(buf.get(), configFile.size()); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); RGBLA returnedVars; String debugLogData; json.printTo(debugLogData); Serial.println("==============="); Serial.println(debugLogData); Serial.println("==============="); returnedVars.R = json["R"]; returnedVars.G = json["G"]; returnedVars.B = json["B"]; returnedVars.L = json["L"]; returnedVars.A = json["A"]; configFile.close(); return returnedVars; } 这是控制台输出: 代码:全选Starting... readSPIFFS 1 R: 0 G: 0 B: 0 L: 0 AmbientLight: 25 ================================== Writing json to Config file...YAY! =============== {} =============== Old Values? R: 0 G: 0 B: 0 L: 0 AmbientLight: 0 saveToSPIFFS #1 R: 25 G: 77 B: 107 L: 250 AmbientLight: 0 ================================== Writing json to Config file...YAY! =============== {} =============== Old Values? R: 0 G: 0 B: 0 L: 0 AmbientLight: 0 saveToSPIFFS #2 R: 0 G: 0 B: 0 L: 0 AmbientLight: 15 YAY! =============== {"R":0,"G":0,"B":0,"L":0,"A":15} =============== readSPIFFS 2 R: 0 G: 0 B: 0 L: 0 AmbientLight: 25 |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
920 浏览 1 评论
552浏览 6评论
461浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
446浏览 5评论
447浏览 4评论
417浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 23:19 , Processed in 0.908916 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号