Skip to content

Commit

Permalink
start system for compile-time checking of using the same twice (inclu…
Browse files Browse the repository at this point in the history
…ding bonded pins)
  • Loading branch information
profezzorn committed Dec 10, 2023
1 parent 391870d commit 3e6de7d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ProffieOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -710,6 +712,8 @@ uint32_t startup_MODER[4];
#ifdef BLADE_DETECT_PIN
LatchingButtonTemplate<FloatingButtonBase<BLADE_DETECT_PIN>>
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"
Expand Down
72 changes: 72 additions & 0 deletions common/resources.h
Original file line number Diff line number Diff line change
@@ -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<int p> class InputPin {};
template<int p> class OutputPin {};
template<int n> class SPI {};
template<int n> class TIMER {};
}

namespace PO_ResourceTracking {

using namespace PO_Resources;

template<int pin> struct BondedPins { static const int other = -1; };

#define PROFFIEOS_BOND_PINS(P1, P2) \
template<> struct BondedPins<P1> { static const int other = P2; }; \
template<> struct BondedPins<P2> { static const int other = P1; }

template <class RESOURCE, class USED_BY>
struct UseResource {
friend USED_BY IsUsing(RESOURCE x) { return {}; }
};

template<int pin, class USER, bool INPUT = false>
struct UsePin2 {
static_assert(sizeof(UseResource<OutputPin<pin>, USER>));
static_assert(sizeof(UseResource<InputPin<pin>, USER>));
};

template<class USER, bool INPUT> struct UsePin2<-1, USER, INPUT> {};

template<int pin, class USER>
struct UsePin2<pin, USER, true> {
static_assert(sizeof(UseResource<InputPin<pin>, USER>));
};


template<int pin, class USER, bool INPUT = false>
struct UseBondedPin {
static_assert(sizeof(UseResource<OutputPin<pin>, USER>));
};

template<class USER> struct UseBondedPin<-1, USER, false> {};
template<int pin, class USER> struct UseBondedPin<pin, USER, true> {};

template<int pin, class USER, bool INPUT = false>
struct UsePin {
static_assert(sizeof(UsePin2<pin, USER, INPUT>));
static_assert(sizeof(UseBondedPin<BondedPins<pin>::other, USER, INPUT>));
};

}

#define USE_PIN_OUTPUT(PIN, CLASS) static_assert( sizeof( PO_ResourceTracking::UsePin<PIN, CLASS>), #CLASS)
#define USE_PIN_INPUT(PIN, CLASS) static_assert( sizeof( PO_ResourceTracking::UsePin<PIN, CLASS, true>), #CLASS)

#endif // COMMON_RESOURCES_H
2 changes: 2 additions & 0 deletions common/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ StaticWrapper<Parser<SerialAdapter>> parser;
#ifdef ENABLE_SERIAL
StaticWrapper<Parser<Serial3Adapter>> 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
Expand Down
9 changes: 9 additions & 0 deletions config/proffieboard_v3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions display/spidisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};

Expand Down Expand Up @@ -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);
};
};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e6de7d

Please sign in to comment.