Skip to content

Commit 67611dd

Browse files
committed
first commmit
0 parents  commit 67611dd

26 files changed

+3912
-0
lines changed

DFRobot_HuskylensV2.cpp

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
#include <DFRobot_HuskylensV2.h>
2+
3+
HuskylensV2::HuskylensV2() : result{} {}
4+
HuskylensV2::~HuskylensV2() {
5+
for (uint8_t algo = 0; algo < ALGORITHM_COUNT; algo++) {
6+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
7+
if (result[algo][i]) {
8+
delete result[algo][i];
9+
result[algo][i] = NULL;
10+
}
11+
}
12+
}
13+
}
14+
15+
bool HuskylensV2::begin(Stream &streamInput) {
16+
stream = &streamInput;
17+
wire = NULL;
18+
return knock();
19+
}
20+
bool HuskylensV2::begin(TwoWire &streamInput) {
21+
DBG("\n");
22+
wire = &streamInput;
23+
stream = NULL;
24+
return knock();
25+
}
26+
27+
int8_t HuskylensV2::getResult(eAlgorithm_t algo) {
28+
eAlgorithm_t _algo = algo;
29+
// 所有结果返回到ProtocolV2内存
30+
// 然后将所有权转给HuskylensV2
31+
#ifdef LARGE_MEMORY
32+
algo = toRealID(algo);
33+
#else
34+
algo = (eAlgorithm_t)0;
35+
#endif
36+
37+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
38+
if (result[algo][i]) {
39+
delete result[algo][i];
40+
result[algo][i] = NULL;
41+
}
42+
}
43+
44+
int8_t count = ProtocolV2::getResult(_algo);
45+
46+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
47+
// DBG(i);
48+
// DBG((long long)ProtocolV2::result[i]);
49+
result[algo][i] = ProtocolV2::result[i];
50+
ProtocolV2::result[i] = NULL;
51+
}
52+
return count;
53+
}
54+
55+
bool HuskylensV2::available(eAlgorithm_t algo) {
56+
bool ret = false;
57+
#ifdef LARGE_MEMORY
58+
algo = toRealID(algo);
59+
#else
60+
algo = (eAlgorithm_t)0;
61+
#endif
62+
63+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
64+
if (result[algo][i] != NULL)
65+
if (!result[algo][i]->used) {
66+
DBG(i);
67+
ret = true;
68+
break;
69+
}
70+
}
71+
72+
return ret;
73+
}
74+
75+
Result *HuskylensV2::popCachedResult(eAlgorithm_t algo) {
76+
DBG("\n");
77+
#ifdef LARGE_MEMORY
78+
algo = toRealID(algo);
79+
#else
80+
algo = (eAlgorithm_t)0;
81+
#endif
82+
for (int8_t i = 0; i < MAX_RESULT_NUM; i++) {
83+
if (result[algo][i]) {
84+
if (result[algo][i]->used) {
85+
continue;
86+
}
87+
88+
result[algo][i]->used = 1;
89+
DBG_PRINT("return ");
90+
DBG_PRINTLN((unsigned long)result[algo][i]);
91+
DBG(i);
92+
return result[algo][i];
93+
}
94+
}
95+
96+
return NULL;
97+
}
98+
#ifdef LARGE_MEMORY
99+
eAlgorithm_t HuskylensV2::toRealID(uint8_t id) {
100+
eAlgorithm_t algo = ALGORITHM_ANY;
101+
if (id >= ALGORITHM_CUSTOM_BEGIN) {
102+
for (uint8_t i = 0; i < CUSTOM_ALGORITHM_COUNT; i++)
103+
if (customId[i] == algo) {
104+
algo = (eAlgorithm_t)((int)ALGORITHM_CUSTOM0 + i);
105+
break;
106+
}
107+
}
108+
return algo;
109+
}
110+
#endif
111+
Result *HuskylensV2::getCachedCenterResult(eAlgorithm_t algo) {
112+
DBG("\n");
113+
#ifdef LARGE_MEMORY
114+
algo = toRealID(algo);
115+
#else
116+
algo = (eAlgorithm_t)0;
117+
#endif
118+
119+
int8_t centerIndex = -1;
120+
int32_t minLen = 0x0FFFFFFF;
121+
for (int8_t i = 0; i < MAX_RESULT_NUM; i++) {
122+
if (result[algo][i]) {
123+
int32_t len = SQUARE(result[algo][i]->xCenter - LCD_WIDTH / 2) +
124+
SQUARE(result[algo][i]->yCenter - LCD_HEIGHT / 2);
125+
if (len < minLen) {
126+
minLen = len;
127+
centerIndex = i;
128+
}
129+
}
130+
}
131+
if (centerIndex != -1) {
132+
return result[algo][centerIndex];
133+
}
134+
return NULL;
135+
}
136+
137+
Result *HuskylensV2::getCachedResultByIndex(eAlgorithm_t algo, int16_t index) {
138+
DBG("\n");
139+
#ifdef LARGE_MEMORY
140+
algo = toRealID(algo);
141+
#else
142+
algo = (eAlgorithm_t)0;
143+
#endif
144+
if (index >= MAX_RESULT_NUM) {
145+
return NULL;
146+
}
147+
return result[algo][index];
148+
}
149+
150+
Result *HuskylensV2::getCachedResultByID(eAlgorithm_t algo, int16_t ID) {
151+
DBG("\n");
152+
#ifdef LARGE_MEMORY
153+
algo = toRealID(algo);
154+
#else
155+
algo = (eAlgorithm_t)0;
156+
#endif
157+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
158+
if (result[algo][i] == NULL) {
159+
continue;
160+
}
161+
if (result[algo][i]->ID == ID) {
162+
return result[algo][i];
163+
}
164+
}
165+
return NULL;
166+
}
167+
168+
int16_t HuskylensV2::getCachedResultNum(eAlgorithm_t algo) {
169+
DBG("\n");
170+
int16_t count = 0;
171+
#ifdef LARGE_MEMORY
172+
algo = toRealID(algo);
173+
#else
174+
algo = (eAlgorithm_t)0;
175+
#endif
176+
177+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
178+
if (result[algo][i]) {
179+
count++;
180+
}
181+
}
182+
return count;
183+
}
184+
185+
int16_t HuskylensV2::getCachedResultLearnedNum(eAlgorithm_t algo) {
186+
DBG("\n");
187+
int16_t count = 0;
188+
#ifdef LARGE_MEMORY
189+
algo = toRealID(algo);
190+
#else
191+
algo = (eAlgorithm_t)0;
192+
#endif
193+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
194+
if (result[algo][i] && result[algo][i]->ID) {
195+
count++;
196+
}
197+
}
198+
return count;
199+
}
200+
201+
int16_t HuskylensV2::getCachedResultNumByID(eAlgorithm_t algo, uint8_t id) {
202+
DBG("\n");
203+
int16_t count = 0;
204+
#ifdef LARGE_MEMORY
205+
algo = toRealID(algo);
206+
#else
207+
algo = (eAlgorithm_t)0;
208+
#endif
209+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
210+
if (result[algo][i] && (id == result[algo][i]->ID)) {
211+
count++;
212+
}
213+
}
214+
return count;
215+
}
216+
217+
Result *HuskylensV2::getCachedIndexResultByID(eAlgorithm_t algo, uint8_t id,
218+
uint8_t index) {
219+
DBG("\n");
220+
Result *rlt = NULL;
221+
uint8_t _index = 0;
222+
#ifdef LARGE_MEMORY
223+
algo = toRealID(algo);
224+
#else
225+
algo = (eAlgorithm_t)0;
226+
#endif
227+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
228+
if (result[algo][i] && (id == result[algo][i]->ID)) {
229+
if (_index == index) {
230+
return result[algo][i];
231+
}
232+
_index++;
233+
}
234+
}
235+
return rlt;
236+
}
237+
238+
int16_t HuskylensV2::getCachedResultMaxID(void) { return maxID; }
239+
240+
241+
Result *HuskylensV2::getCurrentBranch(eAlgorithm_t algo){
242+
DBG("\n");
243+
Result *rlt = NULL;
244+
#ifdef LARGE_MEMORY
245+
algo = toRealID(algo);
246+
#else
247+
algo = (eAlgorithm_t)0;
248+
#endif
249+
if (result[algo][0] && (result[algo][0]->level==1)) {
250+
return result[algo][0];
251+
}
252+
}
253+
254+
int8_t HuskylensV2::getUpcomingBranchCount(eAlgorithm_t algo){
255+
DBG("\n");
256+
int8_t count = 0;
257+
#ifdef LARGE_MEMORY
258+
algo = toRealID(algo);
259+
#else
260+
algo = (eAlgorithm_t)0;
261+
#endif
262+
for (uint8_t i = 0; i < MAX_RESULT_NUM; i++) {
263+
if (result[algo][i]) {
264+
count++;
265+
}
266+
}
267+
return count>0 ? count-1 : 0;
268+
}
269+
270+
Result *HuskylensV2::getBranch(eAlgorithm_t algo , int16_t index){
271+
DBG("\n");
272+
Result *rlt = NULL;
273+
index++;
274+
#ifdef LARGE_MEMORY
275+
algo = toRealID(algo);
276+
#else
277+
algo = (eAlgorithm_t)0;
278+
#endif
279+
for (uint8_t i = 1; i < MAX_RESULT_NUM; i++) {
280+
if (result[algo][i]) {
281+
if(i == index){
282+
rlt = result[algo][i];
283+
break;
284+
}
285+
}
286+
}
287+
return rlt;
288+
}
289+
290+
#ifdef LARGE_MEMORY
291+
bool HuskylensV2::setMultiAlgorithm(eAlgorithm_t algo0, eAlgorithm_t algo1,
292+
eAlgorithm_t algo2, eAlgorithm_t algo3,
293+
eAlgorithm_t algo4) {
294+
DBG("\n");
295+
customAlgoNum = 0;
296+
memset(customId, 0, sizeof(customId));
297+
if (algo0 >= ALGORITHM_CUSTOM_BEGIN) {
298+
customId[customAlgoNum++] = algo0;
299+
}
300+
if (algo1 >= ALGORITHM_CUSTOM_BEGIN) {
301+
customId[customAlgoNum++] = algo1;
302+
}
303+
if (algo2 >= ALGORITHM_CUSTOM_BEGIN) {
304+
customId[customAlgoNum++] = algo2;
305+
}
306+
if (algo3 >= ALGORITHM_CUSTOM_BEGIN) {
307+
customId[customAlgoNum++] = algo3;
308+
}
309+
if (algo4 >= ALGORITHM_CUSTOM_BEGIN) {
310+
customId[customAlgoNum++] = algo4;
311+
}
312+
return doSetMultiAlgorithm(algo0, algo1, algo2, algo3, algo4);
313+
}
314+
#endif

DFRobot_HuskylensV2.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
为ino提供接口
3+
存储分析result
4+
*/
5+
6+
#ifndef DFROBOT_HUSKYLENS_V2_H
7+
#define DFROBOT_HUSKYLENS_V2_H
8+
#include <Arduino.h>
9+
#include <ProtocolV2.h>
10+
11+
class HuskylensV2 : public ProtocolV2 {
12+
public:
13+
HuskylensV2();
14+
~HuskylensV2();
15+
16+
bool begin(Stream &streamInput);
17+
bool begin(TwoWire &streamInput);
18+
19+
int8_t getResult(eAlgorithm_t algo);
20+
bool available(eAlgorithm_t algo);
21+
22+
Result *popCachedResult(eAlgorithm_t algo);
23+
Result *getCachedCenterResult(eAlgorithm_t algo);
24+
Result *getCachedResultByIndex(eAlgorithm_t algo, int16_t index);
25+
Result *getCachedResultByID(eAlgorithm_t algo, int16_t ID);
26+
int16_t getCachedResultNum(eAlgorithm_t algo);
27+
int16_t getCachedResultLearnedNum(eAlgorithm_t algo);
28+
29+
int16_t getCachedResultNumByID(eAlgorithm_t algo, uint8_t id);
30+
Result *getCachedIndexResultByID(eAlgorithm_t algo, uint8_t id,
31+
uint8_t index);
32+
int16_t getCachedResultMaxID(void);
33+
34+
Result *getCurrentBranch(eAlgorithm_t algo);
35+
int8_t getUpcomingBranchCount(eAlgorithm_t algo);
36+
Result *getBranch(eAlgorithm_t algo , int16_t index);
37+
#ifdef LARGE_MEMORY
38+
bool setMultiAlgorithm(eAlgorithm_t algo0, eAlgorithm_t algo1,
39+
eAlgorithm_t algo2 = ALGORITHM_ANY,
40+
eAlgorithm_t algo3 = ALGORITHM_ANY,
41+
eAlgorithm_t algo4 = ALGORITHM_ANY);
42+
#endif
43+
public:
44+
Result *result[ALGORITHM_COUNT][MAX_RESULT_NUM];
45+
46+
#ifdef LARGE_MEMORY
47+
uint8_t customId[5];
48+
uint8_t customAlgoNum;
49+
eAlgorithm_t toRealID(uint8_t id);
50+
#endif
51+
};
52+
53+
#define RET_ITEM_NUM(func, type, item) \
54+
[](type *rlt) { return rlt ? rlt->item : -1; }(static_cast<type *>(func))
55+
56+
#define RET_ITEM_STR(func, type, item) \
57+
[](type *rlt) { return rlt ? rlt->item : ""; }(static_cast<type *>(func))
58+
59+
#endif // DFROBOT_HUSKYLENS_V2_H

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 DFRobot
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)