2019年2月8日 星期五

CharArray_Write EEPROM & Read EEPROM

/*
   2019-02-08 test OK by yulie
*/

char WifiName[] = "jigu";                               // Wifi name
char WifiPass[] = "0000";                               // Wifi password
//char WifiName[] = "M8_1";                             // Wifi name
//char WifiPass[] = "poiuytrewqpoi";                    // Wifi password


#include <EEPROM.h>

void setup() {
  Serial.begin(115200);
  //  EEPROM.begin(2048); // only for  ESP Chip


  //------------ write EEPROM to WifiName --------------------------------------------------------
  //  saveChar( WifiName, sizeof(WifiName), 0x10);  //將 WifiName 寫入EEPROM 0x10位置

  //------------ write EEPROM to WifiPass ----------------------------------------------------
  //  saveChar( WifiPass, sizeof(WifiPass), 0x20);  //將 WifiName 寫入EEPROM 0x20位置

  Serial.print("OLD_SSID: ");   Serial.write(WifiName, sizeof(WifiName)); Serial.println();
  Serial.print("OLD_PASS: ");   Serial.write(WifiPass, sizeof(WifiPass)); Serial.println();

  Serial.println("Read SSID & PASS  form EEPROM ... ");

  //------------ read EEPROM to WifiName --------------------------------------------------------
  String strName = (char*)readChar(0x10);                         // 將0x10讀到的字元陣列轉換成串流(strName)
  uint8_t lenN = strName.length();                                 // 取得串流長度
  char WifiName[lenN];                                          //字元陣列長度重設
  for (word i = 0; i < lenN; i++)WifiName[i] = strName.charAt(i);  // 將strName串流轉換成WifiName字元陣列
  Serial.print("New_SSID: "); Serial.write(WifiName, sizeof(WifiName)); Serial.println();

  //------------- read EEPROM to WifiPass -------------------------------------------------------
  String strPass = (char*)readChar(0x20);                         // 將0x20讀到的字元陣列轉換成串流(strPass)
  uint8_t lenP = strPass.length();                                // 取得串流長度
  char WifiPass[lenP];                                         //字元陣列長度重設
  for (word i = 0; i < lenP; i++)WifiPass[i] = strPass.charAt(i); // 將strPass串流轉換成WifiPass字元陣列
  Serial.print("New_PASS: "); Serial.write(WifiPass, sizeof(WifiPass)); Serial.println();

}

//----------------------- read EEPROM char array function return -----------
char* readChar (word addr) {
  uint8_t len = EEPROM.read(addr);                           // 讀取串流長度
  static  char buf[32];                                         // max len 32 char
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) buf[i] = EEPROM.read(addr + i);
  //  Serial.write(buf, sizeof(buf)); Serial.println();
  return buf;
}


//---------- char array function return demo -----------
char* foo () {
  static char buf [100] = "But does it get goat's blood out?";
  return buf;
}  // end of foo




  //<< ========== write char[] to EEPROM =============================================
void saveChar(char charName[], byte lenIn, word addr) {  //Ex: saveChar( WifiName, sizeof(WifiName), 0x00);
  uint8_t len = lenIn - 1;        // 取得串流長度
  EEPROM.write(addr, len);     // 紀錄串流長度
  delay(5);                       // write takes 3.3 ms to complete
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) {
    EEPROM.write(addr + i, charName[i]);
    delay(5); // write takes 3.3 ms to complete
  }
  delay(200);
//  Serial.print("write char: "); Serial.write(charName, len);      // 顯示字元陣列內容
//  Serial.print("  len: ");    Serial.print(len);
  Serial.print("  address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + len, HEX);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  //  delay(200);
}

/*
  //<< ========== read char[] form EEPROM =============================================
  byte readChar(char charName[],  word addr) {           //Ex:   readChar( WifiName, 0x00);
  uint8_t len = EEPROM.read(addr);                           // 讀取串流長度
  Serial.print("read  len: ");    Serial.println(len);
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) {
    charName[i] = EEPROM.read(addr + i);
    delay(5);
  }
  delay(20);
  Serial.print("read char: "); Serial.write(charName, len);      // 顯示字元陣列內容
  Serial.print("  len: ");    Serial.print(len);
  Serial.print("  address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + len, HEX);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  //  delay(200);
  len = len - 1;
  Serial.print(" return len: "); Serial.println(len);
  return len  ;
  }
*/

void loop() {}

沒有留言:

張貼留言