/* * GpioPinDecoderv2.h * * Created on: 22 окт. 2019 г. * Author: user */ #ifndef SOURCE_DRIVER_GPIOPINDECODERV2_H_ #define SOURCE_DRIVER_GPIOPINDECODERV2_H_ #include "../peripheral/IGpio.hh" #include "Demux.hh" namespace driver { class GpioPinDecoder_v2 { public: static const unsigned short num_pins = 16; typedef unsigned short Id; struct PinPack { unsigned short pin[4]; }; GpioPinDecoder_v2( Demux & demux, unsigned short default_pin, PinPack pack ); peripheral::IGpio * grab( Id pin_id ) { return pin_mapping[pin_id] < num_pins ? &pins[pin_mapping[pin_id]] : nullptr; } private: Demux & demux; const unsigned short default_pin; Id select_pin_id = default_pin; class PinOnDecoder : public peripheral::IGpio { public: PinOnDecoder( GpioPinDecoder_v2 * decoder, Id pin ) : decoder(decoder), pin(pin) {} PinOnDecoder() = default; void set() override { decoder->select( pin ); } void clear() override { decoder->deselect(); } bool read() const override { return pin == decoder->get_active_pin(); } void write(bool value) override { value ? set() : clear(); } void toggle() override { } private: GpioPinDecoder_v2 * decoder = nullptr; Id pin = 0xFFFF; }; void select( GpioPinDecoder_v2::Id pin ) { demux.select( select_pin_id = pin ); } void deselect() { demux.select( select_pin_id = default_pin ); } Id get_active_pin() { return select_pin_id; } PinOnDecoder pins[num_pins]; unsigned short pin_mapping[num_pins]; }; } /* namespace driver */ #endif /* SOURCE_DRIVER_GPIOPINDECODERV2_H_ */