Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.10)
project(profile_map)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include (cmake/helper.cmake)

enable_testing()
Expand Down
1 change: 1 addition & 0 deletions rt_polymorphysm/profile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(cptr)
add_subdirectory(cptr1)
add_subdirectory(cptr2)
add_subdirectory(virtual)
17 changes: 6 additions & 11 deletions rt_polymorphysm/profile/cptr1/light_sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstdio>
#include <cstdint>
#include "sensor_interface.hpp"

struct LightSensor {
float bias = 0;
Expand All @@ -23,17 +24,11 @@ struct LightSensor {
}

SensorT to_interface() {
return SensorT{
.context = this,
.init = [](void* ctx) {
static_cast<LightSensor*>(ctx)->init();
},
.read = [](void* ctx, float& out) {
return static_cast<LightSensor*>(ctx)->read(out);
},
.shutdown = [](void* ctx) {
static_cast<LightSensor*>(ctx)->shutdown();
}
return {
this,
[](void* ctx) { static_cast<LightSensor*>(ctx)->init(); },
[](void* ctx, float& out) { return static_cast<LightSensor*>(ctx)->read(out); },
[](void* ctx) { static_cast<LightSensor*>(ctx)->shutdown(); }
};
}
};
Expand Down
20 changes: 8 additions & 12 deletions rt_polymorphysm/profile/cptr1/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include "sensor_interface.hpp"
#include "sensor_policy.hpp"
#include "temp_sensor_policy.hpp"
#include "light_sensor_policy.hpp"
#include "temp_sensor.hpp"
#include "light_sensor.hpp"

Expand All @@ -16,17 +12,17 @@ void use_sensor(SensorT& sensor) {

// ======= Main =======
int main() {
TemperatureSensor temp;
PressureSensor pressure;
TempSensor ts;
LightSensor ls;

SensorT tempIf = temp.to_interface();
SensorT pressureIf = pressure.to_interface();
SensorT tsIf = ts.to_interface();
SensorT lsIf = ls.to_interface();

printf("\n== Temperature Sensor ==\n");
use_sensor(tempIf);
printf("\n== Temp Sensor ==\n");
use_sensor(tsIf);

printf("\n== Pressure Sensor ==\n");
use_sensor(pressureIf);
printf("\n== Light Sensor ==\n");
use_sensor(lsIf);

return 0;
}
28 changes: 10 additions & 18 deletions rt_polymorphysm/profile/cptr1/temp_sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

#include <cstdio>
#include <cstdint>
#include "sensor_interface.hpp"

struct TempSensor {
float offset = 0;

void init() {
offset = 1.0f;
printf("[TempSensor] Initialized\n");
void init(uint8_t mode) {
printf("[TempSensor] Init with mode: %d\n", mode);
}

bool read(float& out) {
out = 25.0f + offset;
printf("[TempSensor] Read: %.2f\n", out);
out = 25.0f;
printf("[TempSensor] Read value: %.2f\n", out);
return true;
}

Expand All @@ -23,17 +21,11 @@ struct TempSensor {
}

SensorT to_interface() {
return SensorT{
.context = this,
.init = [](void* ctx) {
static_cast<TempSensor*>(ctx)->init();
},
.read = [](void* ctx, float& out) {
return static_cast<TempSensor*>(ctx)->read(out);
},
.shutdown = [](void* ctx) {
static_cast<TempSensor*>(ctx)->shutdown();
}
return {
this,
[](void* ctx) { static_cast<TempSensor*>(ctx)->init(0); },
[](void* ctx, float& out) { return static_cast<TempSensor*>(ctx)->read(out); },
[](void* ctx) { static_cast<TempSensor*>(ctx)->shutdown(); }
};
}
};
Expand Down
2 changes: 2 additions & 0 deletions rt_polymorphysm/profile/cptr2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(cptr2-interface_SOURCES main.cpp temp_sensor.cpp light_sensor.cpp)
configure_target(cptr2-interface ${cptr2-interface_SOURCES})
29 changes: 29 additions & 0 deletions rt_polymorphysm/profile/cptr2/light_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef LIGHT_SENSOR_HPP
#define LIGHT_SENSOR_HPP

#include <cstdio>
#include <cstdint>
#include "sensor_interface.hpp"

struct LightSensor {
float bias;
float value;
};

void light_sensor_init(LightSensor* sensor) {
sensor->bias = 10.0f;
printf("[LightSensor] Initialized\n");
}

bool light_sensor_read(LightSensor* sensor, float& out) {
sensor->value = 1000.0f + sensor->bias;
out = sensor->value;
printf("[LightSensor] Read: %.2f\n", out);
return true;
}

void light_sensor_shutdown(LightSensor* sensor) {
printf("[LightSensor] Shutdown\n");
}

#endif
37 changes: 37 additions & 0 deletions rt_polymorphysm/profile/cptr2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "temp_sensor.hpp"
#include "light_sensor.hpp"

void use_sensor(SensorT& sensor, void* sensor_instance) {
sensor.init(sensor_instance);
float value;
if (sensor.read(sensor_instance, value)) {
printf("-> Value: %.2f\n", value);
}
sensor.shutdown(sensor_instance);
}

// ======= Main =======
int main() {
TempSensor ts;
LightSensor ls;

SensorT tsIf = {
(void (*)(void*))temp_sensor_init,
(bool (*)(void*, float&))temp_sensor_read,
(void (*)(void*))temp_sensor_shutdown
};

SensorT lsIf = {
(void (*)(void*))light_sensor_init,
(bool (*)(void*, float&))light_sensor_read,
(void (*)(void*))light_sensor_shutdown
};

printf("\n== Temp Sensor ==\n");
use_sensor(tsIf, &ts);

printf("\n== Light Sensor ==\n");
use_sensor(lsIf, &ls);

return 0;
}
12 changes: 12 additions & 0 deletions rt_polymorphysm/profile/cptr2/sensor_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SENSOR_INTERFACE_HPP
#define SENSOR_INTERFACE_HPP

#include <cstdint>

struct SensorT {
void (*init)(void* sensor_instance);
bool (*read)(void* sensor_instance, float& out);
void (*shutdown)(void* sensor_instance);
};

#endif // SENSOR_INTERFACE_HPP
29 changes: 29 additions & 0 deletions rt_polymorphysm/profile/cptr2/temp_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef TEMP_SENSOR_HPP
#define TEMP_SENSOR_HPP

#include <cstdio>
#include <cstdint>
#include "sensor_interface.hpp"

struct TempSensor {
uint8_t mode;
float value;
};

void temp_sensor_init(TempSensor* sensor) {
sensor->mode = 0;
printf("[TempSensor] Init with mode: %d\n", sensor->mode);
}

bool temp_sensor_read(TempSensor* sensor, float& out) {
sensor->value = 25.0f;
out = sensor->value;
printf("[TempSensor] Read value: %.2f\n", out);
return true;
}

void temp_sensor_shutdown(TempSensor* sensor) {
printf("[TempSensor] Shutdown\n");
}

#endif