Last active
December 25, 2018 06:28
-
-
Save GeekTree0101/6acf7fa9f7c7892314f3d6e824d20b89 to your computer and use it in GitHub Desktop.
RxCpp Simple Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Google c++ style guide : https://google.github.io/styleguide/cppguide.html