-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.cpp
More file actions
139 lines (137 loc) · 4.2 KB
/
extension.cpp
File metadata and controls
139 lines (137 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "pxt.h"
#include "MuVisionSensor.h"
using namespace pxt;
//% color="#ff6600" weight=20 icon="\uf085"
namespace muvs {
MuVisionSensor *Mu0;
MuVisionSensor *Mu1;
MuVisionSensor *Mu2;
MuVisionSensor *Mu3;
MuVisionSensor* MU[4]={Mu0,Mu1,Mu2,Mu3};
MicroBitSerial *serial=nullptr;
MicroBitI2C *i2c=nullptr;
//%
void begin(int id, int port){
MU[id] = new MuVisionSensor(0x60+id);
MuVisionSensor *mu = MU[id];
if(port==0){
serial=new MicroBitSerial(MICROBIT_PIN_P13,MICROBIT_PIN_P16);
mu->begin(serial,kSerialMode);
}
else if(port==1)
{
i2c=new MicroBitI2C(I2C_SDA0, I2C_SCL0);
mu->begin(i2c,kI2CMode);
}
}
//%
void VisionBegin(int id, int status, int type){
MuVisionSensor *mu = MU[id];
type=1<<(type-1);
if(status)
while(mu->VisionBegin(type)!=MU_OK);
else
mu->VisionSetStatus(type,false);
}
//%
void reset(int id){
MuVisionSensor *mu = MU[id];
while(mu->SensorSetDefault()!=MU_OK);
}
//%
void set_led(int id,int led, int detected_color, int undetected_color) {
MuVisionSensor *mu = MU[id];
while(mu->LedSetColor(MuVsLed(led), MuVsLedColor(detected_color), MuVsLedColor(undetected_color), 1) != MU_OK);
}
//%
void set_level(int id, int type,int level){
MuVisionSensor *mu = MU[id];
type=1<<(type-1);
while(mu->VisionSetLevel(type, MuVsVisionLevel(level)) != MU_OK);
}
//%
void set_zoom(int id, int level){
MuVisionSensor *mu = MU[id];
while(mu->CameraSetZoom(MuVsCameraZoom(level)) != MU_OK);
}
//%
void set_baudrate(int id, int baud){
MuVisionSensor *mu = MU[id];
while(mu->UartSetBaudrate(MuVsBaudrate(baud)) != MU_OK);
}
//%
void set_WB(int id, int wb){
MuVisionSensor *mu = MU[id];
while(mu->CameraSetAwb(MuVsCameraWhiteBalance(wb)) != MU_OK);
}
//%
void onOff(int id,int on){
MuVisionSensor *mu = MU[id];
while(mu->CameraSetFPS(MuVsCameraFPS(on)) != MU_OK);
}
//%
int detected(int id, int type){
MuVisionSensor *mu = MU[id];
type=1<<(type-1);
int ret = mu->GetValue(type,kStatus);
return ret;
}
//%
int MuVs2GetColorRCGLabel(int id, int x ,int y){
MuVisionSensor *mu = MU[id];
static int x_last = -1;
static int y_last = -1;
x = x>100 ? 100:(x<0 ? 0:x);
y = y>100 ? 100:(y<0 ? 0:y);
if (x != x_last) {
x_last = x;
mu->write(VISION_COLOR_RECOGNITION, kXValue, x);
}
if (y != y_last) {
y_last = y;
mu->write(VISION_COLOR_RECOGNITION, kYValue, y);
}
return mu->GetValue(VISION_COLOR_RECOGNITION, kStatus);
}
//%
int MuVs2GetColorDetectLabel(int id, int label) {
MuVisionSensor *mu = MU[id];
static int label_last = -1;
if (label_last != label) {
label_last = label;
mu->write(VISION_COLOR_DETECT, kLabel, label);
}
return mu->GetValue(VISION_COLOR_DETECT, kStatus);
}
//%
int get_value(int id, int type, int item){
MuVisionSensor *mu = MU[id];
type=1<<(type-1);
return mu->GetValue(type,MuVsObjectInf(item));
}
//%
int get_color_value(int id, int item){
MuVisionSensor *mu = MU[id];
return mu->GetValue(VISION_COLOR_RECOGNITION,MuVsObjectInf(item));
}
//%
int get_shape_card_type(int id, int label){
MuVisionSensor *mu = MU[id];
return mu->GetValue(VISION_SHAPE_CARD_DETECT, kLabel) == label;
}
//%
int get_traffic_card_type(int id, int label){
MuVisionSensor *mu = MU[id];
return mu->GetValue(VISION_TRAFFIC_CARD_DETECT, kLabel) == label;
}
//%
int get_number_card_type(int id, int label){
MuVisionSensor *mu = MU[id];
return mu->GetValue(VISION_NUM_CARD_DETECT, kLabel) == label;
}
//%
int get_color_recognize(int id, int label){
MuVisionSensor *mu = MU[id];
return mu->GetValue(VISION_COLOR_RECOGNITION, kLabel) == label;
}
}