-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP23017.ts
460 lines (396 loc) · 13 KB
/
MCP23017.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* MCP23017-control blocks
//% name.min= name.max=16
//% group="LEDs"
*/
enum ADDRESS { // address for MCP23017 (configurable by tying pins 15,16,17 on the mcp23017 high or low)
//% block=0x20
A20 = 0x20, //
//% block=0x21
A21 = 0x21, //
//% block=0x22
A22 = 0x22, //
//% block=0x23
A23 = 0x23, //
//% block=0x24
A24 = 0x24, //
//% block=0x25
A25 = 0x25, //
//% block=0x26
A26 = 0x26, //
//% block=0x27
A27 = 0x27 //
}
const enum BITS { //
//% block=00000001
IOb0 = 0, //
//% block=00000010
IOb1 = 1, //
//% block=00000100
IOb2 = 2, //
//% block=00001000
IOb3 = 3, //
//% block=00010000
IOb4 = 4, //
//% block=00100000
IOb5 = 5, //
//% block=01000000
IOb6 = 6, //
//% block=10000000
IOb7 = 7, //
//% block=#0
IO0 = 0, //
//% block=#1
IO1 = 1, //
//% block=#2
IO2 = 2, //
//% block=#3
IO3 = 3, //
//% block=#4
IO4 = 4, //
//% block=#5
IO5 = 5, //
//% block=#6
IO6 = 6, //
//% block=#7
IO7 = 7 //
}
const enum REG_MCP {
//% A Port bitképe (Bemenetek)
PORT_A_BITS = 0x12,
//% B Port bitképe (Kimenetek)
PORT_B_BITS = 0x13,
//% A Port ki vagy bemeneti irányt meghatározó regiszter
PORT_A_DIRECTION = 0x00, // Alapértelmezetten bemenetek (1111 1111)
//% A Port ki vagy bemeneti irányt meghatározó regiszter
PORT_B_DIRECTION = 0x01,
//% A Port polaritása
PORT_A_POL = 0x02,
//% B Port polaritása
PORT_B_POL = 0x03,
//% A Port interrupt
PORT_A_INTEN = 0x04,
//% B Port interrupt
PORT_B_INTEN = 0x05,
//% A Port interrupt
PORT_A_INTCON = 0x08,
//% B Port interrupt
PORT_B_INTCON = 0x09,
//% A Port interrupt
PORT_A_IOCON = 0x0A,
//% B Port interrupt
PORT_B_IOCON = 0x0B,
//% A Port bemeneti felhúzó ellenállás
PORT_A_PULLUP = 0x0C,
//% B Port bemeneti felhúzó ellenállás
PORT_B_PULLUP = 0x0D
}
//% weight=100 color=#0fbc12 icon= "\uf126"
namespace PLCbit_IO {
/*
PORT_A - BEMENETEK
PORT_B - KIMENETEK
*/
let outputABuffer = 0;
let tempHandler : Action
let thereIsHandler = false
let intPin = DigitalPin.P16
/**
* Egy regiszter írása
* @param adress: A PLC:BIT címe
* @param reg: Regiszter
* @param value: A regiszterbe írandó decimális érték
*/
//% blockId=Regiszter írása
//%advanced=true
//%block="A %adress PLC:BIT %reg regiszterének írása %value értékkel"
export function writeRegister(adress: ADDRESS, reg: REG_MCP, value: number) {
pins.i2cWriteNumber(adress, (reg << 8) + value, NumberFormat.UInt16BE)
}
/**
* Egy regiszter olvasása
* @param adress: A PLC:BIT címe
* @param reg: Regiszter
*/
//%blockId=Regiszter olvasása
//%advanced=true
//%block="A %adress PLC:BIT %reg regiszterének kiolvasása"
export function readRegister(adress: ADDRESS, reg: REG_MCP): number {
pins.i2cWriteNumber(adress, reg, NumberFormat.Int8LE);
return pins.i2cReadNumber(adress, NumberFormat.Int8LE)
}
/**
* A PLC:BIT ki és bemeneteinek inicializálása
* @param adress A PLC:BIT címe
*/
//% blockId="initPlcBit"
//% block="A %adress PLC:BIT inicializálása"
//% adress.defl=ADDRESS.A20
//% weight=80
//% group="Induláshoz"
export function initPlcBit(adress: ADDRESS): void {
writeRegister(adress, REG_MCP.PORT_A_DIRECTION, 0xff) // Inputs
writeRegister(adress, REG_MCP.PORT_B_DIRECTION, 0x00) // Outputs
writeRegister(adress, REG_MCP.PORT_A_PULLUP, 0xff) // Inputs + Pullups
writeRegister(adress, REG_MCP.PORT_B_PULLUP, 0x00) // Outputs
writeRegister(adress, REG_MCP.PORT_A_INTEN, 0xFF) // INTerrupt on PORT B
writeRegister(adress, REG_MCP.PORT_A_IOCON, 0x42) // INTerrupt on PORT B
writeRegister(adress, REG_MCP.PORT_A_POL, 0xff) //A bemenetek alacsony aktívak!
clearAllOuputs
pins.setEvents(intPin, PinEventType.Edge)
pins.setPull(intPin, PinPullMode.PullUp)
//interrupt active low
control.onEvent(intPin, DAL.MICROBIT_PIN_EVENT_ON_EDGE, function () {
// if (intPin == 0) {
tempHandler()
// }
})
}
/**
* Minden kimenet törlése
* @param adress Az eszköz címe, pl.: 0x20
*/
//% blockId="A PLC:BIT kimeneteinek alaphelyzetbe állítása"
//% block="A(z) %adress PLC:BIT minden kimenete KI"
//% weight=70
//% group="Induláshoz"
export function clearAllOuputs(adress: ADDRESS) {
outputABuffer = 0
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Minden kimenet bekapcsolása
* @param adress Az eszköz címe, pl.: 0x20
*/
//% blockId="A PLC:BIT kimeneteinek bekapcsolása"
//% block="A %adress PLC:BIT minden kimenete BE"
//% weight=60
//% group="Induláshoz"
export function setAllOuputs(adress: ADDRESS) {
outputABuffer = 0xff
writeRegister(adress, REG_MCP.PORT_B_BITS , outputABuffer)
}
/**
* Egy kimenet bekapcsolása
* @param adress Az eszköz címe, pl.: 0x20
* @param bit A kimenet sorszáma
*/
//% blockId="A PLC:BIT egy kimenetének bekapcsolása"
//% block="A %adress PLC:BIT %bit. kimenet BE"
//% bit.min=0 bit.max=7
//% weight=50
//% group="Kimenetek"
export function setOutput(adress: ADDRESS, bit: BITS) {
outputABuffer = outputABuffer | (1 << bit)
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Egy kimenet kikapcsolása
* @param adress Az eszköz címe, pl.: 0x20
* @param bit A kimenet sorszáma
*/
//% blockId="Az eszköz egy kimenetének kikapcsolása"
//% block="A %adress PLC:BIT %bit. kimenet KI"
//% bit.min=0 bit.max=7
//% weight=48
//% group="Kimenetek"
export function clearOutput(adress: ADDRESS, bit: BITS) {
let tempMask = 1 << bit
tempMask = tempMask ^ 0B11111111
outputABuffer = outputABuffer & tempMask
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Az összes kimenet módosítása
* @param adress Az eszköz címe, pl.: 0x20
* @param value A kimenetek értéke
*/
//% blockId="Az eszköz minden kimenetének írása"
//% block="A %adress PLC:BIT kimenetei legyen %value"
//% bit.min=0 bit.max=255
//% weight=45
//% group="Kimenetek"
export function writeAllOutput(adress: ADDRESS, value: number) {
outputABuffer = value
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Egy kimenet írása sorszám alapján (0-7)
* @param adress Az eszköz címe, pl.: 0x20
* @param bit A kimenet sorszáma
* @param value A kimenet értéke
*/
//% blockId="Az eszköz egy kimenetének írása sorszám alapján"
//% block="A %adress PLC:BIT %bit. kimenet %value"
//% bit.min=0 bit.max=7
//% weight=44
//% group="Kimenetek"
export function writeOutputNum(adress: ADDRESS, bit: number, value: boolean) {
let bitMask = 1 << bit
outputABuffer = readRegister(adress, REG_MCP.PORT_B_BITS)
if (value) {
outputABuffer = outputABuffer | bitMask;
}
else
{
bitMask = bitMask ^ 0B11111111
outputABuffer = outputABuffer & bitMask;
}
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Egy kimenet írása
* @param adress Az eszköz címe, pl.: 0x20
* @param bit A kimenet sorszáma
* @param value A kimenet értéke
*/
//% blockId="Az eszköz egy kimenetének írása"
//% block="A %adress PLC:BIT %bit. kimenet %value"
//% bit.min=0 bit.max=7
//% weight=43
//% group="Kimenetek"
export function writeOutput(adress: ADDRESS, bit: BITS, value: boolean) {
if (value)
setOutput(adress, bit)
else
clearOutput(adress, bit)
}
/**
* Egy kimenet váltása
* @param adress Az eszköz címe, pl.: 0x20
* @param bit A kimenet sorszáma
*/
//% blockId="Az eszköz egy kimenetének váltása Ki->Be vagy Be->Ki"
//% block="A %adress PLC:BIT %bit. kimenet váltás"
//% bit.min=0 bit.max=7
//% weight=46
//% group="Kimenetek"
export function toggleOutput(adress: ADDRESS, bit: BITS) {
let tempMask = 1 << bit
//tempMask = tempMask ^ 0B11111111
outputABuffer = outputABuffer ^ tempMask
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* A kimenetek frissítése
* @param adress Az eszköz címe, pl.: 0x20
*/
//% blockId="Az eszköz kimeneteinek frissítése"
//% block="A %adress PLC:BIT kimeneteinek frissítése"
//% weight=42
//% group="Kimenetek"
export function updateOutput(adress: ADDRESS) {
writeRegister(adress, REG_MCP.PORT_B_BITS, outputABuffer)
}
/**
* Összes kimenet olvasása
* @param adress Az eszköz címe, pl.: 0x20
*/
//% blockId="Az eszköz kimeneteinek visszaolvasása"
//% block="A %adress PLC:BIT kimeneteinek kiolvasása"
//% weight=40
//% group="Kimenetek"
export function readAllOutputs(adress : ADDRESS): number {
return readRegister(adress, REG_MCP.PORT_B_BITS)
}
/**
* Egy kimenet olvasása
* @param adress Az eszköz címe, pl.: 0x20
* @param output A kimenet sorszáma
*
*/
//% blockId="Az eszköz egy kimenetének visszaolvasása"
//% block="A %adress PLC:BIT %output kimenetének kiolvasása"
//% weight=38
//% group="Kimenetek"
export function readOutput(adress : ADDRESS, output: BITS): boolean {
let port = readRegister(adress, REG_MCP.PORT_B_BITS)
return ((port & (1 << output)) == 1)
}
/**
* A PLC:BIT bemenet válozás esemény
*/
//% blockId="onInputsChanged"
//% block="A PLC:BIT egy bemenete megváltozott"
//% weight=40
//% group="Bemenetek"
export function onInputsChanged(handler: Action): void {
tempHandler = handler
thereIsHandler = true
}
/**
* Összes bemenet olvasása
* @param adress Az eszköz címe, pl.: 0x20
*/
//% blockId="Az eszköz bemeneteinek kiolvasása"
//% block="A %adress PLC:BIT bemeneteinek kiolvasása"
//% weight=30
//% group="Bemenetek"
export function readInputAll(adress : ADDRESS): number {
return (readRegister(adress, REG_MCP.PORT_A_BITS) & 0xff)
}
/**
* Egy bemenet olvasása
* @param adress Az eszköz címe, pl.: 0x20
* @param input A bemenet sorszáma
*/
//% blockId="Az eszköz egy bemenetének kiolvasása"
//% block="A %adress PLC:BIT %input bemenetének kiolvasása"
//% weight=28
//% group="Bemenetek"
export function readInput(adress : ADDRESS, input: BITS): boolean {
let port = readRegister(adress, REG_MCP.PORT_A_BITS)
return ((port & (1 << input)) != 0)
}
/**
* Egy szám egy bitjének módosítása
* @param bin A decimális érték
* @param bit A módosítandó bit
* @param value Az új érték
*/
//% blockId="Szám egy bitjének módosítása"
//% block="%bin szám %bit.bitjének módosítása %value értékre"
//% weight=27
//% bit.min=0 bit.max=7
//% group="Egyéb"
export function setBit( bin : number, bit: number, value : boolean) : number {
let num : number = bin
let bitMask = 1 << bit
if (value) {
num = num | bitMask;
}
else
{
bitMask = bitMask ^ 0B11111111
num = num & bitMask;
}
return num
}
/**
* Egy szám binárissá konvertálása
* @param bin A decimális érték
*/
//% blockId="Szám binárissá konvertálása"
//% block="%bin bináris értéke"
//% weight=27
//% group="Egyéb"
export function showBin( bin : number) : string {
let str : string = ""
let i : number;
for (i = 0; i < 8; i++){
if ((bin & (1<<i)) == 0)
str = str + "0"
else
str = str + "1"
}
return str
}
// block
function ReadNotAnd(addr: ADDRESS, reg: REG_MCP, value: number): boolean {
return (!(readRegister(addr, reg) & value))
}
// block
function writeNumberToPort(adress: ADDRESS, port: REG_MCP, value: number) {
pins.i2cWriteNumber(adress, (port << 8) + value, NumberFormat.UInt16BE)
}
}