-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathir_bit.ts
78 lines (64 loc) · 1.91 KB
/
ir_bit.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
/*******************************************************************************
* Functions for edu:bit - IR Bit.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Default pin.
const IR_BIT_PIN = DigitalPin.P8;
// Event source.
const IR_BIT_EVENT_SOURCE = EventBusSource.MICROBIT_ID_IO_P8;
// Event type.
enum IrEventType {
//% block="triggered"
Rise = EventBusValue.MICROBIT_PIN_EVT_RISE,
//% block="not triggered"
Fall = EventBusValue.MICROBIT_PIN_EVT_FALL,
}
/**
* Blocks for IR Bit.
*/
//% weight=13 color=#ff8000 icon="\uf05b" block="IR Bit"
namespace edubitIrBit {
/**
* Return IR sensor state (0 or 1).
*/
//% weight=20
//% blockGap=8
//% blockId=edubit_read_ir_sensor
//% block="IR sensor state"
export function readIrSensor(): number {
return pins.digitalReadPin(IR_BIT_PIN);
}
/**
* Return true if IR sensor is triggered.
*/
//% weight=19
//% blockGap=40
//% blockId=edubit_is_ir_sensor_triggered
//% block="IR sensor triggered"
export function isIrSensorTriggered(): boolean {
if (pins.digitalReadPin(IR_BIT_PIN) != 0) {
return true;
}
else {
return false;
}
}
/**
* Do something when IR sensor is triggered / not triggered.
* @param event Event type.
* @param handler Code to run when the event is raised.
*/
//% weight=18
//% blockGap=8
//% blockId=edubit_ir_sensor_event
//% block="on IR sensor %event"
export function onIrSensorEvent(event: IrEventType, handler: Action) {
// Set the event type as edge triggered.
pins.setEvents(IR_BIT_PIN, PinEventType.Edge);
// Register the event.
control.onEvent(IR_BIT_EVENT_SOURCE, <number>event, handler);
}
}