#include <EEPROM.h>
uint8_t Count ;
uint16_t Capacity;
template <typename T>
class nvs
{
private:
uint8_t sign = -1; // 返回值时为0, 赋值时为1, 无操作为-1
uint8_t position = 0; // 返回值时为0, 赋值时为1, 无操作为-1
T val;
public:
// 构造函数
nvs(T _val) {
sign = 1;
val = _val;
Count ++; // 创建的总个数
position = Capacity; //变量所在位置
Capacity += sizeof(::nvs<T>) / 2; // 创建总容量
Serial.printf("[构造]Position: %d \n",position);
}
// = 重载
T operator = (T _val) {
Serial.printf("[赋值]Position: %d \n",position);
sign = 0;
val = _val;
return val;
}
// 强制转换重载
operator T() {
if (sign == 0) {
// Serial.printf("[赋值]Position: %d Set value: ",position);
sign = -1;
return val;
}
else if (sign == 1)
{
sign = -1;
// Serial.printf("[构造]Position: %d Set value: ",position);
return val;
}
else {
// Serial.print("Get value: ");
return val;
}
}
};
void setup() {
Serial.begin(115200);
// 构造赋值测试
nvs<uint8_t> u = 23;
// Serial.println(u);
// 赋值测试
// u = 25;
// Serial.println(u);
// 类型转换测试
uint8_t x = u;
// Serial.println(x);
// nvs<uint16_t> v = 23;
// nvs<uint32_t> w = 23;
Serial.printf("总共有 %d 个值, 占 %dbyte 容量\n", Count, Capacity);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}