1 Star 0 Fork 1

yjwpm/ha102m

forked from 海石生风/ha102m 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
record.c 5.34 KB
一键复制 编辑 原始数据 按行查看 历史
chenss 提交于 2018-06-26 23:44 . 开始按时存储数据
/*
* 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);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/yjwpm/ha102m.git
[email protected]:yjwpm/ha102m.git
yjwpm
ha102m
ha102m
master

搜索帮助