-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a C wrapper #60
base: master
Are you sure you want to change the base?
Conversation
Move definitions of enum classes to separate file, so it can be included in the c header.
Not optimal
Thanks for the contribution. |
Also, if the wrapper supports Or maybe you could just remove these numbering modes for the C wrapper. |
Theoretically, it should be possible to have a wrapper around the PWM class. I can do it when I have time. The problem with |
Comment for
// Pin Numbering Modes
enum class NumberingModes
{
// Changed the order of modes
None,
CVM,
TEGRA_SOC,
BCM,
BOARD,
SIZE // the size is not changed
}; And I think adding // check if enum size of c wrapper and the cpp code has the same length at compile time
__attribute__((unused)) void check_enum_equality()
{
static_assert(static_cast<GPIONumberingModes>(GPIO::NumberingModes::BOARD) == GPIO_NUMBERING_MODES_BOARD,
"c wrapper enum must be equal to c++ enum");
static_assert(static_cast<GPIONumberingModes>(GPIO::NumberingModes::BCM) == GPIO_NUMBERING_MODES_BCM,
"c wrapper enum must be equal to c++ enum");
static_assert(static_cast<GPIONumberingModes>(GPIO::NumberingModes::TEGRA_SOC) == GPIO_NUMBERING_MODES_TEGRA_SOC,
"c wrapper enum must be equal to c++ enum");
// add more static_assert ...
} With this approach, you don't need to change the public API and Or you can make enum converter functions and remove GPIONumberingModes ToCStyleEnum(GPIO::NumberingModes mode)
{
switch (mode)
{
case GPIO::NumberingModes::BOARD:
return GPIO_NUMBERING_MODES_BOARD;
case GPIO::NumberingModes::BCM:
return GPIO_NUMBERING_MODES_BCM;
case GPIO::NumberingModes::TEGRA_SOC:
return GPIO_NUMBERING_MODES_TEGRA_SOC;
case GPIO::NumberingModes::CVM:
return GPIO_NUMBERING_MODES_CVM;
case GPIO::NumberingModes::None:
return GPIO_NUMBERING_MODES_NONE;
default:
throw std::runtime_error("invalid numbering mode");
}
}
// ...
GPIONumberingModes gpio_getmode() { return ToCStyleEnum(GPIO::getmode()); } |
Great. I'll look forward to it.
Hmm.. I prefer the later one (using only |
This is true. The problem is, if you only check the equality of the single enum values, you don't check if in the number of elements is equal. When you for example add a new one to the c++ header, it would then not be checked if you had added it in the c header.
This does not enforce the developer to always update both (c and c++) enum structs. So it would be possible to update only one of both. This is the benefit of adding a |
Ok. I thought that your C code will work properly even if the new option was added only to the C++ headers, But now that I realized that there would be problems when your C code uses the enums as output. I'm still not a fan of adding I added // check if enum size of c wrapper and the cpp code has the same length at compile time
__attribute__((unused)) void check_enum_equality()
{
#define CHECK_ENUM_EQUALITY_MSG "c wrapper enum must be equal to c++ enum"
static_assert(static_cast<GPIONumberingModes>(GPIO::details::enum_size<GPIO::NumberingModes>::value) ==
GPIO_NUMBERING_MODES_SIZE,
CHECK_ENUM_EQUALITY_MSG);
static_assert(static_cast<GPIODirections>(GPIO::details::enum_size<GPIO::Directions>::value) ==
GPIO_DIRECTIONS_SIZE,
CHECK_ENUM_EQUALITY_MSG);
static_assert(static_cast<GPIOEdge>(GPIO::details::enum_size<GPIO::Edge>::value) == GPIO_EDGE_SIZE,
CHECK_ENUM_EQUALITY_MSG);
#undef CHECK_ENUM_EQUALITY_MSG
} |
Thanks for the update. Here's the list that I think we need to do next:
@ma-ludw |
I would prefer it in the public API and not as an example. I'm not familiar with c_make options, so the decision is up to you. I'will update the README and optionally add a wrapper for the PWM class. |
An idea for the implementing the // The names can be changed...
typedef int gpio_pwm;
gpio_pwm gpio_create_pwm(int channel, int frequency_hz);
bool gpio_destroy_pwm(gpio_pwm handle);
void gpio_start_pwm(gpio_pwm handle, double duty_cycle_percent);
void gpio_stop_pwm(gpio_pwm handle);
void gpio_change_pwm_frequency(gpio_pwm handle, int frequency_hz);
void gpio_change_pwm_duty_cycle(gpio_pwm handle, double duty_cycle_percent);
// make a resource pool for the PWM objects for the implementation... |
Added a wrapper to use with c code.
To use the enums in the c code, I added theme additionally in the c header. The enums are extended with a SIZE value to check that they have in the c header and the c++ header the same size.