Arduino 从 EEPROM 读取 json/将 uint8_t 转换为 char [英] Arduino reading json from EEPROM / converting uint8_t to char

查看:141
本文介绍了Arduino 从 EEPROM 读取 json/将 uint8_t 转换为 char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ArduinoJSON 向我的 Arduino Uno 上的 EEPROM 写入几个数据点.我遇到了 getGroundedPR 问题,我需要将 uint8_t 转换为 char 以将检索到的数据传递到我的 JSON 解析器中.

I'm using ArduinoJSON to write a couple of data points to my EEPROM on Arduino Uno. I'm running into a problem with getGroundedPR where I need to convert a uint8_t to a char to pass retrieved data into my JSON parser.

这是我第一次使用 EEPROM,所以我敢打赌有更好的方法来做到这一点.我应该继续使用 JSON 还是有更好的方法?我对 EEPROM 的 10k 写入限制(给予或接受)持谨慎态度.

This is my first time using EEPROM so I'm willing to bet there's a better way to do this. Should I continue to use JSON or is there a better way? I'm being cautious of the 10k write limit (give or take) on the EEPROM.

EEPROM 读/写被注释掉,直到我确定我的进程

void IMUController::setGroundedPR(double p, double r) {
  Serial.print("Setting IMU ground: ");

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["pitch"] = p;
  root["roll"] = r;

  root.printTo(Serial);

  char buffer[256];
  root.printTo(buffer, sizeof(buffer));
  Serial.println();

//  EEPROM.write(EEPROM_ADDRESS_IMU_GROUNDED, buffer);
}

double* IMUController::getGroundedPR() {
  double ret[2] = {0, 0};
  StaticJsonBuffer<200> jsonBuffer;
  uint8_t json_saved = EEPROM.read(EEPROM_ADDRESS_IMU_GROUNDED);
  char json[] = "asdf"; // convert json_saved to char here

  JsonObject& root = jsonBuffer.parseObject(json);

  if(!root.success()) {
    // return the result
    ret[0] = (double)root["pitch"];
    ret[1] = (double)root["roll"];
    return ret;
  }

  return ret;
}

推荐答案

EEPROM 函数 read()write() 只处理单个字符.您需要使用put()get() 来处理数组.

The EEPROM functions read() and write() only deal with a single character. You need to use put() and get() to deal with arrays.

char buffer[256];
root.printTo(buffer, sizeof(buffer));
EEPROM.put(EEPROM_ADDRESS_IMU_GROUNDED, buffer);

然后再读一遍:

char json[256];
EEPROM.get(EEPROM_ADDRESS_IMU_GROUNDED, json);
JsonObject& root = jsonBuffer.parseObject(json);

尽管您需要注意数组大小,但 EEPROM 函数将获取并放入数组中的字节数 (256).字符串应该以空字符结尾,这样额外的字节就不会造成问题.

You need to take care with he array sizes though, the EEPROM functions will get and put the number of bytes in the array (256). The string should be null terminated so the extra bytes shouldn't cause a problem.

这篇关于Arduino 从 EEPROM 读取 json/将 uint8_t 转换为 char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆