MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/driver/GpioPinDecoderv2.hh
2024-06-07 11:12:56 +03:00

73 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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_ */