From 3e6de7de9db5862c24d954f6dc04975772532c72 Mon Sep 17 00:00:00 2001 From: profezzorn Date: Sun, 10 Dec 2023 02:55:12 -0600 Subject: [PATCH] start system for compile-time checking of using the same twice (including bonded pins) --- ProffieOS.ino | 4 ++ common/resources.h | 72 +++++++++++++++++++++++++++++++++ common/serial.h | 2 + config/proffieboard_v3_config.h | 9 +++++ display/spidisplay.h | 16 ++++++++ 5 files changed, 103 insertions(+) create mode 100644 common/resources.h diff --git a/ProffieOS.ino b/ProffieOS.ino index 7b14ffa63..b550b18a9 100644 --- a/ProffieOS.ino +++ b/ProffieOS.ino @@ -48,6 +48,8 @@ #error Please set CONFIG_FILE as shown above. #endif +#include "common/resources.h" + #define CONFIG_TOP #include CONFIG_FILE #undef CONFIG_TOP @@ -710,6 +712,8 @@ uint32_t startup_MODER[4]; #ifdef BLADE_DETECT_PIN LatchingButtonTemplate> BladeDetect(BUTTON_BLADE_DETECT, BLADE_DETECT_PIN, "blade_detect"); + +USE_PIN_OUTPUT(BLADE_DETECT_PIN, PO_SubSystems::PO_BLADE_DETECT); #endif #include "common/sd_test.h" diff --git a/common/resources.h b/common/resources.h new file mode 100644 index 000000000..3a3539465 --- /dev/null +++ b/common/resources.h @@ -0,0 +1,72 @@ +#ifndef COMMON_RESOURCES_H +#define COMMON_RESOURCES_H + +namespace PO_SubSystems { + class PO_SPI {}; + class PO_WS2811 {}; + class PO_BUTTON {}; + class PO_BLADE_DETECT {}; + class PO_DISPLAY {}; + class PO_I2S {}; + class PO_SPDIF {}; + class PO_IR {}; + class PO_PWM {}; + class PO_SERIAL {}; +} + +namespace PO_Resources { + template class InputPin {}; + template class OutputPin {}; + template class SPI {}; + template class TIMER {}; +} + +namespace PO_ResourceTracking { + + using namespace PO_Resources; + +template struct BondedPins { static const int other = -1; }; + +#define PROFFIEOS_BOND_PINS(P1, P2) \ +template<> struct BondedPins { static const int other = P2; }; \ +template<> struct BondedPins { static const int other = P1; } + +template +struct UseResource { + friend USED_BY IsUsing(RESOURCE x) { return {}; } +}; + +template +struct UsePin2 { + static_assert(sizeof(UseResource, USER>)); + static_assert(sizeof(UseResource, USER>)); +}; + +template struct UsePin2<-1, USER, INPUT> {}; + +template +struct UsePin2 { + static_assert(sizeof(UseResource, USER>)); +}; + + +template +struct UseBondedPin { + static_assert(sizeof(UseResource, USER>)); +}; + +template struct UseBondedPin<-1, USER, false> {}; +template struct UseBondedPin {}; + +template +struct UsePin { + static_assert(sizeof(UsePin2)); + static_assert(sizeof(UseBondedPin::other, USER, INPUT>)); +}; + +} + +#define USE_PIN_OUTPUT(PIN, CLASS) static_assert( sizeof( PO_ResourceTracking::UsePin), #CLASS) +#define USE_PIN_INPUT(PIN, CLASS) static_assert( sizeof( PO_ResourceTracking::UsePin), #CLASS) + +#endif // COMMON_RESOURCES_H diff --git a/common/serial.h b/common/serial.h index a02ff921b..b5d847880 100644 --- a/common/serial.h +++ b/common/serial.h @@ -243,6 +243,8 @@ StaticWrapper> parser; #ifdef ENABLE_SERIAL StaticWrapper> serial_parser; #define ENABLE_SERIAL_COMMANDS +USE_PIN_OUTPUT(txPin, PO_SubSystems::PO_SERIAL); +USE_PIN_INPUT(rxPin, PO_SubSystems::PO_SERIAL); #endif #ifdef USB_CLASS_WEBUSB diff --git a/config/proffieboard_v3_config.h b/config/proffieboard_v3_config.h index 5ead210e1..d76362196 100644 --- a/config/proffieboard_v3_config.h +++ b/config/proffieboard_v3_config.h @@ -89,6 +89,15 @@ enum SaberPins { trigger8Pin = 4, // data3 }; +namespace PO_ResourceTracking { + PROFFIEOS_BOND_PINS(0, 1); + PROFFIEOS_BOND_PINS(2, 3); + PROFFIEOS_BOND_PINS(4, 5); + PROFFIEOS_BOND_PINS(9, 10); + PROFFIEOS_BOND_PINS(11, 12); + PROFFIEOS_BOND_PINS(13, 14); +} + #if PROFFIEBOARD_VERSION - 0 != 3 #error Please select Proffieboard V3 in Tools->Board #endif diff --git a/display/spidisplay.h b/display/spidisplay.h index 45c28abeb..19ed139f9 100644 --- a/display/spidisplay.h +++ b/display/spidisplay.h @@ -296,6 +296,11 @@ struct StandardDisplayAdapter { static void endTransaction() { // Do nothing, this is a dedicated SPI bus. } + USE_PIN_OUTPUT(DC, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(LIGHT, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(RESET, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade2Pin, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade3Pin, PO_SubSystems::PO_SPI); }; }; @@ -324,6 +329,11 @@ struct StandardDisplayAdapterM3 { static void endTransaction() { // Do nothing, this is a dedicated SPI bus. } + USE_PIN_OUTPUT(DC, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(LIGHT, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(RESET, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade2Pin, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade3Pin, PO_SubSystems::PO_SPI); }; }; @@ -358,6 +368,12 @@ struct CSDisplayAdapter { digitalWrite(CS, HIGH); spi().endTransaction(); } + USE_PIN_OUTPUT(CS, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(DC, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(LIGHT, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(RESET, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade2Pin, PO_SubSystems::PO_SPI); + USE_PIN_OUTPUT(blade3Pin, PO_SubSystems::PO_SPI); }; }; #else