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

查看:576
本文介绍了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).该字符串应以null结尾,这样多余的字节就不会造成问题.

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天全站免登陆