Skip to content

Commit

Permalink
Add platformio demo (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin authored and jwellbelove committed Sep 26, 2019
1 parent 901c6e9 commit f888173
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/platformio/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/settings.json
lib/etl
7 changes: 7 additions & 0 deletions examples/platformio/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
16 changes: 16 additions & 0 deletions examples/platformio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ETL PlatformIO demo
===================

> Template to demonstrate how to use ETL with PlatformIO.
1. Open this folder in VSCode and agree if it suggests to install extension.
2. Use `Terminal -> Run Build Task... -> PlatformIO: Build` menu to compile.
3. Run `.pio/Build/native/program`.

## Details

Use `platformio.ini` for example, see comments inside.

`include` folder contains config, required for `etl`. All is as transparent
as possible. Set all additional variables via `build_flags` option in
`platform.ini`. Currently only `PROFILE_GCC_GENERIC` set to make things work.
1 change: 1 addition & 0 deletions examples/platformio/include/etl_profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "etl/profiles/etl_profile.h"
27 changes: 27 additions & 0 deletions examples/platformio/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html

[platformio]
default_envs = native

[env:native]
platform = native
build_flags =
; ETL configs in `include` folder are minimalistic. Here we can set all
; additional definitions to keep everything in one place and customize values
; for different target platforms
-D PROFILE_GCC_GENERIC
lib_deps =
; Define ETL dependency for this demo. You can use versions from PIO registry,
; or git repository with specific branch, tag or commit. See PIO docs for
; details.
;Embedded Template Library=https://github.com/ETLCPP/etl/archive/master.zip
Embedded Template Library@^14.31.2

142 changes: 142 additions & 0 deletions examples/platformio/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <iostream>

#include "etl/observer.h"

// Position data.
struct Position
{
int x;
int y;
};

// Button data.
struct Button
{
enum
{
Down,
Up
};

int state;
};

// Wheel data.
struct Wheel
{
int delta;
};

// Definition of a mouse observer.
typedef etl::observer<const Position&, Button, Wheel> Mouse_Observer;

// Definition of an event handler for mouse notifications.
class Event_Handler1 : public Mouse_Observer
{
public:

// Handle a position notification.
void notification(const Position& position)
{
std::cout << "Event_Handler1 : Position = " << position.x << "," << position.y << "\n";
}

// Handle a button notification.
void notification(Button button)
{
std::cout << "Event_Handler1 : Button = " << ((button.state == Button::Up) ? "Up\n" : "Down\n");
}

// Handle a wheel notification.
void notification(Wheel wheel)
{
std::cout << "Event_Handler1 : Wheel delta = " << wheel.delta << "\n";
}
};

// Definition of a second event handler for mouse notifications.
class Event_Handler2 : public Mouse_Observer
{
public:

// Handler a position notification.
void notification(const Position& position)
{
std::cout << "Event_Handler2 : Position = " << position.x << "," << position.y << "\n";
}

// Handle a button notification.
void notification(Button button)
{
std::cout << "Event_Handler2 : Button = " << ((button.state == Button::Up) ? "Up\n" : "Down\n");
}

// Handle a wheel notification.
void notification(Wheel wheel)
{
// Not interested in wheel deltas.
}
};

// The observable mouse driver.
const int MAX_MOUSE_OBSERVERS = 2;

class Mouse_Driver : public etl::observable<Mouse_Observer, MAX_MOUSE_OBSERVERS>
{
public:

// Notify observers about a position event.
void Position_Event()
{
Position position = { 100, 200 };
notify_observers(position);
}

// Notify observers about a button up event.
void Button_Event_Up()
{
Button button = { Button::Up };
notify_observers(button);
}

// Notify observers about a button down event.
void Button_Event_Down()
{
Button button = { Button::Down };
notify_observers(button);
}

// Notify observers about a wheel up event.
void Wheel_Event_Up()
{
Wheel wheel = { 50 };
notify_observers(wheel);
}

// Notify observers about a wheel down event.
void Wheel_Event_Down()
{
Wheel wheel = { -25 };
notify_observers(wheel);
}
};

int main()
{
Mouse_Driver mouse_driver;
Event_Handler1 event_handler1;
Event_Handler2 event_handler2;

// Tell the mouse driver about the observers.
mouse_driver.add_observer(event_handler1);
mouse_driver.add_observer(event_handler2);

// Generate some events.
mouse_driver.Button_Event_Down();
mouse_driver.Button_Event_Up();
mouse_driver.Position_Event();
mouse_driver.Wheel_Event_Down();
mouse_driver.Wheel_Event_Up();

return 0;
}

0 comments on commit f888173

Please sign in to comment.