fork download
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #define DEFINE_AVR_PORT(ClassName, PortReg, PinReg, DdrReg) \
  5. struct ClassName { \
  6.   static void setPort (uint8_t x) { PortReg = x; } \
  7.   static uint8_t getPort () { return PortReg; } \
  8.   static void setDdr (uint8_t x) { DdrReg = x; } \
  9.   static uint8_t getDdr () { return DdrReg; } \
  10.   static uint8_t getPin () { return PinReg; } \
  11. };
  12.  
  13. DEFINE_AVR_PORT(AvrPortA, PORTA, PINA, DDRA)
  14. DEFINE_AVR_PORT(AvrPortB, PORTB, PINB, DDRB)
  15. DEFINE_AVR_PORT(AvrPortC, PORTC, PINC, DDRC)
  16. DEFINE_AVR_PORT(AvrPortD, PORTD, PIND, DDRD)
  17.  
  18. template <typename Port, int PortPin>
  19. struct PinInterface {
  20. void setOutput () const
  21. {
  22. Port::setDdr(Port::getDdr() | (1 << PortPin));
  23. }
  24.  
  25. void setInput () const
  26. {
  27. Port::setDdr(Port::getDdr() & ~(1 << PortPin));
  28. }
  29.  
  30. void operator= (bool state) const
  31. {
  32. if (state) {
  33. Port::setPort(Port::getPort() | (1 << PortPin));
  34. } else {
  35. Port::setPort(Port::getPort() & ~(1 << PortPin));
  36. }
  37. }
  38.  
  39. operator bool () const
  40. {
  41. return (Port::getPin() & (1 << PortPin));
  42. }
  43. };
  44.  
  45. static PinInterface<AvrPortA, 0> PinA0;
  46. static PinInterface<AvrPortA, 1> PinA1;
  47. // and so on...
  48.  
  49. int main ()
  50. {
  51. PinA0.setInput();
  52. PinA0 == true; // seems like operator bool works all right, no need for operator==
  53. }
  54.  
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