nrf24l01_commands/
commands.rs1use crate::registers::{self, Register};
20use core::marker::PhantomData;
21
22pub trait Command {
24    const WORD: u8;
26}
27
28pub struct RRegister<R>(PhantomData<R>);
43
44pub struct WRegister<R>(
62    pub R,
64);
65
66pub struct RRxPayload<const N: usize>();
87
88pub struct WTxPayload<const N: usize>(
100    pub [u8; N],
105);
106
107pub struct FlushTx();
118
119pub struct FlushRx();
130
131pub struct ReuseTxPl();
144
145pub struct Activate();
156
157pub struct RRxPlWid();
168
169pub struct WAckPayload<const N: usize> {
184    pub pipe: u8,
186    pub payload: [u8; N],
191}
192
193pub struct WTxPayloadNoack<const N: usize>(
205    pub [u8; N],
210);
211
212pub struct Nop();
223
224impl<R> Command for RRegister<R> {
225    const WORD: u8 = 0;
226}
227impl<R> Command for WRegister<R> {
228    const WORD: u8 = 0b0010_0000;
229}
230impl<const N: usize> Command for RRxPayload<N> {
231    const WORD: u8 = 0b0110_0001;
232}
233impl<const N: usize> Command for WTxPayload<N> {
234    const WORD: u8 = 0b1010_0000;
235}
236impl Command for FlushTx {
237    const WORD: u8 = 0b1110_0001;
238}
239impl Command for FlushRx {
240    const WORD: u8 = 0b1110_0010;
241}
242impl Command for ReuseTxPl {
243    const WORD: u8 = 0b1110_0011;
244}
245impl Command for Activate {
246    const WORD: u8 = 0b0101_0000;
247}
248impl Command for RRxPlWid {
249    const WORD: u8 = 0b0110_0000;
250}
251impl<const N: usize> Command for WAckPayload<N> {
252    const WORD: u8 = 0b1010_1000;
253}
254impl<const N: usize> Command for WTxPayloadNoack<N> {
255    const WORD: u8 = 0b1011_0000;
256}
257impl Command for Nop {
258    const WORD: u8 = 0b1111_1111;
259}
260
261impl RRegister<registers::Config> {
262    pub const fn word() -> u8 {
264        Self::WORD | registers::Config::ADDRESS
265    }
266
267    pub const fn bytes() -> [u8; 2] {
269        [Self::word(), 0]
270    }
271}
272
273impl RRegister<registers::EnAa> {
274    pub const fn word() -> u8 {
276        Self::WORD | registers::EnAa::ADDRESS
277    }
278
279    pub const fn bytes() -> [u8; 2] {
281        [Self::word(), 0]
282    }
283}
284
285impl RRegister<registers::EnRxaddr> {
286    pub const fn word() -> u8 {
288        Self::WORD | registers::EnRxaddr::ADDRESS
289    }
290
291    pub const fn bytes() -> [u8; 2] {
293        [Self::word(), 0]
294    }
295}
296
297impl RRegister<registers::SetupAw> {
298    pub const fn word() -> u8 {
300        Self::WORD | registers::SetupAw::ADDRESS
301    }
302
303    pub const fn bytes() -> [u8; 2] {
305        [Self::word(), 0]
306    }
307}
308
309impl RRegister<registers::SetupRetr> {
310    pub const fn word() -> u8 {
312        Self::WORD | registers::SetupRetr::ADDRESS
313    }
314
315    pub const fn bytes() -> [u8; 2] {
317        [Self::word(), 0]
318    }
319}
320
321impl RRegister<registers::RfCh> {
322    pub const fn word() -> u8 {
324        Self::WORD | registers::RfCh::ADDRESS
325    }
326
327    pub const fn bytes() -> [u8; 2] {
329        [Self::word(), 0]
330    }
331}
332
333impl RRegister<registers::RfSetup> {
334    pub const fn word() -> u8 {
336        Self::WORD | registers::RfSetup::ADDRESS
337    }
338
339    pub const fn bytes() -> [u8; 2] {
341        [Self::word(), 0]
342    }
343}
344
345impl RRegister<registers::Status> {
346    pub const fn word() -> u8 {
348        Self::WORD | registers::Status::ADDRESS
349    }
350
351    pub const fn bytes() -> [u8; 2] {
353        [Self::word(), 0]
354    }
355}
356
357impl RRegister<registers::ObserveTx> {
358    pub const fn word() -> u8 {
360        Self::WORD | registers::ObserveTx::ADDRESS
361    }
362
363    pub const fn bytes() -> [u8; 2] {
365        [Self::word(), 0]
366    }
367}
368
369impl RRegister<registers::Cd> {
370    pub const fn word() -> u8 {
372        Self::WORD | registers::Cd::ADDRESS
373    }
374
375    pub const fn bytes() -> [u8; 2] {
377        [Self::word(), 0]
378    }
379}
380
381impl<const N: usize> RRegister<registers::RxAddrP0<N>> {
382    pub const fn word() -> u8 {
384        Self::WORD | registers::RxAddrP0::<N>::ADDRESS
385    }
386
387    pub const fn bytes() -> [u8; N + 1] {
389        let mut bytes = [0; N + 1];
390        bytes[0] = Self::word();
391        bytes
392    }
393}
394
395impl<const N: usize> RRegister<registers::RxAddrP1<N>> {
396    pub const fn word() -> u8 {
398        Self::WORD | registers::RxAddrP1::<N>::ADDRESS
399    }
400
401    pub const fn bytes() -> [u8; N + 1] {
403        let mut bytes = [0; N + 1];
404        bytes[0] = Self::word();
405        bytes
406    }
407}
408
409impl RRegister<registers::RxAddrP2> {
410    pub const fn word() -> u8 {
412        Self::WORD | registers::RxAddrP2::ADDRESS
413    }
414
415    pub const fn bytes() -> [u8; 2] {
417        [Self::word(), 0]
418    }
419}
420
421impl RRegister<registers::RxAddrP3> {
422    pub const fn word() -> u8 {
424        Self::WORD | registers::RxAddrP3::ADDRESS
425    }
426
427    pub const fn bytes() -> [u8; 2] {
429        [Self::word(), 0]
430    }
431}
432
433impl RRegister<registers::RxAddrP4> {
434    pub const fn word() -> u8 {
436        Self::WORD | registers::RxAddrP4::ADDRESS
437    }
438
439    pub const fn bytes() -> [u8; 2] {
441        [Self::word(), 0]
442    }
443}
444
445impl RRegister<registers::RxAddrP5> {
446    pub const fn word() -> u8 {
448        Self::WORD | registers::RxAddrP5::ADDRESS
449    }
450
451    pub const fn bytes() -> [u8; 2] {
453        [Self::word(), 0]
454    }
455}
456
457impl<const N: usize> RRegister<registers::TxAddr<N>> {
458    pub const fn word() -> u8 {
460        Self::WORD | registers::TxAddr::<N>::ADDRESS
461    }
462
463    pub const fn bytes() -> [u8; N + 1] {
465        let mut bytes = [0; N + 1];
466        bytes[0] = Self::word();
467        bytes
468    }
469}
470
471impl RRegister<registers::RxPwP0> {
472    pub const fn word() -> u8 {
474        Self::WORD | registers::RxPwP0::ADDRESS
475    }
476
477    pub const fn bytes() -> [u8; 2] {
479        [Self::word(), 0]
480    }
481}
482
483impl RRegister<registers::RxPwP1> {
484    pub const fn word() -> u8 {
486        Self::WORD | registers::RxPwP1::ADDRESS
487    }
488
489    pub const fn bytes() -> [u8; 2] {
491        [Self::word(), 0]
492    }
493}
494
495impl RRegister<registers::RxPwP2> {
496    pub const fn word() -> u8 {
498        Self::WORD | registers::RxPwP2::ADDRESS
499    }
500
501    pub const fn bytes() -> [u8; 2] {
503        [Self::word(), 0]
504    }
505}
506
507impl RRegister<registers::RxPwP3> {
508    pub const fn word() -> u8 {
510        Self::WORD | registers::RxPwP3::ADDRESS
511    }
512
513    pub const fn bytes() -> [u8; 2] {
515        [Self::word(), 0]
516    }
517}
518
519impl RRegister<registers::RxPwP4> {
520    pub const fn word() -> u8 {
522        Self::WORD | registers::RxPwP4::ADDRESS
523    }
524
525    pub const fn bytes() -> [u8; 2] {
527        [Self::word(), 0]
528    }
529}
530
531impl RRegister<registers::RxPwP5> {
532    pub const fn word() -> u8 {
534        Self::WORD | registers::RxPwP5::ADDRESS
535    }
536
537    pub const fn bytes() -> [u8; 2] {
539        [Self::word(), 0]
540    }
541}
542
543impl RRegister<registers::FifoStatus> {
544    pub const fn word() -> u8 {
546        Self::WORD | registers::FifoStatus::ADDRESS
547    }
548
549    pub const fn bytes() -> [u8; 2] {
551        [Self::word(), 0]
552    }
553}
554
555impl RRegister<registers::Dynpd> {
556    pub const fn word() -> u8 {
558        Self::WORD | registers::Dynpd::ADDRESS
559    }
560
561    pub const fn bytes() -> [u8; 2] {
563        [Self::word(), 0]
564    }
565}
566
567impl RRegister<registers::Feature> {
568    pub const fn word() -> u8 {
570        Self::WORD | registers::Feature::ADDRESS
571    }
572
573    pub const fn bytes() -> [u8; 2] {
575        [Self::word(), 0]
576    }
577}
578
579impl WRegister<registers::Config> {
580    pub const fn word() -> u8 {
582        Self::WORD | registers::Config::ADDRESS
583    }
584
585    pub const fn bytes(&self) -> [u8; 2] {
587        [Self::word(), self.0.into_bits()]
588    }
589}
590
591impl WRegister<registers::EnAa> {
592    pub const fn word() -> u8 {
594        Self::WORD | registers::EnAa::ADDRESS
595    }
596
597    pub const fn bytes(&self) -> [u8; 2] {
599        [Self::word(), self.0.into_bits()]
600    }
601}
602
603impl WRegister<registers::EnRxaddr> {
604    pub const fn word() -> u8 {
606        Self::WORD | registers::EnRxaddr::ADDRESS
607    }
608
609    pub const fn bytes(&self) -> [u8; 2] {
611        [Self::word(), self.0.into_bits()]
612    }
613}
614
615impl WRegister<registers::SetupAw> {
616    pub const fn word() -> u8 {
618        Self::WORD | registers::SetupAw::ADDRESS
619    }
620
621    pub const fn bytes(&self) -> [u8; 2] {
623        [Self::word(), self.0.into_bits()]
624    }
625}
626
627impl WRegister<registers::SetupRetr> {
628    pub const fn word() -> u8 {
630        Self::WORD | registers::SetupRetr::ADDRESS
631    }
632
633    pub const fn bytes(&self) -> [u8; 2] {
635        [Self::word(), self.0.into_bits()]
636    }
637}
638
639impl WRegister<registers::RfCh> {
640    pub const fn word() -> u8 {
642        Self::WORD | registers::RfCh::ADDRESS
643    }
644
645    pub const fn bytes(&self) -> [u8; 2] {
647        [Self::word(), self.0.into_bits()]
648    }
649}
650
651impl WRegister<registers::RfSetup> {
652    pub const fn word() -> u8 {
654        Self::WORD | registers::RfSetup::ADDRESS
655    }
656
657    pub const fn bytes(&self) -> [u8; 2] {
659        [Self::word(), self.0.into_bits()]
660    }
661}
662
663impl WRegister<registers::Status> {
664    pub const fn word() -> u8 {
666        Self::WORD | registers::Status::ADDRESS
667    }
668
669    pub const fn bytes(&self) -> [u8; 2] {
671        [Self::word(), self.0.into_bits()]
672    }
673}
674
675impl WRegister<registers::ObserveTx> {
676    pub const fn word() -> u8 {
678        Self::WORD | registers::ObserveTx::ADDRESS
679    }
680
681    pub const fn bytes(&self) -> [u8; 2] {
683        [Self::word(), self.0.into_bits()]
684    }
685}
686
687impl WRegister<registers::Cd> {
688    pub const fn word() -> u8 {
690        Self::WORD | registers::Cd::ADDRESS
691    }
692
693    pub const fn bytes(&self) -> [u8; 2] {
695        [Self::word(), self.0.into_bits()]
696    }
697}
698
699#[inline(always)]
701const fn concat_word_addr<const N: usize>(word: u8, addr: [u8; N]) -> [u8; N + 1] {
702    let mut bytes: [u8; N + 1] = [0; N + 1];
703    bytes[0] = word;
704    let mut i = 1;
706    while i < N + 1 {
707        bytes[i] = addr[i - 1];
708        i += 1;
709    }
710    bytes
711}
712
713impl<const N: usize> WRegister<registers::RxAddrP0<N>> {
714    pub const fn word() -> u8 {
716        Self::WORD | registers::RxAddrP0::<N>::ADDRESS
717    }
718
719    pub const fn bytes(&self) -> [u8; N + 1] {
721        concat_word_addr(Self::word(), self.0.into_bytes())
722    }
723}
724
725impl<const N: usize> WRegister<registers::RxAddrP1<N>> {
726    pub const fn word() -> u8 {
728        Self::WORD | registers::RxAddrP1::<N>::ADDRESS
729    }
730
731    pub const fn bytes(&self) -> [u8; N + 1] {
733        concat_word_addr(Self::word(), self.0.into_bytes())
734    }
735}
736
737impl WRegister<registers::RxAddrP2> {
738    pub const fn word() -> u8 {
740        Self::WORD | registers::RxAddrP2::ADDRESS
741    }
742
743    pub const fn bytes(&self) -> [u8; 2] {
745        [Self::word(), self.0.into_bits()]
746    }
747}
748
749impl WRegister<registers::RxAddrP3> {
750    pub const fn word() -> u8 {
752        Self::WORD | registers::RxAddrP3::ADDRESS
753    }
754
755    pub const fn bytes(&self) -> [u8; 2] {
757        [Self::word(), self.0.into_bits()]
758    }
759}
760
761impl WRegister<registers::RxAddrP4> {
762    pub const fn word() -> u8 {
764        Self::WORD | registers::RxAddrP4::ADDRESS
765    }
766
767    pub const fn bytes(&self) -> [u8; 2] {
769        [Self::word(), self.0.into_bits()]
770    }
771}
772
773impl WRegister<registers::RxAddrP5> {
774    pub const fn word() -> u8 {
776        Self::WORD | registers::RxAddrP5::ADDRESS
777    }
778
779    pub const fn bytes(&self) -> [u8; 2] {
781        [Self::word(), self.0.into_bits()]
782    }
783}
784
785impl<const N: usize> WRegister<registers::TxAddr<N>> {
786    pub const fn word() -> u8 {
788        Self::WORD | registers::TxAddr::<N>::ADDRESS
789    }
790
791    pub const fn bytes(&self) -> [u8; N + 1] {
793        concat_word_addr(Self::word(), self.0.into_bytes())
794    }
795}
796
797impl WRegister<registers::RxPwP0> {
798    pub const fn word() -> u8 {
800        Self::WORD | registers::RxPwP0::ADDRESS
801    }
802
803    pub const fn bytes(&self) -> [u8; 2] {
805        [Self::word(), self.0.into_bits()]
806    }
807}
808
809impl WRegister<registers::RxPwP1> {
810    pub const fn word() -> u8 {
812        Self::WORD | registers::RxPwP1::ADDRESS
813    }
814
815    pub const fn bytes(&self) -> [u8; 2] {
817        [Self::word(), self.0.into_bits()]
818    }
819}
820
821impl WRegister<registers::RxPwP2> {
822    pub const fn word() -> u8 {
824        Self::WORD | registers::RxPwP2::ADDRESS
825    }
826
827    pub const fn bytes(&self) -> [u8; 2] {
829        [Self::word(), self.0.into_bits()]
830    }
831}
832
833impl WRegister<registers::RxPwP3> {
834    pub const fn word() -> u8 {
836        Self::WORD | registers::RxPwP3::ADDRESS
837    }
838
839    pub const fn bytes(&self) -> [u8; 2] {
841        [Self::word(), self.0.into_bits()]
842    }
843}
844
845impl WRegister<registers::RxPwP4> {
846    pub const fn word() -> u8 {
848        Self::WORD | registers::RxPwP4::ADDRESS
849    }
850
851    pub const fn bytes(&self) -> [u8; 2] {
853        [Self::word(), self.0.into_bits()]
854    }
855}
856
857impl WRegister<registers::RxPwP5> {
858    pub const fn word() -> u8 {
860        Self::WORD | registers::RxPwP5::ADDRESS
861    }
862
863    pub const fn bytes(&self) -> [u8; 2] {
865        [Self::word(), self.0.into_bits()]
866    }
867}
868
869impl WRegister<registers::FifoStatus> {
870    pub const fn word() -> u8 {
872        Self::WORD | registers::FifoStatus::ADDRESS
873    }
874
875    pub const fn bytes(&self) -> [u8; 2] {
877        [Self::word(), self.0.into_bits()]
878    }
879}
880
881impl WRegister<registers::Dynpd> {
882    pub const fn word() -> u8 {
884        Self::WORD | registers::Dynpd::ADDRESS
885    }
886
887    pub const fn bytes(&self) -> [u8; 2] {
889        [Self::word(), self.0.into_bits()]
890    }
891}
892
893impl WRegister<registers::Feature> {
894    pub const fn word() -> u8 {
896        Self::WORD | registers::Feature::ADDRESS
897    }
898
899    pub const fn bytes(&self) -> [u8; 2] {
901        [Self::word(), self.0.into_bits()]
902    }
903}
904
905impl<const N: usize> RRxPayload<N> {
906    pub const fn bytes() -> [u8; N + 1] {
908        let mut bytes: [u8; N + 1] = [0; N + 1];
909        bytes[0] = Self::WORD;
910        bytes
911    }
912}
913
914#[inline(always)]
916const fn concat_word_payload<const N: usize>(word: u8, payload: [u8; N]) -> [u8; N + 1] {
917    let mut bytes: [u8; N + 1] = [0; N + 1];
918    bytes[0] = word;
919
920    let mut bytes_idx = 1;
921    while bytes_idx < N + 1 {
922        bytes[bytes_idx] = payload[bytes_idx - 1];
923        bytes_idx += 1;
924    }
925    bytes
926}
927
928impl<const N: usize> WTxPayload<N> {
929    pub const fn bytes(&self) -> [u8; N + 1] {
931        concat_word_payload(Self::WORD, self.0)
932    }
933}
934
935impl FlushTx {
936    pub const fn bytes() -> [u8; 1] {
938        [Self::WORD]
939    }
940}
941
942impl FlushRx {
943    pub const fn bytes() -> [u8; 1] {
945        [Self::WORD]
946    }
947}
948
949impl ReuseTxPl {
950    pub const fn bytes() -> [u8; 1] {
952        [Self::WORD]
953    }
954}
955
956impl Activate {
957    pub const fn bytes() -> [u8; 2] {
959        [Self::WORD, 0x73]
960    }
961}
962
963impl RRxPlWid {
964    pub const fn bytes() -> [u8; 2] {
966        [Self::WORD, 0]
967    }
968}
969
970impl<const N: usize> WAckPayload<N> {
971    pub const fn bytes(&self) -> [u8; N + 1] {
973        concat_word_payload(Self::WORD | self.pipe, self.payload)
974    }
975}
976
977impl<const N: usize> WTxPayloadNoack<N> {
978    pub const fn bytes(&self) -> [u8; N + 1] {
980        concat_word_payload(Self::WORD, self.0)
981    }
982}
983
984impl Nop {
985    pub const fn bytes() -> [u8; 1] {
987        [Self::WORD]
988    }
989}