| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /* | |||
| 2 | * @file efi_output.h | |||
| 3 | * | |||
| 4 | */ | |||
| 5 | ||||
| 6 | #pragma once | |||
| 7 | ||||
| 8 | #include "io_pins.h" | |||
| 9 | #include "smart_gpio.h" | |||
| 10 | #if EFI_SIMULATOR | |||
| 11 | #include <rusefi/timer.h> | |||
| 12 | #endif | |||
| 13 | ||||
| 14 | // This class acts as a boolean, but has a switch counter inside | |||
| 15 | class SwitchedState { | |||
| 16 | public: | |||
| 17 | SwitchedState() = default; | |||
| 18 | ||||
| 19 | 7458 | explicit SwitchedState(int8_t* const p_state) : state{ p_state } { } | ||
| 20 | ||||
| 21 | // returns true if the state has been changed | |||
| 22 | bool update(bool newState); | |||
| 23 | [[nodiscard]] uint16_t getCounter() const; | |||
| 24 | ||||
| 25 | ✗ | explicit operator bool() const { | ||
| 26 | ✗ | return state != nullptr; | ||
| 27 | } | |||
| 28 | ||||
| 29 | private: | |||
| 30 | int8_t *state{}; | |||
| 31 | uint16_t counter{}; | |||
| 32 | }; | |||
| 33 | ||||
| 34 | struct SimpleSwitchedState { | |||
| 35 | int8_t value{}; | |||
| 36 | SwitchedState state{&value}; | |||
| 37 | }; | |||
| 38 | ||||
| 39 | // Used if you want a function to be virtual only for unit testing purposes | |||
| 40 | #if EFI_UNIT_TEST | |||
| 41 | #define TEST_VIRTUAL virtual | |||
| 42 | #else | |||
| 43 | #define TEST_VIRTUAL | |||
| 44 | #endif | |||
| 45 | ||||
| 46 | /** | |||
| 47 | * @brief Single output pin reference and state | |||
| 48 | */ | |||
| 49 | class OutputPin { | |||
| 50 | public: | |||
| 51 | // initializes pin & registers it in pin repository | |||
| 52 | void initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e outputMode, bool forceInitWithFatalError = false); | |||
| 53 | ||||
| 54 | // same as above, with OM_DEFAULT mode | |||
| 55 | void initPin(const char *msg, brain_pin_e brainPin); | |||
| 56 | ||||
| 57 | // dissociates pin from this output and un-registers it in pin repository | |||
| 58 | void deInit(); | |||
| 59 | ||||
| 60 | bool isInitialized() const; | |||
| 61 | ||||
| 62 | bool getAndSet(int logicValue); | |||
| 63 | void setValue(const char *msg, int logicValue, bool isForce = false); | |||
| 64 | TEST_VIRTUAL void setValue(int logicValue, bool isForce = false); | |||
| 65 | void toggle(); | |||
| 66 | bool getLogicValue() const; | |||
| 67 | ||||
| 68 | brain_pin_diag_e getDiag() const; | |||
| 69 | ||||
| 70 | #if EFI_GPIO_HARDWARE | |||
| 71 | ioportid_t m_port = 0; | |||
| 72 | uint8_t m_pin = 0; | |||
| 73 | #endif /* EFI_GPIO_HARDWARE */ | |||
| 74 | ||||
| 75 | #if EFI_UNIT_TEST || EFI_SIMULATOR | |||
| 76 | int pinToggleCounter = 0; | |||
| 77 | #endif | |||
| 78 | ||||
| 79 | #if EFI_SIMULATOR | |||
| 80 | Timer pinToggleTimer; | |||
| 81 | uint32_t durationsInStateMs[2]; | |||
| 82 | ||||
| 83 | void resetToggleStats(); | |||
| 84 | #endif | |||
| 85 | ||||
| 86 | brain_pin_e brainPin = Gpio::Unassigned; | |||
| 87 | ||||
| 88 | #if (EFI_GPIO_HARDWARE && (BOARD_EXT_GPIOCHIPS > 0)) | |||
| 89 | /* used for external pins */ | |||
| 90 | bool ext = false; | |||
| 91 | #endif /* EFI_GPIO_HARDWARE */ | |||
| 92 | ||||
| 93 | int8_t currentLogicValue = INITIAL_PIN_STATE; | |||
| 94 | /** | |||
| 95 | * we track current pin status so that we do not touch the actual hardware if we want to write new pin bit | |||
| 96 | * which is same as current pin value. This maybe helps in case of status leds, but maybe it's a total over-engineering | |||
| 97 | */ | |||
| 98 | private: | |||
| 99 | // todo: inline this method? | |||
| 100 | void setDefaultPinState(pin_output_mode_e mode); | |||
| 101 | void setOnchipValue(int electricalValue); | |||
| 102 | ||||
| 103 | // 4 byte pointer is a bit of a memory waste here | |||
| 104 | pin_output_mode_e mode = OM_DEFAULT; | |||
| 105 | }; | |||
| 106 | ||||
| 107 | /** | |||
| 108 | * OutputPin which is reported on Engine Sniffer | |||
| 109 | */ | |||
| 110 | class NamedOutputPin : public virtual OutputPin { | |||
| 111 | public: | |||
| 112 | NamedOutputPin(); | |||
| 113 | explicit NamedOutputPin(const char *name); | |||
| 114 | virtual void setHigh(const char *msg); | |||
| 115 | virtual void setLow(const char *msg); | |||
| 116 | virtual void setHigh(); | |||
| 117 | virtual void setLow(); | |||
| 118 | const char *getName() const; | |||
| 119 | void setName(const char*); | |||
| 120 | const char *getShortName() const; | |||
| 121 | /** | |||
| 122 | * @return true if pin was stopped | |||
| 123 | */ | |||
| 124 | bool stop(); | |||
| 125 | /** | |||
| 126 | * rusEfi Engine Sniffer protocol uses these short names to reduce bytes usage | |||
| 127 | */ | |||
| 128 | const char *shortName = nullptr; | |||
| 129 | ||||
| 130 | private: | |||
| 131 | // todo: char pointer is a bit of a memory waste here, we can reduce RAM usage by software-based getName() method | |||
| 132 | const char *name = nullptr; | |||
| 133 | }; | |||
| 134 |