github.com/embeddedgo/x@v0.0.6-0.20191217015414-d79a36f562e7/radio/nrf24/commands.go (about)

     1  // Copyright 2019 Michal Derkacz. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package nrf24
     6  
     7  func (r *Radio) exec(args ...[]byte) STATUS {
     8  	r.DCI.WriteRead(args...)
     9  	return STATUS(args[1][0])
    10  }
    11  
    12  func (r *Radio) R_REGISTER(addr byte, val []byte) STATUS {
    13  	cmd := []byte{addr}
    14  	return r.exec(cmd, cmd, nil, val)
    15  }
    16  
    17  func (r *Radio) W_REGISTER(addr byte, val ...byte) STATUS {
    18  	cmd := []byte{addr | 0x20}
    19  	return r.exec(cmd, cmd, val)
    20  }
    21  
    22  func checkPayLen(pay []byte) {
    23  	if len(pay) > 32 {
    24  		panic("plen>32")
    25  	}
    26  }
    27  
    28  func (r *Radio) R_RX_PAYLOAD(pay []byte) STATUS {
    29  	checkPayLen(pay)
    30  	cmd := []byte{0x61}
    31  	return r.exec(cmd, cmd, nil, pay)
    32  }
    33  
    34  func (r *Radio) W_TX_PAYLOAD(pay []byte) STATUS {
    35  	checkPayLen(pay)
    36  	cmd := []byte{0xa0}
    37  	return r.exec(cmd, cmd, pay)
    38  }
    39  
    40  func (r *Radio) FLUSH_TX() STATUS {
    41  	cmd := []byte{0xe1}
    42  	return r.exec(cmd, cmd)
    43  }
    44  
    45  func (r *Radio) FLUSH_RX() STATUS {
    46  	cmd := []byte{0xe2}
    47  	return r.exec(cmd, cmd)
    48  }
    49  
    50  func (r *Radio) REUSE_TX_PL() STATUS {
    51  	cmd := []byte{0xe3}
    52  	return r.exec(cmd, cmd)
    53  }
    54  
    55  // ACTIVATE is nRF24L01 specific command.
    56  func (r *Radio) ACTIVATE(b byte) STATUS {
    57  	cmd := []byte{0x50}
    58  	return r.exec(cmd, cmd)
    59  }
    60  
    61  func (d *Radio) R_RX_PL_WID() (int, STATUS) {
    62  	cmd := []byte{0x60, 0xff}
    63  	s := d.exec(cmd, cmd)
    64  	return int(cmd[1]), s
    65  }
    66  
    67  func (r *Radio) W_ACK_PAYLOAD(pn int, pay []byte) STATUS {
    68  	checkPayNum(pn)
    69  	checkPayLen(pay)
    70  	cmd := []byte{byte(0xa8 | pn)}
    71  	return r.exec(cmd, cmd, pay)
    72  }
    73  
    74  func (r *Radio) W_TX_PAYLOAD_NOACK(pay []byte) STATUS {
    75  	checkPayLen(pay)
    76  	cmd := []byte{0xa0}
    77  	return r.exec(cmd, cmd, pay)
    78  }
    79  
    80  func (r *Radio) NOP() STATUS {
    81  	cmd := []byte{0xff}
    82  	return r.exec(cmd, cmd)
    83  }