-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.ts
110 lines (89 loc) · 2.7 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
/**
* Board initialization and helper function.
*/
// I2C slave address for PIC16F1937.
const I2C_ADDRESS = 0x08;
// Register address.
const REG_ADD_REVISION = 0;
const REG_ADD_SERVO_1 = 1;
const REG_ADD_SERVO_2 = 2;
const REG_ADD_SERVO_3 = 3;
const REG_ADD_M1A = 4;
const REG_ADD_M1B = 5;
const REG_ADD_M2A = 6;
const REG_ADD_M2B = 7;
const REG_ADD_LB_UTH = 8;
const REG_ADD_LB_LTH = 9;
const REG_ADD_OV_TH = 10;
const REG_ADD_VIN = 11;
const REG_ADD_PWR_STATE = 12;
const REG_ADD_LB_STATE = 13;
const REG_ADD_OV_STATE = 14;
namespace edubit {
// Brake the motors.
edubitMotors.brakeMotor(MotorChannel.M1);
edubitMotors.brakeMotor(MotorChannel.M2);
// Disable the servos.
edubitMotors.disableServo(ServoChannel.S1);
edubitMotors.disableServo(ServoChannel.S2);
edubitMotors.disableServo(ServoChannel.S3);
// Set the music silence level to 0 (For V2 only).
music.setSilenceLevel(0)
// Background function to monitor the power switch and reset microbit when power cycled.
let oldPowerState = edubitPower.isPowerOn();
control.inBackground(function () {
while (true) {
if (edubitPower.isPowerOn()) {
if (oldPowerState == false) {
control.reset();
}
oldPowerState = true;
} else {
oldPowerState = false;
}
basic.pause(200);
}
})
/**
* Limit the range of a number.
* @param value The number we want to limit.
* @param min Minimum value of the number.
* @param max Maximum value of the number.
*/
//% blockHidden=true
//% blockId=edubit_limit
export function limit(value: number, min: number, max: number): number {
if (value < min) {
value = min;
}
else if (value > max) {
value = max;
}
return value;
}
/**
* I2C read from the register of PIC16F1937.
* @param register Register address.
*/
//% blockHidden=true
//% blockId=edubit_i2c_read
export function i2cRead(register: number): number {
let value = 0;
pins.i2cWriteNumber(I2C_ADDRESS, register, NumberFormat.UInt8LE, true);
value = pins.i2cReadNumber(I2C_ADDRESS, NumberFormat.UInt8LE);
return value;
}
/**
* I2C write to the register of PIC16F1937.
* @param register Register address.
* @param data Data to write.
*/
//% blockHidden=true
//% blockId=edubit_i2c_write
export function i2cWrite(register: number, data: number): void {
let buffer = pins.createBuffer(2);
buffer[0] = register;
buffer[1] = data;
pins.i2cWriteBuffer(I2C_ADDRESS, buffer);
}
}