1 Star 0 Fork 0

DFRobot/pxt-huskylens

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HUKSYLENS.h 18.50 KB
一键复制 编辑 原始数据 按行查看 历史
知音少断弦有谁听t 提交于 2019-12-10 16:45 +08:00 . 1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
#include "Wire.h"
#include "SoftwareSerial.h"
#include "HuskyLensProtocolCore.h"
#include "Arduino.h"
#ifndef _HUKSYLENS_H
#define _HUKSYLENS_H
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
enum protocolCommand{
COMMAND_REQUEST = 0x20,
COMMAND_REQUEST_BLOCKS,
COMMAND_REQUEST_ARROWS,
COMMAND_REQUEST_LEARNED,
COMMAND_REQUEST_BLOCKS_LEARNED,
COMMAND_REQUEST_ARROWS_LEARNED,
COMMAND_REQUEST_BY_ID,
COMMAND_REQUEST_BLOCKS_BY_ID,
COMMAND_REQUEST_ARROWS_BY_ID,
COMMAND_RETURN_INFO,
COMMAND_RETURN_BLOCK,
COMMAND_RETURN_ARROW,
COMMAND_REQUEST_KNOCK,
COMMAND_REQUEST_ALGORITHM,
COMMAND_RETURN_OK,
};
typedef struct{
uint8_t command;
union
{
int16_t first;
int16_t xCenter;
int16_t xOrigin;
int16_t protocolSize;
int16_t algorithmType;
int16_t requestID;
};
union
{
int16_t second;
int16_t yCenter;
int16_t yOrigin;
int16_t knowledgeSize;
};
union
{
int16_t third;
int16_t width;
int16_t xTarget;
int16_t frameNum;
};
union
{
int16_t fourth;
int16_t height;
int16_t yTarget;
};
union
{
int16_t fifth;
int16_t ID;
};
}Protocol_t;
enum protocolAlgorithm{
ALGORITHM_FACE_RECOGNITION,
ALGORITHM_OBJECT_TRACKING,
ALGORITHM_OBJECT_RECOGNITION,
ALGORITHM_LINE_TRACKING,
ALGORITHM_COLOR_RECOGNITION,
ALGORITHM_TAG_RECOGNITION,
ALGORITHM_OBJECT_CLASSIFICATION,
};
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
typedef Protocol_t HUSKYLENSResult;
class HUKSYLENS
{
private:
TwoWire *wire;
Stream *stream;
unsigned long timeOutDuration = 100;
unsigned long timeOutTimer;
int16_t currentIndex = 0;
Protocol_t protocolCache;
void protocolWrite(uint8_t* buffer, int length){
if (wire){
wire->beginTransmission(0x32);
wire->write(buffer, length);
wire->endTransmission();
}
else if(stream){
stream->write(buffer, length);
}
}
void timerBegin(){
timeOutTimer = millis();
}
bool timerAvailable(){
return (millis() - timeOutTimer > timeOutDuration);
}
bool protocolAvailable(){
if (wire)
{
if(!wire->available()){
wire->requestFrom(0x32, 16);
}
while(wire->available()){
int result = wire->read();
if (husky_lens_protocol_receive(result))
{
return true;
}
}
}
else if (stream)
{
while(stream->available()){
int result = stream->read();
if (husky_lens_protocol_receive(result))
{
return true;
}
}
}
return false;
}
Protocol_t protocolInfo;
Protocol_t* protocolPtr = NULL;
bool processReturn(){
if (!wait(COMMAND_RETURN_INFO)) return false;
protocolReadReturnInfo(protocolInfo);
protocolPtr = (Protocol_t*) realloc(protocolPtr, protocolInfo.protocolSize * sizeof(Protocol_t));
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if (!wait()) return false;
if (protocolReadReturnBlock(protocolPtr[i])) continue;
else if (protocolReadReturnArrow(protocolPtr[i])) continue;
else return false;
}
return true;
}
HUSKYLENSResult resultDefault;
bool wait(uint8_t command = 0){
timerBegin();
while (!timerAvailable())
{
if (protocolAvailable())
{
if (command)
{
if (husky_lens_protocol_read_begin(command)) return true;
}
else
{
return true;
}
}
}
return false;
}
bool readKnock(){
for (int i = 0; i < 5; i++)
{
protocolWriteRequestKnock();
if (wait(COMMAND_RETURN_OK))
{
return true;
}
}
return false;
}
HUSKYLENSResult readBlock(int ID = 1, int index=0){
Protocol_t* protocol = readBlockByIDProtocol(ID, index);
return protocol?*protocol:resultDefault;
}
HUSKYLENSResult readBlockDirect(int index){
Protocol_t* protocol = readBlockProtocol(index);
return protocol?*protocol:resultDefault;
}
HUSKYLENSResult readArrow(int ID = 1, int index=0){
Protocol_t* protocol = readArrowByIDProtocol(ID, index);
return protocol?*protocol:resultDefault;
}
HUSKYLENSResult readArrowDirect(int index){
Protocol_t* protocol = readArrowProtocol(index);
return protocol?*protocol:resultDefault;
}
public:
class Blocks
{
private:
HUKSYLENS *entity;
public:
void init(HUKSYLENS *e){
entity = e;
}
int available(){
return entity->countBlocks();
}
HUSKYLENSResult readDirect(int index){
return entity->readBlockDirect(index);
}
int available(int ID){
return entity->countBlocks(ID);
}
HUSKYLENSResult read(int ID = 1, int index =0){
return entity->readBlock(ID, index);
}
} blocks;
class Arrows
{
private:
HUKSYLENS *entity;
public:
void init(HUKSYLENS *e){
entity = e;
}
int available(){
return entity->countArrows();
}
HUSKYLENSResult readDirect(int index){
return entity->readArrowDirect(index);
}
int available(int ID){
return entity->countArrows(ID);
}
HUSKYLENSResult read(int ID = 1, int index = 0){
return entity->readArrow(ID, index);
}
} arrows;
HUKSYLENS(/* args */)
{
blocks.init(this);
arrows.init(this);
wire = NULL;
stream = NULL;
resultDefault.command = -1;
resultDefault.first = -1;
resultDefault.second = -1;
resultDefault.third = -1;
resultDefault.fourth = -1;
resultDefault.fifth = -1;
}
~HUKSYLENS()
{
}
bool begin(Stream& streamInput){
stream = &streamInput;
wire = NULL;
return readKnock();
}
bool begin(TwoWire& streamInput){
stream = NULL;
wire = &streamInput;
return readKnock();
}
void setTimeOutDuration(unsigned long timeOutDurationInput){
timeOutDuration = timeOutDurationInput;
}
bool request(){
protocolWriteRequest();
return processReturn();
}
bool requestBlocks(){
protocolWriteRequestBlocks();
return processReturn();
}
bool requestArrows(){
protocolWriteRequestArrows();
return processReturn();
}
bool requestLearned(){
protocolWriteRequestLearned();
return processReturn();
}
bool requestBlocksLearned(){
protocolWriteRequestBlocksLearned();
return processReturn();
}
bool requestArrowsLearned(){
protocolWriteRequestArrowsLearned();
return processReturn();
}
bool request(int16_t ID){
Protocol_t protocol;
protocol.requestID = ID;
protocolWriteRequestByID(protocol);
return processReturn();
}
bool requestBlocks(int16_t ID){
Protocol_t protocol;
protocol.requestID = ID;
protocolWriteRequestBlocksByID(protocol);
return processReturn();
}
bool requestArrows(int16_t ID){
Protocol_t protocol;
protocol.requestID = ID;
protocolWriteRequestArrowsByID(protocol);
return processReturn();
}
int available(){
return count();
}
int available(int ID){
return count(ID);
}
bool isLearned(){
return countLearnedIDs();
}
bool isLearned(int ID){
return (ID <= countLearnedIDs());
}
int16_t frameNumber(){
return protocolInfo.frameNum;
}
int16_t countLearnedIDs(){
return protocolInfo.knowledgeSize;
}
int16_t count(){
return protocolInfo.protocolSize;
}
int16_t countBlocks(){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK) counter++;
}
return counter;
}
int16_t countArrows(){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW) counter++;
}
return counter;
}
int16_t countLearned(){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].ID) counter++;
}
return counter;
}
int16_t countBlocksLearned(){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK && protocolPtr[i].ID) counter++;
}
return counter;
}
int16_t countArrowsLearned(){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW && protocolPtr[i].ID) counter++;
}
return counter;
}
int16_t count(int16_t ID){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].ID == ID) counter++;
}
return counter;
}
int16_t countBlocks(int16_t ID){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK && protocolPtr[i].ID == ID) counter++;
}
return counter;
}
int16_t countArrows(int16_t ID){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW && protocolPtr[i].ID == ID) counter++;
}
return counter;
}
Protocol_t* readProtocol(int16_t index){
if (index < count())
{
return protocolPtr + index;
}
return NULL;
}
Protocol_t* readBlockProtocol(int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readArrowProtocol(int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readLearnedProtocol(int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readBlockLearnedProtocol(int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK && protocolPtr[i].ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readArrowLearnedProtocol(int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW && protocolPtr[i].ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readByIDProtocol(int16_t ID, int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].ID == ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readBlockByIDProtocol(int16_t ID, int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_BLOCK && protocolPtr[i].ID == ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
Protocol_t* readArrowByIDProtocol(int16_t ID, int16_t index){
int16_t counter = 0;
for (int i = 0; i < protocolInfo.protocolSize; i++)
{
if(protocolPtr[i].command == COMMAND_RETURN_ARROW && protocolPtr[i].ID == ID) if(index == counter++) return protocolPtr + i;
}
return NULL;
}
HUSKYLENSResult read(int ID = 1, int index = 0){
Protocol_t* protocol = readByIDProtocol(ID, index);
return protocol?*protocol:resultDefault;
}
HUSKYLENSResult readDirect(int index){
Protocol_t* protocol = readProtocol(index);
return protocol?*protocol:resultDefault;
}
bool writeAlgorithm(protocolAlgorithm algorithmType){
Protocol_t protocol;
protocol.algorithmType = algorithmType;
protocolWriteRequestAlgorithm(protocol);
return wait(COMMAND_RETURN_OK);
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
void protocolWriteCommand(Protocol_t& protocol, uint8_t command){
protocol.command = command;
uint8_t* buffer = husky_lens_protocol_write_begin(protocol.command);
int length = husky_lens_protocol_write_end();
protocolWrite(buffer, length);
}
bool protocolReadCommand(Protocol_t& protocol, uint8_t command){
if (husky_lens_protocol_read_begin(command))
{
protocol.command = command;
husky_lens_protocol_read_end();
return true;
}
else
{
return false;
}
}
void protocolWriteFiveInt16(Protocol_t& protocol, uint8_t command){
protocol.command = command;
uint8_t* buffer = husky_lens_protocol_write_begin(protocol.command);
husky_lens_protocol_write_int16(protocol.first);
husky_lens_protocol_write_int16(protocol.second);
husky_lens_protocol_write_int16(protocol.third);
husky_lens_protocol_write_int16(protocol.fourth);
husky_lens_protocol_write_int16(protocol.fifth);
int length = husky_lens_protocol_write_end();
protocolWrite(buffer, length);
}
bool protocolReadFiveInt16(Protocol_t& protocol, uint8_t command){
if (husky_lens_protocol_read_begin(command))
{
protocol.command = command;
protocol.first = husky_lens_protocol_read_int16();
protocol.second = husky_lens_protocol_read_int16();
protocol.third = husky_lens_protocol_read_int16();
protocol.fourth = husky_lens_protocol_read_int16();
protocol.fifth = husky_lens_protocol_read_int16();
husky_lens_protocol_read_end();
return true;
}
else
{
return false;
}
}
void protocolWriteOneInt16(Protocol_t& protocol, uint8_t command){
protocol.command = command;
uint8_t* buffer = husky_lens_protocol_write_begin(protocol.command);
husky_lens_protocol_write_int16(protocol.first);
int length = husky_lens_protocol_write_end();
protocolWrite(buffer, length);
}
bool protocolReadOneInt16(Protocol_t& protocol, uint8_t command){
if (husky_lens_protocol_read_begin(command))
{
protocol.command = command;
protocol.first = husky_lens_protocol_read_int16();
husky_lens_protocol_read_end();
return true;
}
else
{
return false;
}
}
#define PROTOCOL_CREATE(function, type, command)\
void protocolWrite##function(Protocol_t& protocol){\
protocolWrite##type(protocol, command);\
}\
void protocolWrite##function(){\
Protocol_t protocol;\
protocolWrite##type(protocol, command);\
}\
bool protocolRead##function(Protocol_t& protocol){\
return protocolRead##type(protocol, command);\
}\
bool protocolRead##function(){\
Protocol_t protocol;\
return protocolRead##type(protocol, command);\
}
PROTOCOL_CREATE(Request, Command, COMMAND_REQUEST)
PROTOCOL_CREATE(RequestBlocks, Command, COMMAND_REQUEST_BLOCKS)
PROTOCOL_CREATE(RequestArrows, Command, COMMAND_REQUEST_ARROWS)
PROTOCOL_CREATE(RequestLearned, Command, COMMAND_REQUEST_LEARNED)
PROTOCOL_CREATE(RequestBlocksLearned, Command, COMMAND_REQUEST_BLOCKS_LEARNED)
PROTOCOL_CREATE(RequestArrowsLearned, Command, COMMAND_REQUEST_ARROWS_LEARNED)
PROTOCOL_CREATE(RequestByID, OneInt16, COMMAND_REQUEST_BY_ID)
PROTOCOL_CREATE(RequestBlocksByID, OneInt16, COMMAND_REQUEST_BLOCKS_BY_ID)
PROTOCOL_CREATE(RequestArrowsByID, OneInt16, COMMAND_REQUEST_ARROWS_BY_ID)
PROTOCOL_CREATE(ReturnInfo, FiveInt16, COMMAND_RETURN_INFO)
PROTOCOL_CREATE(ReturnBlock, FiveInt16, COMMAND_RETURN_BLOCK)
PROTOCOL_CREATE(ReturnArrow, FiveInt16, COMMAND_RETURN_ARROW)
PROTOCOL_CREATE(RequestKnock, Command, COMMAND_REQUEST_KNOCK)
PROTOCOL_CREATE(RequestAlgorithm, OneInt16, COMMAND_REQUEST_ALGORITHM)
PROTOCOL_CREATE(ReturnOK, Command, COMMAND_RETURN_OK)
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
};
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dfrobot/pxt-huskylens.git
git@gitee.com:dfrobot/pxt-huskylens.git
dfrobot
pxt-huskylens
pxt-huskylens
master

搜索帮助