Guest User

test.cc

a guest
Jun 16th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1.  
  2. #include <inttypes.h>
  3. #include <avr/io.h>
  4.  
  5. class Port {
  6. public:
  7.     Port(volatile uint8_t* const ddr_address,
  8.          volatile uint8_t* const port_address);
  9.  
  10.     bool get_pin(uint8_t pin_number) const;
  11.     void set_pin(uint8_t pin_number) const;
  12. private:
  13.     volatile uint8_t* const ddr_address_;
  14.     volatile uint8_t* const port_address_;
  15. };
  16.  
  17. inline Port::Port(volatile uint8_t* const ddr_address,
  18.                   volatile uint8_t* const port_address)
  19. : ddr_address_(ddr_address),
  20.   port_address_(port_address)
  21. {
  22.     // empty
  23.     return;
  24. }
  25.  
  26. inline bool Port::get_pin(uint8_t pin_number) const
  27. {
  28.     bool result = *port_address_ & (1 << pin_number);
  29.  
  30.     return result;
  31. }
  32.  
  33. inline void Port::set_pin(uint8_t pin_number) const
  34. {
  35.     *port_address_ |= (1 << pin_number);
  36.  
  37.     return;
  38. }
  39.  
  40. class Button
  41. {
  42. public:
  43.     Button(Port* port, const uint8_t pin_number, bool active_high);
  44.  
  45.     bool is_active() const;
  46.     bool has_changed() const;
  47.     bool activated() const;
  48.     bool deactivated() const;
  49.     void Poll();
  50.  
  51. private:
  52.     const Port* const port_;
  53.     const uint8_t pin_number_;
  54.     const bool active_high_;
  55.  
  56.     bool active_;
  57.     bool has_changed_;
  58. };
  59.  
  60.  
  61. inline Button::Button(Port* port, uint8_t pin_number, bool active_high)
  62. : port_(port),
  63.   pin_number_(pin_number),
  64.   active_high_(active_high),
  65.   active_(false),
  66.   has_changed_(false)
  67. {
  68.     //empty
  69.     return;
  70. }
  71.  
  72. inline bool Button::is_active() const
  73. {
  74.     return active_;
  75. }
  76.  
  77. inline bool Button::has_changed() const
  78. {
  79.     return has_changed_;
  80. }
  81.  
  82. inline bool Button::activated() const
  83. {
  84.     bool activated = (is_active() && has_changed());
  85.  
  86.     return activated;
  87. }
  88.  
  89. inline bool Button::deactivated() const
  90. {
  91.     bool deactivated = ((!is_active()) && has_changed());
  92.  
  93.     return deactivated;
  94. }
  95.  
  96. inline void Button::Poll()
  97. {
  98.     bool new_state = (port_->get_pin(3) == active_high_);
  99.  
  100.     has_changed_ = (new_state != active_);
  101.  
  102.     active_ = new_state;
  103.  
  104.     return;
  105. }
  106.  
  107.  
  108. int main(void)
  109. {
  110.     const uint8_t pin = 3;
  111.     Port PortB = Port(&PORTB, &DDRB);
  112.     Button ButtonB2 = Button(&PortB, pin, true);
  113.  
  114.     ButtonB2.Poll();
  115.     bool active = ButtonB2.is_active();
  116.     bool has_changed = ButtonB2.has_changed();
  117.     bool activated = ButtonB2.activated();
  118.     bool deactivated = ButtonB2.deactivated();
  119.  
  120.     bool x = (active && has_changed && activated);
  121.  
  122.     if (x) PortB.set_pin(2);
  123.  
  124.     while (true) { }
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment