mad1231999

Untitled

Apr 20th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. /**********************************************************
  2.  **********************************************************
  3.     AN EXAMPLE PROGRAM WHICH DEMONSTRATES AN AND GATE
  4.  **********************************************************
  5.  **********************************************************/
  6.  
  7. #include <iostream>
  8. #include <chrono>
  9. #include <thread>
  10.  
  11. #include "HCF.hpp"
  12.  
  13. /**********************
  14.  * Example AND class
  15.  **********************/
  16. class Gate_AND : public HCF::HardwareComponent
  17. {
  18.     public:
  19.         const int in1ID = 0, in2ID = 1, outID = 2; // Hardcode the IDs
  20.         HCF::HardwareConnection *out;
  21.  
  22.         Gate_AND()
  23.         {
  24.             out = new HCF::HardwareConnection();
  25.             hwConnections.insert(std::pair<int, HCF::HardwareConnection*>(outID, out));
  26.             HCF::registerHardwareConnection(out);
  27.         }
  28.  
  29.         void tick()
  30.         {
  31.             std::cout << "Value at " << in1ID << ": " << hwConnections.at(in1ID)->getValue() << std::endl;
  32.             std::cout << "Value at " << in2ID << ": " << hwConnections.at(in2ID)->getValue() << std::endl;
  33.  
  34.             if(hwConnections.at(in1ID)->getValue() != 0 && hwConnections.at(in2ID)->getValue() != 0)
  35.                 out->setValue(1);
  36.             else
  37.                 out->setValue(0);
  38.         }
  39. };
  40.  
  41. int main(void)
  42. {
  43.     HCF::init();
  44.    
  45.     Gate_AND g;
  46.     HCF::HardwareConnection *i1 = new HCF::HardwareConnection(), *i2 = new HCF::HardwareConnection();
  47.     g.registerHardwareConnection(i1, g.in1ID, false);
  48.     g.registerHardwareConnection(i2, g.in2ID, false);
  49.  
  50.     HCF::registerHardwareConnection(i1);
  51.     HCF::registerHardwareConnection(i2);
  52.  
  53.     HCF::registerHardwareComponent(&g);
  54.  
  55.     std::cout << "Value: " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
  56.  
  57.     i1->setValue(1);
  58.     i2->setValue(1);
  59.  
  60.     HCF::update();
  61.  
  62.     std::cout << "Value after modifications:  " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
  63.  
  64.     i1->setValue(1);
  65.     i2->setValue(0);
  66.  
  67.     HCF::update();
  68.  
  69.     std::cout << "Value after modification 2: " << g.getHardwareConnections().at(g.outID)->getValue() << std::endl;
  70.  
  71.     HCF::destroy();
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment