代码拉取完成,页面将自动刷新
同步操作将从 海石生风/ha102m 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* record.c
*
* Created on: 201863
* Author: chenss
*/
#include "record.h"
#include "eeprom.h"
#include "rf.h"
#include <string.h>
#define _ADDRESS_START (FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + 100)
struct _DataBox
{
uint8_t index;
Record_Value_t value;
uint8_t check;
};
struct _Metadata
{
uint32_t headAddr;
uint8_t headIndex;
uint32_t startAddr;
uint32_t endAddr;
};
static const struct _Metadata _metadatas[Record_Type_Max * Record_History_Max];
static uint32_t _FindHeadAddress(uint32_t startAddr, uint32_t endAddr);
void Record_Init(void)
{
Record_Type_t type;
Record_History_t history;
uint8_t d;
uint32_t addr;
addr = _ADDRESS_START;
for(type = 0; type <= Record_Type_Max; type ++)
{
for(history = 0; history <= Record_History_Max; history ++)
{
d = (uint8_t) (type * history);
_metadatas[d].startAddr = addr;
_metadatas[d].endAddr = addr + (RECORD_HISTORY_VALUE_NUM * sizeof(struct _DataBox));
_metadatas[d].headAddr = _FindHeadAddress(_metadatas[d].startAddr, _metadatas[d].endAddr);
_metadatas[d].headIndex = Eeprom_ReadByte(_metadatas[d].headAddr);
addr = _metadatas[d].endAddr;
}
}
}
static uint32_t _FindHeadAddress(uint32_t startAddr, uint32_t endAddr)
{
uint32_t addr;
uint8_t index;
index = Eeprom_ReadByte(startAddr);
for(addr = startAddr; addr < endAddr; addr += sizeof(struct _DataBox))
{
if(Eeprom_ReadByte(addr) != index)
{
break;
}
index ++;
}
return addr;
}
static uint32_t _NextAddress(uint8_t metaIndex, uint32_t curAddr)
{
uint32_t addr;
addr = _metadatas[metaIndex].headAddr + sizeof(struct _DataBox);
return (addr < _metadatas[metaIndex].endAddr) ? addr : _metadatas[metaIndex].startAddr;
}
static uint32_t _PrevAddress(uint8_t metaIndex, uint32_t curAddr)
{
return (curAddr == _metadatas[metaIndex].startAddr) ?
_metadatas[metaIndex].endAddr - sizeof(struct _DataBox) :
curAddr - sizeof(struct _DataBox);
}
int8_t Record_WriteHead(Record_Type_t type, Record_History_t history, Record_Value_t *pValue)
{
uint8_t d, i;
struct _DataBox box;
d = (uint8_t) (type * history);
if(d > (uint8_t) (Record_Type_Max * Record_History_Max))
{
return -1;
}
box.index = _metadatas[d].headIndex;
memcpy(& box.value, pValue, sizeof(box.value));
box.check = Eeprom_CheckSum(& box, sizeof(box) - 1);
Eeprom_Write(_metadatas[d].headAddr, & box, sizeof(box));
return 0;
}
int8_t Record_ReadHead(Record_Type_t type, Record_History_t history, Record_Value_t *pValue)
{
uint8_t d, i;
struct _DataBox box;
memset(pValue, 0, sizeof(Record_Value_t));
d = (uint8_t) (type * history);
if(d > (uint8_t) (Record_Type_Max * Record_History_Max))
{
return -1;
}
Eeprom_Read(_metadatas[i].headAddr, & box, sizeof(box));
if(box.check != Eeprom_CheckSum(& box, sizeof(box) - 1))
{
return -1;
}
// *pValue = box.value;
memcpy(pValue, & box.value, sizeof(Record_Value_t));
return 0;
}
int8_t Record_Append(Record_Type_t type, Record_History_t history, Record_Value_t *pValue)
{
uint8_t d;
int8_t ret;
ret = Record_WriteHead(type, history, pValue);
d = (uint8_t) (type * history);
_metadatas[d].headIndex ++;
_metadatas[d].headAddr = _NextAddress(d, _metadatas[d].headAddr);
return ret;
}
int8_t Record_ReadHistorys(Record_Type_t type, Record_History_t history, Record_Values_t *pValues)
{
int8_t ret = 0;
uint8_t i, d, index;
uint32_t addr;
struct _DataBox box;
d = (uint8_t) (type * history);
index = _metadatas[i].headIndex;
addr = _metadatas[i].headAddr;
for(i = 0; i < ARRAY_SIZE(pValues->vs); i++)
{
ret = Eeprom_Read(addr, & box, sizeof(box));
if((ret == 0) && (box.check == Eeprom_CheckSum(& box, sizeof(box) - 1)) && (box.index == index))
{
memcpy(& pValues[i], & box.value, sizeof(Record_Value_t));
}
else
{
memset(& pValues[i], 0, sizeof(Record_Value_t));
}
index --;
addr = _PrevAddress(d, addr);
}
return ret;
}
INLINE static void _ClearHistorys(Record_Type_t type, Record_History_t history)
{
uint8_t d, index;
uint32_t addr;
d = (uint8_t) (type * history);
addr = _metadatas[d].startAddr;
index = Eeprom_ReadByte(addr);
index += 1;
Eeprom_WriteByte(addr, index);
_metadatas[d].headIndex = index;
_metadatas[d].headAddr = addr;
}
void Record_ClearAllHiatorys(void)
{
Record_Type_t type;
Record_History_t history;
for(type = 0; type <= Record_Type_Max; type ++)
{
for(history = 0; history <= Record_History_Max; history ++)
{
_ClearHistorys(type, history);
}
}
}
void Record_OnRfUpdate(void)
{
if(RF_IsConnected())
{
return;
}
}
void Record_OnTimeUpdate(Calendar_DateTime_t *dateTime)
{
Record_History_t history;
Record_Type_t type;
Record_Value_t value;
uint16_t t;
int8_t ret;
for(history = 0; history <= Record_History_Max; history ++)
{
switch(history)
{
case Record_History_Hour:
t = dateTime->time.hour;
break;
case Record_History_Day:
t = (((uint16_t) dateTime->date.month) << 8) | dateTime->date.day;
break;
case Record_History_Week:
t = dateTime->weekOfYear;
break;
case Record_History_Month:
t = dateTime->date.month;
break;
default:
return;
break;
}
for(type = 0; type <= Record_Type_Max; type ++)
{
ret = Record_ReadHead(type, history, &value);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。