-
Notifications
You must be signed in to change notification settings - Fork 0
/
disco.nut
223 lines (190 loc) · 5.43 KB
/
disco.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
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
// Disco example for Hannah
// IO Expander Class for SX1509
class IoExpander
{
i2cPort = null;
i2cAddress = null;
constructor(port, address)
{
if(port == I2C_12)
{
// Configure I2C bus on pins 1 & 2
hardware.configure(I2C_12);
i2cPort = hardware.i2c12;
}
else if(port == I2C_89)
{
// Configure I2C bus on pins 8 & 9
hardware.configure(I2C_89);
i2cPort = hardware.i2c89;
}
else
{
server.log("Invalid I2C port specified.");
}
i2cAddress = address << 1;
}
// Read a byte
function read(register)
{
local data = i2cPort.read(i2cAddress, format("%c", register), 1);
if(data == null)
{
server.log("I2C Read Failure");
return -1;
}
return data[0];
}
// Write a byte
function write(register, data)
{
i2cPort.write(i2cAddress, format("%c%c", register, data));
}
// Write a bit to a register
function writeBit(register, bitn, level)
{
local value = read(register);
value = (level == 0)?(value & ~(1<<bitn)):(value | (1<<bitn));
write(register, value);
}
// Write a masked bit pattern
function writeMasked(register, data, mask)
{
local value = read(register);
value = (value & ~mask) | (data & mask);
write(register, value);
}
// Set a GPIO level
function setPin(gpio, level)
{
writeBit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
}
// Set a GPIO direction
function setDir(gpio, output)
{
writeBit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
}
// Set a GPIO internal pull up
function setPullUp(gpio, enable)
{
writeBit(gpio>=8?0x06:0x07, gpio&7, enable);
}
// Set GPIO interrupt mask
function setIrqMask(gpio, enable)
{
writeBit(gpio>=8?0x12:0x13, gpio&7, enable);
}
// Set GPIO interrupt edges
function setIrqEdges(gpio, rising, falling)
{
local addr = 0x17 - (gpio>>2);
local mask = 0x03 << ((gpio&3)<<1);
local data = (2*falling + rising) << ((gpio&3)<<1);
writedMasked(addr, data, mask);
}
// Clear an interrupt
function clearIrq(gpio)
{
writeBit(gpio>=8?0x18:0x19, gpio&7, 1);
}
// Get a GPIO input pin level
function getPin(gpio)
{
return (read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0;
}
}
// RGB LED Class
class RgbLed extends IoExpander
{
// IO Pin assignments
pinR = null;
pinG = null;
pinB = null;
constructor(port, address, r, g, b)
{
base.constructor(port, address);
// Save pin assignments
pinR = r;
pinG = g;
pinB = b;
// Disable pin input buffers
writeBit(pinR>7?0x00:0x01, pinR>7?(pinR-7):pinR, 1);
writeBit(pinG>7?0x00:0x01, pinG>7?(pinG-7):pinG, 1);
writeBit(pinB>7?0x00:0x01, pinB>7?(pinB-7):pinB, 1);
// Set pins as outputs
writeBit(pinR>7?0x0E:0x0F, pinR>7?(pinR-7):pinR, 0);
writeBit(pinG>7?0x0E:0x0F, pinG>7?(pinG-7):pinG, 0);
writeBit(pinB>7?0x0E:0x0F, pinB>7?(pinB-7):pinB, 0);
// Set pins open drain
writeBit(pinR>7?0x0A:0x0B, pinR>7?(pinR-7):pinR, 1);
writeBit(pinG>7?0x0A:0x0B, pinG>7?(pinG-7):pinG, 1);
writeBit(pinB>7?0x0A:0x0B, pinB>7?(pinB-7):pinB, 1);
// Enable LED drive
writeBit(pinR>7?0x20:0x21, pinR>7?(pinR-7):pinR, 1);
writeBit(pinG>7?0x20:0x21, pinG>7?(pinG-7):pinG, 1);
writeBit(pinB>7?0x20:0x21, pinB>7?(pinB-7):pinB, 1);
// Set to use internal 2MHz clock, linear fading
write(0x1e, 0x50);
write(0x1f, 0x10);
// Initialise as inactive
setLevels(0, 0, 0);
setPin(pinR, 0);
setPin(pinG, 0);
setPin(pinB, 0);
}
// Set LED enabled state
function setLed(r, g, b)
{
if(r != null) writeBit(pinR>7?0x20:0x21, pinR&7, r);
if(g != null) writeBit(pinG>7?0x20:0x21, pinG&7, g);
if(b != null) writeBit(pinB>7?0x20:0x21, pinB&7, b);
}
// Set red, green and blue intensity levels
function setLevels(r, g, b)
{
if(r != null) write(pinR<4?0x2A+pinR*3:0x36+(pinR-4)*5, r);
if(g != null) write(pinG<4?0x2A+pinG*3:0x36+(pinG-4)*5, g);
if(b != null) write(pinB<4?0x2A+pinB*3:0x36+(pinB-4)*5, b);
}
// Set red, green and blue fade period
function setFade(r, g, b)
{
if(r != null && pinR > 3)
{
write(0x38+(pinR-4)*5, r);
write(0x39+(pinR-4)*5, r);
}
if(g != null && pinR > 3)
{
write(0x38+(pinG-4)*5, g);
write(0x39+(pinG-4)*5, g);
}
if(b != null && pinR > 3)
{
write(0x38+(pinB-4)*5, b);
write(0x39+(pinB-4)*5, b);
}
}
}
// Input port to accept color values
class LedInput extends InputPort
{
name = "Color";
type = "color";
led = null;
// Construct an LED and enable it
constructor()
{
base.constructor();
led = RgbLed(I2C_89, 0x3E, 7, 5, 6);
led.setLed(1, 1, 1);
}
// Use received colors to set the LED
function set(col)
{
led.setLevels(col[0], col[1], col[2]);
}
}
// Register with the server
imp.configure("Hannah Disco", [ LedInput() ], []);
// End of code.