Skip to content

Instantly share code, notes, and snippets.

@igotit-anything
Created January 27, 2020 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igotit-anything/5c6cecb09035020d3ca1c24e69daba94 to your computer and use it in GitHub Desktop.
Save igotit-anything/5c6cecb09035020d3ca1c24e69daba94 to your computer and use it in GitHub Desktop.
//////file : cy_class_bmft5_unpack.h
#ifndef cy_class_bmft5_unpack_h
#define cy_class_bmft5_unpack_h
#include <stdint.h>
#include <string.h> // memcpy,NULL, ..
#include <stdlib.h>
typedef void(*cy_pf_v_pui8_ui32)(uint8_t *, uint32_t ) ;
class CBMFT5_Unpack
{
public:
CBMFT5_Unpack(uint16_t size_block
, uint16_t size_packet
, cy_pf_v_pui8_ui32 fp_use_packet_one);
~CBMFT5_Unpack();
private:
uint16_t Size_Block; // BMFT4 header 4byte + PacketArea
uint16_t Size_Packet;
uint16_t Size_BlockPacketArea;
uint16_t IndexFirst_BlockPacketArea; //
uint16_t NPE; // Number of Packet Edge in current Block PacketArea.
uint16_t IPF; // Index of Packet Edge in Block.
uint8_t BlockCount;
uint8_t * Packet; // will be dynamic allocation
cy_pf_v_pui8_ui32 cb_use_packet_one;
void proc_unpack_packet_smaller_packetarea_block(uint8_t * p_block);
void proc_unpack_packet_larger_packetarea_block(uint8_t * p_block);
void proc_unpack_packet_equal_packetarea_block(uint8_t * p_block);
void Use_Packet_One(void);
public:
void proc_unpack(uint8_t * p_block);
};
#endif
//////file : cy_class_bmft5_unpack.cpp
#include "cy_class_bmft5_unpack.h"
CBMFT5_Unpack::CBMFT5_Unpack(uint16_t size_block
, uint16_t size_packet
, cy_pf_v_pui8_ui32 fp_use_packet_one)
{
Size_Block = size_block;
Size_Packet = size_packet;
Size_BlockPacketArea = (Size_Block - 5); // 5 means BMFT5 header size.
IndexFirst_BlockPacketArea = 5; //
NPE = 0;
IPF = IndexFirst_BlockPacketArea;
cb_use_packet_one = fp_use_packet_one;
Packet = (uint8_t *)malloc(Size_Packet);
}
CBMFT5_Unpack::~CBMFT5_Unpack()
{
if(Packet != NULL)
{
free((void*)Packet);
Packet = NULL;
}
}
/*
for size_packet < size_packetarea_block
*/
void CBMFT5_Unpack::proc_unpack_packet_smaller_packetarea_block(uint8_t * p_block)
{
uint16_t packet_index_write = 0;
uint16_t block_index_read = IPF; // at first first edge position.
uint16_t size_write_now = Size_Packet;
uint16_t remain_size_BlockPacketArea;
uint8_t idx_npe=0;
if(NPE == 1) // the packet is first and final at the same time. IPF is always larger than IndexFirst_BlockPacketArea.
{
size_write_now = (IPF - IndexFirst_BlockPacketArea);
packet_index_write = Size_Packet-size_write_now;
memcpy(&Packet[packet_index_write], &p_block[IndexFirst_BlockPacketArea], size_write_now);
Use_Packet_One();
//proc for after packet edge
size_write_now = (Size_Block - IPF);
memcpy(&Packet[0], &p_block[IPF], size_write_now);
if(size_write_now == Size_Packet)
{
Use_Packet_One();
}
}// if(NPE == 1)
else if(NPE > 1) // NPE is 2, 3, 4,..
{
/// proc. first edge.
if(IPF == IndexFirst_BlockPacketArea)
{
block_index_read = IPF;
memcpy(&Packet[0], &p_block[block_index_read], Size_Packet);
Use_Packet_One();
}
else if(IPF > IndexFirst_BlockPacketArea)
{
size_write_now = (IPF - IndexFirst_BlockPacketArea);
packet_index_write = Size_Packet-size_write_now;
block_index_read = IndexFirst_BlockPacketArea;
memcpy(&Packet[packet_index_write], &p_block[block_index_read], size_write_now);
Use_Packet_One();
block_index_read = IPF;
memcpy(&Packet[0], &p_block[block_index_read], Size_Packet);
Use_Packet_One();
}
/// proc.middle edge. except first and final.
for(idx_npe=1; idx_npe < (NPE-1); idx_npe++) // loop for proc
{
block_index_read = IPF + (idx_npe*Size_Packet);
memcpy(&Packet[0], &p_block[block_index_read], Size_Packet);
Use_Packet_One();
}
/// proc. fianal edge.
block_index_read = IPF + (NPE-1)*Size_Packet;
remain_size_BlockPacketArea = (Size_Block - block_index_read);
if(remain_size_BlockPacketArea == Size_Packet)
{
memcpy(&Packet[0], &p_block[block_index_read], Size_Packet);
Use_Packet_One();
}
else if(remain_size_BlockPacketArea < Size_Packet)
{
memcpy(&Packet[0], &p_block[block_index_read], remain_size_BlockPacketArea);
}
}//else if(NPE > 1)
}
/*
for size_packet > size_packetarea_block
*/
void CBMFT5_Unpack::proc_unpack_packet_larger_packetarea_block(uint8_t * p_block)
{
static uint16_t packet_index_write = 0;
uint16_t size_edge_front;
if(NPE == 1) // there is a IPF
{
size_edge_front = IPF-IndexFirst_BlockPacketArea;
if(size_edge_front > 0)
{
memcpy(&Packet[Size_Packet-size_edge_front], &p_block[IndexFirst_BlockPacketArea],size_edge_front);
Use_Packet_One();
memcpy(&Packet[0], &p_block[IPF],Size_BlockPacketArea - size_edge_front);
packet_index_write = (Size_BlockPacketArea - size_edge_front); // for next write.
}// if(size_edge_front > 0)
else // size_edge_front == 0
{
memcpy(&Packet[0], &p_block[IPF],Size_BlockPacketArea);
packet_index_write = Size_BlockPacketArea; // for next write.
}
}
else if(NPE == 0) // there is no Packet Edge in this block.
{
if((Size_Packet - packet_index_write) >= Size_BlockPacketArea)
{
memcpy(&Packet[packet_index_write], &p_block[IPF],Size_BlockPacketArea);
packet_index_write += Size_BlockPacketArea; // for next write.
}
else
{
uint16_t size_block_front = (Size_Packet - packet_index_write);
memcpy(&Packet[packet_index_write], &p_block[IPF],size_block_front); // packet is full.
Use_Packet_One();
memcpy(&Packet[0], &p_block[IPF+size_block_front],Size_BlockPacketArea - size_block_front);
packet_index_write = Size_BlockPacketArea - size_block_front ; // for next write.
}
}
}
/*
for size_packet == size_packetarea_block
*/
void CBMFT5_Unpack::proc_unpack_packet_equal_packetarea_block(uint8_t * p_block)
{
memcpy(Packet,&p_block[IndexFirst_BlockPacketArea],Size_Packet);
Use_Packet_One();// one packet extracted, use pPacket
}
void CBMFT5_Unpack::Use_Packet_One(void)
{
if(cb_use_packet_one != NULL)
cb_use_packet_one(Packet, Size_Packet);
// nrf_delay_ms(1); // if no delay spis tx missing in the case of packet smaller than blockpacketarea.
}
void CBMFT5_Unpack::proc_unpack(uint8_t * p_block)
{
NPE = p_block[1]; // always 0 or 1
BlockCount = p_block[2]; // 0~31
IPF = *(uint16_t*)&p_block[3];
if(Size_Packet < Size_BlockPacketArea)
{
proc_unpack_packet_smaller_packetarea_block(p_block);
}
else if(Size_Packet > Size_BlockPacketArea)
{
proc_unpack_packet_larger_packetarea_block(p_block);
}
else
{
proc_unpack_packet_equal_packetarea_block(p_block);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment