-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcsr04.ceu
86 lines (68 loc) · 2.16 KB
/
hcsr04.ceu
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
#ifndef HCSR04_ECHO
#error Missing interrupt pin `HCSR04_ECHO`.
#endif
#ifndef HCSR04_TRIGGER
#error Missing output pin `HCSR04_TRIGGER`.
#endif
///////////////////////////////////////////////////////////////////////////////
// INTERFACE
///////////////////////////////////////////////////////////////////////////////
code/await HCSR04_Init (var int energyPort, var int? time) -> NEVER;
code/await HCSR04_Read (var int? energyPort, var int? time) -> int?;
///////////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION
///////////////////////////////////////////////////////////////////////////////
#include "wclock.ceu"
#define HCSR04_SENSOR_DEFAULT_TIME 80
var bool hcsr04_single_turn_on = false;
code/await HCSR04_Init (var int energyPort, var int? time) -> NEVER do
do finalize with
_digitalWrite(energyPort, low);
outer.hcsr04_single_turn_on = false;
end
outer.hcsr04_single_turn_on = true;
//turn on
{
pinMode(@energyPort, OUTPUT);
digitalWrite(@energyPort, HIGH);
}
// waits for the circuit to stabilize
if (time?) then
await (time!)ms;
else
await (HCSR04_SENSOR_DEFAULT_TIME)ms;
end
await FOREVER;
end
code/await HCSR04_Read (var int? energyPort, var int? time) -> int? do
do finalize with
if ((outer.hcsr04_single_turn_on == false) and (energyPort?)) then
_digitalWrite(energyPort!, low);
end
end
if ((outer.hcsr04_single_turn_on == false) and (energyPort?)) then
//turn on
{
pinMode(@energyPort!, OUTPUT);
digitalWrite(@energyPort!, HIGH);
}
// waits for the circuit to stabilize
if (time?) then
await (time!)ms;
else
await (HCSR04_SENSOR_DEFAULT_TIME)ms;
end
end
emit HCSR04_TRIGGER(high);
call WCLOCK_Freeze(10);
emit HCSR04_TRIGGER(low);
var int? ret;
watching 25ms do
await HCSR04_ECHO;
var u32 old = call WCLOCK_Now();
await HCSR04_ECHO;
var u32 now = call WCLOCK_Now();
ret = (((now - old) as int)/58) as int;
end
escape ret;
end