Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************
- **********************************************************
- AN EXAMPLE PROGRAM WHICH DEMONSTRATES AN AND GATE
- **********************************************************
- **********************************************************/
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include "HCF.hpp"
- /**********************
- * Example AND class
- **********************/
- class Gate_AND : public HCF::HardwareComponent
- {
- public:
- const int in1ID = 0, in2ID = 1, outID = 2; // Hardcode the IDs
- HCF::HardwareConnection *out;
- Gate_AND()
- {
- out = new HCF::HardwareConnection();
- hwConnections.insert(std::pair<int, HCF::HardwareConnection*>(outID, out));
- HCF::registerHardwareConnection(out);
- }
- void tick()
- {
- std::cout << "Value at " << in1ID << ": " << hwConnections.at(in1ID)->getValue() << std::endl;
- std::cout << "Value at " << in2ID << ": " << hwConnections.at(in2ID)->getValue() << std::endl;
- if(hwConnections.at(in1ID)->getValue() != 0 && hwConnections.at(in2ID)->getValue() != 0)
- out->setValue(1);
- else
- out->setValue(0);
- }
- };
- int main(void)
- {
- HCF::init();
- Gate_AND g;
- HCF::HardwareConnection *i1 = new HCF::HardwareConnection(), *i2 = new HCF::HardwareConnection();
- g.registerHardwareConnection(i1, g.in1ID, false);
- g.registerHardwareConnection(i2, g.in2ID, false);
- HCF::registerHardwareConnection(i1);
- HCF::registerHardwareConnection(i2);
- HCF::registerHardwareComponent(&g);
- std::cout << "Value: " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
- i1->setValue(1);
- i2->setValue(1);
- HCF::update();
- std::cout << "Value after modifications: " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
- i1->setValue(1);
- i2->setValue(0);
- HCF::update();
- std::cout << "Value after modification 2: " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
- HCF::destroy();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment