fork download
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #define DEFINE_AVR_PORT(ClassName, PortReg, PinReg, DdrReg) \
  5. class ClassName { \
  6.   template <typename Context> \
  7.   friend struct AvrPins; \
  8.   static void setPort (uint8_t x) { PortReg = x; } \
  9.   static uint8_t getPort () { return PortReg; } \
  10.   static void setDdr (uint8_t x) { DdrReg = x; } \
  11.   static uint8_t getDdr () { return DdrReg; } \
  12.   static uint8_t getPin () { return PinReg; } \
  13. };
  14.  
  15. DEFINE_AVR_PORT(AvrPortA, PORTA, PINA, DDRA)
  16. DEFINE_AVR_PORT(AvrPortB, PORTB, PINB, DDRB)
  17. DEFINE_AVR_PORT(AvrPortC, PORTC, PINC, DDRC)
  18. DEFINE_AVR_PORT(AvrPortD, PORTD, PIND, DDRD)
  19.  
  20. template <typename TPort, int PortPin>
  21. struct AvrPin {
  22. typedef TPort Port;
  23. static const int port_pin = PortPin;
  24. };
  25.  
  26. template <typename Pin>
  27. void pinSetInput ()
  28. {
  29. cli();
  30. Pin::Port::setDdr(Pin::Port::getDdr() & ~(1 << Pin::port_pin));
  31. sei();
  32. }
  33.  
  34. template <typename Pin>
  35. void pinSetOutput ()
  36. {
  37. cli();
  38. Pin::Port::setDdr(Pin::Port::getDdr() | (1 << Pin::port_pin));
  39. sei();
  40. }
  41.  
  42. template <typename Pin>
  43. bool pinGet ()
  44. {
  45. return (Pin::Port::getPin() & (1 << Pin::port_pin));
  46. }
  47.  
  48. template <typename Pin>
  49. void pinSet (, bool x)
  50. {
  51. cli();
  52. if (x) {
  53. Pin::Port::setPort(Pin::Port::getPort() | (1 << Pin::port_pin));
  54. } else {
  55. Pin::Port::setPort(Pin::Port::getPort() & ~(1 << Pin::port_pin));
  56. }
  57. sei();
  58. }
  59.  
  60. template <typename Pin>
  61. void pinFflip ()
  62. {
  63. cli();
  64. Pin::Port::setPort(Pin::Port::getPort() ^ (1 << Pin::port_pin));
  65. sei();
  66. }
  67.  
  68. int main ()
  69. {
  70. typedef AvrPin<AvrPortA, 0> OutPin;
  71. pinSetOutput<OutPin>();
  72. pinSet<OutPin>(true);
  73.  
  74. typedef AvrPin<AvrPortA, 1> InPin;
  75. pinSetInput<InPin>();
  76. bool value = pinGet<InPin>(true);
  77. }
  78.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: fatal error: avr/io.h: No such file or directory
compilation terminated.
stdout
Standard output is empty