Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
PersistentGPIO.hxx
1
36#ifndef _OS_PERSISTENTGPIO_HXX_
37#define _OS_PERSISTENTGPIO_HXX_
38
40#include "os/Gpio.hxx"
41
42extern StoredBitSet *g_gpio_stored_bit_set;
43
44class PersistentGpio : public Gpio
45{
46public:
47 constexpr PersistentGpio(const Gpio *impl, const unsigned bit_ofs)
48 : impl_(impl)
49 , bit_(bit_ofs)
50 {
51 }
52
53 void restore() const {
54 impl_->write(g_gpio_stored_bit_set->get_bit(bit_));
55 }
56
57 void write(Value new_state) const OVERRIDE
58 {
59 impl_->write(new_state);
60 g_gpio_stored_bit_set->set_bit(bit_, new_state).lock_and_flush();
61 }
62
63 void set() const OVERRIDE
64 {
65 impl_->set();
66 g_gpio_stored_bit_set->set_bit(bit_, true).lock_and_flush();
67 }
68
69 void clr() const OVERRIDE
70 {
71 impl_->clr();
72 g_gpio_stored_bit_set->set_bit(bit_, false).lock_and_flush();
73 }
74
76 {
77 return impl_->read();
78 }
79
81 {
82 // NOTE: not sure if it's a good idea to let the direction be modified,
83 // since upon boot we will restore something else. We don't persist the
84 // direction.
85 return impl_->set_direction(dir);
86 }
87
89 {
90 return impl_->direction();
91 }
92
93private:
95 const Gpio *impl_;
97 const unsigned bit_;
98};
99
100#endif // _OS_PERSISTENTGPIO_HXX_
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
virtual void write(Value new_state) const =0
Writes a GPIO output pin (set or clear to a specific state).
virtual Direction direction() const =0
Gets the GPIO direction.
virtual void clr() const =0
Clears the GPIO output pin to low.
Value
Defines the options for GPIO level.
Definition Gpio.hxx:62
virtual void set_direction(Direction dir) const =0
Sets the GPIO direction.
virtual void set() const =0
Sets the GPIO output pin to high.
Direction
Defines the options for GPIO direction.
Definition Gpio.hxx:73
virtual Value read() const =0
Retrieves the current Value of a GPIO input pin.
void set_direction(Direction dir) const OVERRIDE
Sets the GPIO direction.
Direction direction() const OVERRIDE
Gets the GPIO direction.
const Gpio * impl_
Implementation of the gpio object.
void write(Value new_state) const OVERRIDE
Writes a GPIO output pin (set or clear to a specific state).
Value read() const OVERRIDE
Retrieves the current Value of a GPIO input pin.
void set() const OVERRIDE
Sets the GPIO output pin to high.
const unsigned bit_
Implementation of the gpio object.
void clr() const OVERRIDE
Clears the GPIO output pin to low.
Abstract class for representing a set of numbered bits that are stored persistently in some backing s...
virtual bool get_bit(unsigned offset)=0
Retrieves an individual bit.
virtual void lock_and_flush()=0
Grabs a lock and writes the current values to persistent storage.
virtual StoredBitSet & set_bit(unsigned offset, bool value)=0
Sets an individual bit to a specific value.
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180