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
0 commit comments