Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Last active December 25, 2018 06:28
Show Gist options
  • Save GeekTree0101/6acf7fa9f7c7892314f3d6e824d20b89 to your computer and use it in GitHub Desktop.
Save GeekTree0101/6acf7fa9f7c7892314f3d6e824d20b89 to your computer and use it in GitHub Desktop.
RxCpp Simple Example
#include "lib/RxTest.h"
#include <iostream>
#include <string>
#include "Rx/v2/src/rxcpp/rx.hpp"
namespace rx = rxcpp;
using namespace std;
namespace rxtest {
void RxTest::rxInit() {
// generate interval
auto loop = rx::observe_on_event_loop();
auto autobalancerObservable = rx::sources::interval(std::chrono::milliseconds(500), loop)
.map([this](int v) {
if (this->steer > 0) {
return -1;
} else if (this->steer < 0) {
return 1;
} else {
return 0;
}
});
// interver observer bind to subject
autobalancerObservable.subscribe(publishSubject.get_subscriber().get_observer());
// subscribe subject
publishSubject.get_observable().subscribe([this](int balance) {
this -> steer += balance;
cout << this -> steer << endl;
});
}
void RxTest::run() {
while(1) {
string keypress;
cin >> keypress;
// TODO: Keyboard left, right button event
if (keypress == "z") {
publishSubject.get_subscriber().on_next(-1);
} else if (keypress == "x") {
publishSubject.get_subscriber().on_next(1);
} else {
exit(0);
}
}
}
};
#ifndef LIB_RXTEST_H_
#define LIB_RXTEST_H_
#include <iostream>
#include "Rx/v2/src/rxcpp/rx.hpp"
namespace rx = rxcpp;
namespace rxtest {
class RxTest {
public:
void run();
void rxInit();
private:
int steer;
rx::subjects::subject<int> publishSubject;
};
};
#endif // LIB_RXETEST_H_
@GeekTree0101
Copy link
Author

Bazel Setting

WORKSPACE

new_http_archive(
    name = "RxCpp",	
    build_file = "RxCpp.BUILD",
    urls = ["https://github.com/ReactiveX/RxCpp/archive/4.1.0.zip"],
    strip_prefix = "RxCpp-4.1.0"
)

RxCpp.BUILD

cc_library(
    name = "RxCpp",
    hdrs = glob(["Rx/v2/src/rxcpp/*.hpp",
                 "Rx/v2/src/rxcpp/**/*.hpp"]),
    includes = ["Rx/v2/src/"],
    visibility = ["//visibility:public"],
)

RxTest BUILD

cc_library(
    name = "RxTest-Lib",
    srcs = ["RxTest.cc"],
    hdrs = ["RxTest.h"],
    deps = ["@RxCpp//:RxCpp"],
    visibility = ["//visibility:public"],
)

@GeekTree0101
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment