-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
293 lines (269 loc) · 8.08 KB
/
main.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* Provides access to basic micro:bit functionality.
*/
enum ModuleIndex {
//% block="module1"
Module1,
//% block="module2"
Module2,
//% block="module3"
Module3,
//% block="module4"
Module4
}
enum SubIndex {
//% block="1"
subModule1 = 1,
//% block="2"
subModule2,
//% block="3"
subModule3,
//% block="4"
subModule4
}
enum MesureContent {
//% block="onboard temp"
TempOnBoard,
//% block="onboard humidity"
HmOnBoard,
//% block="extend temp"
TempOffBoard
}
enum LedIndex {
//% block="all"
All,
//% block="1"
L1,
//% block="2"
L2,
//% block="3"
L3,
//% block="4"
L4,
//% block="5"
L5,
//% block="6"
L6,
//% block="7"
L7,
//% block="8"
L8,
//% block="9"
L9,
//% block="10"
L10,
//% block="11"
L11,
//% block="12"
L12
}
enum Color {
//% block="red"
Red,
//% block="orange"
Orange,
//% block="yellow"
Yellow,
//% block="green"
Green,
//% block="blue"
Blue,
//% block="indigo"
Indigo,
//% block="purple"
Purple,
//% block="white"
White,
//% block="black"
Black
}
//% color=190 weight=100 icon="\uf1ec" block="Ovobot Modules"
namespace ovobotModules {
const SONAR_ADDRESS = 0x52
const MOTOR_ADDRESS = 0x64
const SERVO_ADDRESS = 0x74
const LED_ADDRESS = 0x53
const SEG_ADDRESS = 0x6C
const TOUCHKEY_ADDRESS = 0x70
const TEMP_ADDRESS = 0x5c
const PM_ADDRESS = 0x60
const lowBright = 8
const selectColors = [0xff0000, 0xffa500, 0xffff00, 0x00ff00, 0x00ffff, 0x0000ff, 0x800080, 0xffffff, 0x000000]
let tempDevEnable = [false,false,false,false]
function sonicEnable() {
pins.i2cWriteRegister(SONAR_ADDRESS, 0x00, 0x01);
}
function constract(val: number, minVal: number, maxVal: number): number {
if (val > maxVal) {
return maxVal;
} else if (val < minVal) {
return minVal;
}
return val;
}
function tempEnable(address: number, index: number) {
pins.i2cWriteRegister(address, 0x00, 0x01);
tempDevEnable[index] = true;
}
function validate(str: String): Boolean {
let isfloat = false;
let len = str.length;
if (len > 5) {
return false;
}
for (let i = 0; i < len; i++) {
if (str.charAt(i) == ".") {
isfloat = true;
return true;
}
}
if (!isfloat && len == 5) {
return false;
}
return true;
}
/**
* TODO: 获取超声波传感器与前方障碍物的距离函数。
*/
//% block weight=50
export function readDistance(): number {
sonicEnable();
let sonarVal = pins.i2cReadRegister(SONAR_ADDRESS, 0x01, NumberFormat.Int16LE);
let distance = sonarVal / 29;
return distance;
}
/**
* TODO: 控制马达PWM输出。
*/
//% block="control motor %module output %speed"
//% speed.min=-255 speed.max=255
//% weight=65
export function controlMotorOutput(module: ModuleIndex, speed: number) {
let buf = pins.createBuffer(8);
buf[0] = 0x00;
buf[1] = speed > 0 ? 0 : 1;
buf[2] = Math.abs(speed)
pins.i2cWriteBuffer(MOTOR_ADDRESS + module, buf);
}
/**
* TODO: 控制舵机旋转。
*/
//% block="control servo %module index %submod rotate to %angle"
//% angle.min=-90 angle.max=90
//% weight=65
export function controlServoOutput(module: ModuleIndex,submod:SubIndex, angle: number) {
let buf = pins.createBuffer(8);
let newangle = constract(angle, -90, 90);
let output = 19 + 24 * angle / 180.0;
buf[0] = 0x00;
buf[1] = submod;
buf[2] = output;
pins.i2cWriteBuffer(SERVO_ADDRESS + module, buf);
}
/**
* TODO: 控制RGB灯条。
*/
//% blockId=control_leds_output block="control neopixels %index color %color"
//% weight=65
//% deprecated=true
export function controlNeopixels(index: LedIndex, color: Color) {
let buf = pins.createBuffer(38);
let startPos;
buf[0] = 0;
buf[1] = 1;
if (index == 0) {
for (let i = 2; i < 36; i += 3) {
buf[i] = ((selectColors[color] >> 8) & 0xff) / lowBright;
buf[i + 1] = ((selectColors[color] >> 16) & 0xff) / lowBright;
buf[i + 2] = (selectColors[color] & 0xff) / lowBright;
}
} else {
startPos = 2 + 3 * (index - 1);
buf[startPos] = ((selectColors[color] >> 8) & 0xff) / lowBright;
buf[startPos + 1] = ((selectColors[color] >> 16) & 0xff) / lowBright;
buf[startPos + 2] = (selectColors[color] & 0xff) / lowBright;
}
pins.i2cWriteBuffer(LED_ADDRESS, buf);
}
/**
* TODO: 显示数码管数值。
*/
//% blockId=display_seg_number block="control seg %module display number %num"
//% weight=65
export function displaySegNumber(module: ModuleIndex, num: number) {
let buf = pins.createBuffer(6);
buf[0] = 0;
buf[1] = 1;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
let str_num = num.toString();
let len = str_num.length;
let j = 0;
if (validate(str_num)) {
for (let i = len - 1; i >= 0; i--) {
if (str_num.charAt(i) == '.') {
buf[5 - j] = (str_num.charCodeAt(i - 1) - '0'.charCodeAt(0)) | 0x80;
i--;
} else if (str_num.charAt(i) == "-") {
buf[5 - j] = 0x40;
} else {
buf[5 - j] = str_num.charCodeAt(i) - '0'.charCodeAt(0);
}
j++;
}
pins.i2cWriteBuffer(SEG_ADDRESS, buf);
}
}
/**
* TODO: 触摸按键是否接触。
*/
//% blockId=isTouchDown block="touchkey %module is touched?"
//% weight=65
export function isTouchDown(module: ModuleIndex): boolean{
pins.i2cWriteRegister(TOUCHKEY_ADDRESS + module, 0x00, 0x01);
let data = pins.i2cReadRegister(TOUCHKEY_ADDRESS + module, 0x01, NumberFormat.UInt8LE);
return (data == 1);
}
/**
* TODO: 读取温湿度。
*/
//% blockId=read_temp_humidity block="read %module %measure data"
//% weight=65
export function readTempOrHumidity(module: ModuleIndex, measure: MesureContent): number{
let buf = pins.createBuffer(6);
let onboardTempValue = 400;
let extendTempValue;
let humidityValue;
let address = TEMP_ADDRESS + module;
if (!tempDevEnable[module]) {
tempEnable(address, module);
return 9999;
} else {
pins.i2cWriteRegister(address, 0x00, 0x01);
let res = pins.i2cReadBuffer(address, 6);//Buffer
onboardTempValue = -450 + 1750 * (res[0] << 8 | res[1]) / 65535;
humidityValue = 100 * (res[2] << 8 | res[3]) / 65535;
extendTempValue = (res[5] << 8 | res[4]) * 10 / 16.0;
if (measure == 0) {
return onboardTempValue * 0.1;
} else if (measure == 1) {
return humidityValue;
} else if (measure == 2) {
return extendTempValue * 0.1;
}
return 9999;
}
}
/**
* TODO: 读取电位器。
*/
//% blockId=read_pm block="read %module pm data"
//% weight=65
export function readPmData(module: ModuleIndex): number{
pins.i2cWriteRegister(PM_ADDRESS + module, 0x00, 0x01);
let data = pins.i2cReadRegister(PM_ADDRESS + module , 0x01, NumberFormat.UInt8LE);
return (255 - data);
}
}