rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
efi_output.h
Go to the documentation of this file.
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
16public:
17 SwitchedState() = default;
18
19 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
29private:
30 int8_t *state{};
31 uint16_t counter{};
32};
33
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 */
49class OutputPin {
50public:
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
69
70#if EFI_GPIO_HARDWARE
72 uint8_t m_pin = 0;
73#endif /* EFI_GPIO_HARDWARE */
74
75#if EFI_UNIT_TEST || EFI_SIMULATOR
77#endif
78
79#if EFI_SIMULATOR
81 uint32_t durationsInStateMs[2];
82
83 void resetToggleStats();
84#endif
85
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 */
98private:
99 // todo: inline this method?
101 void setOnchipValue(int electricalValue);
102
103 // 4 byte pointer is a bit of a memory waste here
105};
106
107/**
108 * OutputPin which is reported on Engine Sniffer
109 */
110class NamedOutputPin : public virtual OutputPin {
111public:
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
130private:
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};
const char * getName() const
Definition efi_gpio.cpp:422
virtual void setLow()
Definition efi_gpio.cpp:460
void setName(const char *)
Definition efi_gpio.cpp:426
const char * shortName
Definition efi_output.h:128
const char * name
Definition efi_output.h:132
virtual void setHigh()
Definition efi_gpio.cpp:438
const char * getShortName() const
Definition efi_gpio.cpp:430
Single output pin reference and state.
Definition efi_output.h:49
Timer pinToggleTimer
Definition efi_output.h:80
brain_pin_diag_e getDiag() const
Definition efi_gpio.cpp:678
uint32_t durationsInStateMs[2]
Definition efi_output.h:81
void resetToggleStats()
Definition efi_gpio.cpp:598
void deInit()
Definition efi_gpio.cpp:802
void initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e outputMode, bool forceInitWithFatalError=false)
Definition efi_gpio.cpp:711
bool getLogicValue() const
Definition efi_gpio.cpp:667
pin_output_mode_e mode
Definition efi_output.h:104
void setOnchipValue(int electricalValue)
Definition efi_gpio.cpp:583
bool getAndSet(int logicValue)
Definition efi_gpio.cpp:575
void setValue(const char *msg, int logicValue, bool isForce=false)
Definition efi_gpio.cpp:604
void toggle()
Definition efi_gpio.cpp:571
int8_t currentLogicValue
Definition efi_output.h:93
ioportid_t m_port
Definition efi_output.h:71
bool isInitialized() const
Definition efi_gpio.cpp:559
int pinToggleCounter
Definition efi_output.h:76
uint8_t m_pin
Definition efi_output.h:72
brain_pin_e brainPin
Definition efi_output.h:86
void setDefaultPinState(pin_output_mode_e mode)
Definition efi_gpio.cpp:672
bool update(bool newState)
Definition efi_output.cpp:9
SwitchedState()=default
SwitchedState(int8_t *const p_state)
Definition efi_output.h:19
uint16_t counter
Definition efi_output.h:31
uint16_t getCounter() const
int8_t * state
Definition efi_output.h:30
@ Unassigned
GPIO_TypeDef * ioportid_t
Port Identifier.
this file is about general input/output utility methods, not much EFI-specifics
brain_pin_diag_e
pin_output_mode_e
SwitchedState state
Definition efi_output.h:36