22#define GPIO_IS_VALID_GPIO(pin) (pin < 100)
23#define TEST_NO_OUTPUT_GPIO 80
24#define GPIO_IS_VALID_OUTPUT_GPIO(pin) (pin < TEST_NO_OUTPUT_GPIO)
25#define TEST_RTC_GPIO1 40
26#define TEST_RTC_GPIO2 49
27#define GPIO_IS_VALID_RTC_GPIO(pin) ((pin >= TEST_RTC_GPIO1) && (pin <= TEST_RTC_GPIO2))
28#define TEST_RESERVED_GPIO 50
35#define GPIO_IS_VALID_RTC_GPIO(pin) rtc_gpio_is_valid_gpio(static_cast<gpio_num_t>(pin))
48#include <initializer_list>
53#include "driver/rtc_io.h"
54#include "hal/gpio_types.h"
55#include "driver/gpio.h"
56#include "esp32-hal-psram.h"
79 std::to_string(value) +
80 " is out of range [0,127]") {}
88 "Trying to use an unspecified input number.") {}
108 : std::runtime_error(
109 "Invalid GPIO number " +
110 std::to_string(value) +
128 : std::runtime_error(
129 "No input numbers were given to: " + hardware) {}
149 : std::runtime_error(
150 "There is an input number not assigned to a hardware input. Usage: " +
170inline int map_value(
int x,
int in_min,
int in_max,
int out_min,
int out_max)
172 const int run = in_max - in_min;
175 const int rise = out_max - out_min;
176 const int delta = x - in_min;
177 return (delta * rise) / run + out_min;
213 explicit constexpr operator bool()
225 constexpr bool bit(uint8_t n)
const noexcept
228 return (1ULL << n) &
low;
230 return (1ULL << (n - 64) &
high);
241 constexpr void set_bit(uint8_t n,
bool value =
true) noexcept
256 high &= ~(1ULL << n);
315 .high = (1ULL << (n - 64)),
337 "Wrong size in the uint128_t data type. Check memory alignment.");
348 ::std::size_t n)
noexcept
359 result.
low = (source.low << n);
360 result.
high = (source.high << n) | (source.low >> (64 - n));
365 result.
high = source.low << (n - 64);
380 ::std::size_t n)
noexcept
391 result.
low = (source.low >> n) | (source.high << (64 - n));
392 result.
high = (source.high >> n);
397 result.
low = source.high >> (n - 64);
413 .low = a.low | b.low,
414 .high = a.high | b.high,
428 .low = a.low & b.low,
429 .high = a.high & b.high,
443 .low = a.low ^ b.low,
444 .high = a.high ^ b.high,
472 return (a.low == b.low) && (a.high == b.high);
572 constexpr operator uint8_t()
const
586 return (_value > 127);
598 return (_value < 128);
605 return (_value == value._value);
609 return (_value != value._value);
611 inline constexpr bool operator<(
const InputNumber value)
const noexcept
613 return (_value < value._value);
615 inline constexpr bool operator<=(
const InputNumber value)
const noexcept
617 return (_value <= value._value);
619 inline constexpr bool operator>(
const InputNumber value)
const noexcept
621 return (_value > value._value);
623 inline constexpr bool operator>=(
const InputNumber value)
const noexcept
625 return (_value >= value._value);
636 _registered.
set_bit(_value,
true);
645 _registered.
set_bit(_value,
false);
666 return _registered.
bit(inputNumber._value);
678 return _registered.
bit(inputNumber);
695 _registered.
set_bit(inputNumber,
false);
703 static void book(uint8_t inputNumber)
705 _registered.
set_bit(inputNumber,
true);
709 static void clearBook()
711 _registered.
low = 0ULL;
712 _registered.
high = 0ULL;
736 std::initializer_list<InputNumber> items) noexcept :
uint128_t{}
763 constexpr uint8_t
size() const noexcept
766 for (uint8_t i = 0; i < 128; i++)
813#define JOY_LSHIFT_PADDLE 4
814#define JOY_RSHIFT_PADDLE 5
817#define JOY_LTHUMBSTICK_CLICK 8
818#define JOY_RTHUMBSTICK_CLICK 9
849 if (!GPIO_IS_VALID_GPIO(pin))
851 bool reserved =
false;
852#if defined(CONFIG_IDF_TARGET_ESP32)
853 reserved = (pin >= GPIO_NUM_6) && (pin <= GPIO_NUM_11);
854 reserved = reserved ||
855 ((pin >= GPIO_NUM_16) && (pin <= GPIO_NUM_17) && psramFound());
858#elif defined(CONFIG_IDF_TARGET_ESP32S2)
861#elif defined(CONFIG_IDF_TARGET_ESP32S3)
862 reserved = ((pin >= GPIO_NUM_26) && (pin <= GPIO_NUM_32));
863 reserved = reserved ||
864 ((pin >= GPIO_NUM_35) && (pin <= GPIO_NUM_37) && psramFound());
867#elif defined(CONFIG_IDF_TARGET_ESP32C3)
868 reserved = (pin >= GPIO_NUM_11) && (pin <= GPIO_NUM_17);
872 reserved = (pin == TEST_RESERVED_GPIO);
875 throw gpio_error(pin,
"Reserved for SPI FLASH, PSRAM or otherwise NOT USABLE");
897 operator int()
const {
return _pin; }
928 inline bool operator<(
const GPIO value)
const {
return (_pin < value.
_pin); }
939 abortIfUnspecified();
940 if (reservedPins.find(_pin) != reservedPins.end())
943 reservedPins.insert(_pin);
952 abortIfUnspecified();
953 reservedPins.insert(_pin);
964 throw gpio_error(_pin,
"Is unspecified but required");
968 static void clearReservations()
970 reservedPins.clear();
975 inline static ::std::set<int> reservedPins{};
996 if ((pin >= 0) && !GPIO_IS_VALID_OUTPUT_GPIO(pin))
1087#if CONFIG_IDF_TARGET_ESP32C3
1205 uint8_t rpmPercent = 0;
1207 uint8_t shiftLight1 = 0;
1209 uint8_t shiftLight2 = 0;
1211 bool revLimiter =
false;
1213 bool engineStarted =
false;
1224 bool absEngaged =
false;
1226 bool tcEngaged =
false;
1228 bool drsEngaged =
false;
1230 bool pitLimiter =
false;
1232 bool lowFuelAlert =
false;
1234 uint8_t absLevel = 0;
1236 uint8_t tcLevel = 0;
1240 uint8_t brakeBias = 0;
1248 bool blackFlag =
false;
1249 bool blueFlag =
false;
1250 bool checkeredFlag =
false;
1251 bool greenFlag =
false;
1252 bool orangeFlag =
false;
1253 bool whiteFlag =
false;
1254 bool yellowFlag =
false;
1256 uint16_t remainingLaps = 0;
1258 uint16_t remainingMinutes = 0;
1267 uint8_t relativeTurboPressure = 0;
1269 float absoluteTurboPressure = 0.0;
1271 uint16_t waterTemperature = 0;
1273 float oilPressure = 0.0;
1275 uint16_t oilTemperature = 0;
1277 uint8_t relativeRemainingFuel = 0;
1279 uint16_t absoluteRemainingFuel = 0;
1289 uint16_t tireTemp[4];
1291 float tirePressure[4];
1293 uint16_t brakeTemp[4];
1295 uint8_t wearPercentage[4];
1345 operator uint32_t() const noexcept
1347 return (blue) | (green << 8) | (red << 16);
1355 operator int() const noexcept
1357 return (blue) | (green << 8) | (red << 16);
1367 red = packedRGB >> 16;
1368 green = packedRGB >> 8;
1399 red = packedRGB >> 16;
1400 green = packedRGB >> 8;
1424 return (packedRGB ==
static_cast<uint32_t
>(*
this));
1436 return (packedRGB ==
static_cast<int>(*
this));
1448 return (red == other.red) &&
1449 (blue == other.blue) &&
1450 (green == other.green);
1462 return (packedRGB !=
static_cast<uint32_t
>(*
this));
1474 return (packedRGB !=
static_cast<int>(*
this));
1486 return (red != other.red) ||
1487 (blue != other.blue) ||
1488 (green != other.green);
1570static_assert(
sizeof(
Pixel) == 3);
1596 uint32_t &timerVariable,
1597 uint32_t elapsedTimeMs,
1598 uint32_t timeLimitMs)
1600 timerVariable += elapsedTimeMs;
1601 uint32_t result = timerVariable / timeLimitMs;
1602 timerVariable %= timeLimitMs;
1608 bool requiresPowertrainTelemetry =
false;
1610 bool requiresECUTelemetry =
false;
1612 bool requiresRaceControlTelemetry =
false;
1614 bool requiresGaugeTelemetry =
false;
1616 bool requiresWheelTelemetry =
false;
1753 virtual void onStart()
override;
1754 virtual void onBitePoint(uint8_t bitePoint)
override;
1755 virtual void onConnected()
override;
1756 virtual void onBLEdiscovering()
override;
1757 virtual void onLowBattery()
override;
1758 virtual void onSaveSettings()
override;
1759 virtual void serveSingleFrame(uint32_t elapsedMs)
override {};
1760 virtual uint8_t getMaxFPS()
override {
return 0; }
1761 virtual void shutdown()
override {};
1772 ::std::vector<Pixel> telemetry_pixel{};
1775 ::std::vector<Pixel> backlit_button_pixel{};
1778 ::std::vector<Pixel> individual_pixel{};
1787 bool notConnectedYet =
true;
1807 static
void reset() noexcept;
1829 bool renderBatteryLevel(uint32_t barColor = 0x00ACFA70);
constexpr bool operator!=(uint128_t a, uint128_t b)
Check inequality.
constexpr uint128_t operator>>(const uint128_t &source, ::std::size_t n) noexcept
Shift right.
constexpr uint128_t operator~(uint128_t a) noexcept
Bitwise negation.
#define GPIO_IS_VALID_RTC_GPIO(pin)
Validation of RTC GPIO pins.
PixelGroup
Available RGB LED groups for pixel control.
@ GRP_INDIVIDUAL
Individual leds group.
@ GRP_BUTTONS
Buttons lighting group.
@ GRP_TELEMETRY
Telemetry leds group.
Connectivity
Connectivity choice.
@ USB_BLE_EXCLUSIVE
Combined USB and BLE connectivity with forced connection drop.
@ USB
USB connectivity only, if available.
@ _DEFAULT
Default connectivity.
@ USB_BLE
Combined USB and BLE connectivity if available.
@ BLE
BLE connectivity only, if available.
@ DUMMY
No connectivity at all (for troubleshooting)
UNSPECIFIED
Unspecified value type.
@ VALUE
Unspecified value.
I2CBus
I2C bus controller.
std::set< InputGPIO > InputGPIOCollection
Collection of input GPIOs.
constexpr uint128_t operator&(uint128_t a, uint128_t b) noexcept
Bitwise AND.
std::set< OutputGPIO > OutputGPIOCollection
Collection of output GPIOs.
int map_value(int x, int in_min, int in_max, int out_min, int out_max)
Equivalent to Arduino's map()
constexpr bool operator==(uint128_t a, uint128_t b) noexcept
Check equality.
PowerLatchMode
Supported power latch modes.
@ POWER_OFF_LOW
Power on when high voltage, power off when low voltage.
@ POWER_OFF_HIGH
Power on when low voltage, power off when high voltage.
@ POWER_OPEN_DRAIN
Power on when low voltage, power off when open drain.
PixelFormat
Byte order of pixel data starting with the least significant byte.
std::set< GPIO > GPIOCollection
Collection of GPIOs.
constexpr uint128_t operator|(uint128_t a, uint128_t b) noexcept
Bitwise OR.
constexpr uint128_t operator<<(const uint128_t &source, ::std::size_t n) noexcept
Shift left.
constexpr uint128_t operator^(uint128_t a, uint128_t b) noexcept
Bitwise XOR.
Abstract interface for notifications and telemetry display.
virtual void shutdown()
Cut power to the UI hardware.
virtual void onStart()
Called just once after initialization.
virtual uint8_t getMaxFPS()
Get the maximum FPS supported by the underlying hardware.
virtual void onLowBattery()
Notify low battery.
virtual void onConnected()
Notify device is connected.
virtual void onBLEdiscovering()
Notify device is in discovery mode.
virtual uint16_t getStackSize()
Get the stack size required by this user interface.
virtual void onSaveSettings()
Notify that user settings have been saved to flash memory.
uint32_t frameTimer(uint32_t &timerVariable, uint32_t elapsedTimeMs, uint32_t timeLimitMs)
Simple timer.
virtual void serveSingleFrame(uint32_t elapsedMs)
Draw a single frame.
virtual void onUserInput(uint8_t inputNumber)
Notify user input.
virtual void onTelemetryData(const TelemetryData *pTelemetryData)
Notify new telemetry data.
virtual void onBitePoint(uint8_t bitePoint)
Notify a change in the current bite point.
Notifications using pixel control.
void show() noexcept
Show pixels all at once in all groups.
virtual void pixelControl_OnConnected()
Notify device is connected.
PixelControlNotification()
Initialize.
virtual void pixelControl_OnBitePoint(uint8_t bitePoint)
Notify a change in current bite point.
virtual void pixelControl_OnLowBattery()
Notify low battery.
void show(PixelGroup group) noexcept
Show pixels all at once in a specific group.
virtual void pixelControl_OnSaveSettings()
Notify that user settings have been saved to flash memory.
virtual void pixelControl_OnBLEdiscovering()
Notify device is in discovery mode.
virtual void pixelControl_OnStart()
Called just once after initialization.
Exception for invalid GPIO pin numbers.
gpio_error(uint8_t value, const std::string &reason)
Construct a new gpio_error exception.
ADC-capable GPIO pin number.
ADC_GPIO(UNSPECIFIED value)
Create a "not connected" ADC GPIO.
ADC_GPIO(int pin)
Create an ADC GPIO connected to a pin number.
ADC_GPIO()
Create a "not connected" ADC GPIO.
bool operator==(const UNSPECIFIED value) const
Check if "not connected".
void reserve() const
Reserve this GPIO for exclusive use.
GPIO(int pin)
Assign a pin number.
GPIO(UNSPECIFIED value)
Assign a "not connected" value.
int _pin
Assigned pin number.
void abortIfUnspecified() const
Throw an exception if unspecified.
void grant() const
Grant this GPIO for non-exclusive use.
GPIO(const GPIO &instance)
Copy another GPIO instance.
bool operator!=(const UNSPECIFIED value) const
Check if a pin number was assigned.
GPIO()
Create a GPIO as "not connected".
Output-capable GPIO pin number.
OutputGPIO()
Create a "not connected" GPIO.
OutputGPIO(int pin)
Create a GPIO.
OutputGPIO(UNSPECIFIED value)
Create a "not connected" GPIO.
Pixel in 3-byte packed RGB format.
Pixel() noexcept
Create as a black pixel.
Pixel(Pixel &&source)=default
Move-constructor.
Pixel(uint32_t packedRGB) noexcept
Create from a packed RGB value.
uint8_t byte2(PixelFormat format) const noexcept
Get the third color channel in a certain pixel format.
uint8_t blue
Blue channel.
Pixel & operator=(const Pixel &source)=default
Copy-assignment.
Pixel & operator=(Pixel &&source)=default
Move-assignment.
uint8_t green
Green channel.
bool operator!=(int packedRGB) const noexcept
Compare to a packed RGB color.
bool operator==(int packedRGB) const noexcept
Compare to a packed RGB color.
bool operator==(uint32_t packedRGB) const noexcept
Compare to a packed RGB color.
bool operator!=(uint32_t packedRGB) const noexcept
Compare to a packed RGB color.
bool operator==(const Pixel &other) const noexcept
Compare to another pixel.
bool operator!=(const Pixel &other) const noexcept
Compare to another pixel.
uint8_t byte0(PixelFormat format) const noexcept
Get the first color channel in a certain pixel format.
uint8_t byte1(PixelFormat format) const noexcept
Get the second color channel in a certain pixel format.
Pixel(const Pixel &source)=default
Copy-constructor.
Pixel & operator=(uint32_t packedRGB) noexcept
Assign a packed RGB color.
RTC-capable GPIO pin number.
RTC_GPIO(int pin)
Create a RTC GPIO connected to a pin number.
uint32_t frameID
Identifies a telemetry frame. For internal use. Do not overwrite.
static constexpr uint128_t bitmap(uint8_t n) noexcept
Get the bitmap that represents a number.
constexpr uint128_t & operator^=(const uint128_t &rhs) noexcept
Compound bitwise XOR.
constexpr uint128_t & operator&=(const uint128_t &rhs) noexcept
Compound bitwise AND.
constexpr uint128_t & operator|=(const uint128_t &rhs) noexcept
Compound bitwise OR.
uint64_t low
Least significant unsigned long long.
constexpr bool bit(uint8_t n) const noexcept
Check bit.
constexpr void set_bit(uint8_t n, bool value=true) noexcept
Set or clear a single bit.
uint64_t high
Most significant unsigned long long.
static constexpr uint128_t neg() noexcept
Get the bitwise negation of zero.