Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <avr/io.h>
- class Port {
- public:
- Port(volatile uint8_t* const ddr_address,
- volatile uint8_t* const port_address);
- bool get_pin(uint8_t pin_number) const;
- void set_pin(uint8_t pin_number) const;
- private:
- volatile uint8_t* const ddr_address_;
- volatile uint8_t* const port_address_;
- };
- inline Port::Port(volatile uint8_t* const ddr_address,
- volatile uint8_t* const port_address)
- : ddr_address_(ddr_address),
- port_address_(port_address)
- {
- // empty
- return;
- }
- inline bool Port::get_pin(uint8_t pin_number) const
- {
- bool result = *port_address_ & (1 << pin_number);
- return result;
- }
- inline void Port::set_pin(uint8_t pin_number) const
- {
- *port_address_ |= (1 << pin_number);
- return;
- }
- class Button
- {
- public:
- Button(Port* port, const uint8_t pin_number, bool active_high);
- bool is_active() const;
- bool has_changed() const;
- bool activated() const;
- bool deactivated() const;
- void Poll();
- private:
- const Port* const port_;
- const uint8_t pin_number_;
- const bool active_high_;
- bool active_;
- bool has_changed_;
- };
- inline Button::Button(Port* port, uint8_t pin_number, bool active_high)
- : port_(port),
- pin_number_(pin_number),
- active_high_(active_high),
- active_(false),
- has_changed_(false)
- {
- //empty
- return;
- }
- inline bool Button::is_active() const
- {
- return active_;
- }
- inline bool Button::has_changed() const
- {
- return has_changed_;
- }
- inline bool Button::activated() const
- {
- bool activated = (is_active() && has_changed());
- return activated;
- }
- inline bool Button::deactivated() const
- {
- bool deactivated = ((!is_active()) && has_changed());
- return deactivated;
- }
- inline void Button::Poll()
- {
- bool new_state = (port_->get_pin(3) == active_high_);
- has_changed_ = (new_state != active_);
- active_ = new_state;
- return;
- }
- int main(void)
- {
- const uint8_t pin = 3;
- Port PortB = Port(&PORTB, &DDRB);
- Button ButtonB2 = Button(&PortB, pin, true);
- ButtonB2.Poll();
- bool active = ButtonB2.is_active();
- bool has_changed = ButtonB2.has_changed();
- bool activated = ButtonB2.activated();
- bool deactivated = ButtonB2.deactivated();
- bool x = (active && has_changed && activated);
- if (x) PortB.set_pin(2);
- while (true) { }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment