public override void WriteRegister(Register register, byte value)
 {
     _spi.SendData(_chipSelect, (byte)register, value);
 }
Exemple #2
0
        public void SetPipe0RxAddress(string address)
        {
            try
            {
                // Read the Address width
                byte[] txBuffer1 = new byte[] { SETUP_AW | R_REGISTER, 0x0 };
                Console.WriteLine(" txBuffer:" + BitConverter.ToString(txBuffer1));

                /*
                 * // Appears to work but not certain it actually does
                 * Console.WriteLine(" nrf24L01Device.WriteRead...SETUP_AW");
                 * byte[] rxBuffer1 = nrf24L01Device.WriteRead(txBuffer1, (ushort)txBuffer1.Length);
                 */

                byte[] rxBuffer1 = new byte[txBuffer1.Length];
                Console.WriteLine(" spiBus.ExchangeData...RX_ADDR_P0");
                spiBus.ExchangeData(spiPeriphChipSelect, ChipSelectMode.ActiveLow, txBuffer1, rxBuffer1);

                Console.WriteLine(" rxBuffer:" + BitConverter.ToString(rxBuffer1));

                // Extract then adjust the address width
                byte addressWidthValue = rxBuffer1[1];
                addressWidthValue &= 0b00000011;
                addressWidthValue += 2;
                Console.WriteLine("Address width 0x{0:x2} - Value 0X{1:x2} - Bits {2} Value adjusted {3}", SETUP_AW, rxBuffer1[1], Convert.ToString(rxBuffer1[1], 2).PadLeft(8, '0'), addressWidthValue);
                Console.WriteLine();


                // Write Pipe0 Receive address
                Console.WriteLine("Address write 1");
                byte[] txBuffer2 = new byte[addressWidthValue + 1];
                txBuffer2[0] = RX_ADDR_P0 | W_REGISTER;
                Array.Copy(Encoding.UTF8.GetBytes(address), 0, txBuffer2, 1, addressWidthValue);
                Console.WriteLine(" txBuffer:" + BitConverter.ToString(txBuffer2));

                /*
                 * // Appears to work but not certain it does
                 * Console.WriteLine(" nrf24L01Device.Write...RX_ADDR_P0");
                 * nrf24L01Device.WriteBytes(txBuffer2);
                 */

                Console.WriteLine(" spiBus.SendData...RX_ADDR_P0");
                spiBus.SendData(spiPeriphChipSelect, ChipSelectMode.ActiveLow, txBuffer2);

                Console.WriteLine();

                // Read Pipe0 Receive address
                Console.WriteLine("Address read 1");
                byte[] txBuffer3 = new byte[addressWidthValue + 1];
                txBuffer3[0] = RX_ADDR_P0 | R_REGISTER;
                Console.WriteLine(" txBuffer:" + BitConverter.ToString(txBuffer3));

                /*
                 * // Broken returns  Address 0x0a - RX Buffer 5A-5A-5A-5A-59-58 RX Address 5A-5A-5A-59-58 Address ZZZYX
                 * Console.WriteLine(" nrf24L01Device.WriteRead...RX_ADDR_P0");
                 * byte[] rxBuffer3 = nrf24L01Device.WriteRead(txBuffer3, (ushort)txBuffer3.Length);
                 */

                byte[] rxBuffer3 = new byte[addressWidthValue + 1];
                Console.WriteLine(" spiBus.ExchangeData...RX_ADDR_P0");
                spiBus.ExchangeData(spiPeriphChipSelect, ChipSelectMode.ActiveLow, txBuffer3, rxBuffer3);

                Console.WriteLine();

                Console.WriteLine("Address 0x{0:x2} - RX Buffer {1} RX Address {2} Address {3}", RX_ADDR_P0, BitConverter.ToString(rxBuffer3, 0), BitConverter.ToString(rxBuffer3, 1), UTF8Encoding.UTF8.GetString(rxBuffer3, 1, addressWidthValue));
            }
            catch (Exception ex)
            {
                Console.WriteLine("ReadDeviceIDDiy " + ex.Message);
            }
        }