void ESPHB::read_configs(configs *_cf,defaults *_df){ firststart = read1byte_EEPROM(__first_start); isdebug = read1byte_EEPROM(__debug); ipstatic=read1byte_EEPROM(__ip_static); char *_a = read_EEPROM(__serial_adrr); serial=_a; free(_a); // Read serial of device char *_c = read_EEPROM(__ssid_adrr); ssid = _c; free(_c); // Read ssid of wifi char *_d = read_EEPROM(__password_adrr); password = _d; free(_d); // Read password of wifi if (!firststart) { // write serial update_EEPROM(__serial_adrr, __serial_max, _df->serial); update_EEPROM(__key_adrr, __key_max, _df->key); update_EEPROM(__admin_adrr, __admin_max, _df->admin); debug("\nUpdated: serial - " + _df->serial + "key: " + _df->key +"Admin key: "+_df->admin); update1byte_EEPROM(__first_start, 1); } char *_b = read_EEPROM(__key_adrr); _cf->key = _b; free(_b); // Read key to connect to device char *_e = read_EEPROM(__admin_adrr); _cf->admin = _e; free(_e); // Read admin key of device _cf->serial = serial; _cf->ssid = ssid; _cf->password = password; for (unsigned char i = 0; i < 4; i++) { ip[i] = read1byte_EEPROM(i + __IP_adrr + 1); _cf->ip[i] = ip[i]; // Read byte to byte, start from second byte of block content in EEPROM } for (unsigned char i = 0; i < 4; i++) { gateway[i] = read1byte_EEPROM(i + __gateway_adrr + 1); _cf->gateway[i] = gateway[i]; // Read byte to byte, start from second byte of block content in EEPROM } for (unsigned char i = 0; i < 4; i++) { mask[i] = read1byte_EEPROM(i + __mask_adrr + 1); _cf->mask[i] = mask[i]; // Read byte to byte, start from second byte of block content in EEPROM } };
boolean ESPHB::save_EEPROM(unsigned char _content,String _value){ char len = _value.length() + 1; // leghth of array (=leghth of string +1) will be write to EEPROM; +1 because: end of array is "\0" char arr[len]; // Init an array _value.toCharArray(arr, len); // Convert string to array if(MAX_LEN_EEPROM(_content)>=len) { // Write array to EEPROM when leghth of array <= max leghth in EEPROM EEPROM.write(_content, len); // Write leghth of array in fisrt byte of block content in EEPROM for (unsigned char i = 0; i < len; i++) { EEPROM.write(i + _content + 2, arr[i]); // Write byte by byte, start from second byte of block content in EEPROM } update_EEPROM(_address, _maxlen, _value); char *_a = read_EEPROM(__serial_adrr); *_return = _a; free(_a); // Read serial of device return true; } else return false; };
int main(int argc, char **argv) { char filename[20]; // device to open int uInput; // user input int pageRWS; // num of pages to read, write char inp[64]; // input from user char *buf; // or set //create the file name according to adapter num snprintf(filename, 19, "/dev/i2c-%d", ADAPT_NUMB); //first we open the device and verify that it was //opened correctly fd = open(filename, O_RDWR); if (fd < 0) { printf("\n%s could not be opened, or was not found\n", filename); exit(1); } //set slave address, check for errors if (ioctl(fd, I2C_SLAVE, SLAVE_ADDR) < 0) { printf("\n%d address not found.\n"); exit(1); } //do some reading or writing do { printf("\n1.Read\n2.Write\n3.Set\n4.Quit\n"); scanf("%d", &uInput); switch(uInput) { case 1 : printf("\nRead Statement\n"); printf("\nHow many pages to read?\n"); scanf("%d", &pageRWS); buf = malloc(sizeof(char) * PAGE_BYTES * pageRWS); memset(buf, 0, sizeof(char) * 64 * pageRWS); read_EEPROM(buf, pageRWS); printBuf(buf, PAGE_BYTES * pageRWS, PAGE_BYTES); free(buf); break; case 2 : printf("\nWrite Statement\n"); printf("\nHow many pages to write?\n"); scanf("%d", &pageRWS); buf = malloc((sizeof(char) * PAGE_BYTES * pageRWS)); memset(buf, 0, sizeof(char) * 64 * pageRWS); printf("\nData to write:\n"); scanf("%s", buf); write_EEPROM(buf, pageRWS); free(buf); break; case 3 : printf("\nSet Statement\n"); printf("\nNew device offset?\n"); scanf("%d", &pageRWS); seek_EEPROM(pageRWS); break; case 4 : printf("\nQuit\n"); break; default: printf("\nUnknown Command\n"); break; } } while (uInput != 4); close(fd); return 0; }