-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathNrf24l01.cs
863 lines (711 loc) · 27.2 KB
/
Nrf24l01.cs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Device.Spi;
using System.Threading;
namespace Iot.Device.Nrf24l01
{
/// <summary>
/// Single chip 2.4 GHz Transceiver nRF24L01
/// </summary>
public class Nrf24l01 : IDisposable
{
private readonly int _ce;
private readonly int _irq;
private readonly byte[] _empty = new byte[0];
private GpioController _gpio;
private SpiDevice _sensor;
private bool _shouldDispose;
#region prop
private byte _packetSize;
/// <summary>
/// nRF24L01 Receive Packet Size
/// </summary>
public byte PacketSize
{
get { return _packetSize; }
set { _packetSize = value < 0 || value > 32 ? throw new ArgumentOutOfRangeException("PacketSize needs to be in the range of 0 to 32.") : value; }
}
/// <summary>
/// nRF24L01 Send Address
/// </summary>
public byte[] Address
{
get => ReadTxAddress();
set => SetTxAddress(value);
}
/// <summary>
/// nRF24L01 Working Channel
/// </summary>
public byte Channel
{
get => ReadChannel();
set => SetChannel(value);
}
/// <summary>
/// nRF24L01 Output Power
/// </summary>
public OutputPower OutputPower
{
get => ReadOutputPower();
set => SetOutputPower(value);
}
/// <summary>
/// nRF24L01 Send Rate
/// </summary>
public DataRate DataRate
{
get => ReadDataRate();
set => SetDataRate(value);
}
/// <summary>
/// nRF24L01 Power Mode
/// </summary>
public PowerMode PowerMode
{
get => ReadPowerMode();
set => SetPowerMode(value);
}
/// <summary>
/// nRF24L01 Working Mode
/// </summary>
public WorkingMode WorkingMode
{
get => ReadWorkwingMode();
set => SetWorkingMode(value);
}
/// <summary>
/// nRF24L01 Pipe 0
/// </summary>
public Nrf24l01Pipe Pipe0 { get; private set; }
/// <summary>
/// nRF24L01 Pipe 1
/// </summary>
public Nrf24l01Pipe Pipe1 { get; private set; }
/// <summary>
/// nRF24L01 Pipe 2
/// </summary>
public Nrf24l01Pipe Pipe2 { get; private set; }
/// <summary>
/// nRF24L01 Pipe 3
/// </summary>
public Nrf24l01Pipe Pipe3 { get; private set; }
/// <summary>
/// nRF24L01 Pipe 4
/// </summary>
public Nrf24l01Pipe Pipe4 { get; private set; }
/// <summary>
/// nRF24L01 Pipe 5
/// </summary>
public Nrf24l01Pipe Pipe5 { get; private set; }
#endregion
#region SpiSettings
/// <summary>
/// NRF24L01 SPI Clock Frequency
/// </summary>
public const int SpiClockFrequency = 2000000;
/// <summary>
/// NRF24L01 SPI Mode
/// </summary>
public const SpiMode SpiMode = System.Device.Spi.SpiMode.Mode0;
#endregion
/// <summary>
/// Creates a new instance of the nRF24L01.
/// </summary>
/// <param name="sensor">The communications channel to a device on a SPI bus</param>
/// <param name="ce">CE Pin</param>
/// <param name="irq">IRQ Pin</param>
/// <param name="packetSize">Receive Packet Size</param>
/// <param name="channel">Working Channel</param>
/// <param name="outputPower">Output Power</param>
/// <param name="dataRate">Send Data Rate</param>
/// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Nrf24l01(SpiDevice sensor, int ce, int irq, byte packetSize, byte channel = 2,
OutputPower outputPower = OutputPower.N00dBm, DataRate dataRate = DataRate.Rate2Mbps, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
{
_sensor = sensor ?? throw new ArgumentNullException(nameof(sensor));
_ce = ce;
_irq = irq;
PacketSize = packetSize;
_shouldDispose = shouldDispose || gpioController is null;
Initialize(pinNumberingScheme, outputPower, dataRate, channel, gpioController);
InitializePipe();
#if !NET5_0_OR_GREATER
if (_gpio is null ||
Pipe0 is null ||
Pipe1 is null ||
Pipe2 is null ||
Pipe3 is null ||
Pipe4 is null ||
Pipe5 is null)
{
throw new Exception($"{nameof(Nrf24l01)} is incorrectly configuted");
}
#endif
}
/// <summary>
/// Send
/// </summary>
/// <param name="data">Send Data</param>
public void Send(byte[] data)
{
// Details in the Datasheet P65
SetWorkingMode(WorkingMode.Transmit);
// wait for some time so that it doesn't go too fast
Thread.Sleep(1);
Write(Command.NRF_W_TX_PAYLOAD, Register.NRF_NOOP, data);
_gpio.Write(_ce, PinValue.High);
Thread.Sleep(1);
_gpio.Write(_ce, PinValue.Low);
Thread.Sleep(1);
SetWorkingMode(WorkingMode.Receive);
Thread.Sleep(1);
}
/// <summary>
/// Receive
/// </summary>
/// <param name="length">Packet Size</param>
/// <returns>Data</returns>
public SpanByte Receive(byte length)
{
// Details in the Datasheet P65
_gpio.Write(_ce, PinValue.Low);
SpanByte writeData = new byte[length];
SpanByte readData = WriteRead(Command.NRF_R_RX_PAYLOAD, Register.NRF_NOOP, writeData);
SpanByte ret = new byte[readData.Length];
readData.CopyTo(ret);
_gpio.Write(_ce, PinValue.Low);
// clear RX FIFO interrupt
Write(Command.NRF_W_REGISTER, Register.NRF_STATUS, 0b_0100_1110);
_gpio.Write(_ce, PinValue.High);
return ret;
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_sensor?.Dispose();
_sensor = null!;
if (_shouldDispose)
{
_gpio?.Dispose();
_gpio = null!;
}
}
/// <summary>
/// Delegate representing event that data was received
/// </summary>
/// <param name="sender">Object sending the event</param>
/// <param name="e">Arguments for the event</param>
public delegate void DataReceivedHandle(object sender, DataReceivedEventArgs e);
/// <summary>
/// Triggering when data was received
/// </summary>
public event DataReceivedHandle? DataReceived;
private void Irq_ValueChanged(object sender, PinValueChangedEventArgs args)
{
if (DataReceived is object)
{
DataReceived(sender, new DataReceivedEventArgs(Receive(_packetSize).ToArray()));
}
}
#region private and internal
/// <summary>
/// Initialize
/// </summary>
#if NET5_0_OR_GREATER
#endif
private void Initialize(PinNumberingScheme pinNumberingScheme, OutputPower outputPower, DataRate dataRate, byte channel, GpioController? gpioController)
{
// open pins
_gpio = gpioController ?? new GpioController(pinNumberingScheme);
_gpio.OpenPin(_ce, PinMode.Output);
_gpio.OpenPin(_irq, PinMode.Input);
_gpio.RegisterCallbackForPinValueChangedEvent(_irq, PinEventTypes.Falling, Irq_ValueChanged);
// wait for some time
Thread.Sleep(10);
// Details in the datasheet P53
_gpio.Write(_ce, PinValue.Low);
Write(Command.NRF_FLUSH_TX, Register.NRF_NOOP, _empty);
Write(Command.NRF_FLUSH_RX, Register.NRF_NOOP, _empty);
// reflect RX_DR interrupt, TX_DS interrupt not reflected, power up, receive mode
Write(Command.NRF_W_REGISTER, Register.NRF_CONFIG, 0b_0011_1011);
_gpio.Write(_ce, PinValue.High);
SetAutoAck(false);
SetChannel(channel);
SetOutputPower(outputPower);
SetDataRate(dataRate);
SetRxPayload(_packetSize);
}
/// <summary>
/// Initialize nRF24L01 Pipe
/// </summary>
#if NET5_0_OR_GREATER
#endif
private void InitializePipe()
{
Pipe0 = new Nrf24l01Pipe(this, 0);
Pipe1 = new Nrf24l01Pipe(this, 1);
Pipe2 = new Nrf24l01Pipe(this, 2);
Pipe3 = new Nrf24l01Pipe(this, 3);
Pipe4 = new Nrf24l01Pipe(this, 4);
Pipe5 = new Nrf24l01Pipe(this, 5);
}
/// <summary>
/// Set nRF24L01 Receive Packet Size (All Pipe)
/// </summary>
/// <param name="payload">Size, from 0 to 32</param>
internal void SetRxPayload(byte payload)
{
// Details in the Datasheet P56
if (payload > 32 || payload < 0)
{
throw new ArgumentOutOfRangeException(nameof(payload), $"{nameof(payload)} needs to be in the range of 0 to 32.");
}
_gpio.Write(_ce, PinValue.Low);
SpanByte writeData = new byte[1]
{
payload
};
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P0, writeData);
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P1, writeData);
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P2, writeData);
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P3, writeData);
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P4, writeData);
Write(Command.NRF_W_REGISTER, Register.NRF_RX_PW_P5, writeData);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Set nRF24L01 Receive Packet Size
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <param name="payload">Size, from 0 to 32</param>
internal void SetRxPayload(byte pipe, byte payload)
{
// Details in the Datasheet P56
if (payload > 32 || payload < 0)
{
throw new ArgumentOutOfRangeException(nameof(payload), $"{nameof(payload)} needs to be in the range of 0 to 32.");
}
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
SpanByte writeData = new byte[]
{
(byte)((byte)Command.NRF_W_REGISTER + (byte)Register.NRF_RX_PW_P0 + pipe),
payload
};
_gpio.Write(_ce, PinValue.Low);
Write(writeData);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Receive Packet Size
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <returns>Size</returns>
internal byte ReadRxPayload(byte pipe)
{
// Details in the Datasheet P56
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, (Register)((byte)Register.NRF_RX_PW_P0 + pipe), 1);
return readData[0];
}
/// <summary>
/// Set nRF24L01 Auto Acknowledgment (All Pipe)
/// </summary>
/// <param name="isAutoAck">Is Enable</param>
internal void SetAutoAck(bool isAutoAck)
{
// Details in the Datasheet P53
_gpio.Write(_ce, PinValue.Low);
if (isAutoAck)
{
// all pipe
Write(Command.NRF_W_REGISTER, Register.NRF_EN_AA, 0b_0011_1111);
}
else
{
Write(Command.NRF_W_REGISTER, Register.NRF_EN_AA, 0b_0000_0000);
}
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Set nRF24L01 Auto Acknowledgment
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <param name="isAutoAck">Is Enable</param>
internal void SetAutoAck(byte pipe, bool isAutoAck)
{
// Details in the Datasheet P53
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_EN_AA, 1);
byte setting;
if (isAutoAck)
{
setting = (byte)(readData[0] | (1 << pipe));
}
else
{
setting = (byte)(readData[0] & ~(1 << pipe));
}
Write(Command.NRF_W_REGISTER, Register.NRF_EN_AA, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Auto Acknowledgment
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <returns>Is Enable</returns>
internal bool ReadAutoAck(byte pipe)
{
// Details in the Datasheet P53
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_EN_AA, 1);
byte ret = (byte)(readData[0] >> pipe & 0b_0000_0001);
if (ret == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// Set nRF24L01 Receive Pipe (All Pipe)
/// </summary>
/// <param name="isEnable">Is Enable Receive</param>
internal void SetRxPipe(bool isEnable)
{
// Details in the Datasheet P54
_gpio.Write(_ce, PinValue.Low);
if (isEnable)
{
// all pipe
Write(Command.NRF_W_REGISTER, Register.NRF_EN_RXADDR, 0b_0011_1111);
}
else
{
Write(Command.NRF_W_REGISTER, Register.NRF_EN_RXADDR, 0b_0000_0000);
}
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Set nRF24L01 Receive Pipe is Enable
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <param name="isEnable">Is Enable the Pipe Receive</param>
internal void SetRxPipe(byte pipe, bool isEnable)
{
// Details in the Datasheet P54
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_EN_RXADDR, 1);
byte setting;
if (isEnable)
{
setting = (byte)(readData[0] | (1 << pipe));
}
else
{
setting = (byte)(readData[0] & ~(1 << pipe));
}
Write(Command.NRF_W_REGISTER, Register.NRF_EN_RXADDR, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Receive Pipe is Enable
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <returns>Is Enable the Pipe Receive</returns>
internal bool ReadRxPipe(byte pipe)
{
// Details in the Datasheet P54
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_EN_RXADDR, 1);
byte ret = (byte)(readData[0] >> pipe & 0x01);
if (ret == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// Set nRF24L01 Power Mode
/// </summary>
/// <param name="mode">Power Mode</param>
internal void SetPowerMode(PowerMode mode)
{
// Details in the Datasheet P53
// Register:0x00, bit[1]
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_CONFIG, 1);
byte setting = mode switch
{
PowerMode.UP => setting = (byte)(readData[0] | (1 << 1)),
PowerMode.DOWN => setting = (byte)(readData[0] & ~(1 << 1)),
_ => setting = (byte)(readData[0] | (1 << 1)),
};
Write(Command.NRF_W_REGISTER, Register.NRF_CONFIG, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Power Mode
/// </summary>
/// <returns>Power Mode</returns>
internal PowerMode ReadPowerMode()
{
// Details in the Datasheet P53
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_CONFIG, 1);
return (PowerMode)((readData[0] >> 1) & 0b_0000_0001);
}
/// <summary>
/// Set nRF24L01 Working Mode
/// </summary>
/// <param name="mode">Working Mode</param>
internal void SetWorkingMode(WorkingMode mode)
{
// Details in the Datasheet P53
// Register:0x00, bit[0]
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_CONFIG, 1);
byte setting = mode switch
{
WorkingMode.Receive => (byte)(readData[0] | 1),
WorkingMode.Transmit => (byte)(readData[0] & ~1),
_ => (byte)(readData[0] | 1),
};
Write(Command.NRF_W_REGISTER, Register.NRF_CONFIG, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Working Mode
/// </summary>
/// <returns>Working Mode</returns>
internal WorkingMode ReadWorkwingMode()
{
// Details in the Datasheet P53
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_CONFIG, 1);
return (WorkingMode)(readData[0] & 0b_0000_0001);
}
/// <summary>
/// Set nRF24L01 Output Power
/// </summary>
/// <param name="power">Output Power</param>
internal void SetOutputPower(OutputPower power)
{
// Details in the Datasheet P54
// Register: 0x06, bit[2:1]
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_RF_SETUP, 1);
byte setting = (byte)(readData[0] & (~0b_0000_0110) | ((byte)power << 1));
Write(Command.NRF_W_REGISTER, Register.NRF_RF_SETUP, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Output Power
/// </summary>
/// <returns>Output Power</returns>
internal OutputPower ReadOutputPower()
{
// Details in the Datasheet P54
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_RF_SETUP, 1);
return (OutputPower)((readData[0] & 0b_0000_0110) >> 1);
}
/// <summary>
/// Set nRF24L01 Send Rate
/// </summary>
/// <param name="rate">Send Rate</param>
internal void SetDataRate(DataRate rate)
{
// Details in the Datasheet P54
// Register: 0x06, bit[3]
_gpio.Write(_ce, PinValue.Low);
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_RF_SETUP, 1);
byte setting = (byte)(readData[0] & (~0b_0000_1000) | ((byte)rate << 1));
Write(Command.NRF_W_REGISTER, Register.NRF_RF_SETUP, setting);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Send Rate
/// </summary>
/// <returns>Send Rate</returns>
internal DataRate ReadDataRate()
{
// Details in the Datasheet P54
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_RF_SETUP, 1);
return (DataRate)((readData[0] & 0b_0000_1000) >> 3);
}
/// <summary>
/// Set nRF24L01 Receive Address
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <param name="address">Address, if (pipe > 1) then (address.Length = 1), else if (pipe = 1 || pipe = 0) then (address.Length ≤ 5)</param>
internal void SetRxAddress(byte pipe, SpanByte address)
{
// Details in the Datasheet P55
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
if (address.Length > 5 || address.Length < 1)
{
throw new ArgumentOutOfRangeException("Array Length must less than 6.");
}
if (pipe > 1 && address.Length > 1)
{
throw new ArgumentOutOfRangeException("Array Length must equal 1 when pipe more than 1. Address equal pipe1's address the first 4 byte + one byte your custom.");
}
SpanByte writeData = new byte[1 + address.Length];
writeData[0] = (byte)((byte)Command.NRF_W_REGISTER + (byte)Register.NRF_RX_ADDR_P0 + pipe);
for (int i = 0; i < address.Length; i++)
{
writeData[i + 1] = address[i];
}
_gpio.Write(_ce, PinValue.Low);
Write(writeData);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Receive Address
/// </summary>
/// <param name="pipe">Pipe, form 0 to 5</param>
/// <returns>Address</returns>
internal byte[] ReadRxAddress(byte pipe)
{
// Details in the Datasheet P55
if (pipe > 5 || pipe < 0)
{
throw new ArgumentOutOfRangeException(nameof(pipe), $"{nameof(pipe)} needs to be in the range of 0 to 5.");
}
int length = 5;
if (pipe > 1)
{
length = 1;
}
byte[] readData = WriteRead(Command.NRF_R_REGISTER, (Register)((byte)Register.NRF_RX_ADDR_P0 + pipe), length);
return readData;
}
/// <summary>
/// Set nRF24L01 Send Address
/// </summary>
/// <param name="address">Address, address.Length = 5</param>
internal void SetTxAddress(SpanByte address)
{
// Details in the Datasheet P56
if (address.Length > 5 || address.Length < 1)
{
throw new ArgumentOutOfRangeException("Array Length must less than 6.");
}
_gpio.Write(_ce, PinValue.Low);
Write(Command.NRF_W_REGISTER, Register.NRF_TX_ADDR, address);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read nRF24L01 Send Address
/// </summary>
/// <returns>Address</returns>
internal byte[] ReadTxAddress()
{
// Details in the Datasheet P56
return WriteRead(Command.NRF_R_REGISTER, Register.NRF_TX_ADDR, 5);
}
/// <summary>
/// Set Working Channel
/// </summary>
/// <param name="channel">From 0 to 127</param>
internal void SetChannel(byte channel)
{
// Details in the Datasheet P54
// Register: 0x05, bit[6:0]
if (channel < 0 || channel > 127)
{
throw new ArgumentOutOfRangeException("Channel needs to be in the range of 0 to 127.");
}
_gpio.Write(_ce, PinValue.Low);
Write(Command.NRF_W_REGISTER, Register.NRF_RF_CH, channel);
_gpio.Write(_ce, PinValue.High);
}
/// <summary>
/// Read Working Channel
/// </summary>
/// <returns>Channel</returns>
internal byte ReadChannel()
{
// Details in the Datasheet P54
SpanByte readData = WriteRead(Command.NRF_R_REGISTER, Register.NRF_RF_CH, 1);
return readData[0];
}
#endregion
#region sensor operation
internal void Write(SpanByte writeData)
{
SpanByte readBuf = new byte[writeData.Length];
_sensor.TransferFullDuplex(writeData, readBuf);
}
internal void Write(Command command, Register register, byte writeByte)
{
SpanByte writeBuf = new byte[2]
{
(byte)((byte)command + (byte)register),
writeByte
};
SpanByte readBuf = new byte[2];
_sensor.TransferFullDuplex(writeBuf, readBuf);
}
internal void Write(Command command, Register register, SpanByte writeData)
{
SpanByte writeBuf = new byte[1 + writeData.Length];
SpanByte readBuf = new byte[1 + writeData.Length];
writeBuf[0] = (byte)((byte)command + (byte)register);
if (writeData.Length > 0)
{
writeData.CopyTo(writeBuf.Slice(1));
}
_sensor.TransferFullDuplex(writeBuf, readBuf);
}
internal byte[] WriteRead(Command command, Register register, int dataLength)
{
SpanByte writeBuf = new byte[1 + dataLength];
SpanByte readBuf = new byte[1 + dataLength];
writeBuf[0] = (byte)((byte)command + (byte)register);
_sensor.TransferFullDuplex(writeBuf, readBuf);
return readBuf.Slice(1).ToArray();
}
internal byte[] WriteRead(Command command, Register register, SpanByte writeData)
{
SpanByte writeBuf = new byte[1 + writeData.Length];
SpanByte readBuf = new byte[1 + writeData.Length];
writeBuf[0] = (byte)((byte)command + (byte)register);
writeData.CopyTo(writeBuf.Slice(1));
_sensor.TransferFullDuplex(writeBuf, readBuf);
return readBuf.Slice(1).ToArray();
}
#endregion
}
}