/
hall-sensor.nut
55 lines (44 loc) · 1.17 KB
/
hall-sensor.nut
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
// Hall Sensor Class for Hannah
//
// Requires: io-expander.squirrel
//
// Usage:
// local output = OutputPort("Sensor", "number");
// sensor <- HallSensor(I2C_89, 0x3e, 2, hardware.pin1, output)
//
class HallSensor extends IoExpander
{
// IO Pin assignment
pin = null;
irq = null;
// Output port
outPort = null;
constructor(port, address, hallPin, irqPin, out)
{
base.constructor(port, address);
// Save assignments
pin = hallPin;
irq = irqPin;
outPort = out;
// Set event handler for irq
irqPin.configure(DIGITAL_IN, irqHandler.bindenv(this));
// Configure pin as input, irq on both edges
setDir(pin, 0);
setIrqMask(pin, 0);
setIrqEdges(pin, 1, 1);
}
function irqHandler()
{
if(irq.read() == 0)
{
// Negative going = interrupt from hall pin state change
// Get the pin state
local state = getPin(pin)?0:1;
// Output to port and display on node
outPort.set(state);
server.show(state);
}
// Clear the interrupt
clearIrq(pin);
}
}