/// <summary> /// Read 1 of 6 available module addresses /// </summary> public byte[] GetAddress(AddressSlot slot, int width) { CheckIsInitialized(); AddressWidth.Check(width); var read = Execute(Commands.R_REGISTER, (byte)slot, new byte[width]); var result = new byte[read.Length - 1]; Array.Copy(read, 1, result, 0, result.Length); return(result); }
/// <summary> /// Set one of 6 available module addresses /// </summary> public void SetAddress(AddressSlot slot, byte[] address) { CheckIsInitialized(); AddressWidth.Check(address); Execute(Commands.W_REGISTER, (byte)slot, address); if (slot == AddressSlot.Zero) { _slot0Address = address; } }
/// <summary> /// Set one of 6 available module addresses to its default value and disables it /// </summary> public void SetToDefaultAddress(AddressSlot s) { byte[] addr = new byte[1]; CheckIsInitialized(); switch (s) { case AddressSlot.Zero: AddressWidth.Check(_def_rx_addr_p0); Execute(Commands.W_REGISTER, (byte)s, _def_rx_addr_p0); _slot0Address = _def_rx_addr_p0; break; case AddressSlot.One: AddressWidth.Check(_def_rx_addr_p1); Execute(Commands.W_REGISTER, (byte)s, _def_rx_addr_p1); _slot0Address = _def_rx_addr_p1; break; case AddressSlot.Two: addr[0] = (byte)_def_rx_addr_p2.GetValue(0); Execute(Commands.W_REGISTER, (byte)s, addr); _slot2Address = addr; break; case AddressSlot.Three: addr[0] = (byte)_def_rx_addr_p3.GetValue(0); Execute(Commands.W_REGISTER, (byte)s, addr); _slot3Address = addr; break; case AddressSlot.Four: addr[0] = (byte)_def_rx_addr_p4.GetValue(0); Execute(Commands.W_REGISTER, (byte)s, addr); _slot4Address = addr; break; case AddressSlot.Five: addr[0] = (byte)_def_rx_addr_p5.GetValue(0); Execute(Commands.W_REGISTER, (byte)s, addr); _slot5Address = addr; break; } DisableAA(s); DisableRX(s); }
/// <summary> /// Set one of 6 available module addresses and enables it when in RX mode, sets also auto-ack if EN_AA is true /// </summary> public void SetAddress(AddressSlot s, byte[] address, bool EN_AA) { byte[] addr = new byte[1]; CheckIsInitialized(); if (s == AddressSlot.Zero || s == AddressSlot.One) { AddressWidth.Check(address); Execute(Commands.W_REGISTER, (byte)s, address); } else { addr[0] = (byte)address.GetValue(0); Execute(Commands.W_REGISTER, (byte)s, addr); } switch (s) { case AddressSlot.Zero: _slot0Address = address; break; case AddressSlot.One: _slot1Address = address; break; case AddressSlot.Two: _slot2Address = addr; break; case AddressSlot.Three: _slot3Address = addr; break; case AddressSlot.Four: _slot4Address = addr; break; case AddressSlot.Five: _slot5Address = addr; break; } if (EN_AA) { EnableAA(s); } EnableRX(s); }
/// <summary> /// Read 1 of 6 available module addresses /// </summary> public byte[] GetAddress(AddressSlot slot, int width) { var read = new byte[width]; var result = new byte[width]; CheckIsInitialized(); if (slot == AddressSlot.Zero || slot == AddressSlot.One) { AddressWidth.Check(width); } else { width = 1; } read = Execute(Commands.R_REGISTER, (byte)slot, new byte[width]); result = new byte[read.Length - 1]; Array.Copy(read, 1, result, 0, result.Length); return(result); }
/// <summary> /// Configure the module basic settings. Module needs to be initiaized. /// </summary> /// <param name="address">RF address (3-5 bytes). The width of this address determins the width of all addresses used for sending/receiving.</param> /// <param name="channel">RF channel (0-127)</param> public void Configure(byte[] address, byte channel) { CheckIsInitialized(); AddressWidth.Check(address); // Set radio channel Execute(Commands.W_REGISTER, Registers.RF_CH, new[] { (byte)(channel & 0x7F) // channel is 7 bits }); // Enable dynamic payload length Execute(Commands.W_REGISTER, Registers.FEATURE, new[] { (byte)(1 << Bits.EN_DPL) }); // Set auto-ack Execute(Commands.W_REGISTER, Registers.EN_AA, new[] { (byte)(0x01) }); // Set RX-addr EN_RXADDR Execute(Commands.W_REGISTER, Registers.EN_RXADDR, new[] { (byte)(0x01) }); // Set dynamic payload length for pipes Execute(Commands.W_REGISTER, Registers.DYNPD, new[] { (byte)(1 << Bits.DPL_P0 | 1 << Bits.DPL_P1) }); // Flush RX FIFO Execute(Commands.FLUSH_RX, 0x00, new byte[0]); // Flush TX FIFO Execute(Commands.FLUSH_TX, 0x00, new byte[0]); // Clear IRQ Masks Execute(Commands.W_REGISTER, Registers.STATUS, new[] { (byte)(1 << Bits.MASK_RX_DR | 1 << Bits.MASK_TX_DS | 1 << Bits.MAX_RT) }); // Set default address Execute(Commands.W_REGISTER, Registers.SETUP_AW, new[] { AddressWidth.Get(address) }); // Set module address SetAddress(AddressSlot.Zero, address, true); //Execute(Commands.W_REGISTER, (byte)AddressSlot.Zero, address); //No cal pq també s'executa quan es posa en receive mode al final de configure _slot1Address = _def_rx_addr_p1; _slot2Address = _def_rx_addr_p2; _slot3Address = _def_rx_addr_p3; _slot4Address = _def_rx_addr_p4; _slot5Address = _def_rx_addr_p5; // Set retransmission values Execute(Commands.W_REGISTER, Registers.SETUP_RETR, new[] { (byte)(0x01 << Bits.ARD | 0x00 << Bits.ARC) }); // Set RF Setup Execute(Commands.W_REGISTER, Registers.RF_SETUP, new[] { (byte)(0xBF & 0x05) // bit 6 is reserved }); // Setup, CRC enabled, Power Up, PRX SetReceiveMode(); }
/// <summary> /// Configure the module basic settings. Module needs to be initiaized. /// </summary> /// <param name="address">RF address (3-5 bytes). The width of this address determins the width of all addresses used for sending/receiving.</param> /// <param name="channel">RF channel (0-127)</param> /// <param name="dataRate">Data Rate to use</param> public void Configure(byte[] address, byte channel, NRFDataRate dataRate) { CheckIsInitialized(); AddressWidth.Check(address); // Set radio channel Execute(Commands.W_REGISTER, Registers.RF_CH, new[] { (byte)(channel & 0x7F) // channel is 7 bits }); // Set Data rate var regValue = Execute(Commands.R_REGISTER, Registers.RF_SETUP, new byte[1])[1]; switch (dataRate) { case NRFDataRate.DR1Mbps: regValue &= (byte)~(1 << Bits.RF_DR_LOW); // 0 regValue &= (byte)~(1 << Bits.RF_DR_HIGH); // 0 break; case NRFDataRate.DR2Mbps: regValue &= (byte)~(1 << Bits.RF_DR_LOW); // 0 regValue |= (byte)(1 << Bits.RF_DR_HIGH); // 1 break; case NRFDataRate.DR250kbps: regValue |= (byte)(1 << Bits.RF_DR_LOW); // 1 regValue &= (byte)~(1 << Bits.RF_DR_HIGH); // 0 break; default: throw new ArgumentOutOfRangeException("dataRate"); } Execute(Commands.W_REGISTER, Registers.RF_SETUP, new[] { regValue }); // Enable dynamic payload length Execute(Commands.W_REGISTER, Registers.FEATURE, new[] { (byte)(1 << Bits.EN_DPL) }); // Set auto-ack Execute(Commands.W_REGISTER, Registers.EN_AA, new[] { (byte)(1 << Bits.ENAA_P0 | 1 << Bits.ENAA_P1) }); // Set dynamic payload length for pipes Execute(Commands.W_REGISTER, Registers.DYNPD, new[] { (byte)(1 << Bits.DPL_P0 | 1 << Bits.DPL_P1) }); // Flush RX FIFO Execute(Commands.FLUSH_RX, 0x00, new byte[0]); // Flush TX FIFO Execute(Commands.FLUSH_TX, 0x00, new byte[0]); // Clear IRQ Masks Execute(Commands.W_REGISTER, Registers.STATUS, new[] { (byte)(1 << Bits.MASK_RX_DR | 1 << Bits.MASK_TX_DS | 1 << Bits.MAX_RT) }); // Set default address Execute(Commands.W_REGISTER, Registers.SETUP_AW, new[] { AddressWidth.Get(address) }); // Set module address _slot0Address = address; Execute(Commands.W_REGISTER, (byte)AddressSlot.Zero, address); // Set retransmission values Execute(Commands.W_REGISTER, Registers.SETUP_RETR, new[] { (byte)(0x0F << Bits.ARD | 0x0F << Bits.ARC) }); // Setup, CRC enabled, Power Up, PRX SetReceiveMode(); }