Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #[warn(dead_code)]
  2. #[derive(Debug)]
  3. enum Gpio {
  4. Bcm1,
  5. Bcm2,
  6. Bcm3,
  7. Bcm4
  8. }
  9.  
  10. struct Ports {
  11. bcm1: Gpio,
  12. bcm2: Gpio,
  13. bcm3: Gpio,
  14. bcm4: Gpio,
  15. }
  16.  
  17. macro_rules! foo {
  18. () => (());
  19. }
  20.  
  21. macro_rules! port_name {
  22. ( &ports.$name:ident ) => ( $name );
  23. ( ports.$name:ident ) => ( $name );
  24. }
  25.  
  26. macro_rules! port_type {
  27. ( (&ports.)$name:ident ) => ( &'a Gpio );
  28. ( (ports.)$name:ident ) => ( Gpio );
  29. }
  30.  
  31. macro_rules! port_value {
  32. ( &ports.$name:ident ) => ( &'a ports.$name );
  33. ( ports.$name:ident ) => ( ports.$name );
  34. }
  35.  
  36. macro_rules! configure {
  37. ( $peripheral:ident: $( $port:expr ),* ) => {
  38. #[derive(Debug)]
  39. struct $peripheral<'a> {
  40. $(
  41. port_name!($port): port_type!($port),
  42. )*
  43. }
  44. let $peripheral = $peripheral { $( a: port_value!($port)),* };
  45. };
  46. }
  47.  
  48. fn main() {
  49. let ports = Ports {bcm1: Gpio::Bcm1, bcm2: Gpio::Bcm2, bcm3: Gpio::Bcm3, bcm4: Gpio::Bcm4};
  50. //let a = ports.bcm1;
  51. //let b = ports.bcm2;
  52. configure!(motor1: &ports.bcm1, ports.bcm2);
  53. //configure!(motor2: &ports.bcm1, ports.bcm3);
  54. /*
  55. #[derive(Debug)]
  56. struct Peripheral<'a> {
  57. port1: &'a Gpio,
  58. port2: Gpio
  59. }
  60. let motor1 = Peripheral { port1: &ports.bcm1, port2: ports.bcm2 };
  61. let motor2 = Peripheral { port1: &ports.bcm1, port2: ports.bcm3 };
  62. */
  63. println!("motor1:{:?}", motor1);
  64. println!("motor2:{:?}", motor2);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement