Last active
February 28, 2019 09:00
-
-
Save johnty/db3ec4ecd4a30f656029ffb3365a4fb8 to your computer and use it in GitHub Desktop.
libapper cpp test
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 <cstring> | |
| #include <iostream> | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <array> | |
| #include "config.h" | |
| #include <mapper/mapper_cpp.h> | |
| #ifdef HAVE_ARPA_INET_H | |
| #include <arpa/inet.h> | |
| #endif | |
| #include "mapper/mapper_cpp.h" | |
| class MapperInterface | |
| { | |
| public: | |
| MapperInterface() : mydev("testDev") { | |
| mydb.subscribe(MAPPER_OBJ_ALL); | |
| mydb.add_signal_callback(sigActionHandler, this); | |
| } | |
| ~MapperInterface() {} | |
| void poll() { | |
| mydb.poll(5); | |
| mydev.poll(5); | |
| } | |
| //database handlers | |
| static void devActionHandler(mapper_database db, | |
| mapper_device dev, | |
| mapper_record_event action, | |
| const void *user) { | |
| reinterpret_cast<const MapperInterface*>(user)->devActionFn(dev, action); | |
| } | |
| static void sigActionHandler(mapper_database db, | |
| mapper_signal sig, | |
| mapper_record_event action, | |
| const void *user) { | |
| reinterpret_cast<const MapperInterface*>(user)->sigActionFn(sig, action); | |
| } | |
| static void mapActionHandler(mapper_database db, | |
| mapper_map map, | |
| mapper_record_event action, | |
| const void *user) { | |
| reinterpret_cast<const MapperInterface*>(user)->mapActionFn(map, action); | |
| } | |
| //static signal update callback | |
| static void sigUpdateHandler(mapper_signal sig, mapper_id instance, const void *value, | |
| int count, mapper_timetag_t *timetag) { | |
| reinterpret_cast<MapperInterface*>(mapper::Signal(sig).user_data())->sigUpdate(sig, instance, value, count, timetag); | |
| } | |
| //instance database callbacks | |
| void devActionFn(mapper_device dev, | |
| mapper_record_event action) const { | |
| } | |
| void sigActionFn(mapper_signal sig, | |
| mapper_record_event action) const { | |
| mapper::Signal Sig(sig); | |
| if ((action == MAPPER_ADDED) && (Sig.direction() == MAPPER_DIR_OUTGOING)) { | |
| std::string name = Sig.name(); | |
| name+= "_l"; | |
| mapper::Signal newSig = mydev.add_input_signal(name.c_str(), Sig.length(), 'f', 0, 0, 0, &sigUpdateHandler, (void*) this); //do we care about sig units, min/max etc? | |
| while (!mydev.ready()) { | |
| mydev.poll(50); | |
| } | |
| if ((mapper_signal)newSig != nullptr) { | |
| //DBG("trying to map "<<Sig.name()<<" to "<<newSig.name()<<"..."); | |
| std::cout<<"OUTPUT FOUND! adding new input sig: "<<newSig.name()<<std::endl; | |
| // while (!myMapperDev->ready()) | |
| // myMapperDev->poll(50); | |
| mapper::Map map(Sig, newSig); | |
| map.push(); //do we need to wait till ready? | |
| while (!map.ready()) { | |
| mydev.poll(50); | |
| } | |
| std::cout<<"\n mapped "<<Sig.name()<<"->"<<newSig.name()<<std::endl; | |
| } | |
| } | |
| if ((action == MAPPER_REMOVED) && (Sig.direction() == MAPPER_DIR_OUTGOING)) { | |
| std::string name = Sig.name(); | |
| name+= "_l"; | |
| std::cout<<"trying to remove "<<name<<std::endl; | |
| mapper::Signal mysig = mydev.signal(name.c_str()); | |
| if ((mapper_signal)mysig != nullptr) { | |
| mydev.remove_signal(mysig); | |
| std::cout<<name<<" removed!\n"<<std::endl;; | |
| } | |
| else { | |
| std::cout<<name<<" not found!\n"<<std::endl;; | |
| } | |
| } | |
| } | |
| void mapActionFn(mapper_map map, | |
| mapper_record_event action) const { | |
| } | |
| //instance signal update callback | |
| void sigUpdate(mapper_signal sig, mapper_id instance, const void *value, | |
| int count, mapper_timetag_t *timetag) { | |
| std::cout<<"Sig update handler!\n"<<std::endl;; | |
| } | |
| private: | |
| mutable mapper::Device mydev; | |
| mapper::Database mydb; | |
| }; | |
| //============================================================================== | |
| int main (int argc, char* argv[]) | |
| { | |
| // ..your code goes here! | |
| MapperInterface myMapper; | |
| while (1) { | |
| myMapper.poll(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment